annotate aim-mat/tools/@signal/envelope.m @ 4:537f939baef0 tip

various bug fixes and changed copyright message
author Stefan Bleeck <bleeck@gmail.com>
date Tue, 16 Aug 2011 14:37:17 +0100
parents 20ada0af3d7d
children
rev   line source
tomwalters@0 1 % method of class @signal
tomwalters@0 2 % function env=envelope(sig,[mintime],[maxtime],[mincontrast])
tomwalters@0 3 %
tomwalters@0 4 % retuns the envelope determined by connecting the maximum points
tomwalters@0 5 % when the maxima are seperated by more then the mintime, then connect
tomwalters@0 6 % minima, that lie in the middle
tomwalters@0 7 % if the difference between maxima is smaller then maxtime
tomwalters@0 8 % look for the next maximum, that is further away
tomwalters@0 9 % if mincontrast is given, than the maxima must have at least such a
tomwalters@0 10 % contrast that is the distance in height to its neighbours in % of the whole signal!!
tomwalters@0 11 %
tomwalters@0 12 % INPUT VALUES:
tomwalters@0 13 % sig: original @signal
tomwalters@0 14 % mintime: minimum time, that must be between two successive maxima
tomwalters@0 15 % maxtime: maximum time, that must be between two successive maxima
tomwalters@0 16 % mincontrast: minimum contrast between two maxima
tomwalters@0 17 %
tomwalters@0 18 % RETURN VALUE:
tomwalters@0 19 % env: @signal
tomwalters@0 20 %
bleeck@3 21 % This external file is included as part of the 'aim-mat' distribution package
bleeck@3 22 % (c) 2011, University of Southampton
bleeck@3 23 % Maintained by Stefan Bleeck (bleeck@gmail.com)
bleeck@3 24 % download of current version is on the soundsoftware site:
bleeck@3 25 % http://code.soundsoftware.ac.uk/projects/aimmat
bleeck@3 26 % documentation and everything is on http://www.acousticscale.org
bleeck@3 27
tomwalters@0 28
tomwalters@0 29 function env=envelope(sig,mintime,maxtime,mincontrast)
tomwalters@0 30
tomwalters@0 31 if nargin < 4
tomwalters@0 32 mincontrast=0;
tomwalters@0 33 end
tomwalters@0 34 if nargin < 3
tomwalters@0 35 maxtime=0;
tomwalters@0 36 end
tomwalters@0 37 if nargin < 2
tomwalters@0 38 mintime=0;
tomwalters@0 39 end
tomwalters@0 40
tomwalters@0 41 [maxpos,minpos,maxs,mins]=getminmax(sig);
tomwalters@0 42
tomwalters@0 43
tomwalters@0 44
tomwalters@0 45 env=signal(sig);
tomwalters@0 46 env=mute(env);
tomwalters@0 47 env=setname(env,sprintf('Envelope of: %s',getname(sig)));
tomwalters@0 48
tomwalters@0 49 if isempty(maxs)
tomwalters@0 50 return
tomwalters@0 51 end
tomwalters@0 52
tomwalters@0 53 threshold=max(sig)*mincontrast;
tomwalters@0 54
tomwalters@0 55 nr_max=length(maxs);
tomwalters@0 56 lastmax=maxpos(1);
tomwalters@0 57 lastval=maxs(1);
tomwalters@0 58 lastminval=getminimumleftof(lastmax,maxpos,minpos,maxs,mins);
tomwalters@0 59 if isempty(lastminval)
tomwalters@0 60 lastminval=0;
tomwalters@0 61 end
tomwalters@0 62 for i=2:nr_max
tomwalters@0 63 newmax=maxpos(i);
tomwalters@0 64 newval=maxs(i);
tomwalters@0 65 newminval=getminimumleftof(newmax,maxpos,minpos,maxs,mins);
tomwalters@0 66
tomwalters@0 67 % wenn die maxima zu nah beeinander liegen, dann such das nächste Maximum
tomwalters@0 68 if newmax-lastmax > maxtime
tomwalters@0 69 if newval > lastminval+threshold
tomwalters@0 70 if newmax-lastmax > mintime
tomwalters@0 71 tmin=getminimumleftof(newmax,maxpos,minpos,maxs,mins);% das ist das Minimum links vom rechten Maximum
tomwalters@0 72 % wenn das Minimum zwischen den Maxima liegt, dann verbinde zwei Linien vom Maximum zum Minimum und weiter
tomwalters@0 73 if tmin>lastmax
tomwalters@0 74 % erste Gerade
tomwalters@0 75 tmitte=tmin;
tomwalters@0 76 x1=time2bin(sig,lastmax);
tomwalters@0 77 x2=time2bin(sig,tmitte);
tomwalters@0 78 y1=lastval;
tomwalters@0 79 y2=gettimevalue(sig,tmitte);
tomwalters@0 80 line=linspace(y1,y2,x2-x1+1);
tomwalters@0 81 env=setvalues(env,line,x1);
tomwalters@0 82
tomwalters@0 83 % zweite Gerade
tomwalters@0 84 x1=time2bin(sig,tmitte);
tomwalters@0 85 x2=time2bin(sig,newmax);
tomwalters@0 86 y1=gettimevalue(sig,tmitte);
tomwalters@0 87 y2=newval;
tomwalters@0 88 line=linspace(y1,y2,x2-x1+1);
tomwalters@0 89 env=setvalues(env,line,x1);
tomwalters@0 90 end
tomwalters@0 91 % maxima weit genug zusammen, also werden nur die Maxima verbunden
tomwalters@0 92 else
tomwalters@0 93 x1=time2bin(sig,lastmax);
tomwalters@0 94 x2=time2bin(sig,newmax);
tomwalters@0 95 y1=lastval;
tomwalters@0 96 y2=newval;
tomwalters@0 97 line=linspace(y1,y2,x2-x1+1);
tomwalters@0 98 env=setvalues(env,line,x1);
tomwalters@0 99 end
tomwalters@0 100 lastmax=newmax;
tomwalters@0 101 lastval=newval;
tomwalters@0 102 lastminval=newminval;
tomwalters@0 103 end
tomwalters@0 104 end
tomwalters@0 105 end