#data #fft #fourier #stream #dsp #data-streaming

ruststft

computes the short-time fourier transform on streaming data

2 releases

0.3.1 Jun 22, 2023
0.3.0 Jun 22, 2023

#2417 in Algorithms

Download history 26/week @ 2024-07-21 21/week @ 2024-07-28 1/week @ 2024-08-04 4/week @ 2024-08-11 13/week @ 2024-08-18 12/week @ 2024-08-25 3/week @ 2024-09-01 14/week @ 2024-09-15 22/week @ 2024-09-22 38/week @ 2024-09-29 114/week @ 2024-10-06 140/week @ 2024-10-13 24/week @ 2024-10-20 29/week @ 2024-10-27

308 downloads per month

MIT/Apache

15KB
239 lines

STFT

status: working. missing some tests. api still in flux.

Build Status

computes the short-time fourier transform on streaming data. written in rust.

to use add stft = "*" to the [dependencies] section of your Cargo.toml and call extern crate stft; in your code.

read the documentation for an example and more !

contributing

licensed under either of apache-2.0 (tl;dr) or MIT (tl;dr) at your option


lib.rs:

computes the short-time fourier transform on streaming data.

to use add ruststft = "*" to the [dependencies] section of your Cargo.toml and use ruststft; in your code.

example

use ruststft::{STFT, WindowType};

// let's 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>>();

// let's initialize our short-time fourier transform
let window_type: WindowType = WindowType::Hanning;
let window_size: usize = 1024;
let step_size: usize = 512;
let mut stft = STFT::new(window_type, window_size, step_size);

// we need a buffer to hold a computed column of the spectrogram
let mut spectrogram_column: Vec<f64> =
std::iter::repeat(0.).take(stft.output_size()).collect();

// iterate over all the samples in chunks of 3000 samples.
// in a real program you would probably read from something instead.
for some_samples in (&all_samples[..]).chunks(3000) {
// append the samples to the internal ringbuffer of the stft
stft.append_samples(some_samples);

// 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
stft.compute_column(&mut spectrogram_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();
}
}

Dependencies

~3MB
~60K SLoC