"What? Another peak finder?" you might say. Some of you may classify this as one of those utilities that has been created by many people over the years, like sudoku and waitbar. Well, peak finding happens to be something dear to my heart.
I have been using MATLAB for almost 10 years since my first year of graduate school. I initially learned by trying to decipher my advisor's code. One day, I was struggling to write some code for finding peaks in my data.
% Sample data t = 0:0.01:10; x = sin(2*t) - 3*cos(3.8*t);
That's when my advisor showed me his code:
dx = diff(x); % get differences between consecutive points pkIDX = (dx(1:end-1) >= 0) & (dx(2:end) < 0); % look for slope changes pkIDX = [dx(1)<0, pkIDX, dx(end)>=0]; % deal with edges plot(t, x, t(pkIDX), x(pkIDX), 'ro');
This was an eye-opener and was the moment I experienced the power of vector operation for the first time. The way I code in MATLAB had changed from that point on. ... So when I see "peak finding", it brings back memories.
There are quite a few File Exchange entries for finding peaks (and valleys), including two previous POTW selections: FPEAK and EXTREMA. But I really like peakfinder by Nate. Not only does his code deal with noisy data (my algorithm above will be useless if the signal is noisy), but also his coding practice is quite solid. He has a great help section, robust error-checking of input arguments, and variable input and output arguments for ease of use.
xNoise = x + 0.3*sin(40*t); % add a few more bumps peakfinder(xNoise);
I looked through a few peak finding entries, but I'm sure I may have missed some. Feel free to let me know of others you really like here.
Get the MATLAB code
Published with MATLAB® 7.9
Comments are closed.
6 CommentsOldest to Newest
hi Im using simulink and im having trouble getting peak time from my microphone input.. i tried using peak finder but to no avail. can you help me out?
@Faris: I’m not sure what your specific setup is or how you are trying to find the peaks. You should try posting your question (with all the details) on MATLAB Answers: http://www.mathworks.com/matlabcentral/answers/
I have photovoltaic moduls. I need a current(I) and voltage (U) curve where you can see the Pmax (performance peak). The given values are the maximum values of the voltage and the maximum values of the current, the time and the modul temperature. Is it possible to get the performance curve out of these maximum values or at least the maximum power point?
Jiro Doke:How to find the minimum value using the same method for maxima value?I have try using dt=diff(t) but fail.Hope you will help me.Thank you.
Jiro Doke:I really really need your help.Please response..
@UMP: I would just pass in the negative of the signal to the peak finder.
For example, if you want to find the minimum values of Data, look for the maximum values of -Data.
Also, I recommend posting your question on http://www.mathworks.com/matlabcentral/answers/