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