comparison AutocorrelationProcessor.cpp @ 26:ff6110f1144b

* Added an additional buffer in AutocorrelationProcessor::process() - possibly a little bit slower, but much easier to debug!
author Carl Bussey <c.bussey@se10.qmul.ac.uk>
date Wed, 20 Aug 2014 16:32:52 +0100
parents fe23998968b4
children a3a37c8dcee7
comparison
equal deleted inserted replaced
25:fe23998968b4 26:ff6110f1144b
7 // 7 //
8 8
9 #include "AutocorrelationProcessor.h" 9 #include "AutocorrelationProcessor.h"
10 using namespace std; 10 using namespace std;
11 11
12 AutocorrelationProcessor::AutocorrelationProcessor(const size_t &windowLength, const unsigned int &hopSize, const unsigned int &lagIncrement) : 12 AutocorrelationProcessor::AutocorrelationProcessor(const size_t &windowLength, const unsigned int &hopSize) :
13 m_windowLength(windowLength), 13 m_windowLength(windowLength),
14 m_hopSize(hopSize), 14 m_hopSize(hopSize),
15 m_lagIncrement(lagIncrement) 15 m_blockInput(0)
16 { 16 {
17 //Nothing to do here 17 m_blockInput = new float [m_windowLength];
18 }
19
20 AutocorrelationProcessor::~AutocorrelationProcessor(){
21 delete []m_blockInput;
22 m_blockInput = 0;
18 } 23 }
19 24
20 AutoCorrelation AutocorrelationProcessor::process(float * input, const size_t &inputLength) const 25 AutoCorrelation AutocorrelationProcessor::process(float * input, const size_t &inputLength) const
21 { 26 {
22 int readBlockPointerIndex = 0; 27 int readBlockPointerIndex = 0;
23 AutoCorrelation autocorrelation; 28 AutoCorrelation autocorrelation;
24 29
25 while(readBlockPointerIndex <= (int)inputLength) { 30 while(readBlockPointerIndex <= (int)inputLength) {
26 int readPointer = readBlockPointerIndex - m_windowLength/2; 31 int readPointer = readBlockPointerIndex - m_windowLength/2; //read window centered at readBlockPointerIndex
27 32
28 autocorrelation.push_back(processBlock(&input[readPointer], min(inputLength-readPointer, m_windowLength))); 33 for (int n = 0; n < (int)m_windowLength; n++){
34 if (readPointer < 0 || readPointer >= (int)inputLength) m_blockInput[n] = 0.0f;
35 else m_blockInput[n] = input[readPointer];
36
37 readPointer++;
38 }
39
40 autocorrelation.push_back(processBlock());
29 readBlockPointerIndex += m_hopSize; 41 readBlockPointerIndex += m_hopSize;
30 } 42 }
31 43
32 return autocorrelation; 44 return autocorrelation;
33 } 45 }
34 46
35 vector<float> AutocorrelationProcessor::processBlock(float * blockInput, const size_t &blockLength) const 47 vector<float> AutocorrelationProcessor::processBlock() const
36 { 48 {
37 vector<float> autocorrelation; 49 vector<float> autocorrelation;
38 50
39 int N = m_windowLength/m_lagIncrement; 51 int N = m_windowLength/m_lagIncrement;
40 52
41 for (int lag = 0; lag < N; lag++){ 53 for (int lag = 0; lag < N; lag++){
42 float sum = 0; 54 float sum = 0;
43 int sampleLag = m_lagIncrement*lag; 55
44 56 for (int n = 0; n < (int)m_windowLength-lag; n++){
45 for (int n = sampleLag; n < (int)blockLength; n++){ 57 sum += m_blockInput[lag]*m_blockInput[n+lag];
46 sum += blockInput[n-sampleLag]*blockInput[n];
47 } 58 }
48 autocorrelation.push_back(sum/(2*N + 1 - lag)); 59 autocorrelation.push_back(sum/(2*N + 1 - lag));
49 } 60 }
50 61
51 return autocorrelation; 62 return autocorrelation;