Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[055026]: filterbank / erbfilters.m Maximize Restore History

Download this file

erbfilters.m    194 lines (171 with data), 6.5 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
function [g,a,fc]=erbfilters(fs,varargin)
%ERBFILTERS ERB-spaced filters
% Usage: [g,a,fc]=erbfilters(fs);
% [g,a,fc]=erbfilters(fs,...);
%
% Input parameters:
% fs : Sampling rate (in Hz).
% Output parameters:
% g : Cell array of filters.
% a : Downsampling rate for each channel.
% fc : Center frequency of each channel.
%
% `[g,a,fc]=erbfilters(fs)` constructs a set of filters *g* that are
% equidistantly spaced on the ERB-scale (see |freqtoerb|) with bandwidths
% that are proportional to the width of the auditory filters
% |audfiltbw|. The filters are intended to work with signals with a
% sampling rate of *fs*.
%
% By default, a Hann window on the frequency side is choosen, but the
% window can be changed by passing any of the window types from
% |firwin| as an optional parameter.
%
% Because the downsampling rates of the channels must all divide the
% signal length, |filterbank| will only work for multiples of the
% least common multiple of the downsampling rates. See the help of
% |filterbanklength|.
%
% `[g,a,fc]=erbfilters(fs,L,'fractional')` constructs a filterbank with
% fractional downsampling rates *a*. The rates are constructed such
% that the filterbank can handle signal length that are multiples of
% *L*, so the benefit of the fractional downsampling is that you get to
% choose the value returned by |filterbanklength|.
%
% `[g,a,fc]=erbfilters(fs,'uniform')` constructs a uniform filterbank
% where the downsampling rate is the same for all channels.
%
% `erbfilters` accepts the following optional parameters:
%
% 'spacing',b Specify the spacing in ERBS between the
% filters. Default value is *b=1*.
%
% 'N',N Specify the number of filters, *N*. If this
% parameter is specified, it overwrites the
% `'spacing'`
% parameter.
%
% 'nonuniform' Construct a non-uniform filterbank. This is the
% default.
%
% 'uniform' Construct a uniform filterbank.
%
% 'symmetric' Create filters that are symmetric around their centre
% frequency. This is the default.
%
% 'warped' Create asymmetric filters that are symmetric on the
% Erb-scale.
%
% 'real' Construct a filterbank that works for real-valued
% signals only (the filters cover only the positive
% frequencies). This is the default.
%
% 'complex' Construct a filterbank that covers the entire
% frequency range.
%
% 'regsampling' Choose the downsampling rates to be products of 2
% and 3 (see |floor23| and |ceil23|). This is the
% default.
%
% 'fractional' Use fractional downsampling. If this flag is
% specified, you must also specify the `'L'` parameter.
%
% 'L',L Specify a transform length for which the fractional
% sampling rates must match up.
%
% 'bwmul',bwmul Bandwidth of the filters relative to the bandwidth
% returned by |audfiltbw|. Default is $bwmul=1$.
%
% Examples:
% ---------
%
% In the first example, we construct a highly redudant uniform
% filterbank and visualize the result:::
%
% [f,fs]=greasy; % Get the test signal
% [g,a,fc]=erbfilters(fs,'uniform','N',100);
% c=filterbank(f,g,a);
% plotfilterbank(c,a,fc,fs,90,'audtick');
%
% In the second example, we construct a non-uniform filterbank with
% fractional sampling that works for this particular signal length, and
% test the reconstruction. The plot displays the response of the
% filterbank to verify that the filters are well-behaved both on a
% normal and an ERB-scale:::
%
% [f,fs]=greasy; % Get the test signal
% L=length(f);
% [g,a,fc]=erbfilters(fs,'fractional','L',L);
% c=filterbank(f,{'realdual',g},a);
% r=2*real(ifilterbank(c,g,a));
% norm(f-r)
%
% % Plot the response
% subplot(2,1,1);
% R=filterbankresponse(g,a,L,fs,'real','plot');
%
% subplot(2,1,2);
% semiaudplot(linspace(0,fs/2,L/2+1),R(1:L/2+1));
% ylabel('Magnitude');
%
% See also: filterbank, ufilterbank, ifilterbank, ceil23
%
% References: ltfatnote027
% Authors: Peter L. S��ndergaard
definput.import = {'firwin'};
definput.keyvals.L=[];
definput.keyvals.N=[];
definput.keyvals.bwmul=1;
definput.flags.warp = {'symmetric','warped'};
definput.flags.uniform = {'nonuniform','uniform'};
definput.flags.real = {'real','complex'};
definput.flags.sampling = {'regsampling','fractional'};
[flags,kv,L]=ltfatarghelper({'L'},definput,varargin);
% Get the bandwidth of the choosen window by doing a probe
winbw=norm(firwin(flags.wintype,1000)).^2/1000;
% Construct the Erb filterbank
N=kv.N;
if isempty(N)
N=ceil(freqtoerb(fs/2))+1;
end;
fc=erbspace(0,fs/2,N);
% Improve the scaling of the first and last channel
scal=ones(1,N);
scal(1)=scal(1)/sqrt(2);
scal(N)=scal(N)/sqrt(2);
if flags.do_nonuniform
% Energy scaling works best
scaltype='2';
else
% Peak-frequency scaling works best
scaltype='inf';
end;
if flags.do_symmetric
% fsupp is measured in Hz
fsupp=round(audfiltbw(fc)/winbw*kv.bwmul);
g=blfilter(flags.wintype,fsupp,fc,'fs',fs,'scal',scal,scaltype);
else
% fsupp is measured in Erbs
% The scaling is incorrect, it does not account for the warping
fsupp=1/winbw*kv.bwmul;
g=warpedblfilter(flags.wintype,fsupp,fc,fs,@freqtoerb,@erbtofreq, ...
'scal',scal,scaltype);
% Convert fsupp into the correct widths in Hz, necessary to compute
% "a" in the next if-statement
fsupp=erbtofreq(freqtoerb(fc)+fsupp/2)-erbtofreq(freqtoerb(fc)-fsupp/2);
end;
if flags.do_nonuniform
% Find suitable channel subsampling rates
aprecise=round(fs./fsupp/2); % "/2" is just a heuristic, no justification
aprecise=aprecise(:);
if flags.do_fractional
Nfilts=round(L./aprecise);
a=[repmat(L,N,1),Nfilts];
else
a=ceil23(aprecise); % Grow "a" to the next composite number
% Determine the minimal transform length
L=filterbanklength(1,a);
end;
else
% Totally heuristic, no justification for this choice
a=4;
end;