Krotos Modules 3
Loading...
Searching...
No Matches
krotos::ShortTimeFourierTransform Class Reference

Apply STFT analysis to a full audio signal offline. More...

#include <ShortTimeFourierTransform.h>

Classes

struct  BinParams
 
struct  DFTParams
 

Public Types

enum  fftMode { freqMagOnly = 0 , realOnly , complex , modeNUM_MODES }
 
enum class  FrequencyRange { Whole , Half }
 

Public Member Functions

 ShortTimeFourierTransform (int winSizeSamples, int fftSizeSamples, int hopSize, WindowType winMethod, fftMode mode, int sampleRate)
 
std::vector< std::vector< float > > stft (const AudioSampleBuffer &inputSignal)
 
Eigen::MatrixXf processSignal (const Eigen::VectorXf inputSignal)
 
std::vector< float > istft (std::vector< std::vector< float > > stftMatrix)
 
void drawSpectrogram (juce::Image &image, const std::vector< std::vector< float > > &stftMatrix)
 
std::vector< float > getBinsSTL (ShortTimeFourierTransform::FrequencyRange freqRange)
 
Eigen::VectorXf getBins (ShortTimeFourierTransform::FrequencyRange freqRange)
 Calculates and returns the frequency bins for a given frequency range within the context of a short-time Fourier transform analysis. This function computes the frequency resolution based on the FFT size and sample rate, adjusts the frequency bins for odd or even number of points, and then selects the appropriate range of bins to return based on the specified frequency range.
 
std::vector< float > getColumnsSTL ()
 
Eigen::VectorXf getColumns ()
 Computes and returns the time indices for the center of each STFT window.
 
float getFFTBinSize () const
 

Public Attributes

enum krotos::ShortTimeFourierTransform::fftMode m_mode
 
struct krotos::ShortTimeFourierTransform::DFTParams dftParams
 

Private Member Functions

void NPTSinfo (float NPTS)
 
Eigen::VectorXf finalGrid (const Eigen::VectorXf &w1, float Nyq, bool centerDC, ShortTimeFourierTransform::FrequencyRange freqRange)
 

Private Attributes

struct krotos::ShortTimeFourierTransform::BinParams binParams
 
juce::dsp::FFT m_fft
 
std::vector< float > m_window
 
std::vector< float > m_sigFrame
 
Eigen::VectorXf sigFrame
 
int m_numFrames {0}
 
const float m_scalingConstant = 0.1f
 
int m_inputSize {0}
 

Detailed Description

Apply STFT analysis to a full audio signal offline.

This class implements the STFT analysis on an audio signal. Depending on the frame size and hop size it segments the signal into overlapping windowed frames and performs an fft on each one, depending on the value fftMode has been set to. Also includes a spectrogram drawing function for debugging purposes and an istft routine. Window for now uses n+1 samples for perfect signal reconstruction(hann Window + overlap = 50%)

Member Enumeration Documentation

◆ fftMode

enumeration choosing which FFT to use.

Enumerator
freqMagOnly 
realOnly 
complex 
modeNUM_MODES 

◆ FrequencyRange

Enumerator
Whole 
Half 

Constructor & Destructor Documentation

◆ ShortTimeFourierTransform()

krotos::ShortTimeFourierTransform::ShortTimeFourierTransform ( int winSizeSamples,
int fftSizeSamples,
int hopSize,
WindowType winMethod,
fftMode mode,
int sampleRate )

Member Function Documentation

◆ drawSpectrogram()

void krotos::ShortTimeFourierTransform::drawSpectrogram ( juce::Image & image,
const std::vector< std::vector< float > > & stftMatrix )

Draws a simple spectrogram for POC purposes. Assumes we have already produced the STFT matrix using the stft() above.

Parameters
imagethe Image component whose pixels we want to colour according to the mag of the bins. TODO: write a better scaling algorithm and average the signal magnitudes.
stftMatrixthe STFT matrix in mxn dimensions where m: mth frame, n: nth bin. NOTE: you should call repaint after this so that the new Image appears.

◆ finalGrid()

Eigen::VectorXf krotos::ShortTimeFourierTransform::finalGrid ( const Eigen::VectorXf & w1,
float Nyq,
bool centerDC,
ShortTimeFourierTransform::FrequencyRange freqRange )
private

◆ getBins()

Eigen::VectorXf krotos::ShortTimeFourierTransform::getBins ( ShortTimeFourierTransform::FrequencyRange freqRange)

Calculates and returns the frequency bins for a given frequency range within the context of a short-time Fourier transform analysis. This function computes the frequency resolution based on the FFT size and sample rate, adjusts the frequency bins for odd or even number of points, and then selects the appropriate range of bins to return based on the specified frequency range.

Parameters
freqRangeSpecifies the frequency range for which the bins should be calculated. This can be WHOLE, HALF, or any other defined range within the FrequencyRange enum.
Returns
Eigen::VectorXf A vector containing the calculated frequency bins for the specified frequency range. The size of the vector and the values within depend on the FFT size, sample rate, and the selected frequency range.
Note
The function assumes the ShortTimeFourierTransform object has been properly initialized and contains valid DFT parameters.

◆ getBinsSTL()

std::vector< float > krotos::ShortTimeFourierTransform::getBinsSTL ( ShortTimeFourierTransform::FrequencyRange freqRange)

◆ getColumns()

Eigen::VectorXf krotos::ShortTimeFourierTransform::getColumns ( )

Computes and returns the time indices for the center of each STFT window.

This function calculates the column offsets for the short-time Fourier transform (STFT) output based on the input size, window size, overlap, and hop size. The time indices represent the center of each STFT window, normalized by the sample rate to convert from samples to seconds. These indices can be used to understand the temporal positioning of each column in the STFT result.

Precondition
The STFT must have been computed prior to calling this function, as indicated by a non-zero input size (m_inputSize). This function relies on the assumption that m_inputSize, dftParams.winSize, dftParams.nOverlap, and dftParams.hopSize are properly initialized and represent the actual parameters used in the STFT computation.
Returns
Eigen::VectorXf A vector containing the time indices (in seconds) for the center of each STFT window. The length of the vector equals the number of columns in the STFT output.

◆ getColumnsSTL()

std::vector< float > krotos::ShortTimeFourierTransform::getColumnsSTL ( )

◆ getFFTBinSize()

float krotos::ShortTimeFourierTransform::getFFTBinSize ( ) const

◆ istft()

std::vector< float > krotos::ShortTimeFourierTransform::istft ( std::vector< std::vector< float > > stftMatrix)

Performs an inverse STFT. Assumes we have already produced the STFT matrix using the stft() above. This means that the frames retrieved from the istft are not windowed again and the mode is real only fft. TODO: If needed write version that rewindows the frames.

Parameters
stftMatrixthe STFT matrix in mxn dimensions where m: mth frame, n: nth bin. returns a vector of floats representing the reconstructed signal

◆ NPTSinfo()

void krotos::ShortTimeFourierTransform::NPTSinfo ( float NPTS)
private

◆ processSignal()

Eigen::MatrixXf krotos::ShortTimeFourierTransform::processSignal ( const Eigen::VectorXf inputSignal)

◆ stft()

std::vector< std::vector< float > > krotos::ShortTimeFourierTransform::stft ( const AudioSampleBuffer & inputSignal)

Performs a forward STFT. Depending on the mode of the FFT we want to use it either performs a real only or a freqMagnitude only FFT. TODO: Extract information from the complex number fft.

Parameters
bufferthe whole input Sample we want to analyse frame by frame. returns a vector of vectors where each column is a different frame and each row a frequency bin. TODO:: stl removal

Member Data Documentation

◆ binParams

struct krotos::ShortTimeFourierTransform::BinParams krotos::ShortTimeFourierTransform::binParams
private

◆ dftParams

struct krotos::ShortTimeFourierTransform::DFTParams krotos::ShortTimeFourierTransform::dftParams

◆ m_fft

juce::dsp::FFT krotos::ShortTimeFourierTransform::m_fft
private

◆ m_inputSize

int krotos::ShortTimeFourierTransform::m_inputSize {0}
private

◆ m_mode

enum krotos::ShortTimeFourierTransform::fftMode krotos::ShortTimeFourierTransform::m_mode

◆ m_numFrames

int krotos::ShortTimeFourierTransform::m_numFrames {0}
private

◆ m_scalingConstant

const float krotos::ShortTimeFourierTransform::m_scalingConstant = 0.1f
private

◆ m_sigFrame

std::vector<float> krotos::ShortTimeFourierTransform::m_sigFrame
private

◆ m_window

std::vector<float> krotos::ShortTimeFourierTransform::m_window
private

◆ sigFrame

Eigen::VectorXf krotos::ShortTimeFourierTransform::sigFrame
private

The documentation for this class was generated from the following files: