1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2006-2009  Josh Coalson
3  * Copyright (C) 2011-2013  Xiph.Org Foundation
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * - Neither the name of the Xiph.org Foundation nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifdef HAVE_CONFIG_H
34 #  include <config.h>
35 #endif
36 
37 #include <math.h>
38 #include "share/compat.h"
39 #include "FLAC/assert.h"
40 #include "FLAC/format.h"
41 #include "private/window.h"
42 
43 #ifndef FLAC__INTEGER_ONLY_LIBRARY
44 
45 
FLAC__window_bartlett(FLAC__real * window,const FLAC__int32 L)46 void FLAC__window_bartlett(FLAC__real *window, const FLAC__int32 L)
47 {
48 	const FLAC__int32 N = L - 1;
49 	FLAC__int32 n;
50 
51 	if (L & 1) {
52 		for (n = 0; n <= N/2; n++)
53 			window[n] = 2.0f * n / (float)N;
54 		for (; n <= N; n++)
55 			window[n] = 2.0f - 2.0f * n / (float)N;
56 	}
57 	else {
58 		for (n = 0; n <= L/2-1; n++)
59 			window[n] = 2.0f * n / (float)N;
60 		for (; n <= N; n++)
61 			window[n] = 2.0f - 2.0f * (N-n) / (float)N;
62 	}
63 }
64 
FLAC__window_bartlett_hann(FLAC__real * window,const FLAC__int32 L)65 void FLAC__window_bartlett_hann(FLAC__real *window, const FLAC__int32 L)
66 {
67 	const FLAC__int32 N = L - 1;
68 	FLAC__int32 n;
69 
70 	for (n = 0; n < L; n++)
71 		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)));
72 }
73 
FLAC__window_blackman(FLAC__real * window,const FLAC__int32 L)74 void FLAC__window_blackman(FLAC__real *window, const FLAC__int32 L)
75 {
76 	const FLAC__int32 N = L - 1;
77 	FLAC__int32 n;
78 
79 	for (n = 0; n < L; n++)
80 		window[n] = (FLAC__real)(0.42f - 0.5f * cos(2.0f * M_PI * n / N) + 0.08f * cos(4.0f * M_PI * n / N));
81 }
82 
83 /* 4-term -92dB side-lobe */
FLAC__window_blackman_harris_4term_92db_sidelobe(FLAC__real * window,const FLAC__int32 L)84 void FLAC__window_blackman_harris_4term_92db_sidelobe(FLAC__real *window, const FLAC__int32 L)
85 {
86 	const FLAC__int32 N = L - 1;
87 	FLAC__int32 n;
88 
89 	for (n = 0; n <= N; n++)
90 		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));
91 }
92 
FLAC__window_connes(FLAC__real * window,const FLAC__int32 L)93 void FLAC__window_connes(FLAC__real *window, const FLAC__int32 L)
94 {
95 	const FLAC__int32 N = L - 1;
96 	const double N2 = (double)N / 2.;
97 	FLAC__int32 n;
98 
99 	for (n = 0; n <= N; n++) {
100 		double k = ((double)n - N2) / N2;
101 		k = 1.0f - k * k;
102 		window[n] = (FLAC__real)(k * k);
103 	}
104 }
105 
FLAC__window_flattop(FLAC__real * window,const FLAC__int32 L)106 void FLAC__window_flattop(FLAC__real *window, const FLAC__int32 L)
107 {
108 	const FLAC__int32 N = L - 1;
109 	FLAC__int32 n;
110 
111 	for (n = 0; n < L; n++)
112 		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));
113 }
114 
FLAC__window_gauss(FLAC__real * window,const FLAC__int32 L,const FLAC__real stddev)115 void FLAC__window_gauss(FLAC__real *window, const FLAC__int32 L, const FLAC__real stddev)
116 {
117 	const FLAC__int32 N = L - 1;
118 	const double N2 = (double)N / 2.;
119 	FLAC__int32 n;
120 
121 	for (n = 0; n <= N; n++) {
122 		const double k = ((double)n - N2) / (stddev * N2);
123 		window[n] = (FLAC__real)exp(-0.5f * k * k);
124 	}
125 }
126 
FLAC__window_hamming(FLAC__real * window,const FLAC__int32 L)127 void FLAC__window_hamming(FLAC__real *window, const FLAC__int32 L)
128 {
129 	const FLAC__int32 N = L - 1;
130 	FLAC__int32 n;
131 
132 	for (n = 0; n < L; n++)
133 		window[n] = (FLAC__real)(0.54f - 0.46f * cos(2.0f * M_PI * n / N));
134 }
135 
FLAC__window_hann(FLAC__real * window,const FLAC__int32 L)136 void FLAC__window_hann(FLAC__real *window, const FLAC__int32 L)
137 {
138 	const FLAC__int32 N = L - 1;
139 	FLAC__int32 n;
140 
141 	for (n = 0; n < L; n++)
142 		window[n] = (FLAC__real)(0.5f - 0.5f * cos(2.0f * M_PI * n / N));
143 }
144 
FLAC__window_kaiser_bessel(FLAC__real * window,const FLAC__int32 L)145 void FLAC__window_kaiser_bessel(FLAC__real *window, const FLAC__int32 L)
146 {
147 	const FLAC__int32 N = L - 1;
148 	FLAC__int32 n;
149 
150 	for (n = 0; n < L; n++)
151 		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));
152 }
153 
FLAC__window_nuttall(FLAC__real * window,const FLAC__int32 L)154 void FLAC__window_nuttall(FLAC__real *window, const FLAC__int32 L)
155 {
156 	const FLAC__int32 N = L - 1;
157 	FLAC__int32 n;
158 
159 	for (n = 0; n < L; n++)
160 		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));
161 }
162 
FLAC__window_rectangle(FLAC__real * window,const FLAC__int32 L)163 void FLAC__window_rectangle(FLAC__real *window, const FLAC__int32 L)
164 {
165 	FLAC__int32 n;
166 
167 	for (n = 0; n < L; n++)
168 		window[n] = 1.0f;
169 }
170 
FLAC__window_triangle(FLAC__real * window,const FLAC__int32 L)171 void FLAC__window_triangle(FLAC__real *window, const FLAC__int32 L)
172 {
173 	FLAC__int32 n;
174 
175 	if (L & 1) {
176 		for (n = 1; n <= L+1/2; n++)
177 			window[n-1] = 2.0f * n / ((float)L + 1.0f);
178 		for (; n <= L; n++)
179 			window[n-1] = - (float)(2 * (L - n + 1)) / ((float)L + 1.0f);
180 	}
181 	else {
182 		for (n = 1; n <= L/2; n++)
183 			window[n-1] = 2.0f * n / (float)L;
184 		for (; n <= L; n++)
185 			window[n-1] = ((float)(2 * (L - n)) + 1.0f) / (float)L;
186 	}
187 }
188 
FLAC__window_tukey(FLAC__real * window,const FLAC__int32 L,const FLAC__real p)189 void FLAC__window_tukey(FLAC__real *window, const FLAC__int32 L, const FLAC__real p)
190 {
191 	if (p <= 0.0)
192 		FLAC__window_rectangle(window, L);
193 	else if (p >= 1.0)
194 		FLAC__window_hann(window, L);
195 	else {
196 		const FLAC__int32 Np = (FLAC__int32)(p / 2.0f * L) - 1;
197 		FLAC__int32 n;
198 		/* start with rectangle... */
199 		FLAC__window_rectangle(window, L);
200 		/* ...replace ends with hann */
201 		if (Np > 0) {
202 			for (n = 0; n <= Np; n++) {
203 				window[n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * n / Np));
204 				window[L-Np-1+n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * (n+Np) / Np));
205 			}
206 		}
207 	}
208 }
209 
FLAC__window_welch(FLAC__real * window,const FLAC__int32 L)210 void FLAC__window_welch(FLAC__real *window, const FLAC__int32 L)
211 {
212 	const FLAC__int32 N = L - 1;
213 	const double N2 = (double)N / 2.;
214 	FLAC__int32 n;
215 
216 	for (n = 0; n <= N; n++) {
217 		const double k = ((double)n - N2) / N2;
218 		window[n] = (FLAC__real)(1.0f - k * k);
219 	}
220 }
221 
222 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
223