1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2006-2009  Josh Coalson
3  * Copyright (C) 2011-2016  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 #include <math.h>
34 #include "share/compat.h"
35 #include "FLAC/format.h"
36 #include "private/window.h"
37 
FLAC__window_bartlett(FLAC__real * window,const FLAC__int32 L)38 void FLAC__window_bartlett(FLAC__real *window, const FLAC__int32 L)
39 {
40 	const FLAC__int32 N = L - 1;
41 	FLAC__int32 n;
42 
43 	if (L & 1) {
44 		for (n = 0; n <= N/2; n++)
45 			window[n] = 2.0f * n / (float)N;
46 		for (; n <= N; n++)
47 			window[n] = 2.0f - 2.0f * n / (float)N;
48 	}
49 	else {
50 		for (n = 0; n <= L/2-1; n++)
51 			window[n] = 2.0f * n / (float)N;
52 		for (; n <= N; n++)
53 			window[n] = 2.0f - 2.0f * n / (float)N;
54 	}
55 }
56 
FLAC__window_bartlett_hann(FLAC__real * window,const FLAC__int32 L)57 void FLAC__window_bartlett_hann(FLAC__real *window, const FLAC__int32 L)
58 {
59 	const FLAC__int32 N = L - 1;
60 	FLAC__int32 n;
61 
62 	for (n = 0; n < L; n++)
63 		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)));
64 }
65 
FLAC__window_blackman(FLAC__real * window,const FLAC__int32 L)66 void FLAC__window_blackman(FLAC__real *window, const FLAC__int32 L)
67 {
68 	const FLAC__int32 N = L - 1;
69 	FLAC__int32 n;
70 
71 	for (n = 0; n < L; n++)
72 		window[n] = (FLAC__real)(0.42f - 0.5f * cos(2.0f * M_PI * n / N) + 0.08f * cos(4.0f * M_PI * n / N));
73 }
74 
75 /* 4-term -92dB side-lobe */
FLAC__window_blackman_harris_4term_92db_sidelobe(FLAC__real * window,const FLAC__int32 L)76 void FLAC__window_blackman_harris_4term_92db_sidelobe(FLAC__real *window, const FLAC__int32 L)
77 {
78 	const FLAC__int32 N = L - 1;
79 	FLAC__int32 n;
80 
81 	for (n = 0; n <= N; n++)
82 		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));
83 }
84 
FLAC__window_connes(FLAC__real * window,const FLAC__int32 L)85 void FLAC__window_connes(FLAC__real *window, const FLAC__int32 L)
86 {
87 	const FLAC__int32 N = L - 1;
88 	const double N2 = (double)N / 2.;
89 	FLAC__int32 n;
90 
91 	for (n = 0; n <= N; n++) {
92 		double k = ((double)n - N2) / N2;
93 		k = 1.0f - k * k;
94 		window[n] = (FLAC__real)(k * k);
95 	}
96 }
97 
FLAC__window_flattop(FLAC__real * window,const FLAC__int32 L)98 void FLAC__window_flattop(FLAC__real *window, const FLAC__int32 L)
99 {
100 	const FLAC__int32 N = L - 1;
101 	FLAC__int32 n;
102 
103 	for (n = 0; n < L; n++)
104 		window[n] = (FLAC__real)(0.21557895f - 0.41663158f * cos(2.0f * M_PI * n / N) + 0.277263158f * cos(4.0f * M_PI * n / N) - 0.083578947f * cos(6.0f * M_PI * n / N) + 0.006947368f * cos(8.0f * M_PI * n / N));
105 }
106 
FLAC__window_gauss(FLAC__real * window,const FLAC__int32 L,const FLAC__real stddev)107 void FLAC__window_gauss(FLAC__real *window, const FLAC__int32 L, const FLAC__real stddev)
108 {
109 	const FLAC__int32 N = L - 1;
110 	const double N2 = (double)N / 2.;
111 	FLAC__int32 n;
112 
113 	for (n = 0; n <= N; n++) {
114 		const double k = ((double)n - N2) / (stddev * N2);
115 		window[n] = (FLAC__real)exp(-0.5f * k * k);
116 	}
117 }
118 
FLAC__window_hamming(FLAC__real * window,const FLAC__int32 L)119 void FLAC__window_hamming(FLAC__real *window, const FLAC__int32 L)
120 {
121 	const FLAC__int32 N = L - 1;
122 	FLAC__int32 n;
123 
124 	for (n = 0; n < L; n++)
125 		window[n] = (FLAC__real)(0.54f - 0.46f * cos(2.0f * M_PI * n / N));
126 }
127 
FLAC__window_hann(FLAC__real * window,const FLAC__int32 L)128 void FLAC__window_hann(FLAC__real *window, const FLAC__int32 L)
129 {
130 	const FLAC__int32 N = L - 1;
131 	FLAC__int32 n;
132 
133 	for (n = 0; n < L; n++)
134 		window[n] = (FLAC__real)(0.5f - 0.5f * cos(2.0f * M_PI * n / N));
135 }
136 
FLAC__window_kaiser_bessel(FLAC__real * window,const FLAC__int32 L)137 void FLAC__window_kaiser_bessel(FLAC__real *window, const FLAC__int32 L)
138 {
139 	const FLAC__int32 N = L - 1;
140 	FLAC__int32 n;
141 
142 	for (n = 0; n < L; n++)
143 		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));
144 }
145 
FLAC__window_nuttall(FLAC__real * window,const FLAC__int32 L)146 void FLAC__window_nuttall(FLAC__real *window, const FLAC__int32 L)
147 {
148 	const FLAC__int32 N = L - 1;
149 	FLAC__int32 n;
150 
151 	for (n = 0; n < L; n++)
152 		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));
153 }
154 
FLAC__window_rectangle(FLAC__real * window,const FLAC__int32 L)155 void FLAC__window_rectangle(FLAC__real *window, const FLAC__int32 L)
156 {
157 	FLAC__int32 n;
158 
159 	for (n = 0; n < L; n++)
160 		window[n] = 1.0f;
161 }
162 
FLAC__window_triangle(FLAC__real * window,const FLAC__int32 L)163 void FLAC__window_triangle(FLAC__real *window, const FLAC__int32 L)
164 {
165 	FLAC__int32 n;
166 
167 	if (L & 1) {
168 		for (n = 1; n <= (L+1)/2; n++)
169 			window[n-1] = 2.0f * n / ((float)L + 1.0f);
170 		for (; n <= L; n++)
171 			window[n-1] = (float)(2 * (L - n + 1)) / ((float)L + 1.0f);
172 	}
173 	else {
174 		for (n = 1; n <= L/2; n++)
175 			window[n-1] = 2.0f * n / ((float)L + 1.0f);
176 		for (; n <= L; n++)
177 			window[n-1] = (float)(2 * (L - n + 1)) / ((float)L + 1.0f);
178 	}
179 }
180 
FLAC__window_tukey(FLAC__real * window,const FLAC__int32 L,const FLAC__real p)181 void FLAC__window_tukey(FLAC__real *window, const FLAC__int32 L, const FLAC__real p)
182 {
183 	if (p <= 0.0)
184 		FLAC__window_rectangle(window, L);
185 	else if (p >= 1.0)
186 		FLAC__window_hann(window, L);
187 	else {
188 		const FLAC__int32 Np = (FLAC__int32)(p / 2.0f * L) - 1;
189 		FLAC__int32 n;
190 		/* start with rectangle... */
191 		FLAC__window_rectangle(window, L);
192 		/* ...replace ends with hann */
193 		if (Np > 0) {
194 			for (n = 0; n <= Np; n++) {
195 				window[n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * n / Np));
196 				window[L-Np-1+n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * (n+Np) / Np));
197 			}
198 		}
199 	}
200 }
201 
FLAC__window_partial_tukey(FLAC__real * window,const FLAC__int32 L,const FLAC__real p,const FLAC__real start,const FLAC__real end)202 void FLAC__window_partial_tukey(FLAC__real *window, const FLAC__int32 L, const FLAC__real p, const FLAC__real start, const FLAC__real end)
203 {
204 	const FLAC__int32 start_n = (FLAC__int32)(start * L);
205 	const FLAC__int32 end_n = (FLAC__int32)(end * L);
206 	const FLAC__int32 N = end_n - start_n;
207 	FLAC__int32 Np, n, i;
208 
209 	if (p <= 0.0f)
210 		FLAC__window_partial_tukey(window, L, 0.05f, start, end);
211 	else if (p >= 1.0f)
212 		FLAC__window_partial_tukey(window, L, 0.95f, start, end);
213 	else {
214 
215 		Np = (FLAC__int32)(p / 2.0f * N);
216 
217 		for (n = 0; n < start_n && n < L; n++)
218 			window[n] = 0.0f;
219 		for (i = 1; n < (start_n+Np) && n < L; n++, i++)
220 			window[n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * i / Np));
221 		for (; n < (end_n-Np) && n < L; n++)
222 			window[n] = 1.0f;
223 		for (i = Np; n < end_n && n < L; n++, i--)
224 			window[n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * i / Np));
225 		for (; n < L; n++)
226 			window[n] = 0.0f;
227 	}
228 }
229 
FLAC__window_punchout_tukey(FLAC__real * window,const FLAC__int32 L,const FLAC__real p,const FLAC__real start,const FLAC__real end)230 void FLAC__window_punchout_tukey(FLAC__real *window, const FLAC__int32 L, const FLAC__real p, const FLAC__real start, const FLAC__real end)
231 {
232 	const FLAC__int32 start_n = (FLAC__int32)(start * L);
233 	const FLAC__int32 end_n = (FLAC__int32)(end * L);
234 	FLAC__int32 Ns, Ne, n, i;
235 
236 	if (p <= 0.0f)
237 		FLAC__window_punchout_tukey(window, L, 0.05f, start, end);
238 	else if (p >= 1.0f)
239 		FLAC__window_punchout_tukey(window, L, 0.95f, start, end);
240 	else {
241 
242 		Ns = (FLAC__int32)(p / 2.0f * start_n);
243 		Ne = (FLAC__int32)(p / 2.0f * (L - end_n));
244 
245 		for (n = 0, i = 1; n < Ns && n < L; n++, i++)
246 			window[n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * i / Ns));
247 		for (; n < start_n-Ns && n < L; n++)
248 			window[n] = 1.0f;
249 		for (i = Ns; n < start_n && n < L; n++, i--)
250 			window[n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * i / Ns));
251 		for (; n < end_n && n < L; n++)
252 			window[n] = 0.0f;
253 		for (i = 1; n < end_n+Ne && n < L; n++, i++)
254 			window[n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * i / Ne));
255 		for (; n < L - (Ne) && n < L; n++)
256 			window[n] = 1.0f;
257 		for (i = Ne; n < L; n++, i--)
258 			window[n] = (FLAC__real)(0.5f - 0.5f * cos(M_PI * i / Ne));
259 	}
260 }
261 
FLAC__window_welch(FLAC__real * window,const FLAC__int32 L)262 void FLAC__window_welch(FLAC__real *window, const FLAC__int32 L)
263 {
264 	const FLAC__int32 N = L - 1;
265 	const double N2 = (double)N / 2.;
266 	FLAC__int32 n;
267 
268 	for (n = 0; n <= N; n++) {
269 		const double k = ((double)n - N2) / N2;
270 		window[n] = (FLAC__real)(1.0f - k * k);
271 	}
272 }
273