1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2006,2007 Josh Coalson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include "FLAC/assert.h"
38 #include "FLAC/format.h"
39 #include "private/window.h"
41 #ifndef FLAC__INTEGER_ONLY_LIBRARY
44 /* math.h in VC++ doesn't seem to have this (how Microsoft is that?) */
45 #define M_PI 3.14159265358979323846
49 void FLAC__window_bartlett(FLAC__real *window, const FLAC__int32 L)
51 const FLAC__int32 N = L - 1;
55 for (n = 0; n <= N/2; n++)
56 window[n] = 2.0f * n / (float)N;
58 window[n] = 2.0f - 2.0f * n / (float)N;
61 for (n = 0; n <= L/2-1; n++)
62 window[n] = 2.0f * n / (float)N;
64 window[n] = 2.0f - 2.0f * (N-n) / (float)N;
68 void FLAC__window_bartlett_hann(FLAC__real *window, const FLAC__int32 L)
70 const FLAC__int32 N = L - 1;
73 for (n = 0; n < L; n++)
74 window[n] = (FLAC__real)(0.62f - 0.48f * fabs((float)n/(float)N+0.5f) + 0.38f * cos(2.0f * M_PI * ((float)n/(float)N+0.5f)));
77 void FLAC__window_blackman(FLAC__real *window, const FLAC__int32 L)
79 const FLAC__int32 N = L - 1;
82 for (n = 0; n < L; n++)
83 window[n] = (FLAC__real)(0.42f - 0.5f * cos(2.0f * M_PI * n / N) + 0.08f * cos(4.0f * M_PI * n / N));
86 /* 4-term -92dB side-lobe */
87 void FLAC__window_blackman_harris_4term_92db_sidelobe(FLAC__real *window, const FLAC__int32 L)
89 const FLAC__int32 N = L - 1;
92 for (n = 0; n <= N; n++)
93 window[n] = (FLAC__real)(0.35875f - 0.48829f * cos(2.0f * M_PI * n / N) + 0.14128f * cos(4.0f * M_PI * n / N) - 0.01168f * cos(6.0f * M_PI * n / N));
96 void FLAC__window_connes(FLAC__real *window, const FLAC__int32 L)
98 const FLAC__int32 N = L - 1;
99 const double N2 = (double)N / 2.;
102 for (n = 0; n <= N; n++) {
103 double k = ((double)n - N2) / N2;
105 window[n] = (FLAC__real)(k * k);
109 void FLAC__window_flattop(FLAC__real *window, const FLAC__int32 L)
111 const FLAC__int32 N = L - 1;
114 for (n = 0; n < L; n++)
115 window[n] = (FLAC__real)(1.0f - 1.93f * cos(2.0f * M_PI * n / N) + 1.29f * cos(4.0f * M_PI * n / N) - 0.388f * cos(6.0f * M_PI * n / N) + 0.0322f * cos(8.0f * M_PI * n / N));
118 void FLAC__window_gauss(FLAC__real *window, const FLAC__int32 L, const FLAC__real stddev)
120 const FLAC__int32 N = L - 1;
121 const double N2 = (double)N / 2.;
124 for (n = 0; n <= N; n++) {
125 const double k = ((double)n - N2) / (stddev * N2);
126 window[n] = (FLAC__real)exp(-0.5f * k * k);
130 void FLAC__window_hamming(FLAC__real *window, const FLAC__int32 L)
132 const FLAC__int32 N = L - 1;
135 for (n = 0; n < L; n++)
136 window[n] = (FLAC__real)(0.54f - 0.46f * cos(2.0f * M_PI * n / N));
139 void FLAC__window_hann(FLAC__real *window, const FLAC__int32 L)
141 const FLAC__int32 N = L - 1;
144 for (n = 0; n < L; n++)
145 window[n] = (FLAC__real)(0.5f - 0.5f * cos(2.0f * M_PI * n / N));
148 void FLAC__window_kaiser_bessel(FLAC__real *window, const FLAC__int32 L)
150 const FLAC__int32 N = L - 1;
153 for (n = 0; n < L; n++)
154 window[n] = (FLAC__real)(0.402f - 0.498f * cos(2.0f * M_PI * n / N) + 0.098f * cos(4.0f * M_PI * n / N) - 0.001f * cos(6.0f * M_PI * n / N));
157 void FLAC__window_nuttall(FLAC__real *window, const FLAC__int32 L)
159 const FLAC__int32 N = L - 1;
162 for (n = 0; n < L; n++)
163 window[n] = (FLAC__real)(0.3635819f - 0.4891775f*cos(2.0f*M_PI*n/N) + 0.1365995f*cos(4.0f*M_PI*n/N) - 0.0106411f*cos(6.0f*M_PI*n/N));
166 void FLAC__window_rectangle(FLAC__real *window, const FLAC__int32 L)
170 for (n = 0; n < L; n++)
174 void FLAC__window_triangle(FLAC__real *window, const FLAC__int32 L)
179 for (n = 1; n <= L+1/2; n++)
180 window[n-1] = 2.0f * n / ((float)L + 1.0f);
182 window[n-1] = - (float)(2 * (L - n + 1)) / ((float)L + 1.0f);
185 for (n = 1; n <= L/2; n++)
186 window[n-1] = 2.0f * n / (float)L;
188 window[n-1] = ((float)(2 * (L - n)) + 1.0f) / (float)L;
192 void FLAC__window_tukey(FLAC__real *window, const FLAC__int32 L, const FLAC__real p)
195 FLAC__window_rectangle(window, L);
197 FLAC__window_hann(window, L);
199 const FLAC__int32 Np = (FLAC__int32)(p / 2.0f * L) - 1;
201 /* start with rectangle... */
202 FLAC__window_rectangle(window, L);
203 /* ...replace ends with hann */
205 for (n = 0; n <= Np; n++) {
206 window[n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * n / Np));
207 window[L-Np-1+n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * (n+Np) / Np));
213 void FLAC__window_welch(FLAC__real *window, const FLAC__int32 L)
215 const FLAC__int32 N = L - 1;
216 const double N2 = (double)N / 2.;
219 for (n = 0; n <= N; n++) {
220 const double k = ((double)n - N2) / N2;
221 window[n] = (FLAC__real)(1.0f - k * k);
225 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */