forked from ltfat/ltfat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirwin.m
362 lines (310 loc) · 11.1 KB
/
firwin.m
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
function [g,info]=firwin(name,M,varargin)
%FIRWIN FIR window
% Usage: g=firwin(name,M);
% g=firwin(name,M,...);
% g=firwin(name,x);
%
% `firwin(name,M)` will return an FIR window of length *M* of type *name*.
%
% All windows are symmetric and generate zero delay and zero phase
% filters. They can be used for the Wilson and WMDCT transform, except
% when noted otherwise.
%
% `firwin(name,x)` where *x* is a vector will sample the window
% definition as the specified points. The normal sampling interval for
% the windows is $-.5< x <.5$.
%
% In the following PSL means "Peak Sidelobe level", and the main lobe
% width is measured in normalized frequencies.
%
% If a window *g* forms a "partition of unity" (PU) it means specifically
% that::
%
% g+fftshift(g)==ones(L,1);
%
% A PU can only be formed if the window length is even, but some windows
% may work for odd lengths anyway.
%
% If a window is the square root of a window that forms a PU, the window
% will generate a tight Gabor frame / orthonormal Wilson/WMDCT basis if
% the number of channels is less than *M*.
%
% The windows available are:
%
% 'hann' von Hann window. Forms a PU. The Hann window has a
% mainlobe with of 8/M, a PSL of -31.5 dB and decay rate
% of 18 dB/Octave.
%
% 'sine' Sine window. This is the square root of the Hanning
% window. The sine window has a mainlobe width of 8/M,
% a PSL of -22.3 dB and decay rate of 12 dB/Octave.
% Aliases: `'cosine'`, `'sqrthann'`
%
% 'rect' (Almost) rectangular window. The rectangular window has a
% mainlobe width of 4/M, a PSL of -13.3 dB and decay
% rate of 6 dB/Octave. Forms a PU if the order is odd.
% Alias: `'square'`
%
% 'tria' (Almost) triangular window. Forms a PU. Alias: `'bartlett'`
%
% 'sqrttria' Square root of the triangular window.
%
% 'hamming' Hamming window. Forms a PU that sums to 1.08 instead
% of 1.0 as usual. The Hamming window has a
% mainlobe width of 8/M, a PSL of -42.7 dB and decay
% rate of 6 dB/Octave. This window should not be used for
% a Wilson basis, as a reconstruction window cannot be
% found by |wildual|.
%
% 'blackman' Blackman window. The Blackman window has a
% mainlobe width of 12/M, a PSL of -58.1 dB and decay
% rate of 18 dB/Octave.
%
% 'blackman2' Alternate Blackman window. This window has a
% mainlobe width of 12/M, a PSL of -68.24 dB and decay
% rate of 6 dB/Octave.
%
% 'itersine' Iterated sine window. Generates an orthonormal
% Wilson/WMDCT basis. This window is described in
% Wesfreid and Wickerhauser (1993) and is used in the
% ogg sound codec. Alias: `'ogg'`
%
% 'nuttall' Nuttall window. The Nuttall window has a
% mainlobe width of 16/M, a PSL of -93.32 dB and decay
% rate of 18 dB/Octave.
%
% 'nuttall10' 2-term Nuttall window with 1 continuous derivative.
% Alias: `'hann'`, `'hanning'`.
%
% 'nuttall01' 2-term Nuttall window with 0 continuous derivatives.
% This is a slightly improved Hamming window. It has a
% mainlobe width of 8/M, a PSL of -43.19 dB and decay
% rate of 6 dB/Octave.
%
% 'nuttall20' 3-term Nuttall window with 3 continuous derivatives.
% The window has a mainlobe width of 12/M, a PSL of
% -46.74 dB and decay rate of 30 dB/Octave.
%
% 'nuttall11' 3-term Nuttall window with 1 continuous derivative.
% The window has a mainlobe width of 12/M, a PSL of
% -64.19 dB and decay rate of 18 dB/Octave.
%
% 'nuttall02' 3-term Nuttall window with 0 continuous derivatives.
% The window has a mainlobe width of 12/M, a PSL of
% -71.48 dB and decay rate of 6 dB/Octave.
%
% 'nuttall30' 4-term Nuttall window with 5 continuous derivatives.
% The window has a mainlobe width of 16/M, a PSL of
% -60.95 dB and decay rate of 42 dB/Octave.
%
% 'nuttall21' 4-term Nuttall window with 3 continuous derivatives.
% The window has a mainlobe width of 16/M, a PSL of
% -82.60 dB and decay rate of 30 dB/Octave.
%
% 'nuttall12' 4-term Nuttall window with 1 continuous derivatives.
% Alias: `'nuttall'`.
%
% 'nuttall03' 4-term Nuttall window with 0 continuous derivatives.
% The window has a mainlobe width of 16/M, a PSL of
% -98.17 dB and decay rate of 6 dB/Octave.
%
% 'truncgauss' Gaussian window truncated at 1% of its height.
% Alternatively, a custom percentage can be appended to
% 'truncgauss' e.g. 'truncgauss0.1' will create Gaussian
% window truncated at 0.1% of its height.
%
% `firwin` understands the following flags at the end of the list of input
% parameters:
%
% 'shift',s Shift the window by $s$ samples. The value can be a
% fractional number.
%
% 'wp' Output is whole point even. This is the default. It
% corresponds to a shift of $s=0$.
%
% 'hp' Output is half point even, as most Matlab filter
% routines. This corresponds to a shift of $s=-.5$
%
%
% 'taper',t Extend the window by a flat section in the middle. The
% argument t is the ratio of the rising and falling
% parts as compared to the total length of the
% window. The default value of 1 means no
% tapering. Accepted values lie in the range from 0 to 1.
%
% Additionally, `firwin` accepts flags to normalize the output. Please see
% the help of |normalize|. Default is to use `'peak'` normalization,
% which is useful for using the output from `firwin` for windowing in the
% time-domain. For filtering in the time-domain, a normalization of `'1'`
% or `'area'` is preferable.
%
% Examples:
% ---------
%
% The following plot shows the magnitude response for some common
% windows:::
%
% hold all;
% L=30;
% dr=110;
%
% magresp(firwin('hanning',L,'1'),'fir','dynrange',dr);
% magresp(firwin('hamming',L,'1'),'fir','dynrange',dr);
% magresp(firwin('blackman',L,'1'),'fir','dynrange',dr);
% magresp(firwin('nuttall',L,'1'),'fir','dynrange',dr);
% magresp(firwin('itersine',L,'1'),'fir','dynrange',dr);
%
% legend('Hann','Hamming','Blackman','Nuttall','Itersine');
%
% See also: freqwin, pgauss, pbspline, firkaiser, normalize
%
% References: opsc89 harris1978 nuttall1981 wesfreid1993
% AUTHORS : Peter L. Søndergaard, Nicki Holighaus.
% REFERENCE: NA
if nargin<2
error('%s: Too few input parameters.',upper(mfilename));
end
if ~ischar(name)
error('%s: First input argument must the name of a window.',...
upper(mfilename));
end
if ~isnumeric(M)
error('%s: M must be numeric.',upper(mfilename));
end
% Always set to this
info.isfir=1;
% Default values, may be overwritten later in the code
info.ispu=0;
info.issqpu=0;
name=lower(name);
% Define initial value for flags and key/value pairs.
definput.import={'normalize'};
definput.importdefaults={'null'};
definput.flags.centering={'wp','hp','shift'};
definput.keyvals.shift=0;
definput.keyvals.taper=1;
[flags,kv]=ltfatarghelper({},definput,varargin);
if flags.do_wp
kv.shift=0;
end;
if flags.do_hp
kv.shift=0.5;
end;
if M==0
g=[];
return;
end;
if numel(M)==1
complainif_notposint(M,'M',mfilename);
% Deal with tapering
if kv.taper<1
if kv.taper==0
% Window is only tapering, do this and bail out, because subsequent
% code may fail.
g=ones(M,1);
return;
end;
% Modify M to fit with tapering
Morig=M;
M=round(M*kv.taper);
Mtaper=Morig-M;
p1=round(Mtaper/2);
p2=Mtaper-p1;
% Switch centering if necessary
if flags.do_wp && p1~=p2
kv.shift=.5;
end;
if flags.do_hp && p1~=p2
kv.shift=1;
end;
end;
% This is the normally used sampling vector.
if mod(M,2) == 0 % For even M the sampling interval is [-.5,.5-1/M]
x = [0:1/M:.5-1/M,-.5:1/M:-1/M]';
else % For odd M the sampling interval is [-.5+1/(2M),.5-1/(2M)]
x = [0:1/M:.5-.5/M,-.5+.5/M:1/M:-1/M]';
end
x = x+kv.shift/M;
else
% Use sampling vector specified by the user
x=M;
end;
startswith = 'truncgauss';
if regexpi(name,['^',startswith])
percent = 1;
if numel(name) > numel(startswith)
suffix = name(numel(startswith)+1:end);
percent = str2double(suffix);
if isnan(percent)
error('%s: Passed "%s" and "%s" cannot be parsed to a number.',...
upper(mfilename),name,suffix);
end
end
name = startswith;
end
do_sqrt=0;
switch name
case {'hanning','hann','nuttall10'}
g=(0.5+0.5*cos(2*pi*x));
info.ispu=1;
case {'sine','cosine','sqrthann'}
g=firwin('hanning',M,varargin{:});
info.issqpu=1;
do_sqrt=1;
case 'hamming'
g=0.54+0.46*cos(2*pi*(x));
% This is the definition taken from the Harris paper
%case 'hammingacc'
%g=25/46+21/46*cos(2*pi*(x));
case 'nuttall01'
g=0.53836+0.46164*cos(2*pi*(x));
case {'square','rect'}
g=double(abs(x) < .5);
case {'tria','triangular','bartlett'}
g=1-2*abs(x);
info.ispu=1;
case {'sqrttria'}
g=firwin('tria',M,flags.centering);
info.issqpu=1;
do_sqrt=1;
% Rounded version of blackman2
case {'blackman'}
g=0.42+0.5*cos(2*pi*(x))+0.08*cos(4*pi*(x));
case {'blackman2'}
g=7938/18608+9240/18608*cos(2*pi*(x))+1430/18608*cos(4*pi*(x));
case {'nuttall','nuttall12'}
g = 0.355768+0.487396*cos(2*pi*(x))+0.144232*cos(4*pi*(x)) ...
+0.012604*cos(6*pi*(x));
case {'itersine','ogg'}
g=sin(pi/2*cos(pi*x).^2);
info.issqpu=1;
case {'nuttall20'}
g=3/8+4/8*cos(2*pi*(x))+1/8*cos(4*pi*(x));
case {'nuttall11'}
g=0.40897+0.5*cos(2*pi*(x))+0.09103*cos(4*pi*(x));
case {'nuttall02'}
g=0.4243801+0.4973406*cos(2*pi*(x))+0.0782793*cos(4*pi*(x));
case {'nuttall30'}
g = 10/32+15/32*cos(2*pi*(x))+6/32*cos(4*pi*(x))+1/32*cos(6*pi*(x));
case {'nuttall21'}
g = 0.338946+0.481973*cos(2*pi*(x))+0.161054*cos(4*pi*(x)) ...
+0.018027*cos(6*pi*(x));
case {'nuttall03'}
g=0.3635819+0.4891775*cos(2*pi*(x))+0.1365995*cos(4*pi*(x)) ...
+0.0106411*cos(6*pi*(x));
case {'truncgauss'}
g = exp(4*log(percent/100)*x.^2);
otherwise
error('Unknown window: %s.',name);
end;
% Force the window to 0 outside (-.5,.5)
g = g.*(abs(x) < .5);
if numel(M) == 1 && kv.taper<1
% Perform the actual tapering.
g=[ones(p1,1);g;ones(p2,1)];
end;
% Do sqrt if needed.
if do_sqrt
g=sqrt(g);
end;
g=normalize(g,flags.norm);