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

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).


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

  • Doug: Ben, The name is in the name field as you saw in the video: files(1).name will give you just the name of the...
  • Ben: Hey, Thank you for the tutorial but I must ask, once you have the files required in the array, how do you parse...
  • jiro: Nisa, I’m not familiar with the Psychtoolbox. It’s not a toolbox developed by the MathWorks. As far...
  • nisa: Hi, I have just started to learn to use mathlab can this myaa codes be used with mathlab 7.5.0(R2007b)using...
  • Doug: @Rupa, Could you do this algorithm and then remove the two points and run again? Not efficient, but could be...
  • Doug: From the convhull doc http://www.mathworks .com/access/helpdesk /help/techdoc/ref/co nvhull.html xx = -1:.05:1;...
  • Mark: Very nice function, indeed, thanks for that. But why does’t MATLAB freeze the colormaps for subplots...
  • Steve L: MAuricio, From the description it sounds like the features variable is a cell array that itself contains a...
  • Fred: Doug, I have a set of coordinates for the perimeter of an object in random order. I want to plot these using...
  • Rupa: Hi, sorry to digress but I have a similar problem, I think. I would like to find the three (or n) smallest...

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