Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
Rename files and functions for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
krscott committed Oct 6, 2022
1 parent 2e8e677 commit f23e59d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
20 changes: 12 additions & 8 deletions src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export function App() {
const freqArray = new Float32Array(analyzer.frequencyBinCount);

const libFftPerfMs = new PerfCache();
const fftPerfMs = new PerfCache();
const simdFftPerfMs = new PerfCache();
const cooleyPerfMs = new PerfCache();
const simdCoolyPerfMs = new PerfCache();

const baseScale = 3;
const baseOffset = 0;
Expand Down Expand Up @@ -122,16 +122,20 @@ export function App() {
// ctx.fillText(`lib: ${libFftPerfMs.avg?.toFixed(3)} ms`, 0, 10);

ctx.strokeStyle = ctx.fillStyle = "orange";
fftPerfMs.put(
execFft((i, o) => wasmFft.fft(i, o), -36.50907 - 30, 19.57467)
cooleyPerfMs.put(
execFft((i, o) => wasmFft.cooley_tukey(i, o), -36.50907 - 30, 19.57467)
);
ctx.fillText(`naive: ${fftPerfMs.avg?.toFixed(3)} ms`, 0, 20);
ctx.fillText(`naive: ${cooleyPerfMs.avg?.toFixed(3)} ms`, 0, 20);

ctx.strokeStyle = ctx.fillStyle = "pink";
simdFftPerfMs.put(
execFft((i, o) => wasmFft.simd_fft(i, o), -36.50907 - 30, 19.57467)
simdCoolyPerfMs.put(
execFft(
(i, o) => wasmFft.simd_cooley_tukey(i, o),
-36.50907 - 30,
19.57467
)
);
ctx.fillText(`simd: ${simdFftPerfMs.avg?.toFixed(3)} ms`, 0, 30);
ctx.fillText(`simd: ${simdCoolyPerfMs.avg?.toFixed(3)} ms`, 0, 30);
};

requestAnimationFrame(animate);
Expand Down
2 changes: 1 addition & 1 deletion wasm-audio/src/fft.rs → wasm-audio/src/cooley_tukey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const NTWO_PI_I: Complex<f32> = Complex {
im: -2.0 * PI,
};

pub fn fft(input: &[Complex<f32>], output: &mut [Complex<f32>]) {
pub fn cooley_tukey_fft(input: &[Complex<f32>], output: &mut [Complex<f32>]) {
assert_eq!(input.len(), output.len());
assert!(crate::is_power_of_2(input.len()));

Expand Down
4 changes: 2 additions & 2 deletions wasm-audio/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod cooley_tukey;
mod dft;
mod fft;
mod simdfft;
mod simd_cooley_tukey;
mod wasmfft;

// use wasm_bindgen::prelude::*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#[cfg(target_arch = "wasm32")]
use crate::fft::{bit_reverse_copy, numbits};
use crate::cooley_tukey::{bit_reverse_copy, numbits};
use core::f32::consts::PI;
use rustfft::num_complex::Complex;
use std::arch::wasm32::*;

#[target_feature(enable = "simd128")]
pub fn fft_simd(input: &[Complex<f32>], output: &mut [Complex<f32>]) {
pub fn simd_cooley_tukey_fft(input: &[Complex<f32>], output: &mut [Complex<f32>]) {
const TWO_PI: f32 = 2.0 * PI;

// const NTWO_PI_I: Complex<f32> = Complex {
Expand Down
13 changes: 8 additions & 5 deletions wasm-audio/src/wasmfft.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rustfft::{num_complex::Complex, num_traits::Zero, FftPlanner};
use wasm_bindgen::prelude::*;

use crate::{dft::dft, fft::fft};
use crate::{cooley_tukey::cooley_tukey_fft, dft::dft};

#[wasm_bindgen]
pub struct WasmFft {
Expand Down Expand Up @@ -85,7 +85,7 @@ impl WasmFft {
});
}

pub fn fft(&mut self, input: &[f32], output: &mut [f32]) {
pub fn cooley_tukey(&mut self, input: &[f32], output: &mut [f32]) {
assert_eq!(input.len(), output.len() * 2);
assert!(crate::is_power_of_2(input.len()));

Expand All @@ -96,15 +96,15 @@ impl WasmFft {
self.input_buffer[i].re = r;
}

fft(&self.input_buffer, &mut self.output_buffer);
cooley_tukey_fft(&self.input_buffer, &mut self.output_buffer);

(0..output.len()).for_each(|i| {
output[i] = (self.output_buffer[i].norm()).log10();
});
}

#[cfg(target_arch = "wasm32")]
pub fn simd_fft(&mut self, input: &[f32], output: &mut [f32]) {
pub fn simd_cooley_tukey(&mut self, input: &[f32], output: &mut [f32]) {
assert_eq!(input.len(), output.len() * 2);
assert!(crate::is_power_of_2(input.len()));

Expand All @@ -115,7 +115,10 @@ impl WasmFft {
self.input_buffer[i].re = r;
}

crate::simdfft::fft_simd(&self.input_buffer, &mut self.output_buffer);
crate::simd_cooley_tukey::simd_cooley_tukey_fft(
&self.input_buffer,
&mut self.output_buffer,
);

(0..output.len()).for_each(|i| {
output[i] = (self.output_buffer[i].norm()).log10();
Expand Down

0 comments on commit f23e59d

Please sign in to comment.