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
/*
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.
*/
//! This module contains convenient public transform functions that you can use
//! as parameters in [`samples_fft_to_spectrum`] for scaling the frequency value
//! (the FFT result).
//!
//! ## Which scaling should I use?
//! * [`divide_by_N`]: the default. It makes the values independent of the
//! number of samples, so spectra of different lengths are comparable.
//! * [`scale_20_times_log10`]: decibels, for a display that should match how
//! loudness is perceived.
//! * [`scale_to_zero_to_one`]: for a single block where only the relative
//! height of the peaks matters, e.g. a plot or a test. Avoid it for a live
//! view: it normalises every block to its own loudest value, so silence
//! gets amplified to full scale.
//! * [`divide_by_N_sqrt`]: preserves the energy of the signal, for a forward
//! and inverse transform pair.
//! * None at all is fine if you only compare values within one spectrum, for
//! example to find the loudest frequency.
//!
//! To read the amplitude of a tone, use [`divide_by_N`], multiply by `2` and
//! divide by the coherent gain of your window, see
//! [`crate::samples_fft_to_spectrum`].
//!
//! They act as "idea/inspiration". Feel free to create your own derivation
//! from them. To chain two of them, write a closure:
//!
//! ```
//! use spectrum_analyzer::scaling::{divide_by_N, scale_20_times_log10};
//! let scaling_fn = |val, stats: &_| scale_20_times_log10(divide_by_N(val, stats), stats);
//! ```
//!
//! [`samples_fft_to_spectrum`]: crate::samples_fft_to_spectrum
use crateFrequencyValue;
/// Helper struct for [`SpectrumScalingFunction`] that is passed into the
/// scaling function together with the current frequency value.
///
/// This structure can be used to scale each value. All properties reference the
/// current data of a [`FrequencySpectrum`].
///
/// [`FrequencySpectrum`]: crate::FrequencySpectrum
/// [`FrequencyValue`]: crate::FrequencyValue
/// Describes the type for a function that scales/normalizes the data inside
/// [`FrequencySpectrum`].
///
/// The scaling only affects the value of the frequency, but not the
/// frequency itself. It is applied to every single element.
///
/// A scaling function can be used for example to subtract the minimum (`min`)
/// from each value. It is optional to use the second parameter
/// [`SpectrumDataStats`], which describes the spectrum before the function is
/// applied to it.
///
/// The type works with static functions as well as dynamically created
/// closures.
///
/// You must take care of, that you don't have division by zero in your function
/// or that the result is NaN or Infinity (regarding IEEE-754). If the result
/// is NaN or Infinity, the library will return `Err`.
///
/// [`FrequencySpectrum`]: crate::FrequencySpectrum
/// [`FrequencyValue`]: crate::FrequencyValue
pub type SpectrumScalingFunction = dyn Fn ;
/// Lower bound for the input of [`scale_20_times_log10`], i.e., `-100 dB`.
const DB_FLOOR: f32 = 1e-5;
/// Converts each value to decibels: `20 * log10(value)`.
///
/// See the [module docs](crate::scaling) for picking a scaling function.
///
/// A value of `1.0` becomes `0 dB`. Unscaled values grow with the number of
/// samples (see [`crate::samples_fft_to_spectrum`]), so the absolute levels
/// depend on `N` and on the input range. For levels relative to a full-scale
/// sine wave (dBFS), scale to amplitudes first, in a separate call to
/// `apply_scaling_fn`.
///
/// Values below `1e-5` are clamped, so the result is never below `-100 dB`
/// and silence stays at the bottom of the scale.
///
/// This scaling is quite common, you can find more information for example
/// here:
/// <https://www.sjsu.edu/people/burford.furman/docs/me120/FFT_tutorial_NI.pdf>
///
/// ## Usage
/// ```rust
///use spectrum_analyzer::{samples_fft_to_spectrum, scaling, FrequencyLimit};
///let window = [0.0, 0.1, 0.2, 0.3]; // add real data here
///let spectrum = samples_fft_to_spectrum(
/// &window,
/// 44100,
/// FrequencyLimit::All,
/// Some(&scaling::scale_20_times_log10),
/// );
/// ```
/// Function is of type [`SpectrumScalingFunction`].
/// Divides each value by the maximum, so that the loudest frequency becomes
/// `1.0` and every other keeps its ratio to it.
///
/// See the [module docs](crate::scaling) for picking a scaling function.
///
/// The smallest value only becomes `0.0` if it already was `0.0`; the values
/// are not stretched over the whole interval. All of them must be positive or
/// zero, which holds for magnitudes but not for the output of
/// [`scale_20_times_log10`]. If the maximum is `0.0`, all values become `0.0`.
///
/// Function is of type [`SpectrumScalingFunction`].
/// Divides each value by `N`, the number of samples.
///
/// See the [module docs](crate::scaling) for picking a scaling function.
///
/// This makes spectra of different lengths comparable. A sine wave with
/// amplitude `A` on a bin frequency then shows up as `A / 2` (times the
/// window's coherent gain), see [`crate::samples_fft_to_spectrum`].
/// Like [`divide_by_N`] but divides each value by `sqrt(N)`.
///
/// See the [module docs](crate::scaling) for picking a scaling function.
///
/// This is the normalization that preserves the energy of the signal, which
/// `rustfft` recommends for a forward and inverse transform pair. The values
/// still grow with `sqrt(N)`, so for comparing spectra of different lengths
/// use [`divide_by_N`] instead.
/// See <https://docs.rs/rustfft/latest/rustfft/#normalization>