0001 function filtered = Filter(samples,varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 passband = [];
0033 stopband = [];
0034 order = 4;
0035 ripple = 20;
0036 nyquist = 625;
0037 type = 'cheby2';
0038
0039
0040 if nargin < 1 | mod(length(varargin),2) ~= 0,
0041 error('Incorrect number of parameters (type ''help <a href="matlab:help Filter">Filter</a>'' for details).');
0042 end
0043
0044
0045 if size(samples,2) < 2,
0046 error('Parameter ''samples'' is not a matrix (type ''help <a href="matlab:help Filter">Filter</a>'' for details).');
0047 end
0048
0049 if isempty(samples),
0050 filtered = samples;
0051 return;
0052 end
0053
0054
0055 for i = 1:2:length(varargin),
0056 if ~ischar(varargin{i}),
0057 error(['Parameter ' num2str(i) ' is not a property (type ''help <a href="matlab:help Filter">Filter</a>'' for details).']);
0058 end
0059 switch(lower(varargin{i})),
0060 case 'passband',
0061 if ~isempty(stopband),
0062 error('Cannot specify both a passband and stopband (type ''help <a href="matlab:help Filter">Filter</a>'' for details).');
0063 end
0064 passband = varargin{i+1};
0065 if ~isdvector(passband,'#2','>=0'),
0066 error('Incorrect value for ''passband'' (type ''help <a href="matlab:help Filter">Filter</a>'' for details).');
0067 end
0068
0069 case 'stopband',
0070 if ~isempty(passband),
0071 error('Cannot specify both a passband and stopband (type ''help <a href="matlab:help Filter">Filter</a>'' for details).');
0072 end
0073 stopband = varargin{i+1};
0074 if ~isdvector(stopband,'#2','>=0'),
0075 error('Incorrect value for ''stopband'' (type ''help <a href="matlab:help Filter">Filter</a>'' for details).');
0076 end
0077
0078 case 'filter',
0079 type = lower(varargin{i+1});
0080 if ~isastring(type,'cheby2','fir1'),
0081 error(['Unknown filter type ''' type ''' (type ''help <a href="matlab:help Filter">Filter</a>'' for details).']);
0082 end
0083
0084 case 'order',
0085 order = lower(varargin{i+1});
0086 if ~isiscalar(order,'>0'),
0087 error('Incorrect value for ''order'' (type ''help <a href="matlab:help Filter">Filter</a>'' for details).');
0088 end
0089
0090 case 'ripple',
0091 ripple = lower(varargin{i+1});
0092 if ~isiscalar(ripple,'>0'),
0093 error('Incorrect value for ''ripple'' (type ''help <a href="matlab:help Filter">Filter</a>'' for details).');
0094 end
0095
0096 case 'nyquist',
0097 nyquist = varargin{i+1};
0098 if ~isiscalar(nyquist,'>0'),
0099 error('Incorrect value for property ''nyquist'' (type ''help <a href="matlab:help Filter">Filter</a>'' for details).');
0100 end
0101
0102 otherwise,
0103 error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help Filter">Filter</a>'' for details).']);
0104
0105 end
0106 end
0107
0108 if isempty(passband) && isempty(stopband),
0109 error('Missing passband or stopband (type ''help <a href="matlab:help Filter">Filter</a>'' for details).');
0110 end
0111
0112 switch(type),
0113 case 'cheby2',
0114 if ~isempty(passband),
0115 if passband(1) == 0,
0116 [b a] = cheby2(order,ripple,passband(2)/nyquist,'low');
0117 else
0118 [b a] = cheby2(order,ripple,passband/nyquist);
0119 end
0120 else
0121 [b a] = cheby2(order,ripple,stopband/nyquist,'stop');
0122 end
0123 case 'fir1',
0124 if ~isempty(passband),
0125 if passband(1) == 0,
0126 [b a] = fir1(order,passband(2)/nyquist,'low');
0127 else
0128 [b a] = fir1(order,passband/nyquist);
0129 end
0130 else
0131 [b a] = fir1(order,stopband/nyquist,'stop');
0132 end
0133 end
0134 filtered(:,1) = samples(:,1);
0135 for i = 2:size(samples,2),
0136 filtered(:,i) = filtfilt(b,a,samples(:,i));
0137 end