1 | function [lmval,indd]=lmax(xx,filt)
|
---|
2 | %LMAX [lmval, indd]=lmax(xx,filt). Find local maxima in vector XX,where
|
---|
3 | % LMVAL is the output vector with maxima values, INDD is the
|
---|
4 | % corresponding indexes, FILT is the number of passes of the small
|
---|
5 | % running average filter in order to get rid of small peaks. Default
|
---|
6 | % value FILT =0 (no filtering). FILT in the range from 1 to 3 is
|
---|
7 | % usially sufficient to remove most of a small peaks
|
---|
8 | % For example:
|
---|
9 | % xx=0:0.01:35; y=sin(xx) + cos(xx ./3);
|
---|
10 | % plot(xx,y); grid; hold on;
|
---|
11 | % [b,a]=lmax(y,2)
|
---|
12 | % plot(xx(a),y(a),'r+')
|
---|
13 | % see also LMIN, MAX, MIN
|
---|
14 |
|
---|
15 | %**************************************************|
|
---|
16 | % Serge Koptenko, Guigne International Ltd., |
|
---|
17 | % phone (709)895-3819, fax (709)895-3822 |
|
---|
18 | %--------------06/03/97----------------------------|
|
---|
19 |
|
---|
20 | x=xx;
|
---|
21 | len_x = length(x);
|
---|
22 | fltr=[1 1 1]/3;
|
---|
23 | if nargin <2, filt=0;
|
---|
24 | else
|
---|
25 | x1=x(1); x2=x(len_x);
|
---|
26 | for jj=1:filt,
|
---|
27 | c=conv(fltr,x);
|
---|
28 | x=c(2:len_x+1);
|
---|
29 | x(1)=x1;
|
---|
30 | x(len_x)=x2;
|
---|
31 | end
|
---|
32 | end
|
---|
33 | lmval=[]; indd=[];
|
---|
34 | i=2; % start at second data point in time series
|
---|
35 | while i < len_x-1,
|
---|
36 | if x(i) > x(i-1)
|
---|
37 | if x(i) > x(i+1) % definite max
|
---|
38 | lmval =[lmval x(i)];
|
---|
39 | indd = [ indd i];
|
---|
40 | elseif x(i)==x(i+1)&x(i)==x(i+2) % 'long' flat spot
|
---|
41 | %lmval =[lmval x(i)]; %1 comment these two lines for strict case
|
---|
42 | %indd = [ indd i]; %2 when only definite max included
|
---|
43 | i = i + 2; % skip 2 points
|
---|
44 | elseif x(i)==x(i+1) % 'short' flat spot
|
---|
45 | %lmval =[lmval x(i)]; %1 comment these two lines for strict case
|
---|
46 | %indd = [ indd i]; %2 when only definite max included
|
---|
47 | i = i + 1; % skip one point
|
---|
48 | end
|
---|
49 | end
|
---|
50 | i = i + 1;
|
---|
51 | end
|
---|
52 | if filt>0 & ~isempty(indd),
|
---|
53 | if (indd(1)<= 3)|(indd(length(indd))+2>length(xx)),
|
---|
54 | rng=1; %check if index too close to the edge
|
---|
55 | else rng=2;
|
---|
56 | end
|
---|
57 | for ii=1:length(indd), % Find the real maximum value
|
---|
58 | [val(ii) iind(ii)] = max(xx(indd(ii) -rng:indd(ii) +rng));
|
---|
59 | iind(ii)=indd(ii) + iind(ii) -rng-1;
|
---|
60 | end
|
---|
61 | indd=iind; lmval=val;
|
---|
62 | else
|
---|
63 | end
|
---|
64 |
|
---|
65 |
|
---|