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
/*
MIT License
Copyright (c) 2023 Philipp Schuster
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
//! Several window functions which you can apply before doing the FFT.
//! For more information:
//! - <https://en.wikipedia.org/wiki/Window_function>
//! - <https://www.youtube.com/watch?v=dCeHOf4cJE0> (FFT and windowing by Texas Instruments)
//!
//! A window reduces spectral leakage: without one, a frequency that does not
//! fit a whole number of times into the block smears over the whole spectrum
//! and can bury quieter frequencies. Applying one is almost always the right
//! choice.
//!
//! ## Which window should I use?
//! * [`hann_window`]: the default. A good compromise, use it unless you have
//! a reason not to.
//! * [`hamming_window`]: close to Hann, with slightly different trade-offs
//! between near and distant leakage.
//! * [`blackman_harris_4term`]: when a quiet frequency sits next to a loud
//! one. It suppresses distant leakage much more, at the price of lower and
//! wider peaks.
//! * [`blackman_harris_7term`]: the same idea taken further. Rarely needed.
//!
//! Skipping the window only makes sense if every frequency fits a whole
//! number of times into the block, which in practice means synthetic signals.
//!
//! ## Periodic and symmetric windows
//! Every window comes in two variants that differ in a single value. NumPy
//! and SciPy build the *symmetric* one by default, which is made for filter
//! design. This crate uses the *periodic* one, the variant for FFT analysis,
//! which also makes the coherent gain below exact.
//!
//! You only notice the difference when comparing coefficients with another
//! library: for `N` values, the periodic variant is the symmetric one for
//! `N + 1` values with the last value cut off. See [conventions] on
//! Wikipedia and the `sym` parameter in [SciPy].
//!
//! Every window shrinks the values in the spectrum by a constant factor, its
//! coherent gain (the average of its coefficients, i.e., the first
//! coefficient of the cosine sum). Divide by it to undo the
//! effect, see [`crate::samples_fft_to_spectrum`].
//!
//! [conventions]: https://en.wikipedia.org/wiki/Window_function#Conventions
//! [SciPy]: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.windows.hann.html
use Vec;
use PI;
// replacement for std functions like sin and cos in no_std-environments
use cosf;
/// Applies a Hann window (<https://en.wikipedia.org/wiki/Window_function#Hann_and_Hamming_windows>)
/// to an array of samples.
///
/// Coherent gain: `0.5`, i.e., the values in the spectrum are halved.
///
/// See the [module docs](crate::windows) for picking a window function.
///
/// ## Return value
/// New vector with Hann window applied to the values.
/// Applies a Hamming window (<https://en.wikipedia.org/wiki/Window_function#Hann_and_Hamming_windows>)
/// to an array of samples.
///
/// Coherent gain: `0.54`.
///
/// See the [module docs](crate::windows) for picking a window function.
///
/// ## Return value
/// New vector with Hamming window applied to the values.
/// Applies a Blackman-Harris 4-term window (<https://en.wikipedia.org/wiki/Window_function#Blackman%E2%80%93Harris_window>)
/// to an array of samples.
///
/// Coherent gain: `0.35875`.
///
/// See the [module docs](crate::windows) for picking a window function.
///
/// ## Return value
/// New vector with Blackman-Harris 4-term window applied to the values.
/// Applies a Blackman-Harris 7-term window to an array of samples.
///
/// Coherent gain: `0.2710514`.
///
/// See the [module docs](crate::windows) for picking a window function.
///
/// ## More information
/// * <https://en.wikipedia.org/wiki/Window_function#Blackman%E2%80%93Harris_window>
/// * <https://ieeexplore.ieee.org/document/940309>
/// * <https://dsp.stackexchange.com/questions/51095/seven-term-blackman-harris-window>
///
/// ## Return value
/// New vector with Blackman-Harris 7-term window applied to the values.
/// Applies a Blackman-Harris x-term window
/// (<https://en.wikipedia.org/wiki/Window_function#Blackman%E2%80%93Harris_window>)
/// to an array of samples. The x is specified by `alphas.len()`.
///
/// ## Return value
/// New vector with Blackman-Harris x-term window applied to the values.