File Exchange Pick of the Week

November 13th, 2009

This “Peaks” My Interest

Jiro's pick this week is PeakFinder by Nate Yoder.

"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

3 Responses to “This “Peaks” My Interest”

  1. Faris replied on :

    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?

  2. jiro replied on :

    @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/

  3. Cody Anglin replied on :

    Hello,
    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?
    Sincerely

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


MathWorks

Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

These postings are the author's and don't necessarily represent the opinions of The MathWorks.