comparison m-toolbox/classes/@ao/dsmean.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 % DSMEAN performs a simple downsampling by taking the mean of every N samples.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: DSMEAN performs a simple downsampling by taking the mean of
5 % every N samples. The downsample factor (N) is taken as
6 % round(fs/fsout). The original vector is then truncated to a
7 % integer number of segments of length N. It is the reshaped
8 % to N x length(y)/N. Then the mean is taken.
9 %
10 % CALL: b = dsmean(a, pl)
11 %
12 % <a href="matlab:utils.helper.displayMethodInfo('ao', 'dsmean')">Parameters Description</a>
13 %
14 % VERSION: $Id: dsmean.m,v 1.23 2011/05/11 08:42:19 mauro Exp $
15 %
16 % HISTORY: 20-04-08 M Hewitson
17 % Creation
18 %
19 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
20
21 function varargout = dsmean(varargin)
22
23 % Check if this is a call for parameters
24 if utils.helper.isinfocall(varargin{:})
25 varargout{1} = getInfo(varargin{3});
26 return
27 end
28
29 import utils.const.*
30 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
31
32 % Collect input variable names
33 in_names = cell(size(varargin));
34 for ii = 1:nargin,in_names{ii} = inputname(ii);end
35
36 % Collect all AOs
37 [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names);
38 pl = utils.helper.collect_objects(varargin(:), 'plist', in_names);
39
40 % Decide on a deep copy or a modify
41 bs = copy(as, nargout);
42
43 % Combine plists
44 pl = parse(pl, getDefaultPlist);
45
46 % Extract necessary parameters
47 fsout = find(pl, 'fsout');
48
49 % Loop over input AOs
50 for jj = 1:numel(bs)
51 if ~isa(bs(jj).data, 'tsdata')
52 warning('!!! Can only downsample time-series (tsdata) objects. Skipping AO %s', ao_invars{j});
53 else
54 % downsample factor
55 dsf = round(bs(jj).data.fs/fsout);
56 if dsf < 1
57 error('### I can''t downsample - the sample rate is already lower than the requested.');
58 elseif dsf>1
59 % Do Y data
60 n = floor(length(bs(jj).data.y) / dsf);
61 y = bs(jj).data.y(1:n*dsf);
62 % reshape and take mean
63 bs(jj).data.setY(mean(reshape(y, dsf, length(y)/dsf)));
64
65 % If we have an x we should resample it
66 if ~isempty(bs(jj).data.x)
67 x = bs(jj).data.x(1:n*dsf);
68 % reshape and take mean
69 bs(jj).data.setX(mean(reshape(x, dsf, length(x)/dsf)));
70 else
71 % otherwise we need to adjust t0
72 bs(jj).data.setT0(bs(jj).data.t0 + dsf/(2*bs(jj).data.fs));
73 end
74 end
75 % Build output AO
76 bs(jj).data.setFs(fsout);
77 bs(jj).name = sprintf('dsmean(%s)', ao_invars{jj});
78 % Add history
79 bs(jj).addHistory(getInfo('None'), pl, ao_invars(jj), bs(jj).hist);
80 % Clear the errors since they don't make sense anymore
81 clearErrors(bs(jj));
82 end
83 end
84
85 % Set output
86 varargout = utils.helper.setoutputs(nargout, bs);
87 end
88
89 %--------------------------------------------------------------------------
90 % Get Info Object
91 %--------------------------------------------------------------------------
92 function ii = getInfo(varargin)
93 if nargin == 1 && strcmpi(varargin{1}, 'None')
94 sets = {};
95 pl = [];
96 else
97 sets = {'Default'};
98 pl = getDefaultPlist();
99 end
100 % Build info object
101 ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: dsmean.m,v 1.23 2011/05/11 08:42:19 mauro Exp $', sets, pl);
102 end
103
104 %--------------------------------------------------------------------------
105 % Get Default Plist
106 %--------------------------------------------------------------------------
107
108 function plout = getDefaultPlist()
109 persistent pl;
110 if ~exist('pl', 'var') || isempty(pl)
111 pl = buildplist();
112 end
113 plout = pl;
114 end
115
116 function pl = buildplist()
117 pl = plist({'fsout', 'The output sample rate.'}, {1, {10}, paramValue.OPTIONAL});
118 end
119
120