Class thl::dsp::fft::StftAnalyser#

class StftAnalyser#

Inheritance diagram for thl::dsp::fft::StftAnalyser:

digraph { graph [bgcolor="#00000000"] node [shape=rectangle style=filled fillcolor="#FFFFFF" font=Helvetica padding=2] edge [color="#1414CE"] "2" [label="thl::dsp::analysis::SpectrumAnalyser" tooltip="thl::dsp::analysis::SpectrumAnalyser"] "1" [label="thl::dsp::fft::StftAnalyser" tooltip="thl::dsp::fft::StftAnalyser" fillcolor="#BFBFBF"] "2" -> "1" [dir=forward tooltip="public-inheritance"] }

Cuts a stream of samples into overlapping frames and hands each frame’s spectrum to process_spectrum(): the analysis half of a short-time Fourier transform.

push() takes samples in chunks of any size (as they come from a SampleProbe). Every hop_size() samples, once fft_size() have arrived, each channel’s newest fft_size() samples are windowed, transformed (RealFft) and handed to process_spectrum(), channel by channel, the same frame for every channel. The bins are scaled so a sine of amplitude A reads |X| = A at its bin: the window’s coherent gain and the FFT’s N / 2 undone, so a full-scale sine reads 0 dB. DC and Nyquist, which have no mirror image, read their amplitude too.

reset() drops what has been collected: the next frame comes only after fft_size() new samples (after a discontinuity, so no frame spans a gap).

Threads: prepare() allocates; push() and reset() allocate nothing and belong to one thread at a time (a reader thread, not necessarily the audio thread).

Subclassed by thl::dsp::analysis::SpectrumAnalyser

Public Types

enum class Window : uint8_t#

Values:

enumerator Hann#

the usual: -31 dB sidelobes, narrow main lobe

enumerator BlackmanHarris#

4-term: -92 dB sidelobes for a high dynamic range, wider main lobe

Public Functions

StftAnalyser() = default#
virtual ~StftAnalyser()#
StftAnalyser(const StftAnalyser&) = delete#
StftAnalyser &operator=(const StftAnalyser&) = delete#
bool prepare(size_t fft_size, size_t hop_size, size_t num_channels, Window window = Window::Hann)#

Frames of fft_size (a power of two RealFft takes), one every hop_size samples (1 to fft_size), for num_channels channels.

False, and unprepared, otherwise.

inline size_t fft_size() const#
inline size_t hop_size() const#
inline size_t num_bins() const#
inline size_t num_channels() const#
void push(std::span<const float*const> channels, size_t num_samples)#

Feeds num_samples of every channel (channels[c]: channel c; a missing or null one reads as silence).

Calls process_spectrum() for every frame that completes.

void reset()#

Drops what has been collected: the next frame needs fft_size() new samples.

Protected Functions

virtual void process_spectrum(size_t channel, std::span<const std::complex<float>> bins) = 0#

One frame of channel channel: num_bins() bins, scaled as described above.

Called for every channel of a frame in turn.

void clear()#

Back to unprepared, everything freed (as after a rejected prepare()).