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
|
/*
* Copyright (c) 2008 MUSIC TECHNOLOGY GROUP (MTG)
* UNIVERSITAT POMPEU FABRA
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/*! \file windows.c
* \brief functions for creating various windows
*
* Use sms_getWindow() for selecting which window will be made
*/
#include "sms.h"
/* \brief scale a window by its integral (numeric quadrature)
*
* In order to get a normalized magnitude spectrum (ex. Fourier analysis
* of a sinusoid with linear magnitude 1 gives one peak of magnitude 1 in
* the frequency domain), the spectrum windowing function should be
* normalized by its area under the curve.
*
* \param sizeWindow the size of the window
* \param pWindow pointer to an array that will hold the window
*/
void sms_scaleWindow(int sizeWindow, sfloat *pWindow)
{
int i;
sfloat fSum = 0;
sfloat fScale;
for(i = 0; i < sizeWindow; i++)
fSum += pWindow[i];
fScale = 2. / fSum;
for(i = 0; i < sizeWindow; i++)
pWindow[i] *= fScale;
}
/*! \brief window to be used in the IFFT synthesis
*
* contains both an inverse Blackman-Harris and triangular window.
*
* \todo read X. Rodet, Ph. Depalle, "Spectral Envelopes and Inverse FFT
* Synthesis." Proc. 93rd AES Convention, October 1992
* \param sizeWindow the size of the window
* \param pFWindow pointer to an array that will hold the window
*/
void IFFTwindow(int sizeWindow, sfloat *pFWindow)
{
int i;
sfloat a0 = .35875, a1 = .48829, a2 = .14128, a3 = .01168;
double fConst = TWO_PI / sizeWindow, fIncr = 2.0 /sizeWindow, fVal = 0;
/* compute inverse of Blackman-Harris 92dB window */
for(i = 0; i < sizeWindow; i++)
{
pFWindow[i] = 1 / (a0 - a1 * cos(fConst * i) +
a2 * cos(fConst * 2 * i) - a3 * cos(fConst * 3 * i));
}
/* scale function by a triangular */
for(i = 0; i < sizeWindow / 2; i++)
{
pFWindow[i] = fVal * pFWindow[i] / 2.787457;
fVal += fIncr;
}
for(i = sizeWindow / 2; i < sizeWindow; i++)
{
pFWindow[i] = fVal * pFWindow[i] / 2.787457;
fVal -= fIncr;
}
}
/*! \brief BlackmanHarris window with 62dB rolloff
*
* \todo where did these come from?
* \param sizeWindow the size of the window
* \param pFWindow pointer to an array that will hold the window
*/
void BlackmanHarris62(int sizeWindow, sfloat *pFWindow)
{
int i;
double fSum = 0;
/* for 3 term -62.05 */
sfloat a0 = .44959, a1 = .49364, a2 = .05677;
double fConst = TWO_PI / sizeWindow;
/* compute window */
for(i = 0; i < sizeWindow; i++)
{
fSum += pFWindow[i] = a0 - a1 * cos(fConst * i) +
a2 * cos(fConst * 2 * i);
}
}
/*! \brief BlackmanHarris window with 70dB rolloff
*
* \param sizeWindow the size of the window
* \param pFWindow pointer to an array that will hold the window
*/
void BlackmanHarris70(int sizeWindow, sfloat *pFWindow)
{
int i;
double fSum = 0;
/* for 3 term -70.83 */
sfloat a0 = .42323, a1 = .49755, a2 = .07922;
double fConst = TWO_PI / sizeWindow;
/* compute window */
for(i = 0; i < sizeWindow; i++)
{
fSum += pFWindow[i] = a0 - a1 * cos(fConst * i) +
a2 * cos(fConst * 2 * i);
}
}
/*! \brief BlackmanHarris window with 74dB rolloff
*
* \param sizeWindow the size of the window
* \param pFWindow pointer to an array that will hold the window
*/
void BlackmanHarris74(int sizeWindow, sfloat *pFWindow)
{
int i;
double fSum = 0;
/* for -74dB from the Nuttall paper */
sfloat a0 = .40217, a1 = .49703, a2 = .09892, a3 = .00188;
double fConst = TWO_PI / sizeWindow;
/* compute window */
for(i = 0; i < sizeWindow; i++)
{
fSum += pFWindow[i] = a0 - a1 * cos(fConst * i) +
a2 * cos(fConst * 2 * i) + a3 * cos(fConst * 3 * i);
}
}
/*! \brief BlackmanHarris window with 92dB rolloff
*
* \param sizeWindow the size of the window
* \param pFWindow pointer to an array that will hold the window
*/
void BlackmanHarris92(int sizeWindow, sfloat *pFWindow)
{
int i;
double fSum = 0;
/* for -92dB */
sfloat a0 = .35875, a1 = .48829, a2 = .14128, a3 = .01168;
double fConst = TWO_PI / sizeWindow;
/* compute window */
for(i = 0; i < sizeWindow; i++)
{
fSum += pFWindow[i] = a0 - a1 * cos(fConst * i) +
a2 * cos(fConst * 2 * i) + a3 * cos(fConst * 3 * i);
}
}
/*! \brief default BlackmanHarris window (70dB rolloff)
*
* \param sizeWindow the size of the window
* \param pFWindow pointer to an array that will hold the window
*/
void BlackmanHarris(int sizeWindow, sfloat *pFWindow)
{
BlackmanHarris70(sizeWindow, pFWindow);
}
/*! \brief Hamming window
*
* \param sizeWindow window size
* \param pWindow window array
*/
void Hamming(int sizeWindow, sfloat *pWindow)
{
int i;
sfloat fSum = 0;
for(i = 0; i < sizeWindow; i++)
{
fSum += pWindow[i] = 0.53836 - 0.46164*cos(TWO_PI*i/(sizeWindow-1));
}
}
/*! \brief Hanning window
*
* \param sizeWindow window size
* \param pWindow window array
*/
void Hanning(int sizeWindow, sfloat *pWindow)
{
int i;
for(i = 0; i < sizeWindow; i++)
pWindow[i] = (sin(PI*i/(sizeWindow-1)))*(sin(PI*i/(sizeWindow-1)));
}
/*! \brief main function for getting various windows
*
* \todo note on window scales
*
* \see SMS_WINDOWS for the different window types available
* \param sizeWindow window size
* \param pFWindow window array
* \param iWindowType the desired window type defined by #SMS_WINDOWS
*/
void sms_getWindow(int sizeWindow, sfloat *pFWindow, int iWindowType)
{
switch(iWindowType)
{
case SMS_WIN_BH_62:
BlackmanHarris62(sizeWindow, pFWindow);
break;
case SMS_WIN_BH_70:
BlackmanHarris70(sizeWindow, pFWindow);
break;
case SMS_WIN_BH_74:
BlackmanHarris74(sizeWindow, pFWindow);
break;
case SMS_WIN_BH_92:
BlackmanHarris92(sizeWindow, pFWindow);
break;
case SMS_WIN_HAMMING:
Hamming(sizeWindow, pFWindow);
break;
case SMS_WIN_HANNING:
Hanning(sizeWindow, pFWindow);
break;
case SMS_WIN_IFFT:
IFFTwindow(sizeWindow, pFWindow);
break;
default:
BlackmanHarris(sizeWindow, pFWindow);
}
}
/*! \brief apply a window and center around sample 0
*
* function to center a waveform around sample 0, also known
* as 'zero-phase windowing'. Half the samples are at the beginning,
* half at the end, with the remaining samples (sizeFft-sizeWindow)
* in the middle (zero-padding for an interpolated spectrum).
*
* \todo do I need to garuntee that sizeWindow is odd-lengthed?
*
* \param sizeWindow size of waveform/waveform
* \param pWaveform pointer to waveform
* \param pWindow pointer to window
* \param sizeFft size of FFT
* \param pFftBuffer pointer to FFT buffer
*/
void sms_windowCentered(int sizeWindow, sfloat *pWaveform, sfloat *pWindow,
int sizeFft, sfloat *pFftBuffer)
{
int iMiddleWindow = (sizeWindow+1) >> 1;
int iOffset = sizeFft - (iMiddleWindow - 1);
int i;
for(i=0; i<iMiddleWindow-1; i++)
pFftBuffer[iOffset + i] = pWindow[i] * pWaveform[i];
iOffset = iMiddleWindow - 1;
for(i=0; i<iMiddleWindow; i++)
pFftBuffer[i] = pWindow[iOffset + i] * pWaveform[iOffset + i];
}
|