use burn::{
Tensor,
config::Config,
prelude::Backend,
tensor::TensorCreationOptions,
};
use crate::ops::{
arange::tensor_arange_start_step,
signal::window_builder::SamplingWindowBuilder,
};
#[derive(Config, Copy, Debug, PartialEq)]
pub struct CosineWindowConfig {
pub alpha: f64,
pub beta: f64,
pub periodic: bool,
}
impl CosineWindowConfig {
pub fn from_alpha_complement(
alpha: f64,
periodic: bool,
) -> CosineWindowConfig {
let beta = 1.0 - alpha;
CosineWindowConfig {
alpha,
beta,
periodic,
}
}
pub fn hann(periodic: bool) -> CosineWindowConfig {
CosineWindowConfig::from_alpha_complement(0.5, periodic)
}
pub fn hamming(periodic: bool) -> CosineWindowConfig {
CosineWindowConfig::from_alpha_complement(0.54, periodic)
}
pub fn is_periodic(&self) -> bool {
self.periodic
}
pub fn is_symmetric(&self) -> bool {
!self.is_periodic()
}
}
impl SamplingWindowBuilder for CosineWindowConfig {
fn to_vec_window(
&self,
size: usize,
) -> Vec<f64> {
match size {
0 | 1 => return vec![1.0; size],
_ => (),
};
let n = if self.is_periodic() { size } else { size - 1 };
let step = core::f64::consts::TAU / n as f64;
(0..size)
.map(|n| {
let theta = (n as f64) * step;
self.alpha - self.beta * theta.cos()
})
.collect()
}
fn to_tensor_window<B: Backend>(
&self,
size: usize,
options: impl Into<TensorCreationOptions<B>>,
) -> Tensor<B, 1> {
match size {
0 | 1 => return Tensor::ones([size], options),
_ => (),
};
let n = if self.is_periodic() { size } else { size - 1 };
let step = core::f64::consts::TAU / n as f64;
let theta = tensor_arange_start_step(size, 0.0, Some(step), options);
theta.cos().mul_scalar(-self.beta).add_scalar(self.alpha)
}
}
#[derive(Config, Copy, Debug, PartialEq)]
pub struct DualCosineWindow {
pub alpha: f64,
pub beta: f64,
pub gamma: f64,
pub periodic: bool,
}
impl DualCosineWindow {
pub fn from_alpha_beta_complement(
alpha: f64,
beta: f64,
periodic: bool,
) -> DualCosineWindow {
let gamma = 1.0 - (alpha + beta);
DualCosineWindow {
alpha,
beta,
gamma,
periodic,
}
}
pub fn blackman(periodic: bool) -> DualCosineWindow {
Self::from_alpha_beta_complement(0.42, 0.5, periodic)
}
pub fn is_periodic(&self) -> bool {
self.periodic
}
pub fn is_symmetric(&self) -> bool {
!self.periodic
}
pub fn angular_increment(
&self,
size: usize,
) -> f64 {
let n = if self.is_periodic() { size } else { size - 1 };
core::f64::consts::TAU / n as f64
}
}
impl SamplingWindowBuilder for DualCosineWindow {
fn to_vec_window(
&self,
size: usize,
) -> Vec<f64> {
match size {
0 | 1 => return vec![1.0; size],
_ => (),
};
let ang_inc = self.angular_increment(size);
(0..size)
.map(|i| {
let i = i as f64;
let cos_val = (i * ang_inc).cos();
(self.alpha - self.gamma) - self.beta * cos_val
+ (2.0 * self.gamma) * cos_val.powi(2)
})
.collect()
}
fn to_tensor_window<B: Backend>(
&self,
size: usize,
options: impl Into<TensorCreationOptions<B>>,
) -> Tensor<B, 1> {
match size {
0 | 1 => return Tensor::ones([size], options),
_ => (),
};
let ang_inc = self.angular_increment(size);
let cos_val = tensor_arange_start_step(size, 0.0, Some(ang_inc), options).cos();
let b = cos_val.clone().mul_scalar(self.beta);
let g = cos_val.square().mul_scalar(2.0 * self.gamma);
(g - b).add_scalar(self.alpha - self.gamma)
}
}
#[cfg(test)]
mod tests {
use burn::{
prelude::TensorData,
tensor::{
Tolerance,
backend::BackendTypes,
signal::{
blackman_window,
hann_window,
},
},
};
use tracing::{
debug,
info,
};
use tracing_test::traced_test;
use super::*;
use crate::{
ops::signal::testing::assert_sampling_window_builder_implementation,
prelude::*,
support::testing::CpuBackend,
};
type B = CpuBackend;
type F = <B as BackendTypes>::FloatElem;
fn check_hann_impl<B: Backend>(
periodic: bool,
expected: &[f64],
options: impl Into<TensorCreationOptions<B>>,
) {
let cfg = CosineWindowConfig::hann(periodic);
debug!("cfg: {:?}", cfg);
debug!("expected: {:?}", expected);
let size = expected.len();
let options = options.into();
info!("checking hann_window reference implementation");
hann_window::<B>(size, periodic, options.clone())
.to_data_as::<F>()
.assert_approx_eq::<F>(
&TensorData::from(expected).convert::<F>(),
Tolerance::default(),
);
info!("cross-checking vec/tensor impls");
assert_sampling_window_builder_implementation::<B>(&cfg, expected, options.clone());
}
#[test]
#[traced_test]
fn test_hann() {
let device = Default::default();
check_hann_impl::<B>(true, &[], &device);
check_hann_impl::<B>(false, &[], &device);
check_hann_impl::<B>(true, &[1.0], &device);
check_hann_impl::<B>(false, &[1.0], &device);
check_hann_impl::<B>(true, &[0.0, 1.0], &device);
check_hann_impl::<B>(false, &[0.0, 0.0], &device);
check_hann_impl::<B>(true, &[0.0, 0.75, 0.75], &device);
check_hann_impl::<B>(false, &[0.0, 1.0, 0.0], &device);
check_hann_impl::<B>(
true,
&[0.0, 0.146447, 0.5, 0.853553, 1.0, 0.853553, 0.5, 0.146447],
&device,
);
check_hann_impl::<B>(
false,
&[
0.0, 0.188255, 0.611260, 0.950484, 0.950484, 0.611260, 0.188255, 0.0,
],
&device,
);
}
fn check_blackman_impl<B: Backend>(
periodic: bool,
expected: &[f64],
options: impl Into<TensorCreationOptions<B>>,
) {
let cfg = DualCosineWindow::blackman(periodic);
debug!("cfg: {:?}", cfg);
debug!("expected: {:?}", expected);
let size = expected.len();
let options = options.into();
info!("checking blackman_window reference implementation");
blackman_window::<B>(size, periodic, options.clone())
.to_data_as::<F>()
.assert_approx_eq::<F>(
&TensorData::from(expected).convert::<F>(),
Tolerance::default(),
);
info!("cross-checking vec/tensor impls");
assert_sampling_window_builder_implementation::<B>(&cfg, expected, options.clone());
}
#[test]
#[traced_test]
fn test_blackman() {
let device = Default::default();
check_blackman_impl::<B>(true, &[], &device);
check_blackman_impl::<B>(false, &[], &device);
check_blackman_impl::<B>(true, &[1.0], &device);
check_blackman_impl::<B>(false, &[1.0], &device);
check_blackman_impl::<B>(true, &[0.0, 1.0], &device);
check_blackman_impl::<B>(false, &[0.0, 0.0], &device);
check_blackman_impl::<B>(true, &[0.0, 0.63, 0.63], &device);
check_blackman_impl::<B>(false, &[0.0, 1.0, 0.0], &device);
check_blackman_impl::<B>(
true,
&[0.0, 0.0664466, 0.34, 0.77355, 1.0, 0.77355, 0.34, 0.0664466],
&device,
);
check_blackman_impl::<B>(
false,
&[
0.0, 0.09045343, 0.45918, 0.92036, 0.92036, 0.45918, 0.09045343, 0.0,
],
&device,
);
}
}