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