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
271
272
273
274
275
276
277
278
//! Reference-defined analysis windows and explicit normalization evidence.
use std::f64::consts::TAU;
use crate::SignalError;
/// Whether a formula includes both endpoints or one period's left endpoint.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum WindowSampling {
/// Include both endpoints. This is appropriate for finite, non-repeating
/// records and uses `N - 1` as the cosine denominator.
#[default]
Symmetric,
/// Sample one period without repeating its endpoint, using `N` as the
/// cosine denominator.
Periodic,
}
/// A reference analysis-window formula or caller-supplied coefficient vector.
#[derive(Clone, Debug, PartialEq)]
pub enum WindowFunction {
/// Constant unit coefficients.
Rectangular,
/// Hann's raised cosine, `0.5 - 0.5 cos(theta)`.
Hann,
/// Hamming's raised cosine, `0.54 - 0.46 cos(theta)`.
Hamming,
/// General three-term Blackman window.
Blackman {
/// Third-term coefficient. The exact Blackman definition uses `0.16`.
alpha: f64,
},
/// Four-term minimum-sidelobe Blackman-Harris window.
BlackmanHarris,
/// Kaiser window based on the order-zero modified Bessel function.
Kaiser {
/// Non-negative sidelobe/width trade-off parameter.
beta: f64,
},
/// Coefficients supplied explicitly by the caller.
Explicit(Vec<f64>),
}
/// Scale applied after the reference coefficients are generated.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum WindowNormalization {
/// Preserve the reference coefficients exactly.
#[default]
None,
/// Scale the coefficients so their arithmetic mean is one.
CoherentGain,
/// Scale the coefficients so their sum of squares is one.
UnitEnergy,
}
/// Complete, reusable policy for generating an analysis window.
#[derive(Clone, Debug, PartialEq)]
pub struct WindowSpec {
/// Reference formula or explicit coefficient vector.
pub function: WindowFunction,
/// Endpoint convention for formula-based windows.
pub sampling: WindowSampling,
/// Post-generation scaling policy.
pub normalization: WindowNormalization,
}
impl WindowSpec {
/// Creates a symmetric, unnormalized window policy.
pub fn new(function: WindowFunction) -> Self {
Self {
function,
sampling: WindowSampling::Symmetric,
normalization: WindowNormalization::None,
}
}
/// Generates `len` coefficients and their reconstructable metrics.
pub fn generate(&self, len: usize) -> Result<Window, SignalError> {
if len == 0 {
return Err(SignalError::InvalidLength {
len,
reason: "an analysis window requires at least one coefficient",
});
}
let mut samples = match &self.function {
WindowFunction::Explicit(samples) => {
if samples.len() != len {
return Err(SignalError::LengthMismatch {
expected: len,
actual: samples.len(),
});
}
samples.clone()
}
function => formula_window(function, self.sampling, len)?,
};
for (index, value) in samples.iter().copied().enumerate() {
if !value.is_finite() {
return Err(SignalError::NonFinite {
index,
component: "window",
});
}
}
let raw_sum = stable_sum(&samples);
let raw_energy = stable_sum(
&samples
.iter()
.map(|value| value * value)
.collect::<Vec<_>>(),
);
let normalization_scale = match self.normalization {
WindowNormalization::None => 1.0,
WindowNormalization::CoherentGain => {
if !raw_sum.is_finite() || raw_sum.abs() <= f64::EPSILON {
return Err(SignalError::DegenerateNormalization {
normalization: "window coherent-gain",
});
}
len as f64 / raw_sum
}
WindowNormalization::UnitEnergy => {
if !raw_energy.is_finite() || raw_energy <= f64::EPSILON {
return Err(SignalError::DegenerateNormalization {
normalization: "window unit-energy",
});
}
raw_energy.sqrt().recip()
}
};
for value in &mut samples {
*value *= normalization_scale;
}
let sum = stable_sum(&samples);
let energy = stable_sum(
&samples
.iter()
.map(|value| value * value)
.collect::<Vec<_>>(),
);
let equivalent_noise_bandwidth_bins = if sum.abs() <= f64::EPSILON {
None
} else {
Some(len as f64 * energy / (sum * sum))
};
Ok(Window {
samples,
metrics: WindowMetrics {
len,
raw_coherent_gain: raw_sum / len as f64,
raw_energy,
normalization_scale,
coherent_gain: sum / len as f64,
energy,
equivalent_noise_bandwidth_bins,
},
})
}
}
impl Default for WindowSpec {
fn default() -> Self {
Self::new(WindowFunction::Hann)
}
}
/// Generated coefficients and the exact gain/energy facts used to scale them.
#[derive(Clone, Debug, PartialEq)]
pub struct Window {
/// Coefficients after the requested normalization.
pub samples: Vec<f64>,
/// Metrics before and after normalization.
pub metrics: WindowMetrics,
}
/// Gain, energy, and equivalent-bandwidth evidence for a generated window.
#[derive(Clone, Debug, PartialEq)]
pub struct WindowMetrics {
/// Number of coefficients.
pub len: usize,
/// Arithmetic mean of the unnormalized reference coefficients.
pub raw_coherent_gain: f64,
/// Sum of squared unnormalized reference coefficients.
pub raw_energy: f64,
/// Multiplier applied by [`WindowNormalization`].
pub normalization_scale: f64,
/// Arithmetic mean after normalization.
pub coherent_gain: f64,
/// Sum of squared coefficients after normalization.
pub energy: f64,
/// Scale-invariant equivalent noise bandwidth in FFT bins. A zero-sum
/// explicit window has no finite equivalent bandwidth.
pub equivalent_noise_bandwidth_bins: Option<f64>,
}
fn formula_window(
function: &WindowFunction,
sampling: WindowSampling,
len: usize,
) -> Result<Vec<f64>, SignalError> {
if len == 1 {
return Ok(vec![1.0]);
}
let denominator = match sampling {
WindowSampling::Symmetric => (len - 1) as f64,
WindowSampling::Periodic => len as f64,
};
match function {
WindowFunction::Blackman { alpha }
if !alpha.is_finite() || !(0.0..=1.0).contains(alpha) =>
{
return Err(SignalError::InvalidPolicy {
policy: "Blackman alpha",
reason: "alpha must be finite and between zero and one",
});
}
WindowFunction::Kaiser { beta } if !beta.is_finite() || *beta < 0.0 => {
return Err(SignalError::InvalidPolicy {
policy: "Kaiser beta",
reason: "beta must be finite and non-negative",
});
}
_ => {}
}
Ok((0..len)
.map(|index| {
let theta = TAU * index as f64 / denominator;
match function {
WindowFunction::Rectangular => 1.0,
WindowFunction::Hann => 0.5 - 0.5 * theta.cos(),
WindowFunction::Hamming => 0.54 - 0.46 * theta.cos(),
WindowFunction::Blackman { alpha } => {
let a0 = (1.0 - alpha) / 2.0;
a0 - 0.5 * theta.cos() + alpha / 2.0 * (2.0 * theta).cos()
}
WindowFunction::BlackmanHarris => {
0.35875 - 0.48829 * theta.cos() + 0.14128 * (2.0 * theta).cos()
- 0.01168 * (3.0 * theta).cos()
}
WindowFunction::Kaiser { beta } => {
let position = 2.0 * index as f64 / denominator - 1.0;
modified_bessel_i0(beta * (1.0 - position * position).max(0.0).sqrt())
/ modified_bessel_i0(*beta)
}
WindowFunction::Explicit(_) => unreachable!("handled before formula generation"),
}
})
.collect())
}
// The convergent power series is stable across the practical Kaiser range and
// avoids a platform-dependent special-function dependency.
fn modified_bessel_i0(value: f64) -> f64 {
let quarter_square = value * value / 4.0;
let mut sum = 1.0;
let mut term = 1.0;
for order in 1..=100 {
term *= quarter_square / (order as f64 * order as f64);
sum += term;
if term <= sum.abs() * f64::EPSILON {
break;
}
}
sum
}
fn stable_sum(values: &[f64]) -> f64 {
let mut sum = 0.0;
let mut correction = 0.0;
for value in values {
let adjusted = *value - correction;
let next = sum + adjusted;
correction = (next - sum) - adjusted;
sum = next;
}
sum
}