|
| 1 | +use super::coefficients::Coefficients; |
| 2 | + |
| 3 | +/// N == pd + 1 |
| 4 | +#[derive(Debug, Clone)] |
| 5 | +pub struct MelLogSpectrumApproximation<const N: usize> { |
| 6 | + d11: [f64; N], |
| 7 | + d12: [f64; N], |
| 8 | + d21: [Vec<f64>; N], |
| 9 | + d22: [f64; N], |
| 10 | +} |
| 11 | + |
| 12 | +pub trait Pade<const N: usize> { |
| 13 | + const PPADE: [f64; N]; |
| 14 | +} |
| 15 | +macro_rules! impl_pade { |
| 16 | + ($($i:literal: $ppade:expr),* $(,)?) => { |
| 17 | + $( |
| 18 | + impl Pade<$i> for MelLogSpectrumApproximation<$i> { |
| 19 | + const PPADE: [f64; $i] = $ppade; |
| 20 | + } |
| 21 | + )* |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +impl_pade!( |
| 26 | + 1: [1.00000000000f64], |
| 27 | + 2: [1.00000000000f64, 0.00000000000f64], |
| 28 | + 3: [1.00000000000f64, 0.00000000000f64, 0.00000000000f64], |
| 29 | + 4: [1.00000000000f64, 0.00000000000f64, 0.00000000000f64, 0.00000000000f64], |
| 30 | + 5: [1.00000000000f64, 0.49992730000f64, 0.10670050000f64, 0.01170221000f64, 0.00056562790f64], |
| 31 | + 6: [1.00000000000f64, 0.49993910000f64, 0.11070980000f64, 0.01369984000f64, 0.00095648530f64, 0.00003041721f64], |
| 32 | +); |
| 33 | + |
| 34 | +impl<const N: usize> MelLogSpectrumApproximation<N> |
| 35 | +where |
| 36 | + Self: Pade<N>, |
| 37 | +{ |
| 38 | + pub fn new(nmcp: usize) -> Self { |
| 39 | + Self { |
| 40 | + d11: [0.0; N], |
| 41 | + d12: [0.0; N], |
| 42 | + d21: std::array::from_fn(|_| vec![0.0; nmcp]), |
| 43 | + d22: [0.0; N], |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + #[inline(always)] |
| 48 | + pub fn df(&mut self, x: &mut f64, alpha: f64, coefficients: &'_ Coefficients) { |
| 49 | + self.df1(x, alpha, coefficients); |
| 50 | + self.df2(x, alpha, coefficients); |
| 51 | + } |
| 52 | + |
| 53 | + #[inline(always)] |
| 54 | + fn df1(&mut self, x: &mut f64, alpha: f64, coefficients: &'_ Coefficients) { |
| 55 | + let aa = 1.0 - alpha * alpha; |
| 56 | + let mut out = 0.0; |
| 57 | + for i in (1..N).rev() { |
| 58 | + self.d11[i] = aa * self.d12[i - 1] + alpha * self.d11[i]; |
| 59 | + self.d12[i] = self.d11[i] * coefficients[1]; |
| 60 | + let v = self.d12[i] * Self::PPADE[i]; |
| 61 | + *x += if i & 1 != 0 { v } else { -v }; |
| 62 | + out += v; |
| 63 | + } |
| 64 | + self.d12[0] = *x; |
| 65 | + *x += out; |
| 66 | + } |
| 67 | + |
| 68 | + #[inline(always)] |
| 69 | + fn df2(&mut self, x: &mut f64, alpha: f64, coefficients: &'_ Coefficients) { |
| 70 | + let mut out = 0.0; |
| 71 | + for i in (1..N).rev() { |
| 72 | + self.d22[i] = fir(&mut self.d21[i - 1], self.d22[i - 1], alpha, coefficients); |
| 73 | + let v = self.d22[i] * Self::PPADE[i]; |
| 74 | + *x += if i & 1 != 0 { v } else { -v }; |
| 75 | + out += v; |
| 76 | + } |
| 77 | + self.d22[0] = *x; |
| 78 | + *x += out; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +fn fir(d: &mut [f64], x: f64, alpha: f64, coefficients: &[f64]) -> f64 { |
| 83 | + d[0] = x; |
| 84 | + |
| 85 | + let a = alpha; |
| 86 | + let aa = a * a; |
| 87 | + let aaaa = aa * aa; |
| 88 | + let iaa = 1.0 - aa; |
| 89 | + let mut rem = 0.0; |
| 90 | + |
| 91 | + let mut chunks = d.chunks_exact_mut(4); |
| 92 | + for chunk in chunks.by_ref() { |
| 93 | + (chunk[0], chunk[1], chunk[2], chunk[3], rem) = ( |
| 94 | + iaa * rem + a * chunk[0], |
| 95 | + iaa * (-a * rem + chunk[0]) + a * chunk[1], |
| 96 | + iaa * (aa * rem + (-a * chunk[0] + chunk[1])) + a * chunk[2], |
| 97 | + iaa * (aa * (-a * rem + chunk[0]) + (-a * chunk[1] + chunk[2])) + a * chunk[3], |
| 98 | + aaaa * rem + (aa * (-a * chunk[0] + chunk[1]) + (-a * chunk[2] + chunk[3])), |
| 99 | + ); |
| 100 | + } |
| 101 | + for di in chunks.into_remainder() { |
| 102 | + (*di, rem) = (iaa * rem + a * *di, -a * rem + *di); |
| 103 | + } |
| 104 | + |
| 105 | + let mut y = [0.0; 8]; |
| 106 | + let mut c = coefficients[2..d.len()].chunks_exact(8); |
| 107 | + let mut d = d[2..d.len()].chunks_exact(8); |
| 108 | + |
| 109 | + use std::iter::zip; |
| 110 | + for (c, d) in zip(&mut c, &mut d) { |
| 111 | + for (y, (c, d)) in zip(&mut y, zip(c, d)) { |
| 112 | + *y += c * d; |
| 113 | + } |
| 114 | + } |
| 115 | + for (y, (c, d)) in zip(&mut y, zip(c.remainder(), d.remainder())) { |
| 116 | + *y += c * d; |
| 117 | + } |
| 118 | + ((y[0] + y[1]) + (y[2] + y[3])) + ((y[4] + y[5]) + (y[6] + y[7])) |
| 119 | +} |
0 commit comments