annotate aim-mat/tools/@signal/add.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 sigresult=add(sig1,sig2,[start_time],[duration])
tomwalters@0 3 % INPUT VALUES:
tomwalters@0 4 % sig1: first @signal
tomwalters@0 5 % sig2: second @signal or struct
tomwalters@0 6 % start_time: start time for the addition. [default: 0]
tomwalters@0 7 % duration: duration of the signal, that is added [default: getlength(sig2)]
tomwalters@0 8 % RETURN VALUE:
tomwalters@0 9 % sigresult: @signal that is the sum of signals sig1 and sig2`
tomwalters@0 10 % the resulting signal can be longer then sig1 or sig2, when start_time
tomwalters@0 11 % and duration is according. sig2 can be a signal-object or an struct
tomwalters@0 12 %
tomwalters@0 13 %
bleeck@3 14 % This external file is included as part of the 'aim-mat' distribution package
bleeck@3 15 % (c) 2011, University of Southampton
bleeck@3 16 % Maintained by Stefan Bleeck (bleeck@gmail.com)
bleeck@3 17 % download of current version is on the soundsoftware site:
bleeck@3 18 % http://code.soundsoftware.ac.uk/projects/aimmat
bleeck@3 19 % documentation and everything is on http://www.acousticscale.org
bleeck@3 20
tomwalters@0 21
tomwalters@0 22
tomwalters@0 23 function sig=add(a,b,start_time,dauer)
tomwalters@0 24
tomwalters@0 25 if nargin < 4
tomwalters@0 26 if isobject(b)
tomwalters@0 27 dauer=getlength(b);
tomwalters@0 28 end
tomwalters@0 29 end
tomwalters@0 30 if nargin < 3
tomwalters@0 31 start_time=0;
tomwalters@0 32 end
tomwalters@0 33
tomwalters@0 34 % wenn ein Spaltenvektor hinaufaddier werden soll
tomwalters@0 35 if isnumeric(b)
tomwalters@0 36 sr=GetSR(a);
tomwalters@0 37 nr=size(b)
tomwalters@0 38 if nr>1
tomwalters@0 39 temp=signal(b,sr); % erzeuge ein neues Signal aus den Werten
tomwalters@0 40 sig=add(a,temp); % und lasse dann die beiden Signale zusammenaddieren
tomwalters@0 41 else
tomwalters@0 42 sig=a+b;
tomwalters@0 43 end
tomwalters@0 44 return;
tomwalters@0 45 end
tomwalters@0 46
tomwalters@0 47
tomwalters@0 48
tomwalters@0 49 % das resultierede Signal kann länger sein als die Ausgangssignale
tomwalters@0 50 % erst feststellen, wie lang das nachher sein soll
tomwalters@0 51 laenge1=getlength(a);
tomwalters@0 52 laenge2=getlength(b); %so lang ist das zweite Signal
tomwalters@0 53 if laenge2<dauer
tomwalters@0 54 disp('error: the signal is shorter then the duration');
tomwalters@0 55 return;
tomwalters@0 56 end
tomwalters@0 57
tomwalters@0 58 sr1=getsr(a);
tomwalters@0 59 sr2=getsr(b);
tomwalters@0 60 if sr1~=sr2
tomwalters@0 61 error('signal::add::error: samplerates differ - not implemented yet');
tomwalters@0 62 end
tomwalters@0 63
tomwalters@0 64 lneu=start_time+dauer; % so lang wird das neue Signal
tomwalters@0 65 if lneu<laenge1 % oder es ist nicht länger als vorher
tomwalters@0 66 lneu=laenge1;
tomwalters@0 67 end
tomwalters@0 68
tomwalters@0 69 binl1=time2bin(a,lneu+getminimumtime(a));
tomwalters@0 70 binl2=time2bin(a,laenge1+getminimumtime(a)); %achtung, sonst rundungsfehler
tomwalters@0 71 if binl1 > binl2 % wenn das neue Signal länger wird
tomwalters@0 72 temp=signal(lneu,sr1,a.name,a.unit_x,a.unit_y,a.start_time);
tomwalters@0 73 % kopiere zuerst das alte Signal
tomwalters@0 74 start=1;
tomwalters@0 75 stop=time2bin(a,laenge1+getminimumtime(a));
tomwalters@0 76 temp.werte(start:stop)=a.werte(start:stop);
tomwalters@0 77 % rekursiver Aufruf, denn nun ist das Signal lang genug
tomwalters@0 78 sig=add(temp,b,start_time,dauer);
tomwalters@0 79 return;
tomwalters@0 80 end
tomwalters@0 81
tomwalters@0 82 % normalfall: Das Ergebnissignal ist nun höchstens genauso lang
tomwalters@0 83 sig=a; %kopieren des alten Signals in das Rückgabesignal
tomwalters@0 84 start1=time2bin(a,start_time)+1;
tomwalters@0 85 % stop1=time2bin(a,start_time+dauer-1/sr1);
tomwalters@0 86 stop1=time2bin(a,start_time+dauer);
tomwalters@0 87 start2=1;
tomwalters@0 88 stop2=time2bin(a,dauer+getminimumtime(a));
tomwalters@0 89
tomwalters@0 90 % do some error checking:
tomwalters@0 91 if stop2-start2 == stop1-start1
tomwalters@0 92 sig.werte(start1:stop1)=sig.werte(start1:stop1)+b.werte(start2:stop2);
tomwalters@0 93 else
tomwalters@0 94 if start1>1
tomwalters@0 95 stop1=start1+stop2-start2;
tomwalters@0 96 sig.werte(start1:stop1)=sig.werte(start1:stop1)+b.werte(start2:stop2);
tomwalters@0 97 else
tomwalters@0 98
tomwalters@0 99 disp('signal::add problem with adding - havent added')
tomwalters@0 100 end
tomwalters@0 101 end
tomwalters@0 102
tomwalters@0 103 return;
tomwalters@0 104
tomwalters@0 105
tomwalters@0 106
tomwalters@0 107