Mercurial > hg > ltpda
comparison m-toolbox/classes/@ao/fixfs.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 % FIXFS resamples the input time-series to have a fixed sample rate. | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % | |
4 % FIXFS resamples the input time-series to have a fixed sample rate. | |
5 % | |
6 % The new sampling grid is computed from the specified sample rate. If no | |
7 % sample rate is specified, the target is taken from a fit to the input tsdata | |
8 % object. The new sampling grid starts at the time returned from the fit | |
9 % (unless specified) and contains the same number of points or spans the | |
10 % same time as specified. | |
11 % | |
12 % CALL: bs = fixfs(a1,a2,a3,...,pl) | |
13 % bs = fixfs(as,pl) | |
14 % bs = as.fixfs(pl) | |
15 % | |
16 % INPUTS: aN - input analysis objects | |
17 % as - input analysis objects array | |
18 % pl - input parameter list | |
19 % | |
20 % OUTPUTS: bs - array of analysis objects, one for each input | |
21 % | |
22 % <a href="matlab:utils.helper.displayMethodInfo('ao', 'fixfs')">Parameters Description</a> | |
23 % | |
24 % $Id: fixfs.m,v 1.40 2011/08/23 13:48:48 hewitson Exp $ | |
25 % | |
26 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
27 | |
28 | |
29 function varargout = fixfs(varargin) | |
30 | |
31 % Check if this is a call for parameters | |
32 if utils.helper.isinfocall(varargin{:}) | |
33 varargout{1} = getInfo(varargin{3}); | |
34 return | |
35 end | |
36 | |
37 import utils.const.* | |
38 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename); | |
39 | |
40 % Collect input variable names | |
41 in_names = cell(size(varargin)); | |
42 for ii = 1:nargin,in_names{ii} = inputname(ii);end | |
43 | |
44 % Collect all AOs and plists | |
45 [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names); | |
46 [pl, pl_invars] = utils.helper.collect_objects(varargin(:), 'plist', in_names); | |
47 | |
48 % Decide on a deep copy or a modify | |
49 bs = copy(as, nargout); | |
50 | |
51 % combine plists | |
52 pl = parse(pl, getDefaultPlist()); | |
53 | |
54 | |
55 % Get fs | |
56 t0s = -1; % Please keep the -1. At the moment we don' use the t0. | |
57 fss = find(pl, 'fs'); | |
58 method = find(pl, 'method'); | |
59 interp = find(pl, 'interpolation'); | |
60 alias = find(pl, 'filter'); | |
61 | |
62 if numel(fss) ~= 1 && numel(fss) < numel(as) | |
63 error('### Please specify either a no sample rate, a single sample rate, or one for each input time-series.'); | |
64 end | |
65 | |
66 % Get only tsdata AOs | |
67 for jj = 1:numel(bs) | |
68 if isa(bs(jj).data, 'tsdata') | |
69 % record input hist | |
70 hin = bs(jj).hist; | |
71 bs(jj).timeshift; | |
72 utils.helper.msg(msg.PROC1, 'fixing AO: %s', bs(jj).name); | |
73 %------------- Fit sample rate and t0 | |
74 [ffs, ft0, unevenly] = tsdata.fitfs(bs(jj).data.getX); | |
75 %---------------- Get target sample rate | |
76 if numel(fss) > 1 | |
77 fs = fss(jj); | |
78 else | |
79 fs = fss; | |
80 end | |
81 if fs < 0 | |
82 utils.helper.msg(msg.PROC1, 'using sample rate from fit: %f', ffs); | |
83 fs = ffs; | |
84 end | |
85 %---------------- Get target start time | |
86 if numel(t0s) > 1 | |
87 t0 = t0s(jj); | |
88 else | |
89 t0 = t0s; | |
90 end | |
91 if t0 < 0 | |
92 utils.helper.msg(msg.PROC1, 'using start time from fit: %f', ft0); | |
93 t0 = ft0; | |
94 end | |
95 if unevenly % then the fitted t0 is empty so we need to get it from the first input datum | |
96 t0 = bs(jj).x(1); | |
97 end | |
98 %-------------- Compute new grid | |
99 switch lower(method) | |
100 case 'samples' | |
101 N = length(bs(jj).data.y); | |
102 t = linspace(t0, t0+(N-1)/fs, N); | |
103 case 'time' | |
104 Nsecs = bs(jj).data.nsecs; | |
105 t = t0 + [0:1/fs:Nsecs-1/fs].'; | |
106 otherwise | |
107 error('### Unknown interpolation method. Do you want to preserve data duration or number of samples?'); | |
108 end | |
109 %-------------- Antialiasing filter | |
110 switch lower(alias) | |
111 case 'iir' | |
112 utils.helper.msg(msg.PROC1, 'applying iir antialising filter'); | |
113 pl = plist('type', 'lowpass',... | |
114 'order', 8,... | |
115 'fs', bs(jj).data.fs,... | |
116 'fc', 0.9*(fs/2)); | |
117 f = miir(pl); | |
118 filtfilt(bs(jj),f); | |
119 case 'fir' | |
120 utils.helper.msg(msg.PROC1, 'applying fir antialising filter'); | |
121 pl = plist('type', 'lowpass',... | |
122 'order', 64,... | |
123 'fs', bs(jj).data.fs,... | |
124 'fc', 0.9*(fs/2)); | |
125 f = mfir(pl); | |
126 filter(bs(jj),f); | |
127 case 'off' | |
128 otherwise | |
129 error('### Unknown filtering method. Please choose: ''iir'', ''fir'' or ''off'' '); | |
130 end | |
131 %-------------- Interpolate | |
132 bs(jj).interp(plist('vertices', t, 'method', interp)); | |
133 % Set name | |
134 bs(jj).name = sprintf('%s(%s)', mfilename, ao_invars{jj}); | |
135 % Add history | |
136 bs(jj).addHistory(getInfo, pl, ao_invars(jj), hin); | |
137 % clear errors | |
138 bs(jj).clearErrors; | |
139 else | |
140 warning('!!! Skipping AO %s - it''s not a time-series AO.', ao_invars{jj}); | |
141 bs(jj) = []; | |
142 end | |
143 end | |
144 | |
145 % Set output | |
146 if nargout == numel(bs) | |
147 % List of outputs | |
148 for ii = 1:numel(bs) | |
149 varargout{ii} = bs(ii); | |
150 end | |
151 else | |
152 % Single output | |
153 varargout{1} = bs; | |
154 end | |
155 end | |
156 | |
157 %-------------------------------------------------------------------------- | |
158 % Get Info Object | |
159 %-------------------------------------------------------------------------- | |
160 function ii = getInfo(varargin) | |
161 if nargin == 1 && strcmpi(varargin{1}, 'None') | |
162 sets = {}; | |
163 pl = []; | |
164 else | |
165 sets = {'Default'}; | |
166 pl = getDefaultPlist; | |
167 end | |
168 % Build info object | |
169 ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: fixfs.m,v 1.40 2011/08/23 13:48:48 hewitson Exp $', sets, pl); | |
170 end | |
171 | |
172 %-------------------------------------------------------------------------- | |
173 % Get Default Plist | |
174 %-------------------------------------------------------------------------- | |
175 function plout = getDefaultPlist() | |
176 persistent pl; | |
177 if exist('pl', 'var')==0 || isempty(pl) | |
178 pl = buildplist(); | |
179 end | |
180 plout = pl; | |
181 end | |
182 | |
183 function pl = buildplist() | |
184 pl = plist(); | |
185 | |
186 % Fs | |
187 p = param({'fs', 'The target sampling frequency.'}, {1, {-1}, paramValue.OPTIONAL}); | |
188 pl.append(p); | |
189 | |
190 % Method | |
191 p = param({'method','Choose if the new data should span the same time or preserve the number of samples (time/samples)'},... | |
192 {1, {'time', 'samples'}, paramValue.SINGLE}); | |
193 pl.append(p); | |
194 | |
195 % Filter | |
196 p = param({'filter','Specify options for the antialiasing filter.'},{3, {'iir', 'fir', 'off'}, paramValue.SINGLE}); | |
197 pl.append(p); | |
198 | |
199 % Interpolation | |
200 pli = ao.getInfo('interp').plists; | |
201 p = setKey(pli.params(pli.getIndexForKey('method')), 'interpolation'); | |
202 pl.append(p); | |
203 | |
204 end | |
205 | |
206 % Parameter list: | |
207 % 'fs' - specify the target sample rate. Either a single value | |
208 % for all input time-series, or a vector of values, one | |
209 % for each input. To take a fitted value from the data, | |
210 % specify a sample rate of -1. | |
211 % e.g.: fs = [1 2 -1 4] to work on 4 input time-series | |
212 % [default: take from data] | |
213 % | |
214 % 'method' - Choose the behaviour | |
215 % 'Time' - new data span the same time [default] | |
216 % 'Samples' - new data preserves number of samples | |
217 % | |
218 % 'filter' - specify if antialising filter is applied | |
219 % 'off' - no filter applied [default] | |
220 % 'iir' - 8th order iir filter at fc = fs/2 | |
221 % filter is applied forward and backward (filtfilt) | |
222 % 'fir' - 64th order fir filter at fc = fs/2 | |
223 % filter is applied only forward | |
224 % | |
225 % 'interpolation' - specify interpolation method as for interp method | |
226 % 'nearest' - | |
227 % 'linear' - | |
228 % 'spline' - default | |
229 % 'cubic' - |