[249cf9]: fourier / blfilter.m Maximize Restore History

Download this file

blfilter.m    135 lines (120 with data), 5.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
function gout=blfilter(winname,fsupp,varargin)
%BLFILTER Construct a band-limited filter
% Usage: g=blfilter(winname,fsupp,fc);
% g=blfilter(winname,fsupp,fc,...);
%
% Input parameters:
% winname : Name of prototype
% fsupp : Support length of the prototype
%
% `blfilter(winname,fsupp)` constructs a band-limited filter. The parameter
% *winname* specifies the shape of the frequency response. The name must be
% one of the shapes accepted by |firwin|. The support of the frequency
% response measured in normalized frequencies is specified by *fsupp*.
%
% `blfilter(winname,fsupp,fc)` constructs a filter with a centre
% frequency of *fc* measured in normalized frequencies.
%
% If one of the inputs is a vector, the output will be a cell array
% with one entry in the cell array for each element in the vector. If
% more input are vectors, they must have the same size and shape and the
% the filters will be generated by stepping through the vectors. This
% is a quick way to create filters for |filterbank| and |ufilterbank|.
%
% `blfilter` accepts the following optional parameters:
%
% 'fs',fs If the sampling frequency *fs* is specified then the support
% *fsupp* and the centre frequency *fc* is specified in Hz.
%
% 'complex' Make the filter complex valued if the centre frequency
% is non-zero.necessary. This is the default.
%
% 'real' Make the filter real-valued if the centre frequency
% is non-zero.
%
% 'delay',d Set the delay of the filter. Default value is zero.
%
% 'scal',s Scale the filter by the constant *s*. This can be
% useful to equalize channels in a filterbank.
%
% It is possible to normalize the transfer function of the filter by
% passing any of the flags from the |normalize| function. The default
% normalization is `'energy'`.
%
% The filter can be used in the |pfilt| routine to filter a signal, or
% in can be placed in a cell-array for use with |filterbank| or
% |ufilterbank|.
%
% Output format:
% --------------
%
% The output *g* from `blfilter` is a structure. This type of structure can
% be used to describe any bandlimited filter defined in terms of its
% transfer function. The structure contains the following fields:
%
% g.H This is an anonymous function taking the transform length *L* as
% input and producing the bandlimited transfer function in the
% form of a vector.
%
% g.foff This is an anonymous function taking the transform length *L* as
% input and procing the frequency offset of *H* as an integer. The
% offset is the value of the lowest frequency of *H* measured in
% frequency samples. *foff* is used to position the bandlimited
% tranfer function stored in *H* correctly when multiplying in the
% frequency domain.
%
% g.delay This is the desired delay of the filter measured in samples.
%
% g.realonly
% This is an integer with value *1* if the filter defined a
% real-valued filter. In this case, the bandlimited transfer
% function *H* will be mirrored from the positive frequencies to
% the negative frequencies. If the filter is a natural lowpass
% filter correctly centered around *0*, `realonly` does not need
% to be *1*.
%
% g.fs The intended sampling frequency. This is an optional parameter
% that is **only** used for plotting and visualization.
%
% See also: firfilter, firwin, pfilt, filterbank
% Define initial value for flags and key/value pairs.
definput.import={'normalize'};
definput.importdefaults={'energy'};
definput.keyvals.delay=0;
definput.keyvals.fc=0;
definput.keyvals.fs=[];
definput.keyvals.scal=1;
definput.flags.real={'complex','real'};
[flags,kv]=ltfatarghelper({'fc'},definput,varargin);
[fsupp,kv.fc,kv.delay,kv.scal]=scalardistribute(fsupp,kv.fc,kv.delay,kv.scal);
if ~isempty(kv.fs)
fsupp=fsupp/kv.fs*2;
kv.fc=kv.fc/kv.fs*2;
end;
% Sanitize
kv.fc=modcent(kv.fc,2);
Nfilt=numel(fsupp);
gout=cell(1,Nfilt);
for ii=1:Nfilt
g=struct();
if flags.do_1 || flags.do_area
g.H=@(L) fftshift(firwin(winname,floor(L/2*fsupp(ii)), ...
flags.norm))*kv.scal(ii)*L;
end;
if flags.do_2 || flags.do_energy
g.H=@(L) fftshift(firwin(winname,floor(L/2*fsupp(ii)), ...
flags.norm))*kv.scal(ii)*sqrt(L);
end;
if flags.do_inf || flags.do_peak
g.H=@(L) fftshift(firwin(winname,floor(L/2*fsupp(ii)), ...
flags.norm))*kv.scal(ii);
end;
g.foff=@(L) round(L/2*kv.fc(ii))-floor(floor(L/2*fsupp(ii))/2);
g.realonly=flags.do_real;
g.delay=kv.delay(ii);
g.fs=kv.fs;
gout{ii}=g;
end;
if Nfilt==1
gout=g;
end;