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

FFT-based cochlear power spectrogram. More...

#include <ERB_FFTSpectrogram.h>

Public Member Functions

 ERB_FFTSpectrogram ()
 
void setSampleRate (float fs)
 
std::vector< std::vector< float > > filterSpectrum (std::vector< float > &inputSignal)
 
void drawSpectrogram (juce::Image &image, const std::vector< std::vector< float > > &erbSTFTPowMatrix)
 
std::vector< float > getERBSpace ()
 

Static Public Member Functions

static std::vector< float > erbs2Hz (const std::vector< float > &erbsVector)
 
static Eigen::VectorXf erbs2Hz (const Eigen::VectorXf &erbsVector)
 
static std::vector< float > hz2Erbs (const std::vector< float > &hzVector)
 
static Eigen::VectorXf hz2Erbs (const Eigen::VectorXf &hzVector)
 
static float hz2Erbs (const float hz)
 

Private Member Functions

std::vector< float > applyEarFilter (std::vector< float > &inputSignal)
 
std::vector< float > gtWindow (int numberOfPoints, float bParam, int order)
 
std::vector< float > cferb (std::vector< float > cf)
 
float cferb (const float cf)
 
double cferb (const double cf)
 
std::vector< float > erbSpace (float low, float hi, int N)
 
float centroid (const std::vector< float > &x)
 
float getNextPowerOfTwo (float value)
 
void buildFilterBank (int startSamplesSize)
 
std::vector< std::vector< float > > calculatePowerSpectrum (const std::vector< std::vector< float > > &inFrames)
 
void writeMatrixToFile (const std::vector< std::vector< float > > &matrix, const std::string &filename)
 

Private Attributes

float m_sampleRate {-1.0f}
 
std::vector< std::vector< double > > m_wfunct
 
std::vector< float > m_cfArray
 
EarFilter m_earFilter
 
float m_hopSizeSec = 0.0058f
 
int m_hopSizeSamples
 
const float m_exponent = 0.25f
 
float m_lo = 30.0f
 
float m_hi = 16000.0f
 
int m_numChans = 77
 
float m_bw0 = 24.7f
 
float m_b0 = 25.15274949f
 
float m_ERD = 0.0196797f
 
int m_wSize
 
std::vector< float > m_gtWin
 
const float m_scalingConstant = 0.1f
 
int m_fftSize
 

Detailed Description

FFT-based cochlear power spectrogram.

Power spectrogram with same frequency resolution and scale as human ear. ERB_FFTSpectrogram splits the signal into overlapping frames and windows each with a function shaped like the time-reversed envelope of a gammatone impulse response with parameters appropriate for a low-frequency cochlear filter, with an equivalent rectangular duration (ERD) of about 20ms. The FFT size is set to the power of two immediately larger than twice that value (sr*0.040), and the windowed slices are Fourier transformed to obtain a power spectrum. The frequency resolution is that of a "cf=0 Hz" channel, ie narrower than even the lowest cf channels, and the frequency axis is linear. To get a resolution similar to the cochlea, and channels evenly spaced on an equal-resolution scale (Cambridge ERBs), the power spectrum is remapped. Each channel of the new spectrum is the weighted sum of power spectrum coefficients, obtained by forming the vector product with a weighting function so that the channel has its proper spectral width.

Constructor & Destructor Documentation

◆ ERB_FFTSpectrogram()

krotos::ERB_FFTSpectrogram::ERB_FFTSpectrogram ( )

Constructor

Copyright Krotos LTD 2023

All rights reserved.

Member Function Documentation

◆ applyEarFilter()

std::vector< float > krotos::ERB_FFTSpectrogram::applyEarFilter ( std::vector< float > & inputSignal)
private

Applies the outer/middle ear filter using the EarFilter object

Parameters
inputSignalvector representing the input Signal
Returns
effective signal at entrance of cochlear filter bank

◆ buildFilterBank()

void krotos::ERB_FFTSpectrogram::buildFilterBank ( int startSamplesSize)
private

Builds the ERB scale Filterbank - saves result in m_wfunct. Casted to double to rectify float underflow!!

Parameters
startSampleSizelast hop index where the window will fit within the end of the signal

◆ calculatePowerSpectrum()

std::vector< std::vector< float > > krotos::ERB_FFTSpectrogram::calculatePowerSpectrum ( const std::vector< std::vector< float > > & inFrames)
private

Calculates the power spectrum per column

Parameters
inFramesmatrix represting the overlapping window frames of signal
Returns
powerSpectrum of signal TODO:: In the future it can be improved a lot for performance and also call ShortTimeFourierTransform to perform

◆ centroid()

float krotos::ERB_FFTSpectrogram::centroid ( const std::vector< float > & x)
private

Centroid of matrix x

Parameters
xmatrix
Returns
centroid as weighted average of row index

◆ cferb() [1/3]

double krotos::ERB_FFTSpectrogram::cferb ( const double cf)
private

◆ cferb() [2/3]

float krotos::ERB_FFTSpectrogram::cferb ( const float cf)
private

◆ cferb() [3/3]

std::vector< float > krotos::ERB_FFTSpectrogram::cferb ( std::vector< float > cf)
private

◆ drawSpectrogram()

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

Draws a simple spectrogram for POC purposes. Assumes we have already produced the STFT matrix using the filterSpectrum() 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.

◆ erbs2Hz() [1/2]

Eigen::VectorXf krotos::ERB_FFTSpectrogram::erbs2Hz ( const Eigen::VectorXf & erbsVector)
static

◆ erbs2Hz() [2/2]

std::vector< float > krotos::ERB_FFTSpectrogram::erbs2Hz ( const std::vector< float > & erbsVector)
static

Create ERB - scale uniformly - spaced frequencies(in Hertz)

◆ erbSpace()

std::vector< float > krotos::ERB_FFTSpectrogram::erbSpace ( float low,
float hi,
int N )
private

Computes an array of N frequencies uniformly spaced between lo and hi in the ERB scale. For a definition of ERB, see Moore, B. C. J., and Glasberg, B. R. (1983). "Suggested formulae for calculating auditory-filter bandwidths and excitation patterns," J. Acoust. Soc. Am. 74, 750-753.

Parameters
lowHz - lower limit of vector
hiHz - upper limit of vector
Nnumber of values to produce
Returns
vector of values

◆ filterSpectrum()

std::vector< std::vector< float > > krotos::ERB_FFTSpectrogram::filterSpectrum ( std::vector< float > & inputSignal)

Performs the fft power filtering operation

Parameters
inputSignalvector representing the input Signal
Returns
vector of vectors where each column is a different frame and each row a frequency bin.

◆ getERBSpace()

std::vector< float > krotos::ERB_FFTSpectrogram::getERBSpace ( )
inline

◆ getNextPowerOfTwo()

float krotos::ERB_FFTSpectrogram::getNextPowerOfTwo ( float value)
private

Next power of two of float

Parameters
valuenumber whose nextpow2 I'm looking for
Returns
nextpow2

◆ gtWindow()

std::vector< float > krotos::ERB_FFTSpectrogram::gtWindow ( int numberOfPoints,
float bParam,
int order )
private

Caclulates a window shaped like a time-reversed gammatone envelope

Parameters
numberOfPointsnumber of points
bParamb-parameter
orderorder of function

◆ hz2Erbs() [1/3]

Eigen::VectorXf krotos::ERB_FFTSpectrogram::hz2Erbs ( const Eigen::VectorXf & hzVector)
static

◆ hz2Erbs() [2/3]

float krotos::ERB_FFTSpectrogram::hz2Erbs ( const float hz)
static

◆ hz2Erbs() [3/3]

std::vector< float > krotos::ERB_FFTSpectrogram::hz2Erbs ( const std::vector< float > & hzVector)
static

Create frequency vector in Hz from the ERB equivalent vector

◆ setSampleRate()

void krotos::ERB_FFTSpectrogram::setSampleRate ( float fs)

Sets the sampling Rate

Parameters
fssampling Rate [Hz]

◆ writeMatrixToFile()

void krotos::ERB_FFTSpectrogram::writeMatrixToFile ( const std::vector< std::vector< float > > & matrix,
const std::string & filename )
inlineprivate

This function exists as a debugging mechanism to compare the spectrogram created here with it's equivalent in MATLAB.

Member Data Documentation

◆ m_b0

float krotos::ERB_FFTSpectrogram::m_b0 = 25.15274949f
private

◆ m_bw0

float krotos::ERB_FFTSpectrogram::m_bw0 = 24.7f
private

◆ m_cfArray

std::vector<float> krotos::ERB_FFTSpectrogram::m_cfArray
private

◆ m_earFilter

EarFilter krotos::ERB_FFTSpectrogram::m_earFilter
private

◆ m_ERD

float krotos::ERB_FFTSpectrogram::m_ERD = 0.0196797f
private

◆ m_exponent

const float krotos::ERB_FFTSpectrogram::m_exponent = 0.25f
private

◆ m_fftSize

int krotos::ERB_FFTSpectrogram::m_fftSize
private

◆ m_gtWin

std::vector<float> krotos::ERB_FFTSpectrogram::m_gtWin
private

◆ m_hi

float krotos::ERB_FFTSpectrogram::m_hi = 16000.0f
private

◆ m_hopSizeSamples

int krotos::ERB_FFTSpectrogram::m_hopSizeSamples
private

◆ m_hopSizeSec

float krotos::ERB_FFTSpectrogram::m_hopSizeSec = 0.0058f
private

◆ m_lo

float krotos::ERB_FFTSpectrogram::m_lo = 30.0f
private

◆ m_numChans

int krotos::ERB_FFTSpectrogram::m_numChans = 77
private

◆ m_sampleRate

float krotos::ERB_FFTSpectrogram::m_sampleRate {-1.0f}
private

◆ m_scalingConstant

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

◆ m_wfunct

std::vector<std::vector<double> > krotos::ERB_FFTSpectrogram::m_wfunct
private

◆ m_wSize

int krotos::ERB_FFTSpectrogram::m_wSize
private

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