help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: findpeak


From: Luke M
Subject: Re: findpeak
Date: Thu, 29 Mar 2012 11:23:10 -0700 (PDT)

asha g wrote
> 
> I wrote the following code. I know it needs to be tweaked a bit. Can you
> help me do that and get it to run ? Help appreciated. Asha G 
> 
> for iter = 1: (niter-2)
> 
>          if (vv1(iter)<vv1(iter+1)>vv1(iter+2))
>            x = (iter +1)
>            maxima(niter) = vv1(x)
>          tx(iter,:) = (iter+1)
>         
> elseif (vv1(iter)>vv1(iter+1)<vv1(iter+2))
> minima(iter,:) = vv1(iter+1);
>          txmin = [(iter+1),1];
>                   
>                 endif 
>     
> endfor
> 

The problem with your idea is that it doesn't find flat peaks.  I guess
that's okay if you don't want it to do so, but if that's the case you might
as well use the diff approach mentioned before.  Some quick untested code
for my algorithm:

candlist = vv1(1);
maxima = []; maximaIdx = [];
for k = 2:length(vv1)
  if (vv1(k) > vv1(k-1))
    candlist = vv1(k);
  elseif (vv1(k) == vv1(k-1))
    candlist = [candlist vv1(k)];
  else % vv1(k) < vv1(k-1)
    if ~isempty(candlist)
      maxima = [maxima vv1(k-1)];
      maximaIdx = [maximaIdx k-1];
      candlist = [];
    end
  end
end

This (hopefully) saves the last index of flat peaks, as well as the sharp
peaks.  To save the middle index of a flat peak, you'd do something like
maximaIdx = [maximaIdx ceil(k-1-length(candlist)/2)].  Note again this won't
save a peak that includes the last index.  To do that, in the if and elseif
section you check if k == length(vv1), and do the maxima and maximaIdx lines
except on index k instead of k-1.

To extend this to find minima as well, I believe you'd have to keep track of
two separate candidate lists, but it's the same method, you just flip the
less than/greater than signs (you could even use the same loop).

--
View this message in context: 
http://octave.1599824.n4.nabble.com/Re-Reading-mat-file-tp4487833p4516404.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]