2 * Copyright (C) 2001 Anders Johansson ajh@atri.curtin.edu.au
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 /* Calculates a number of window functions. The following window
22 functions are currently implemented: Boxcar, Triang, Hanning,
23 Hamming, Blackman, Flattop and Kaiser. In the function call n is
24 the number of filter taps and w the buffer in which the filter
25 coefficients will be stored.
35 // w buffer for the window parameters
37 void af_window_boxcar(int n
, FLOAT_TYPE
* w
)
40 // Calculate window coefficients
47 // Triang a.k.a Bartlett
52 // w = 1.0 - ---------------
55 // w buffer for the window parameters
57 void af_window_triang(int n
, FLOAT_TYPE
* w
)
59 FLOAT_TYPE k1
= (FLOAT_TYPE
)(n
& 1);
60 FLOAT_TYPE k2
= 1/((FLOAT_TYPE
)n
+ k1
);
61 int end
= (n
+ 1) >> 1;
64 // Calculate window coefficients
65 for (i
=0 ; i
<end
; i
++)
66 w
[i
] = w
[n
-i
-1] = (2.0*((FLOAT_TYPE
)(i
+1))-(1.0-k1
))*k2
;
73 // w = 0.5 - 0.5*cos(------), where 0 < k <= N
76 // w buffer for the window parameters
78 void af_window_hanning(int n
, FLOAT_TYPE
* w
)
81 FLOAT_TYPE k
= 2*M_PI
/((FLOAT_TYPE
)(n
+1)); // 2*pi/(N+1)
83 // Calculate window coefficients
85 *w
++ = 0.5*(1.0 - cos(k
*(FLOAT_TYPE
)(i
+1)));
91 // w(k) = 0.54 - 0.46*cos(------), where 0 <= k < N
95 // w buffer for the window parameters
97 void af_window_hamming(int n
,FLOAT_TYPE
* w
)
100 FLOAT_TYPE k
= 2*M_PI
/((FLOAT_TYPE
)(n
-1)); // 2*pi/(N-1)
102 // Calculate window coefficients
104 *w
++ = 0.54 - 0.46*cos(k
*(FLOAT_TYPE
)i
);
110 // w(k) = 0.42 - 0.5*cos(------) + 0.08*cos(------), where 0 <= k < N
114 // w buffer for the window parameters
116 void af_window_blackman(int n
,FLOAT_TYPE
* w
)
119 FLOAT_TYPE k1
= 2*M_PI
/((FLOAT_TYPE
)(n
-1)); // 2*pi/(N-1)
120 FLOAT_TYPE k2
= 2*k1
; // 4*pi/(N-1)
122 // Calculate window coefficients
124 *w
++ = 0.42 - 0.50*cos(k1
*(FLOAT_TYPE
)i
) + 0.08*cos(k2
*(FLOAT_TYPE
)i
);
130 // w(k) = 0.2810638602 - 0.5208971735*cos(------) + 0.1980389663*cos(------), where 0 <= k < N
134 // w buffer for the window parameters
136 void af_window_flattop(int n
,FLOAT_TYPE
* w
)
139 FLOAT_TYPE k1
= 2*M_PI
/((FLOAT_TYPE
)(n
-1)); // 2*pi/(N-1)
140 FLOAT_TYPE k2
= 2*k1
; // 4*pi/(N-1)
142 // Calculate window coefficients
144 *w
++ = 0.2810638602 - 0.5208971735*cos(k1
*(FLOAT_TYPE
)i
)
145 + 0.1980389663*cos(k2
*(FLOAT_TYPE
)i
);
148 /* Computes the 0th order modified Bessel function of the first kind.
149 // (Needed to compute Kaiser window)
151 // y = sum( (x/(2*n))^2 )
154 #define BIZ_EPSILON 1E-21 // Max error acceptable
156 static FLOAT_TYPE
besselizero(FLOAT_TYPE x
)
159 FLOAT_TYPE sum
= 1.0;
161 FLOAT_TYPE halfx
= x
/2.0;
165 temp
= halfx
/(FLOAT_TYPE
)n
;
169 } while (u
>= BIZ_EPSILON
* sum
);
177 // w buffer for the window parameters
178 // b beta parameter of Kaiser window, Beta >= 1
180 // Beta trades the rejection of the low pass filter against the
181 // transition width from passband to stop band. Larger Beta means a
182 // slower transition and greater stop band rejection. See Rabiner and
183 // Gold (Theory and Application of DSP) under Kaiser windows for more
184 // about Beta. The following table from Rabiner and Gold gives some
185 // feel for the effect of Beta:
187 // All ripples in dB, width of transition band = D*N where N = window
190 // BETA D PB RIP SB RIP
191 // 2.120 1.50 +-0.27 -30
192 // 3.384 2.23 0.0864 -40
193 // 4.538 2.93 0.0274 -50
194 // 5.658 3.62 0.00868 -60
195 // 6.764 4.32 0.00275 -70
196 // 7.865 5.0 0.000868 -80
197 // 8.960 5.7 0.000275 -90
198 // 10.056 6.4 0.000087 -100
200 void af_window_kaiser(int n
, FLOAT_TYPE
* w
, FLOAT_TYPE b
)
203 FLOAT_TYPE k1
= 1.0/besselizero(b
);
204 int k2
= 1 - (n
& 1);
205 int end
= (n
+ 1) >> 1;
208 // Calculate window coefficients
209 for (i
=0 ; i
<end
; i
++){
210 tmp
= (FLOAT_TYPE
)(2*i
+ k2
) / ((FLOAT_TYPE
)n
- 1.0);
211 w
[end
-(1&(!k2
))+i
] = w
[end
-1-i
] = k1
* besselizero(b
*sqrt(1.0 - tmp
*tmp
));