Home > FMAToolbox > General > Filter.m

Filter

PURPOSE ^

Filter - Filter samples.

SYNOPSIS ^

function filtered = Filter(samples,varargin)

DESCRIPTION ^

Filter - Filter samples.

  USAGE

    filtered = Filter(samples,<options>)

    samples        <a href="matlab:help samples">samples</a> to filter
    <options>      optional list of property-value pairs (see table below)

    =========================================================================
     Properties    Values
    -------------------------------------------------------------------------
     'passband'    pass frequency range
     'stopband'    stop frequency range
     'order'       filter order (default = 4)
     'ripple'      filter ripple (default = 20)
     'nyquist'     nyquist frequency (default = 625)
     'filter'      choose filter type between 'cheby2' (default) and 'fir1'
    =========================================================================

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function filtered = Filter(samples,varargin)
0002 
0003 %Filter - Filter samples.
0004 %
0005 %  USAGE
0006 %
0007 %    filtered = Filter(samples,<options>)
0008 %
0009 %    samples        <a href="matlab:help samples">samples</a> to filter
0010 %    <options>      optional list of property-value pairs (see table below)
0011 %
0012 %    =========================================================================
0013 %     Properties    Values
0014 %    -------------------------------------------------------------------------
0015 %     'passband'    pass frequency range
0016 %     'stopband'    stop frequency range
0017 %     'order'       filter order (default = 4)
0018 %     'ripple'      filter ripple (default = 20)
0019 %     'nyquist'     nyquist frequency (default = 625)
0020 %     'filter'      choose filter type between 'cheby2' (default) and 'fir1'
0021 %    =========================================================================
0022 %
0023 
0024 % Copyright (C) 2004-2011 by Michaƫl Zugaro
0025 %
0026 % This program is free software; you can redistribute it and/or modify
0027 % it under the terms of the GNU General Public License as published by
0028 % the Free Software Foundation; either version 3 of the License, or
0029 % (at your option) any later version.
0030 
0031 % Default values
0032 passband = [];
0033 stopband = [];
0034 order = 4;
0035 ripple = 20;
0036 nyquist = 625;
0037 type = 'cheby2';
0038 
0039 % Check number of parameters
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 % Check parameter sizes
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 % Parse parameter list
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

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005