-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSpectrogram.h
More file actions
143 lines (116 loc) · 5.59 KB
/
Spectrogram.h
File metadata and controls
143 lines (116 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#if !defined(__ACA_Spectrogram_HEADER_INCLUDED__)
#define __ACA_Spectrogram_HEADER_INCLUDED__
#include <string>
#include "ErrorDef.h"
// forward declarations
class CAudioFileIf;
class CFft;
class CNormalizeAudio;
class CBlockAudioIf;
/*! \brief class for computation of a magnitude spectrogram from either a file or a vector
*/
class CSpectrogramIf
{
public:
enum AxisLabel_t
{
kFrequencyInHz,
kTimeInS,
kNumAxisLabels
};
/*! initializes a Spectrogram instance with file reading
\param pCInstance pointer to instance to be written
\param strAudioFilePath complete path to audio file
\param iBlockLength: FFT block length in samples
\param iHopLength: hop length in samples
\param bNormalize: flag if input audio should be normalized
\param pfWindow: window function of length iBlockLength (optional, default will be von-Hann if 0)
\return Error_t
*/
static Error_t create(CSpectrogramIf *&pCInstance, const std::string &strAudioFilePath, int iBlockLength = 2048, int iHopLength = 1024, bool bNormalize = true, float *pfWindow = 0);
/*! initializes a Spectrogram instance from audio data
\param pCInstance pointer to instance to be written
\param pfAudio complete audio data
\param iNumSamples: length of pfAudio
\param fSampleRate: sample rate in Hz
\param iBlockLength: FFT block length in samples
\param iHopLength: hop length in samples
\param bNormalize: flag if input audio should be normalized
\param pfWindow: window function of length iBlockLength (optional, default will be von-Hann if 0)
\return Error_t
*/
static Error_t create(CSpectrogramIf *&pCInstance, const float *pfAudio, long long iNumSamples, float fSampleRate, int iBlockLength = 2048, int iHopLength = 1024, bool bNormalize = true, float *pfWindow = 0);
/*! destroys a Spectrogram instance
\param pCInstance pointer to instance to be destroyed
\return Error_t
*/
static Error_t destroy(CSpectrogramIf *&pCInstance);
/*! returns size of matrix to be allocated by user
\param iNumRows (number of rows, to be written) equals number of frequency bins
\param iNumCols (number of columns, to be written) equals number of blocks
\return Error_t
*/
Error_t getSpectrogramDimensions(int &iNumRows, int &iNumCols) const;
/*! returns axis ticks
\param pfAxisTicks (to be written) equals iNumRows if eAxisLabel == kFrequencyInHz, otherwise iNumCols
\param eAxisLabel indicator which axis
\return Error_t
*/
Error_t getSpectrogramAxisVectors(float *pfAxisTicks, enum AxisLabel_t eAxisLabel) const;
/*! performs the Spectrogram computation
\param ppfSpectrogram (user-allocated, to be written, dimensions from CSpectrogramIf::getSpectrogramDimensions)
\return Error_t
*/
Error_t compSpectrogram(float **ppfSpectrogram);
/*! \brief structure holding configuration specifics for the mel spectrogram computation
*/
struct MelSpectrogramConfig_t
{
int iNumMelBins = 0; //!< number of frequency bins
float fMinFreqInHz = 0; //!< maximum frequency
float fMaxFreqInHz = 0; //!< minimum frequency
bool bIsLogarithmic = false; //!< true for logarithmic amplitude
};
/*! returns size of matrix to be allocated by user
\param iNumRows (number of rows, to be written) equals number of frequency bins
\param iNumCols (number of columns, to be written) equals number of blocks
\param pMelSpecConfig parametrization of the mel spectrogram
\return Error_t
*/
Error_t getMelSpectrogramDimensions(int &iNumRows, int &iNumCols, const MelSpectrogramConfig_t *pMelSpecConfig) const;
/*! returns axis ticks
\param pfAxisTicks (to be written) equals iNumRows if eAxisLabel == kFrequencyInHz, otherwise iNumCols
\param eAxisLabel indicator which axis
\param pMelSpecConfig parametrization of the mel spectrogram
\return Error_t
*/
Error_t getMelSpectrogramAxisVectors(float *pfAxisTicks, enum AxisLabel_t eAxisLabel, const MelSpectrogramConfig_t *pMelSpecConfig);
/*! performs the Spectrogram computation
\param ppfMelSpectrogram (user-allocated, to be written, dimensions from CSpectrogramIf::getSpectrogramDimensions)
\param pMelSpecConfig parametrization of the mel spectrogram
\return Error_t
*/
Error_t compMelSpectrogram(float **ppfMelSpectrogram, const MelSpectrogramConfig_t *pMelSpecConfig);
protected:
CSpectrogramIf();
virtual ~CSpectrogramIf();
CSpectrogramIf(const CSpectrogramIf &that);
CSpectrogramIf &operator=(const CSpectrogramIf &c);
Error_t reset_(); //!< reset configuration
Error_t init_(float *pfWindow); //!< init configuration
void computeMagSpectrum_(int iLength);
Error_t generateMelFb_(const MelSpectrogramConfig_t *pMelSpecConfig);
void destroyMelFb_(const MelSpectrogramConfig_t *pMelSpecConfig);
CNormalizeAudio *m_pCNormalize = 0; //!< instantiate if audio file normalization is wanted
CBlockAudioIf *m_pCBlockAudio = 0; //!< instantiate for blocking time domain signal
CFft *m_pCFft = 0; //!< fft instance
int m_iBlockLength = 0, //!< fft length
m_iHopLength = 0; //!< hop length
float m_fSampleRate = 0; //!< sample rate
float *m_pfSpectrum = 0; //!< temporary buffer for current spectrum
float *m_pfProcBuff = 0; //!< temporary buffer
float **m_ppfHMel = 0; //!< Mel filterbank
float *m_pffcMel = 0; //!< Mel center frequencies
bool m_bIsInitialized = false;//!< true if initialized
};
#endif // #if !defined(__ACA_Spectrogram_HEADER_INCLUDED__)