From 6445949083d18e37c9615bb913da5bcafcb4cbaa Mon Sep 17 00:00:00 2001 From: Lucas Schumacher Date: Sun, 12 May 2024 21:19:18 -0400 Subject: [PATCH] Fix FFT scaling --- src/app/audio_fft.rs | 3 ++- src/app/debug_plot.rs | 13 ++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/app/audio_fft.rs b/src/app/audio_fft.rs index 26df136..a98f0ca 100644 --- a/src/app/audio_fft.rs +++ b/src/app/audio_fft.rs @@ -57,7 +57,8 @@ impl AudioFFT { let output: Vec = fft_out .iter() .map(|c| { - (((c.re * c.re) + (c.im * c.im)).sqrt() / size as f32 * 255.0) as u8 + (((c.re * c.re) + (c.im * c.im)).sqrt() / output_len as f32 * 255.0) + as u8 }) .collect(); assert_eq!(output_len, output.len()); diff --git a/src/app/debug_plot.rs b/src/app/debug_plot.rs index bd8346f..eff2344 100644 --- a/src/app/debug_plot.rs +++ b/src/app/debug_plot.rs @@ -71,7 +71,7 @@ impl DebugPlots { plot_ui.line(line); plot_ui.set_plot_bounds(PlotBounds::from_min_max( [-1.0, -1.0], - [(v.len() + 1) as f64, 256.0], + [(v.len() + 1) as f64, core::u8::MAX as f64 + 1.0], )); }); } @@ -79,17 +79,24 @@ impl DebugPlots { ui.heading("Bode Plot"); let mag_line = Line::new(PlotPoints::from_iter(v.iter().enumerate().map(|(i, c)| { - [i as f64, ((c.re * c.re) + (c.im * c.im)).sqrt() as f64] + [ + i as f64, + ((c.re * c.re) + (c.im * c.im)).sqrt() as f64 / v.len() as f64, + ] }))); let phase_line = Line::new(PlotPoints::from_iter( v.iter() .enumerate() - .map(|(i, c)| [i as f64, c.arg() as f64]), + .map(|(i, c)| [i as f64, c.arg() as f64 / core::f64::consts::PI]), )); let plot = Plot::new(title); plot.show(ui, |plot_ui| { plot_ui.line(mag_line); plot_ui.line(phase_line); + plot_ui.set_plot_bounds(PlotBounds::from_min_max( + [0.0, -1.0], + [(v.len() + 1) as f64, 1.0], + )); }); } };