Mercurial > hg > vamp-tempogram
comparison Spectrogram.cpp @ 7:21147df9cb2d
* Error when deleting Spectrogram object in Tempogram::getRemainingFeatures().
* Moved Spectrogram computation into own class.
author | Carl Bussey <c.bussey@se10.qmul.ac.uk> |
---|---|
date | Thu, 07 Aug 2014 16:21:21 +0100 |
parents | |
children | 4e429b9f2b4d |
comparison
equal
deleted
inserted
replaced
6:14a143a2c4c9 | 7:21147df9cb2d |
---|---|
1 // | |
2 // Spectrogram.cpp | |
3 // Tempogram | |
4 // | |
5 // Created by Carl Bussey on 07/08/2014. | |
6 // Copyright (c) 2014 Carl Bussey. All rights reserved. | |
7 // | |
8 | |
9 #include "Spectrogram.h" | |
10 #include <iostream> | |
11 using namespace std; | |
12 using Vamp::FFT; | |
13 | |
14 Spectrogram::Spectrogram(unsigned int inputLength, unsigned int fftLength, unsigned int hopSize) : | |
15 m_inputLength(inputLength), | |
16 m_fftLength(fftLength), | |
17 m_hopSize(hopSize), | |
18 m_numberOfOutputBins(floor(fftLength/2 + 0.5) + 1), | |
19 fftInput(NULL), | |
20 fftOutputReal(NULL), | |
21 fftOutputImag(NULL) | |
22 { | |
23 initialise(); | |
24 } | |
25 | |
26 Spectrogram::~Spectrogram(){ | |
27 cleanup(); | |
28 } | |
29 | |
30 void Spectrogram::initialise(){ | |
31 fftInput = new double [m_fftLength]; | |
32 fftOutputReal = new double [m_fftLength]; | |
33 fftOutputImag = new double [m_fftLength]; | |
34 | |
35 int numberOfBlocks = ceil(m_inputLength/m_hopSize) + m_fftLength/m_hopSize-1; | |
36 spectrogramOutput = vector< vector<float> >(m_numberOfOutputBins, vector<float>(numberOfBlocks)); | |
37 } | |
38 | |
39 void Spectrogram::cleanup(){ | |
40 delete []fftInput; | |
41 delete []fftOutputReal; | |
42 delete []fftOutputImag; | |
43 | |
44 fftInput = fftOutputReal = fftOutputImag = NULL; | |
45 | |
46 cerr << "Spectrogram" << endl; | |
47 } | |
48 | |
49 vector< vector<float> > Spectrogram::audioToMagnitudeSpectrogram(const float * const input, const float * window){ | |
50 | |
51 int readPointerBeginIndex = m_hopSize-m_fftLength; | |
52 int writeBlockPointer = 0; | |
53 | |
54 while(readPointerBeginIndex < m_inputLength){ | |
55 | |
56 int readPointer = readPointerBeginIndex; | |
57 for (int n = 0; n < m_fftLength; n++){ | |
58 if(readPointer < 0 || readPointer >= m_inputLength){ | |
59 fftInput[n] = 0.0; //pad with zeros | |
60 } | |
61 else{ | |
62 fftInput[n] = input[readPointer] * window[n]; | |
63 } | |
64 readPointer++; | |
65 } | |
66 | |
67 FFT::forward(m_fftLength, fftInput, NULL, fftOutputReal, fftOutputImag); | |
68 | |
69 //@todo: sample at logarithmic spacing? Leave for host? | |
70 for(int k = 0; k < m_numberOfOutputBins; k++){ | |
71 spectrogramOutput[k][writeBlockPointer] = (fftOutputReal[k]*fftOutputReal[k] + fftOutputImag[k]*fftOutputImag[k]); //Magnitude or power? | |
72 } | |
73 | |
74 readPointerBeginIndex += m_hopSize; | |
75 writeBlockPointer++; | |
76 } | |
77 | |
78 return spectrogramOutput; | |
79 } |