Skip to content

Commit

Permalink
Simplify fft's API
Browse files Browse the repository at this point in the history
  • Loading branch information
Luni-4 committed Apr 22, 2020
1 parent 200d3fa commit 2197ee8
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 211 deletions.
114 changes: 80 additions & 34 deletions fft/src/fftwrap.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::smallft::*;

/* Copyright (C) 2005-2006 Jean-Marc Valin
File: fftwrap.c
Expand Down Expand Up @@ -33,46 +31,94 @@ use crate::smallft::*;
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
pub fn spx_fft_init(size: usize) -> DrftLookup {
DrftLookup::new(size)

use crate::smallft::*;

#[derive(Clone)]
pub struct DrftLookup {
pub n: usize,
pub trigcache: Vec<f32>,
pub splitcache: Vec<i32>,
}

pub fn spx_fft(table: &mut DrftLookup, in_0: &mut [f32], out: &mut [f32]) {
let scale = (1.0f64 / table.n as f64) as f32;
if in_0 == out {
eprintln!("FFT should not be done in-place");
impl DrftLookup {
pub fn new(n: usize) -> Self {
let mut drft = Self {
n: n,
trigcache: vec![0.0; 3 * n],
splitcache: vec![0; 32],
};

fdrffti(n, &mut drft.trigcache, &mut drft.splitcache);

drft
}

out.iter_mut()
.zip(in_0.iter())
.take(table.n as usize)
.for_each(|(o, i)| *o = scale * *i);
pub fn spx_fft(&mut self, in_0: &[f32], out: &mut [f32]) {
let scale = (1.0f64 / self.n as f64) as f32;
if in_0 == out {
eprintln!("FFT should not be done in-place");
}

spx_drft_forward(table, out);
}
out.iter_mut()
.zip(in_0.iter())
.take(self.n as usize)
.for_each(|(o, i)| *o = scale * *i);

pub fn spx_ifft(table: &mut DrftLookup, in_0: &mut [f32], out: &mut [f32]) {
if in_0 == out {
eprintln!("FFT should not be done in-place");
} else {
out.copy_from_slice(&in_0[..table.n as usize]);
self.spx_drft_forward(out);
}

spx_drft_backward(table, out);
}
pub fn spx_ifft(&mut self, in_0: &[f32], out: &mut [f32]) {
if in_0 == out {
eprintln!("FFT should not be done in-place");
} else {
out.copy_from_slice(&in_0[..self.n as usize]);
}

pub fn spx_fft_float(
table: &mut DrftLookup,
in_0: &mut [f32],
out: &mut [f32],
) {
spx_fft(table, in_0, out);
}
self.spx_drft_backward(out);
}

pub fn spx_fft_float(&mut self, in_0: &[f32], out: &mut [f32]) {
self.spx_fft(in_0, out);
}

pub fn spx_ifft_float(&mut self, in_0: &[f32], out: &mut [f32]) {
self.spx_ifft(in_0, out);
}

pub fn spx_drft_forward(&mut self, data: &mut [f32]) {
if self.n == 1 {
return;
}

let mut trigcache_temp = self.trigcache[self.n as usize..].to_vec();

drftf1(
self.n as i32,
data,
&mut self.trigcache,
&mut trigcache_temp,
&mut self.splitcache,
);

pub fn spx_ifft_float(
table: &mut DrftLookup,
in_0: &mut [f32],
out: &mut [f32],
) {
spx_ifft(table, in_0, out);
self.trigcache[self.n as usize..].copy_from_slice(&trigcache_temp);
}

pub fn spx_drft_backward(&mut self, data: &mut [f32]) {
if self.n == 1 {
return;
}

let mut trigcache_temp = self.trigcache[self.n as usize..].to_vec();

drftb1(
self.n as i32,
data,
&mut self.trigcache,
&mut trigcache_temp,
&mut self.splitcache,
);

self.trigcache[self.n as usize..].copy_from_slice(&trigcache_temp);
}
}
1 change: 0 additions & 1 deletion fft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ mod fftwrap;
mod smallft;

pub use crate::fftwrap::*;
pub use crate::smallft::*;
65 changes: 4 additions & 61 deletions fft/src/smallft.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
use crate::dradb::*;
use crate::dradf::*;

#[derive(Clone)]
pub struct DrftLookup {
pub n: usize,
pub trigcache: Vec<f32>,
pub splitcache: Vec<i32>,
}

impl DrftLookup {
pub fn new(n: usize) -> Self {
let mut drft = Self {
n: n,
trigcache: vec![0.0; 3 * n],
splitcache: vec![0; 32],
};

fdrffti(n, &mut drft.trigcache, &mut drft.splitcache);

drft
}
}

#[inline(always)]
fn drfti1_c_10244(ifac: &mut [i32], n: i32, nf: &mut i32) {
const NTRYH: [i32; 4] = [4, 2, 3, 5];
Expand Down Expand Up @@ -62,7 +41,7 @@ fn drfti1_c_10244(ifac: &mut [i32], n: i32, nf: &mut i32) {
}
}

fn drfti1(wa: &mut [f32], ifac: &mut [i32]) {
pub(crate) fn drfti1(wa: &mut [f32], ifac: &mut [i32]) {
const TPI: f32 = 6.283_185_307_179_586_48;

let n = wa.len() as i32;
Expand Down Expand Up @@ -112,7 +91,7 @@ fn drfti1(wa: &mut [f32], ifac: &mut [i32]) {
}
}

fn fdrffti(n: usize, wsave: &mut [f32], ifac: &mut [i32]) {
pub(crate) fn fdrffti(n: usize, wsave: &mut [f32], ifac: &mut [i32]) {
if n == 1 {
return;
}
Expand Down Expand Up @@ -152,7 +131,7 @@ fn drftf1_l102(
}
}

fn drftf1(
pub(crate) fn drftf1(
n: i32,
c: &mut [f32],
ch: &mut [f32],
Expand Down Expand Up @@ -254,7 +233,7 @@ fn drftb1_l102(
}
}

fn drftb1(
pub(crate) fn drftb1(
n: i32,
c: &mut [f32],
ch: &mut [f32],
Expand Down Expand Up @@ -312,42 +291,6 @@ fn drftb1(
c[..n as usize].copy_from_slice(&ch[..n as usize]);
}

pub fn spx_drft_forward(l: &mut DrftLookup, data: &mut [f32]) {
if l.n == 1 {
return;
}

let mut trigcache_temp = l.trigcache[l.n as usize..].to_vec();

drftf1(
l.n as i32,
data,
&mut l.trigcache,
&mut trigcache_temp,
&mut l.splitcache,
);

l.trigcache[l.n as usize..].copy_from_slice(&trigcache_temp);
}

pub fn spx_drft_backward(l: &mut DrftLookup, data: &mut [f32]) {
if l.n == 1 {
return;
}

let mut trigcache_temp = l.trigcache[l.n as usize..].to_vec();

drftb1(
l.n as i32,
data,
&mut l.trigcache,
&mut trigcache_temp,
&mut l.splitcache,
);

l.trigcache[l.n as usize..].copy_from_slice(&trigcache_temp);
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading

0 comments on commit 2197ee8

Please sign in to comment.