Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: local maxima lines:MATLAB CODE

Subject: local maxima lines:MATLAB CODE

From: carlosmvmendes@gmail.com

Date: 04 May, 2007 04:11:19

Message: 1 of 4

Hello!

Is there any Matlab function to obtain the local maxima lines?

Wavemenu in the wavelet toolbox does it but i can=B4t find the actual
Matlab command line function.

If that fucntion doesn=B4t exist in Matlab function, is there any free
available Matlab code to perform it?

Thanks in advance.

Carlos

Subject: Re: local maxima lines:MATLAB CODE

From: Paul Mennen

Date: 04 May, 2007 19:10:46

Message: 2 of 4

> Is there any Matlab function to obtain the local
> maxima lines?
> Carlos

I assume you just want to find the local maxima in a vector?
If so, a fairly simple minded expression to return the indexes of all
the local maxima of a vector "a" follows:

find(a>=[a(2:end) inf] & a>[inf a(1:end-1)])

If the maxima is a sequence of several equal values, this will return
the index of the first of those. If you wanted it to return the index
of the last one, just swap the >= and the >. If you wanted to
include endpoints of the sequence as possible local maxima, just
change the two instances of "inf" to "-inf".

If you think about how this works, I think you will find it
easy to extend it to look for local minima as well.

This may be good enough for most applications, although the reason I
called it "simple minded" is that it can be fooled by a plateau. For
instance a sequence such as [1 2 3 3 3 4 4] has no local maximum, but
one will be identified by my simple minded approach.

Suppose we take this 15 element example:

a = [1 2 2 3 2 1 1 5 1 17 17 17 15 20 1 ];
% 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 - index of a

A better algorithm that handles plateaus should return the following
indices as local maxima: 4, 8, 10, 14. And for local minima is should
return indicies: 6, 9, 13

This is tricky, but I think you will find that the following code
produces this result exactly:

--------------------------------------------------------------
p = 1:length(a);
b = diff([inf a])~=0;
aa = a(b); pp = p(b);
maxima = pp(find(aa>[aa(2:end) inf] & aa>[inf aa(1:end-1)]))
minima = pp(find(aa<[aa(2:end) -inf] & aa<[-inf aa(1:end-1)]))
---------------------------------------------------------------

~Paul

Subject: Re: local maxima lines:MATLAB CODE

From: Pavle Boskoski

Date: 11 May, 2008 15:32:02

Message: 3 of 4

carlosmvmendes@gmail.com wrote in message
<1178277079.853382.320360@n59g2000hsh.googlegroups.com>...
> Hello!
>
> Is there any Matlab function to obtain the local maxima lines?
>
> Wavemenu in the wavelet toolbox does it but i can=B4t find
the actual
> Matlab command line function.
>
> If that fucntion doesn=B4t exist in Matlab function, is
there any free
> available Matlab code to perform it?
>
> Thanks in advance.
>
> Carlos
>


Here is some script that recreates the plot that you can see
from the wave menu:

% First generate the wavelet coefficients
% sig is your signal normal use of CWT command
r=cwt(sig,'haar',15000);

%clear some stuff if you run this over and over
clear coefs coefs_max coefs_fin;

indBeg = size(r,1);
scales = a_coefs;
coefs = r;
%--------------------
[tmp,I1] = sort(scales);
[tmp,I2] = sort(I1);
coefs = coefs(I2,:);
coefs_max = localmax(coefs,indBeg); %localmax is a built-in
in wavelet toolbox
coefs_fin = coefs_max(I1,:);

clear coefs coefs_max;

% Prepare the plot
[nbRow,nbCol] = size(coefs_fin);
markersize = 2;
marker = 'o';
linestyle = 'none';
color = wtbutils('colors','cw1d','spy');
x = [1:nbCol];

[iRow, iCol]= find(coefs_fin);


%Plot the maxima
plot(x(iCol),a_coefs(iRow), ...
   'marker',marker, ...
   'markersize',markersize, ...
   'linestyle',linestyle, ...
   'color','r');


I hope this was the thing that you were looking for.

Pavle

Subject: Re: local maxima lines:MATLAB CODE

From: Pavle Boskoski

Date: 11 May, 2008 15:39:03

Message: 4 of 4

There was a small error in my previous script:

r=cwt(sig,'haar',15000);

should be:

r=cwt(sig,a_coefs,'haar');

where a_coefs are the scales on which you would like to
perform the CWT

Pavle

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics