annotate AutocorrelationProcessor.cpp @ 27:a3a37c8dcee7

* Fixed bug causing ACT not to work
author Carl Bussey <c.bussey@se10.qmul.ac.uk>
date Wed, 20 Aug 2014 16:48:54 +0100
parents ff6110f1144b
children 723af5b3303a
rev   line source
c@25 1 //
c@25 2 // AutocorrelationProcessor.cpp
c@25 3 // Tempogram
c@25 4 //
c@25 5 // Created by Carl Bussey on 20/08/2014.
c@25 6 // Copyright (c) 2014 Carl Bussey. All rights reserved.
c@25 7 //
c@25 8
c@25 9 #include "AutocorrelationProcessor.h"
c@25 10 using namespace std;
c@25 11
c@26 12 AutocorrelationProcessor::AutocorrelationProcessor(const size_t &windowLength, const unsigned int &hopSize) :
c@25 13 m_windowLength(windowLength),
c@25 14 m_hopSize(hopSize),
c@26 15 m_blockInput(0)
c@25 16 {
c@26 17 m_blockInput = new float [m_windowLength];
c@26 18 }
c@26 19
c@26 20 AutocorrelationProcessor::~AutocorrelationProcessor(){
c@26 21 delete []m_blockInput;
c@26 22 m_blockInput = 0;
c@25 23 }
c@25 24
c@25 25 AutoCorrelation AutocorrelationProcessor::process(float * input, const size_t &inputLength) const
c@25 26 {
c@25 27 int readBlockPointerIndex = 0;
c@25 28 AutoCorrelation autocorrelation;
c@25 29
c@25 30 while(readBlockPointerIndex <= (int)inputLength) {
c@26 31 int readPointer = readBlockPointerIndex - m_windowLength/2; //read window centered at readBlockPointerIndex
c@25 32
c@26 33 for (int n = 0; n < (int)m_windowLength; n++){
c@26 34 if (readPointer < 0 || readPointer >= (int)inputLength) m_blockInput[n] = 0.0f;
c@26 35 else m_blockInput[n] = input[readPointer];
c@26 36
c@26 37 readPointer++;
c@26 38 }
c@26 39
c@26 40 autocorrelation.push_back(processBlock());
c@25 41 readBlockPointerIndex += m_hopSize;
c@25 42 }
c@25 43
c@25 44 return autocorrelation;
c@25 45 }
c@25 46
c@26 47 vector<float> AutocorrelationProcessor::processBlock() const
c@25 48 {
c@25 49 vector<float> autocorrelation;
c@25 50
c@27 51 int N = m_windowLength;
c@25 52
c@25 53 for (int lag = 0; lag < N; lag++){
c@25 54 float sum = 0;
c@26 55
c@27 56 for (int n = 0; n < N-lag; n++){
c@27 57 sum += m_blockInput[n]*m_blockInput[n+lag];
c@25 58 }
c@25 59 autocorrelation.push_back(sum/(2*N + 1 - lag));
c@25 60 }
c@25 61
c@25 62 return autocorrelation;
c@25 63 }