Mercurial > hg > ltpda
comparison m-toolbox/classes/@ao/downsample.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 % DOWNSAMPLE AOs containing time-series data. | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % | |
4 % DESCRIPTION: DOWNSAMPLE AOs containing time-series data. All other AOs with | |
5 % no time-series data are skipped but appear in the output. | |
6 % Note that no anti-aliasing filter is applied to the | |
7 % original data!!! | |
8 % CALL: b = downsample(a, pl) - use plist to get parameters | |
9 % b = downsample(a1, a2, pl) - downsample both a1 and a2; | |
10 % b is then a 2x1 vector. | |
11 % | |
12 % <a href="matlab:utils.helper.displayMethodInfo('ao', 'downsample')">Parameters Description</a> | |
13 % | |
14 % EXAMPLES: 1) downsample x4; offset is set to default of 0 | |
15 % | |
16 % >> p = plist('factor',4); | |
17 % >> b = downsample(a, p); | |
18 % | |
19 % 2) downsample x2 with 1 sample offset | |
20 % | |
21 % >> p = plist('factor',2,'offset',1); | |
22 % >> b = downsample(a,p); | |
23 % | |
24 % | |
25 % VERSION: $Id: downsample.m,v 1.34 2011/07/14 05:33:18 mauro Exp $ | |
26 % | |
27 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
28 | |
29 function varargout = downsample(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 | |
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 % Get parameters from plist | |
55 offset = find(pl, 'offset'); | |
56 factor = find(pl, 'factor'); | |
57 | |
58 % Checking downsampling value is valid | |
59 if isempty(factor) | |
60 error('### Please give a plist with a parameter ''Factor''.'); | |
61 end | |
62 | |
63 % Checking downsampling value is integer | |
64 if rem(factor, floor(factor)) ~= 0 | |
65 warning('!!! Downsample factor should be an integer. Rounding. !!!'); | |
66 factor = round(factor); | |
67 end | |
68 | |
69 % Checking sample offset value | |
70 if isempty(offset) | |
71 warning('!!! No offset specified; using default of 0 samples !!!'); | |
72 offset = 0; | |
73 end | |
74 | |
75 if factor == 0 | |
76 error('### The downsampling factor is zero. Please set a positive integer value.'); | |
77 end | |
78 | |
79 % Loop over input AOs | |
80 for jj = 1:numel(bs) | |
81 if isa(bs(jj).data, 'tsdata') | |
82 % get samples | |
83 ss = 1+offset; | |
84 samples = ss:factor:length(bs(jj).data.y); | |
85 % select samples | |
86 bs(jj).data.setXY(bs(jj).data.getX(samples), bs(jj).data.getY(samples)); | |
87 % Set new sample rate | |
88 bs(jj).data.setFs(bs(jj).data.fs/factor); | |
89 % drop X vector again if we can | |
90 bs(jj).data.collapseX; | |
91 % set name | |
92 bs(jj).name = sprintf('downsample(%s)', ao_invars{jj}); | |
93 % Add history | |
94 bs(jj).addHistory(getInfo('None'), pl, ao_invars(jj), bs(jj).hist); | |
95 % Clear the errors since they don't make sense anymore | |
96 clearErrors(bs(jj)); | |
97 else | |
98 warning('!!! Downsample only works on tsdata objects. Skipping AO %s', ao_invars{jj}); | |
99 end | |
100 end | |
101 | |
102 % Set output | |
103 varargout = utils.helper.setoutputs(nargout, bs); | |
104 end | |
105 | |
106 %-------------------------------------------------------------------------- | |
107 % Get Info Object | |
108 %-------------------------------------------------------------------------- | |
109 function ii = getInfo(varargin) | |
110 if nargin == 1 && strcmpi(varargin{1}, 'None') | |
111 sets = {}; | |
112 pl = []; | |
113 else | |
114 sets = {'Default'}; | |
115 pl = getDefaultPlist; | |
116 end | |
117 % Build info object | |
118 ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: downsample.m,v 1.34 2011/07/14 05:33:18 mauro Exp $', sets, pl); | |
119 end | |
120 | |
121 %-------------------------------------------------------------------------- | |
122 % Get Default Plist | |
123 %-------------------------------------------------------------------------- | |
124 | |
125 function plout = getDefaultPlist() | |
126 persistent pl; | |
127 if ~exist('pl', 'var') || isempty(pl) | |
128 pl = buildplist(); | |
129 end | |
130 plout = pl; | |
131 end | |
132 | |
133 function pl = buildplist() | |
134 | |
135 pl = plist(); | |
136 | |
137 % Factor | |
138 p = param({'factor', 'The decimation factor. Should be an integer.'}, {1, {1}, paramValue.OPTIONAL}); | |
139 pl.append(p); | |
140 | |
141 % Offset | |
142 p = param({'offset', 'The sample offset used in the decimation.'}, {1, {0}, paramValue.OPTIONAL}); | |
143 pl.append(p); | |
144 | |
145 end | |
146 | |
147 |