summaryrefslogtreecommitdiff
path: root/src/windowed_fft.rs
blob: 3e4e648c5a57a4edbd775a9dce8a95fd89fd8730 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#![allow(clippy::must_use_candidate)]
#![allow(clippy::return_self_not_must_use)]
#![allow(unused)]
#![allow(dead_code)]

use crate::{
    window_function::{
        BlackmanHarrisWindow, HannWindow, RectangularWindow, Sine4Window, HammingWindow,
        WindowFunction,
    },
    window_size::WindowSize,
};
use num_complex::Complex;
use realfft::{ComplexToReal, RealFftPlanner, RealToComplex};
use std::{collections::VecDeque, sync::Arc};

pub struct WindowedRealFft {
    window_size: usize,
    planner: RealFftPlanner<f32>,
    forward: Arc<dyn RealToComplex<f32>>,
    inverse: Arc<dyn ComplexToReal<f32>>,
    window_function: Box<dyn WindowFunction>,
    input: VecDeque<f32>,
    output: VecDeque<f32>,
    spectrum: Vec<Complex<f32>>,
    scratch: Vec<Complex<f32>>,
}

impl WindowedRealFft {
    pub fn new(window_size: WindowSize) -> Self {
        let window_function = Box::new(HannWindow::new(&window_size));
        let window_size = window_size.inner();

        let mut planner = RealFftPlanner::new();
        let forward = planner.plan_fft_forward(window_size);
        let inverse = planner.plan_fft_inverse(window_size);

        let input = VecDeque::with_capacity(window_size);
        let output = VecDeque::with_capacity(window_size);

        let spectrum = vec![Complex::ZERO; (window_size / 2) + 1];

        // scratches should be the same length for both left and right channels
        let scratch = forward.make_scratch_vec();

        Self {
            window_size,
            planner,
            forward,
            inverse,
            window_function,
            input,
            output,
            spectrum,
            scratch,
        }
    }

    pub fn window_function(&mut self, window_function: Box<dyn WindowFunction>) {}

    pub fn window_size(&mut self, window_size: WindowSize) {
        if window_size == self.window_size.into() {
            return;
        }

        self.window_function = Box::new(HannWindow::new(&window_size));
        self.window_size = window_size.inner();

        self.forward = self.planner.plan_fft_forward(self.window_size);
        self.inverse = self.planner.plan_fft_inverse(self.window_size);

        self.input.reserve_exact(self.window_size);
        self.output.reserve_exact(self.window_size);

        self.input.clear();
        self.output.clear();

        self.spectrum
            .resize((self.window_size / 2) + 1, Complex::ZERO);

        self.scratch
            .resize(self.forward.get_scratch_len(), Complex::ZERO);
    }

    pub fn clear_input(&mut self) {
        self.input.clear();
    }

    pub fn push_back_input(&mut self, value: f32) -> bool {
        self.input.push_back(value);

        self.input.len() >= self.window_function.needed()
    }

    pub fn pop_front_output(&mut self) -> f32 {
        self.output.pop_front().unwrap_or(0.0)
    }

    pub fn get_spectrum(&mut self) -> &mut [Complex<f32>] {
        &mut self.spectrum
    }

    pub fn forward(&mut self) {
        self.window_function.apply(&mut self.input);

        let _ = self.forward.process_with_scratch(
            self.input.make_contiguous(),
            &mut self.spectrum,
            &mut self.scratch,
        );
    }

    pub fn inverse(&mut self) {
        self.output.resize(self.window_size, 0.0);

        let _ = self.inverse.process_with_scratch(
            &mut self.spectrum,
            self.output.make_contiguous(),
            &mut self.scratch,
        );

        let window_size_f32 = self.window_size as f32;
        for sample in &mut self.output {
            *sample /= window_size_f32;
        }

        self.window_function.reverse(self.output.make_contiguous());
    }
}