0
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 1 % QUASISWEPTSING computes a transfer function from swept-sine measurements
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 3 %
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 4 % DESCRIPTION: QUASISWEPTSING computes a transfer function from discrete
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 5 % swept-sine measurements.
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 6 %
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 7 % In order for the calculation to work, you need to give it an array of
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 8 % start and stop times (or durations), and (optionally) an array of
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 9 % amplitudes and frequencies of the injected sine-waves. If you don't
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 10 % specify the frequencies, you must give a time-series of the injected
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 11 % signal and the algorithm will try to determine the amplitudes and
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 12 % frequencies from the data.
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 13 %
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 14 %
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 15 % CALL: T = quasiSweptSine(out, pl);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 16 %
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 17 % INPUTS: out - The measured output of the system
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 18 % PL - parameter list
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 19 %
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 20 % OUTPUT: T - the measured transfer function
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 21 %
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 22 % The procinfo of the output AOs contains the following fields:
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 23 %
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 24 % 'frequencies' - the frequencies used in the DFT estimation.
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 25 % 'timespans' - an array of timespan objects, one for each sine-wave
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 26 % segment
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 27 %
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 28 %
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 29 % <a href="matlab:utils.helper.displayMethodInfo('ao', 'quasiSweptSine')">Parameters Description</a>
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 30 %
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 31 % VERSION: $Id: quasiSweptSine.m,v 1.10 2011/04/08 08:56:16 hewitson Exp $
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 32 %
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 34
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 35
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 36 function varargout = quasiSweptSine(varargin)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 37
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 38 % check if this is a call for parameters
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 39 if utils.helper.isinfocall(varargin{:})
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 40 varargout{1} = getInfo(varargin{3});
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 41 return
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 42 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 43
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 44 % tell the system we are runing
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 45 import utils.const.*
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 46 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 47
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 48 % collect input variable names
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 49 in_names = cell(size(varargin));
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 50 for ii = 1:nargin,in_names{ii} = inputname(ii);end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 51
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 52 % collect all AOs and plists
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 53 [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 54 pl = utils.helper.collect_objects(varargin(:), 'plist', in_names);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 55
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 56 if nargout == 0
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 57 error('### quasiSweptSine can not be used as a modifier method. Please give at least one output');
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 58 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 59
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 60 % Make copies or handles to inputs
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 61 bs = copy(as, nargout);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 62
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 63 % combine plists
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 64 pl = parse(pl, getDefaultPlist());
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 65
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 66 % Parameters
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 67 input = pl.find('input');
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 68 startTimes = pl.find('Start times');
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 69 stopTimes = pl.find('Stop times');
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 70 durations = pl.find('durations');
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 71 amplitudes = pl.find('amplitudes');
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 72 frequencies = pl.find('frequencies');
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 73 % phases = pl.find('phases');
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 74 inUnits = pl.find('Input Units');
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 75 win = pl.find('Win');
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 76 Nerror = pl.find('Nerror');
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 77
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 78 if isa(input, 'ao') && ~isa(input.data, 'tsdata')
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 79 utils.helper.err('quasiSweptSine requires time-series as input');
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 80 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 81
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 82
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 83 %---------------------------------------------------
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 84 %--------- Convert the times to timespans
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 85 if isempty(durations) && isempty(stopTimes)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 86 utils.helper.err('You need to specify either an array of stop times, or an array of durations');
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 87 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 88
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 89
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 90 %---------------------------------------------------
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 91 %--------- if we have the input, we use it to determine amplitudes and
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 92 %--------- frequencies
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 93
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 94 if isempty(input) && isempty(amplitudes)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 95 utils.helper.err('You need to specify either an input signal, or a full description of the signals including amplitudes and frequecies.');
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 96 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 97
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 98 computeFrequecies = false;
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 99 if isempty(frequencies)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 100 computeFrequecies = true;
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 101 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 102
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 103
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 104 % Go through each ao
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 105 for jj=1:numel(bs)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 106
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 107
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 108 if ~isa(bs.data, 'tsdata')
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 109 utils.helper.err('quasiSweptSine requires time-series as input');
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 110 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 111
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 112 [timespans, startTimes, stopTimes] = generateTimespans(bs(jj).t0, startTimes, stopTimes, durations);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 113
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 114
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 115 % Go through each time-span
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 116 Txx = zeros(size(timespans));
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 117 dT = zeros(size(timespans));
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 118 for kk=1:numel(timespans)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 119 if isa(input, 'ao')
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 120 inseg = input.split(plist('timespan', timespans(kk)));
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 121 else
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 122 % We don't have an input, so we need to create inputs from the
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 123 % signal specs
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 124
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 125 inseg = ao(plist('waveform', 'sine wave', ...
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 126 'nsecs', timespans(kk).interval, ...
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 127 'fs', bs(jj).fs, ...
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 128 'A', amplitudes(kk), ...
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 129 'f', frequencies(kk), ...
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 130 'toff', 0, ...
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 131 't0', startTimes(kk), ...
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 132 'yunits', inUnits));
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 133 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 134 if computeFrequecies
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 135 b = sineParams(inseg, plist('N', 1));
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 136 frequencies(kk) = b.y(2);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 137 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 138
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 139 utils.helper.msg(msg.PROC1, 'Computing TF at %g Hz', frequencies(kk));
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 140
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 141 % Window
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 142 w = ao(plist('win', win, 'length', inseg.len));
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 143
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 144 % Output
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 145 outseg = bs(jj).split(plist('timespan', timespans(kk)));
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 146
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 147 % Compute DFT
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 148 fs = outseg.data.fs;
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 149 N = outseg.len;
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 150 J = -2*pi*1i.*(0:N-1)/fs;
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 151 outxx = exp(frequencies(kk)*J)*(w.y.*outseg.data.getY);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 152 inxx = exp(frequencies(kk)*J)*(w.y.*inseg.data.getY);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 153 Txx(kk) = outxx./inxx;
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 154
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 155 % Compute error
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 156 %
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 157 nout = noiseAroundLine(outseg, frequencies(kk), Nerror);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 158 nin = noiseAroundLine(inseg, frequencies(kk), Nerror);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 159 snr1 = abs(outxx)./nout;
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 160 snr2 = abs(inxx)./nin;
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 161 dT(kk) = abs(Txx(kk)) * sqrt( (1./snr1)^2 + (1./snr2)^2);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 162
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 163
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 164 end % End loop over timespans (segments)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 165
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 166 % Make output
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 167 bs(jj).data = fsdata(frequencies, Txx, bs(jj).data.fs);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 168 bs(jj).data.dy = dT;
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 169 bs(jj).data.setXunits('Hz');
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 170 bs(jj).data.setYunits(outseg.yunits./inseg.yunits);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 171 % Set name
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 172 bs(jj).name = sprintf('sweptsine(%s,%s)', input.name, ao_invars{jj});
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 173 % Set procinfo
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 174 bs(jj).procinfo = plist('frequencies', frequencies, ...
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 175 'timespan', timespans);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 176 % Add history
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 177 bs(jj).addHistory(getInfo('None'), pl, ao_invars(jj), bs(jj).hist);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 178
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 179 end % Loop over input aos
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 180
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 181 % set outputs
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 182 varargout{1} = bs;
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 183
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 184 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 185
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 186 function n = noiseAroundLine(sig, f0, N)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 187
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 188 xx = abs(fft(sig));
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 189
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 190 % get the noise floor around the frequency of interest
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 191 % - we need the bin that is nearest the frequency
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 192 f = abs(xx.x - f0);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 193 [m, mi] = min(f);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 194 % xx.x(mi)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 195 % iplot(xx)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 196 % plot(xx.x(mi), xx.y(mi), 'x')
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 197
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 198 M = N-1;
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 199 % Measure below the line frequency if we can
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 200 nl = 0;
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 201 if (mi>1)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 202 ls = max(1, mi-2*M);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 203 le = max(1, mi-M);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 204 nl = xx.y(ls:le);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 205 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 206 % Measure above the line frequency if we can
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 207 nu = 0;
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 208 if mi<xx.len
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 209 us = min(xx.len, mi+M);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 210 ue = min(xx.len, mi+2*M);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 211 nu = xx.y(us:ue);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 212 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 213 n = mean([nl;nu]);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 214
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 215 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 216
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 217 function [ts, startTimes, stopTimes] = generateTimespans(t0, startTimes, stopTimes, durations)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 218
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 219
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 220 if isempty(durations) && numel(startTimes) ~= numel(stopTimes)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 221 utils.helper.err('You need to specify the same number of start and stop times.');
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 222 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 223 if isempty(stopTimes) && numel(startTimes) ~= numel(durations)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 224 utils.helper.err('You need to specify the same number of durations and stop times.');
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 225 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 226
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 227
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 228 ts = timespan.initObjectWithSize(1,numel(startTimes));
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 229
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 230 % Convert to time objects
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 231 if iscell(startTimes)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 232 startTimes = time(startTimes);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 233 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 234 if isnumeric(startTimes)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 235 newStarts = time.initObjectWithSize(1,numel(startTimes));
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 236 for kk=1:numel(startTimes)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 237 newStarts(kk) = t0+startTimes(kk);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 238 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 239 startTimes = newStarts;
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 240 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 241
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 242 if isempty(durations) && iscell(stopTimes)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 243 stopTimes = time(stopTimes);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 244 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 245
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 246 if isempty(durations) && isnumeric(stopTimes)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 247 if isnumeric(stopTimes)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 248 newStops = time.initObjectWithSize(1,numel(startTimes));
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 249 for kk=1:numel(stopTimes)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 250 newStops(kk) = t0+stopTimes(kk);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 251 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 252 stopTimes = newStops;
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 253 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 254 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 255
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 256 useDuration = isempty(stopTimes);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 257 for kk=1:numel(startTimes)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 258 if useDuration
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 259 ts(kk) = timespan(startTimes(kk), startTimes(kk)+durations(kk));
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 260 else
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 261 ts(kk) = timespan(startTimes(kk), stopTimes(kk));
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 262 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 263 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 264
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 265 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 266
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 267
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 268 % get info object
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 269 function ii = getInfo(varargin)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 270 if nargin == 1 && strcmpi(varargin{1}, 'None')
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 271 sets = {};
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 272 pl = [];
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 273 else
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 274 sets = {'Default'};
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 275 pl = getDefaultPlist;
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 276 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 277 % build info object
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 278 ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.op, '$Id: quasiSweptSine.m,v 1.10 2011/04/08 08:56:16 hewitson Exp $', sets, pl);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 279 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 280
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 281 % get default plist
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 282 function plout = getDefaultPlist()
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 283 persistent pl;
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 284 if exist('pl', 'var')==0 || isempty(pl)
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 285 pl = buildplist();
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 286 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 287 plout = pl;
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 288 end
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 289
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 290 function pl = buildplist()
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 291
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 292 % default plist for linear fitting
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 293 pl = plist();
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 294
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 295 % Input
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 296 p = param({'input', 'The input data series.'}, paramValue.EMPTY_DOUBLE);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 297 pl.append(p);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 298
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 299 % Start times
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 300 p = param({'Start Times', 'A cell array of start times, or an array of time objects.'}, ...
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 301 paramValue.EMPTY_CELL);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 302 pl.append(p);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 303
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 304 % Stop times
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 305 p = param({'Stop Times', 'A cell array of stop times, or an array of time objects.'}, ...
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 306 paramValue.EMPTY_CELL);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 307 pl.append(p);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 308
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 309 % Durations
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 310 p = param({'Durations', 'An array of durations that can be used instead of the stop times.'}, ...
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 311 paramValue.EMPTY_DOUBLE);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 312 pl.append(p);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 313
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 314 % Amplitudes
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 315 p = param({'Amplitudes', 'An array of amplitudes.'}, ...
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 316 paramValue.EMPTY_DOUBLE);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 317 pl.append(p);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 318
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 319 % Frequencies
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 320 p = param({'Frequencies', 'An array of frequencies [Hz].'}, ...
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 321 paramValue.EMPTY_DOUBLE);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 322 pl.append(p);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 323
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 324 % Input units
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 325 p = param({'Input Units', 'If you don''t give an input signal AO, you can specify the units of the signal that will be constructed internally.'}, ...
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 326 {1, {'V'}, paramValue.OPTIONAL});
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 327 pl.append(p);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 328
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 329 % Window
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 330 p = param({'Win', 'A window to apply to each segment when computing the DFT.'}, ...
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 331 paramValue.WINDOW);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 332 pl.append(p);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 333
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 334 % Error samples
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 335 p = param({'Nerror', ['The number of samples either side of the line frequency to use to estimate the noise floor.'...
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 336 'The noise is estimated from <br><br><tt>mean([y(idx-2*M:idx-M);y(idx+M:idx+2M)])</tt><br><br> where <tt>M=N-1</tt> and <tt>idx</tt> is the index '...
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 337 'of the bin nearest to the frequency of the signal.']}, ...
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 338 paramValue.DOUBLE_VALUE(5));
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 339 pl.append(p);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 340
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 341 % % Phases
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 342 % p = param({'Phases', 'An array of phases [degrees].'}, paramValue.DOUBLE_VALUE(0));
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 343 % pl.append(p);
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 344
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
+ − 345 end