comparison m-toolbox/classes/@ao/diff.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % DIFF differentiates the data in AO.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: DIFF differentiates the data in AO. The result is a data
5 % series the same length as the input series.
6 % In case of method 'diff' computes the difference between two samples, in which
7 % case the resulting time object has the length of the input
8 % series -1 sample.
9 % CALL: bs = diff(a1,a2,a3,...,pl)
10 % bs = diff(as,pl)
11 % bs = as.diff(pl)
12 %
13 % INPUTS: aN - input analysis objects
14 % as - input analysis objects array
15 % pl - input parameter list
16 %
17 % OUTPUTS: bs - array of analysis objects, one for each input,
18 % containing the differentiated data
19 %
20 % <a href="matlab:utils.helper.displayMethodInfo('ao', 'diff')">Parameters Description</a>
21 %
22 % REFERENCES:
23 % [1] L. Ferraioli, M. Hueller and S. Vitale, Discrete derivative
24 % estimation in LISA Pathfinder data reduction,
25 % <a
26 % href="matlab:web('http://www.iop.org/EJ/abstract/0264-9381/26/9/094013/','-browser')">Class. Quantum Grav. 26 (2009) 094013.</a>
27 % [2] L. Ferraioli, M. Hueller and S. Vitale, Discrete derivative
28 % estimation in LISA Pathfinder data reduction
29 % <a href="matlab:web('http://arxiv.org/abs/0903.0324v1','-browser')">http://arxiv.org/abs/0903.0324v1</a>
30 %
31 % VERSION: $Id: diff.m,v 1.36 2011/08/03 19:18:56 adrien Exp $
32 %
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34
35 % PARAMETERS: method - the method to use: [default: '3POINT']
36 % 'diff' - like MATLABs diff
37 % compute difference between each two samples
38 % xaxis will be integers from 1 to length
39 % of resulting object
40 % '2POINT' - 2 point derivative computed as
41 % [y(i+1)-y(i)]./[x(i+1)-x(i)].
42 % '3POINT' - 3 point derivative. Compute derivative
43 % at i as [y(i+1)-y(i-1)] / [x(i+1)-x(i-1)].
44 % For i==1, the output is computed as
45 % [y(2)-y(1)]/[x(2)-x(1)]. The last sample
46 % is computed as [y(N)-y(N-1)]/[x(N)-x(N-1)].
47 % '5POINT' - 5 point derivative. Compute derivative dx
48 % at i as
49 % [-y(i+2)+8*y(i+1)-8*y(i-1)+y(i-2)] /
50 % [3*(x(i+2)-x(i-2))].
51 % For i==1, the output is computed as
52 % [y(2)-y(1)]/[x(2)-x(1)]. The last sample
53 % is computed as [y(N)-y(N-1)]/[x(N)-x(N-1)].
54 % 'ORDER2' - Compute derivative using a 2nd order
55 % method.
56 % 'ORDER2SMOOTH' - Compute derivative using a 2nd order
57 % method with a parabolic fit to 5
58 % consecutive samples.
59 % 'filter' - applies an IIR filter built from a
60 % single pole at the chosen frequency. The
61 % filter is applied forwards and backwards
62 % (filtfilt) to achieve the desired f^2
63 % response. This only works for time-series
64 % AOs. For this method, you can specify the
65 % pole frequency with an additional parameter
66 % 'f0' [default: 1/Nsecs]
67 % 'FPS' - Calculates five points derivative using
68 % utils.math.fpsder function. If you call
69 % with this oprtion you may add also the
70 % parameters:
71 % 'ORDER' derivative order, supperted
72 % values are:
73 % 'ZERO', 'FIRST', 'SECOND'
74 % 'COEFF' coefficient used for the
75 % derivation. Refers to the fpsder help
76 % for further details.
77 %
78 %
79
80 function varargout = diff(varargin)
81
82 % Check if this is a call for parameters
83 if utils.helper.isinfocall(varargin{:})
84 varargout{1} = getInfo(varargin{3});
85 return
86 end
87
88 import utils.const.*
89 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
90
91 % Collect input variable names
92 in_names = cell(size(varargin));
93 for ii = 1:nargin,in_names{ii} = inputname(ii);end
94
95 % Collect all AOs and plists
96 [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names);
97 pl = utils.helper.collect_objects(varargin(:), 'plist', in_names);
98
99 % Decide on a deep copy or a modify
100 bs = copy(as, nargout);
101
102 % combine plists
103 pl = parse(pl, getDefaultPlist());
104
105 % Extract method
106 method = find(pl, 'method');
107
108 for jj = 1:numel(bs)
109
110 % Diff can't work for cdata objects since we need x data
111 if isa(bs(jj).data, 'cdata')
112 error('### diff doesn''t work with cdata AOs since we need an x-data vector.');
113 end
114
115 % Compute derivative with selected method
116 switch lower(method)
117 case 'diff'
118 yunit = bs(jj).yunits;
119 y = bs(jj).y;
120 x = bs(jj).x;
121 newX = x(1:end-1); % cut the last sample from the time series to make x and y same length
122 dy = diff(y);
123 bs(jj).data.setY(dy);
124 bs(jj).data.setX(newX);
125 bs(jj).setYunits(yunit);
126 case '2point'
127 x = bs(jj).data.getX;
128 dx = diff(x);
129 y = bs(jj).data.getY;
130 dy = diff(y);
131 z = dy./dx;
132 bs(jj).data.setY(z);
133 bs(jj).data.setX((x(1:end-1)+x(2:end))/2);
134 bs(jj).data.setYunits(bs(jj).data.yunits./bs(jj).data.xunits);
135 case '3point'
136 x = bs(jj).data.getX;
137 dx = diff(x);
138 y = bs(jj).data.getY;
139 z = zeros(size(y));
140 z(2:end-1) = (y(3:end)-y(1:end-2)) ./ (dx(2:end)+dx(1:end-1));
141 z(1) = (y(2)-y(1)) ./ (dx(1));
142 z(end) = 2*z(end-1)-z(end-2);
143 bs(jj).data.setY(z);
144 bs(jj).data.setYunits(bs(jj).data.yunits./bs(jj).data.xunits);
145 case '5point'
146 x = bs(jj).data.getX;
147 dx = diff(x);
148 y = bs(jj).data.getY;
149 z = zeros(size(y));
150 z(1) = (y(2)-y(1)) ./ (dx(1));
151 z(2) = (y(3)-y(1))./(dx(2)+dx(1));
152 z(3:end-2) = (-y(5:end) + 8.*y(4:end-1) - 8.*y(2:end-3) + y(1:end-4)) ./ (3.*(x(5:end)-x(1:end-4)));
153 z(end-1) = 2*z(end-2)-z(end-3);
154 z(end) = 2*z(end-1)-z(end-2);
155 bs(jj).data.setY(z);
156 bs(jj).data.setYunits(bs(jj).data.yunits./bs(jj).data.xunits);
157 case 'order2'
158 x = bs(jj).data.getX;
159 dx = diff(x);
160 y = bs(jj).data.getY;
161 z = zeros(size(y));
162 m = length(y);
163 % y'(x1)
164 z(1) = (1/dx(1)+1/dx(2))*(y(2)-y(1))+...
165 dx(1)/(dx(1)*dx(2)+dx(2)^2)*(y(1)-y(3));
166 % y'(xm)
167 z(m) = (1/dx(m-2)+1/dx(m-1))*(y(m)-y(m-1))+...
168 dx(m-1)/(dx(m-1)*dx(m-2)+dx(m-2)^2)*(y(m-2)-y(m));
169 % y'(xi) (i>1 & i<m)
170 dx1 = repmat(dx(1:m-2),1,1);
171 dx2 = repmat(dx(2:m-1),1,1);
172 y1 = y(1:m-2); y2 = y(2:m-1); y3 = y(3:m);
173 z(2:m-1) = 1./(dx1.*dx2.*(dx1+dx2)).*...
174 (-dx2.^2.*y1+(dx2.^2-dx1.^2).*y2+dx1.^2.*y3);
175 bs(jj).data.setY(z);
176 bs(jj).data.setYunits(bs(jj).data.yunits./bs(jj).data.xunits);
177 case 'order2smooth'
178 x = bs(jj).data.getX;
179 y = bs(jj).data.getY;
180 dx = diff(x);
181 m = length(y);
182 if max(abs(diff(dx)))>sqrt(eps(max(abs(dx))))
183 error('### The x-step must be constant for method ''ORDER2SMOOTH''')
184 elseif m<5
185 error('### Length of y must be at least 5 for method ''ORDER2SMOOTH''.')
186 end
187 h = mean(dx);
188 z = zeros(size(y));
189 % y'(x1)
190 z(1) = sum(y(1:5).*[-54; 13; 40; 27; -26])/70/h;
191 % y'(x2)
192 z(2) = sum(y(1:5).*[-34; 3; 20; 17; -6])/70/h;
193 % y'(x{m-1})
194 z(m-1) = sum(y(end-4:end).*[6; -17; -20; -3; 34])/70/h;
195 % y'(xm)
196 z(m) = sum(y(end-4:end).*[26; -27; -40; -13; 54])/70/h;
197 % y'(xi) (i>2 & i<(N-1))
198 Dc = [2 1 0 -1 -2];
199 tmp = convn(Dc,y)/10/h;
200 z(3:m-2) = tmp(5:m);
201 bs(jj).data.setY(z);
202 bs(jj).data.setYunits(bs(jj).data.yunits./bs(jj).data.xunits);
203 case 'filter'
204 error('### Comming with release 2.5');
205 case 'fps'
206 order = find(pl, 'ORDER');
207 coeff = find(pl, 'COEFF');
208 x = bs(jj).data.getX;
209 dx = x(2)-x(1);
210 fs = 1/dx;
211 y = bs(jj).data.getY;
212 params = struct('ORDER', order, 'COEFF', coeff, 'FS', fs);
213 z = utils.math.fpsder(y, params);
214 bs(jj).data.setY(z);
215 % setting units
216 switch lower(order)
217 case 'first'
218 bs(jj).data.setYunits(bs(jj).data.yunits./bs(jj).data.xunits);
219 case 'second'
220 bs(jj).data.setYunits(bs(jj).data.yunits.*bs(jj).data.xunits.^(-2));
221 end
222 otherwise
223 error('### Unknown method for computing the derivative.');
224 end
225
226 % name for this object
227 bs(jj).name = sprintf('diff(%s)', ao_invars{jj});
228 % add history
229 bs(jj).addHistory(getInfo('None'), pl, ao_invars(jj), bs(jj).hist);
230 end
231
232 % Clear the errors since they don't make sense anymore
233 clearErrors(bs);
234
235 % Set output
236 if nargout == numel(bs)
237 % List of outputs
238 for ii = 1:numel(bs)
239 varargout{ii} = bs(ii);
240 end
241 else
242 % Single output
243 varargout{1} = bs;
244 end
245 end
246
247 %--------------------------------------------------------------------------
248 % Get Info Object
249 %--------------------------------------------------------------------------
250 function ii = getInfo(varargin)
251
252 if nargin == 1 && strcmpi(varargin{1}, 'None')
253 sets = {};
254 pl = [];
255 else
256 sets = {'Default'};
257 pl = getDefaultPlist;
258 end
259 % Build info object
260 ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: diff.m,v 1.36 2011/08/03 19:18:56 adrien Exp $', sets, pl);
261 end
262
263 %--------------------------------------------------------------------------
264 % Get Default Plist
265 %--------------------------------------------------------------------------
266
267 function plout = getDefaultPlist()
268 persistent pl;
269 if exist('pl', 'var')==0 || isempty(pl)
270 pl = buildplist();
271 end
272 plout = pl;
273 end
274
275 function pl = buildplist()
276 pl = plist();
277
278 % Method
279 p = param({'method',['The method to use. Choose between:<ul>', ...
280 '' ...
281 '<li>''2POINT'' - 2 point derivative computed as [y(i+1)-y(i)]./[x(i+1)-x(i)]', ...
282 '</li>' ...
283 '<li>''3POINT'' - 3 point derivative. Compute derivative dx at i as <br>', ...
284 '<tt>[y(i+1)-y(i-1)] / [x(i+1)-x(i-1)]</tt><br>', ...
285 'For <tt>i==1</tt>, the output is computed as <tt>[y(2)-y(1)]/[x(2)-x(1)]</tt>.<br>', ...
286 'The last sample is computed as <tt>[y(N)-y(N-1)]/[x(N)-x(N-1)]</tt>', ...
287 '</li>' ...
288 '<li>''5POINT'' - 5 point derivative. Compute derivative dx at i as <br>', ...
289 '<tt>[-y(i+2)+8*y(i+1)-8*y(i-1)+y(i-2)] / [3*(x(i+2)-x(i-2))]</tt><br>', ...
290 'For <tt>i==1</tt>, the output is computed as <tt>[y(2)-y(1)]/[x(2)-x(1)]</tt><br>', ...
291 'The last sample is computed as <tt>[y(N)-y(N-1)]/[x(N)-x(N-1)]</tt>', ...
292 '</li>' ...
293 '<li>''ORDER2'' - Compute derivative using a 2nd order method', ...
294 '</li>' ...
295 '<li>''ORDER2SMOOTH'' - Compute derivative using a 2nd order method<br>', ...
296 'with a parabolic fit to 5 consecutive samples', ...
297 '</li>' ...
298 '<li>''filter'' - applies an IIR filter built from a single pole at the chosen frequency.<br>', ...
299 'The filter is applied forwards and backwards (filtfilt) to achieve the desired f^2<br>', ...
300 'response. This only works for time-series AOs.<br>', ...
301 'For this method, you can specify the pole frequency with an additional parameter ''F0'' (see below):', ...
302 '</li>'...
303 '<li>''FPS'' - Calculates five points derivative using utils.math.fpsder.<br>', ...
304 'When calling with this option you may add also the parameters ''ORDER'' (see below)<br>', ...
305 'and ''COEFF'' (see below)' ...
306 '</li>' ...
307 ]}, {1, {'2POINT', '3POINT', '5POINT', 'ORDER2', 'ORDER2SMOOTH', 'FILTER', 'FPS'}, paramValue.SINGLE});
308 pl.append(p);
309
310 % F0
311 p = param({'f0','The pole frequency for the ''filter'' method.'}, {1, {'1/Nsecs'}, paramValue.OPTIONAL});
312 pl.append(p);
313
314 % Order
315 p = param({'ORDER','Derivative order'}, {1, {'ZERO', 'FIRST', 'SECOND'}, paramValue.SINGLE});
316 pl.append(p);
317
318 % Coeff
319 p = param({'COEFF',['Coefficient used for the derivation. <br>', ...
320 'Refer to the <a href="matlab:doc(''utils.math.fpsder'')">fpsder help</a> for further details']}, paramValue.EMPTY_DOUBLE);
321 pl.append(p);
322
323 end
324