Mercurial > hg > vamp-tempogram
comparison SpectrogramProcessor.cpp @ 14:c11367df624d
* Renamed NoveltyCurve.* and Spectrogram.* to $(Name)Processor.*
* Aligned novelty curve with audio - when performing FIRFilter::process(params), take inputLength after group delay.
* Removed trail of Spectrogram.
* General tidying!
author | Carl Bussey <c.bussey@se10.qmul.ac.uk> |
---|---|
date | Thu, 14 Aug 2014 10:31:49 +0100 |
parents | Spectrogram.cpp@7680cc4c0073 |
children | 1e4c02ca8b81 |
comparison
equal
deleted
inserted
replaced
13:7680cc4c0073 | 14:c11367df624d |
---|---|
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 "SpectrogramProcessor.h" | |
10 using namespace std; | |
11 using Vamp::FFT; | |
12 #include <iostream> | |
13 | |
14 SpectrogramProcessor::SpectrogramProcessor(const size_t &windowLength, const size_t &fftLength, const size_t &hopSize) : | |
15 m_windowLength(windowLength), | |
16 m_fftLength(fftLength), | |
17 m_hopSize(hopSize), | |
18 m_numberOfOutputBins(ceil(fftLength/2) + 1), | |
19 m_pFftInput(0), | |
20 m_pFftOutputReal(0), | |
21 m_pFftOutputImag(0) | |
22 { | |
23 initialise(); | |
24 } | |
25 | |
26 SpectrogramProcessor::~SpectrogramProcessor(){ | |
27 cleanup(); | |
28 } | |
29 | |
30 void SpectrogramProcessor::initialise(){ | |
31 m_pFftInput = new double [m_fftLength]; | |
32 m_pFftOutputReal = new double [m_fftLength]; | |
33 m_pFftOutputImag = new double [m_fftLength]; | |
34 } | |
35 | |
36 void SpectrogramProcessor::cleanup(){ | |
37 delete []m_pFftInput; | |
38 delete []m_pFftOutputReal; | |
39 delete []m_pFftOutputImag; | |
40 | |
41 m_pFftInput = m_pFftOutputReal = m_pFftOutputImag = 0; | |
42 } | |
43 | |
44 SpectrogramTransposed SpectrogramProcessor::transpose(const Spectrogram &spectrogram){ | |
45 int numberOfBlocks = spectrogram.size(); | |
46 int numberOfBins = spectrogram[0].size(); | |
47 | |
48 SpectrogramTransposed spectrogramT(numberOfBins, vector<float>(numberOfBlocks)); | |
49 | |
50 for (int i = 0; i < numberOfBlocks; i++){ | |
51 for (int j = 0; j < numberOfBins; j++){ | |
52 spectrogramT[j][i] = spectrogram[i][j]; | |
53 } | |
54 } | |
55 | |
56 return spectrogramT; | |
57 } | |
58 | |
59 //process method | |
60 Spectrogram SpectrogramProcessor::process(const float * const pInput, const size_t &inputLength, const float * pWindow, const bool &transposeOutput) const | |
61 { | |
62 Spectrogram spectrogram; | |
63 | |
64 unsigned int readBlockPointerIndex = 0; | |
65 unsigned int writeBlockPointer = 0; | |
66 | |
67 //cout << m_hopSize << endl; | |
68 while(readBlockPointerIndex <= inputLength) { | |
69 | |
70 int readPointer = readBlockPointerIndex - m_windowLength/2; | |
71 for (unsigned int n = 0; n < m_windowLength; n++){ | |
72 if(readPointer < 0 || readPointer >= (int)inputLength){ | |
73 m_pFftInput[n] = 0.0; //pad with zeros | |
74 } | |
75 else{ | |
76 m_pFftInput[n] = pInput[readPointer] * pWindow[n]; | |
77 } | |
78 readPointer++; | |
79 } | |
80 for (unsigned int n = m_windowLength; n < m_fftLength; n++){ | |
81 m_pFftInput[n] = 0.0; | |
82 } | |
83 | |
84 FFT::forward(m_fftLength, m_pFftInput, 0, m_pFftOutputReal, m_pFftOutputImag); | |
85 | |
86 vector<float> binValues; | |
87 //@todo: sample at logarithmic spacing? Leave for host? | |
88 for(unsigned int k = 0; k < m_numberOfOutputBins; k++){ | |
89 binValues.push_back(m_pFftOutputReal[k]*m_pFftOutputReal[k] + m_pFftOutputImag[k]*m_pFftOutputImag[k]); //Magnitude or power? | |
90 //std::cout << spectrogram[k][writeBlockPointer] << std::endl; | |
91 } | |
92 spectrogram.push_back(binValues); | |
93 | |
94 readBlockPointerIndex += m_hopSize; | |
95 writeBlockPointer++; | |
96 } | |
97 | |
98 if(transposeOutput) return transpose(spectrogram); | |
99 else return spectrogram; | |
100 } |