Skip to content

Commit 3c0cee6

Browse files
committed
fix fft freq shift
1 parent 45591bf commit 3c0cee6

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/math/fft.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ use crate::ui::state::UiState;
1010
/// The spectrum is calculated using the FFT algorithm and a Hann window. The corresponding window size is specified in [`FFT_WINDOW_SIZE`].
1111
/// The spectrum is then mapped to a logarithmic scale and returned.
1212
/// * `microphone` - The microphone to calculate the spectrum for.
13-
/// * `delta_t` - The time between two samples.
1413
/// * `ui_state` - The current state of the UI.
1514
pub fn calc_mic_spectrum(
1615
microphone: &mut Microphone,
17-
delta_t: f32,
1816
ui_state: &UiState,
1917
) -> Vec<[f64; 2]> {
2018
let samples = if microphone.record.len() < FFT_WINDOW_SIZE {
@@ -29,7 +27,10 @@ pub fn calc_mic_spectrum(
2927
// always returns frequencies up to sampling_rate/2
3028
let spectrum_hann_window = samples_fft_to_spectrum(
3129
&hann_window,
32-
(1. / delta_t) as u32,
30+
// "Normally" the sample rate should be calculated as: (1. / delta_t) as u32
31+
// But because the rate of samples provided does not actually change when changing delta_l,
32+
// we can just use the constant value. Calculated as 1/(0.001 / 343) = 343200
33+
343200,
3334
FrequencyLimit::All,
3435
Some(&scale_to_zero_to_one),
3536
)

src/ui/draw.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ pub fn draw_egui(
895895
== ui_state.current_fft_microphone.expect("no mic selected")
896896
}) {
897897
let mapped_spectrum =
898-
calc_mic_spectrum(&mut mic, grid.delta_t, &ui_state);
898+
calc_mic_spectrum(&mut mic, &ui_state);
899899
// remove the first element, because of log it is at x=-inf
900900
let mapped_spectrum = &mapped_spectrum[1..];
901901

0 commit comments

Comments
 (0)