Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I found the following code in matlab for peak detection:

C#
maxtab = [];
mintab = [];

v = v(:); % Just in case this wasn't a proper vector

if nargin < 3
  x = (1:length(v))';
else
  x = x(:);
  if length(v)~= length(x)
    error('Input vectors v and x must have same length');
  end
end

if (length(delta(:)))>1
  error('Input argument DELTA must be a scalar');
end

if delta <= 0
  error('Input argument DELTA must be positive');
end

mn = Inf; mx = -Inf;
mnpos = NaN; mxpos = NaN;

lookformax = 1;

for i=1:length(v)
  this = v(i);
  if this > mx, mx = this; mxpos = x(i); end
  if this < mn, mn = this; mnpos = x(i); end

  if lookformax
    if this < mx-delta
      maxtab = [maxtab ; mxpos mx];
      mn = this; mnpos = x(i);
      lookformax = 0;
    end
  else
    if this > mn+delta
      mintab = [mintab ; mnpos mn];
      mx = this; mxpos = x(i);
      lookformax = 1;
    end
  end
end


source: http://billauer.co.il/peakdet.html[^]

And I want covert to C#, I made the following method code in C#:

C#
public double[,] EncontraPicos(double[] Y, double Delta, double[] T)
        {
            for (int i = 0; i < (Y.Length -1); i++)
            {
                This = Y[i];
                if (This > mx)
                {
                    mx = This;
                    mxpos = T[i];
                }
                if (This < mn)
                {
                    mn = This;
                    mnpos = T[i];
                }

                if (lookformax)
                {
                    if (This < (mx - Delta))
                    {
                        Ymax.Add(mx);
                        Tmax.Add(mxpos);
                        mn = This;
                        mnpos = T[i];
                        lookformax = false;
                        //j++;
                    }
                    if (This > (mn + Delta))
                    {
                        Ymin.Add(mn);
                        Tmin.Add(mnpos);
                        mx = This;
                        mxpos = T[i];
                        lookformax = true;
                       // k++;
                    }
                }
            }
            return maxtab;
        }



The original code search peak values ​​in a given vector Y, dependent on a vector X, which are passed to functions But compiling both, the results come out different, someone can say why? And what would an alternative code in C#?
Posted

1 solution

Here is the problem: I started to look but immediately saw: this is not your C# code. This is the code which cannot compile, quite apparently. So, it could not be the code which can give any results you are referring to, different or not.

The code won't compile due to undeclared object This (don't mix up with the reference this which also would not compile). However, it would compile if you add a double field or property with this name in a declaring class, but then it would be inadequate implementation. Here is the hint: declare the method static and use only stack memory in it. That would be a correct code design. The general impression that this C# code has very little do to with original code. For example, you never use double.NegativeInfinity and double.PositiveInfinity, which are essentially used in the original code. Please see:
http://msdn.microsoft.com/en-us/library/system.double.negativeinfinity.aspx[^],
http://msdn.microsoft.com/en-us/library/system.double.positiveinfinity.aspx[^].

I think this is enough; there is no much sense to analyze it any further. You need to make a real translation, to have something to discuss.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900