summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorozpv <39195175+ozpv@users.noreply.github.com>2026-05-29 06:08:20 +0000
committerGitHub <noreply@github.com>2026-05-29 06:08:20 +0000
commit3cc7363ab0cc169d96481467236288e63f332131 (patch)
treeae515c2607c05e4ebd6dec4243eb9b0396cb8421 /src
parent24b1401a881d7ddc49b47a6b7fa28af649417ae2 (diff)
update hann
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs2
-rw-r--r--src/window_function.rs51
2 files changed, 18 insertions, 35 deletions
diff --git a/src/lib.rs b/src/lib.rs
index f3f6d1b..70ed064 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -51,7 +51,7 @@ impl Plugin for RetainPlugin {
impl DefaultPluginFactory for RetainPlugin {
fn get_descriptor() -> PluginDescriptor {
PluginDescriptor::new("com.haemolacriaa.retain", "Retain")
- .with_description("Retains only the nth largest magnitude frequencies in the signal")
+ .with_description("Retains only the nth largest magnitude frequencies in a signal")
.with_version("0.1.0-pre")
.with_vendor("haemolacriaa")
.with_features([AUDIO_EFFECT, STEREO])
diff --git a/src/window_function.rs b/src/window_function.rs
index 9ae15f8..0095ccc 100644
--- a/src/window_function.rs
+++ b/src/window_function.rs
@@ -43,7 +43,6 @@ impl WindowFunction for RectangularWindow {
pub struct HannWindow {
window_size: usize,
function: Vec<f32>,
- normalize: Vec<f32>,
previous: Vec<f32>,
overlap_add: Vec<f32>,
}
@@ -54,33 +53,20 @@ impl HannWindow {
let half_window_size = window_size / 2;
let window_size_f32 = window_size as f32;
- // 50% overlap
- let overlap_add = vec![0.0; window_size];
-
- // with capacity is important for the first needed samples
- let previous = Vec::with_capacity(half_window_size);
-
+ // bypass the need for a normalize buffer by using sqrt
let function = (0..window_size)
.map(|i| {
let i = i as f32;
- 0.5 * (1.0 - f32::cos((2.0 * PI * i) / (window_size_f32)))
- })
- .collect::<Vec<f32>>();
-
- let normalize = (0..half_window_size)
- .map(|i| {
- (function[i] * function[i])
- + (function[i + half_window_size] * function[i + half_window_size])
+ f32::sqrt(0.5 - 0.5 * f32::cos((2.0 * PI * i) / (window_size_f32)))
})
.collect::<Vec<f32>>();
Self {
window_size,
function,
- normalize,
- previous,
- overlap_add,
+ previous: Vec::with_capacity(half_window_size),
+ overlap_add: vec![0.0; window_size],
}
}
}
@@ -126,17 +112,14 @@ impl WindowFunction for HannWindow {
let half_window_size = self.window_size / 2;
// overlap add data and save the next overlap (latter half of this buffer)
- for (i, sample) in data.iter().copied().enumerate() {
- self.overlap_add[i] += sample * self.function[i];
+ for i in 0..self.window_size {
+ self.overlap_add[i] += data[i] * self.function[i];
}
- // normalize the output
- for (i, sample) in data.iter_mut().enumerate().take(half_window_size) {
- if self.normalize[i] > 1e-6 {
- *sample = self.overlap_add[i] / self.normalize[i];
- } else {
- *sample = 0.0;
- }
+ // set the output samples
+ // in other functions this will also include normalizing
+ for i in 0..half_window_size {
+ data[i] = self.overlap_add[i];
}
// shift the saved overlap to become the next overlap
@@ -184,7 +167,7 @@ impl HammingWindow {
.map(|i| {
let i = i as f32;
- 0.53836 - (0.46164 * f32::cos((2.0 * PI * i) / (window_size_f32)))
+ 0.54 - (0.46 * f32::cos((2.0 * PI * i) / (window_size_f32 - 1.0)))
})
.collect::<Vec<f32>>();
@@ -300,11 +283,11 @@ impl BlackmanHarrisWindow {
.map(|i| {
let i = i as f32;
- let two = f32::cos((2.0 * PI * i) / (window_size_f32));
- let four = f32::cos((4.0 * PI * i) / (window_size_f32));
- let six = f32::cos((6.0 * PI * i) / (window_size_f32));
+ let one = Self::A_1 * f32::cos((2.0 * PI * i) / (window_size_f32));
+ let two = Self::A_2 * f32::cos((4.0 * PI * i) / (window_size_f32));
+ let three = Self::A_3 * f32::cos((6.0 * PI * i) / (window_size_f32));
- Self::A_0 - (Self::A_1 * two) + (Self::A_2 * four) - (Self::A_3 * six)
+ Self::A_0 - one + two - three
})
.collect::<Vec<f32>>();
@@ -423,8 +406,8 @@ impl Sine4Window {
.map(|i| {
let i = i as f32;
- let two = f32::cos((2.0 * PI * i) / (window_size_f32));
- let four = f32::cos((4.0 * PI * i) / (window_size_f32));
+ let two = f32::cos((2.0 * PI * i) / (window_size_f32 - 1.0));
+ let four = f32::cos((4.0 * PI * i) / (window_size_f32 - 1.0));
Self::A_0 - (Self::A_1 * two) + (Self::A_2 * four)
})