Module librustosa::stft
source · [−]Expand description
Computes the short-time fourier transform on streaming data.
example
use stft::STFT;
// Generate ten seconds of fake audio
let sample_rate: usize = 44100;
let seconds: usize = 10;
let sample_count = sample_rate * seconds;
let all_samples = (0..sample_count).map(|x| x as f64).collect::<Vec<f64>>();
// Initialize the short-time fourier transform
let window_size: usize = 1024;
let step_size: usize = 512;
let mut stft = STFT::new(window_size, step_size).unwrap();
// Iterate over all the samples in chunks of 3000 samples.
// In a real program you would probably read from a stream instead.
for some_samples in (&all_samples[..]).chunks(3000) {
// Append the samples to the internal ringbuffer of the stft
stft.append_samples(some_samples);
// Loop as long as there remain window_size samples in the internal
// ringbuffer of the stft
while stft.contains_enough_to_compute() {
// Compute one column of the stft by
// taking the first window_size samples of the internal ringbuffer,
// multiplying them with the window,
// computing the fast fourier transform,
// taking half of the symetric complex outputs,
// computing the norm of the complex outputs and
// taking the log10
let spectrogram_column = stft.compute_column();
// Here's where you would do something with the
// spectrogram_column...
// Drop step_size samples from the internal ringbuffer of the stft
// making a step of size step_size
stft.move_to_next_column();
}
}
assert!(!stft.is_empty())