comparison Spectrogram.cpp @ 8:4e429b9f2b4d

* Fixed memory leak bug. Issue with calculating size of spectrogram array. * Fixed bug in Tempogram::reset() where specData wasn't reinitialised.
author Carl Bussey <c.bussey@se10.qmul.ac.uk>
date Thu, 07 Aug 2014 17:25:24 +0100
parents 21147df9cb2d
children be59b4a73f49
comparison
equal deleted inserted replaced
7:21147df9cb2d 8:4e429b9f2b4d
13 13
14 Spectrogram::Spectrogram(unsigned int inputLength, unsigned int fftLength, unsigned int hopSize) : 14 Spectrogram::Spectrogram(unsigned int inputLength, unsigned int fftLength, unsigned int hopSize) :
15 m_inputLength(inputLength), 15 m_inputLength(inputLength),
16 m_fftLength(fftLength), 16 m_fftLength(fftLength),
17 m_hopSize(hopSize), 17 m_hopSize(hopSize),
18 m_numberOfOutputBins(floor(fftLength/2 + 0.5) + 1), 18 m_numberOfOutputBins(ceil(fftLength/2) + 1),
19 fftInput(NULL), 19 fftInput(NULL),
20 fftOutputReal(NULL), 20 fftOutputReal(NULL),
21 fftOutputImag(NULL) 21 fftOutputImag(NULL)
22 { 22 {
23 initialise(); 23 initialise();
30 void Spectrogram::initialise(){ 30 void Spectrogram::initialise(){
31 fftInput = new double [m_fftLength]; 31 fftInput = new double [m_fftLength];
32 fftOutputReal = new double [m_fftLength]; 32 fftOutputReal = new double [m_fftLength];
33 fftOutputImag = new double [m_fftLength]; 33 fftOutputImag = new double [m_fftLength];
34 34
35 int numberOfBlocks = ceil(m_inputLength/m_hopSize) + m_fftLength/m_hopSize-1; 35 int numberOfBlocks = ceil(m_inputLength/m_hopSize) + 2*(ceil(m_fftLength/m_hopSize)-1); //The last term corresponds to overlaps at the beginning and end with padded zeros. I.e., if m_hopSize = m_fftLength/2, there'll be 1 overlap at each end. If m_hopSize = m_fftLength/4, there'll be 3 overlaps at each end, etc...
36 spectrogramOutput = vector< vector<float> >(m_numberOfOutputBins, vector<float>(numberOfBlocks)); 36 spectrogramOutput = vector< vector<float> >(m_numberOfOutputBins, vector<float>(numberOfBlocks));
37 } 37 }
38 38
39 void Spectrogram::cleanup(){ 39 void Spectrogram::cleanup(){
40 delete []fftInput; 40 delete []fftInput;
41 delete []fftOutputReal; 41 delete []fftOutputReal;
42 delete []fftOutputImag; 42 delete []fftOutputImag;
43 43
44 fftInput = fftOutputReal = fftOutputImag = NULL; 44 fftInput = fftOutputReal = fftOutputImag = NULL;
45
46 cerr << "Spectrogram" << endl;
47 } 45 }
48 46
49 vector< vector<float> > Spectrogram::audioToMagnitudeSpectrogram(const float * const input, const float * window){ 47 vector< vector<float> > Spectrogram::audioToMagnitudeSpectrogram(const float * const input, const float * window){
50 48
51 int readPointerBeginIndex = m_hopSize-m_fftLength; 49 int readPointerBeginIndex = m_hopSize-m_fftLength;
67 FFT::forward(m_fftLength, fftInput, NULL, fftOutputReal, fftOutputImag); 65 FFT::forward(m_fftLength, fftInput, NULL, fftOutputReal, fftOutputImag);
68 66
69 //@todo: sample at logarithmic spacing? Leave for host? 67 //@todo: sample at logarithmic spacing? Leave for host?
70 for(int k = 0; k < m_numberOfOutputBins; k++){ 68 for(int k = 0; k < m_numberOfOutputBins; k++){
71 spectrogramOutput[k][writeBlockPointer] = (fftOutputReal[k]*fftOutputReal[k] + fftOutputImag[k]*fftOutputImag[k]); //Magnitude or power? 69 spectrogramOutput[k][writeBlockPointer] = (fftOutputReal[k]*fftOutputReal[k] + fftOutputImag[k]*fftOutputImag[k]); //Magnitude or power?
70 cerr << writeBlockPointer << " : " << spectrogramOutput[k].size() << endl;
72 } 71 }
73 72
74 readPointerBeginIndex += m_hopSize; 73 readPointerBeginIndex += m_hopSize;
75 writeBlockPointer++; 74 writeBlockPointer++;
76 } 75 }