c@25: // c@25: // AutocorrelationProcessor.cpp c@25: // Tempogram c@25: // c@25: // Created by Carl Bussey on 20/08/2014. c@25: // Copyright (c) 2014 Carl Bussey. All rights reserved. c@25: // c@25: c@25: #include "AutocorrelationProcessor.h" c@25: using namespace std; c@25: c@26: AutocorrelationProcessor::AutocorrelationProcessor(const size_t &windowLength, const unsigned int &hopSize) : c@25: m_windowLength(windowLength), c@25: m_hopSize(hopSize), c@26: m_blockInput(0) c@25: { c@26: m_blockInput = new float [m_windowLength]; c@26: } c@26: c@26: AutocorrelationProcessor::~AutocorrelationProcessor(){ c@26: delete []m_blockInput; c@26: m_blockInput = 0; c@25: } c@25: c@25: AutoCorrelation AutocorrelationProcessor::process(float * input, const size_t &inputLength) const c@25: { c@25: int readBlockPointerIndex = 0; c@25: AutoCorrelation autocorrelation; c@25: c@25: while(readBlockPointerIndex <= (int)inputLength) { c@26: int readPointer = readBlockPointerIndex - m_windowLength/2; //read window centered at readBlockPointerIndex c@25: c@26: for (int n = 0; n < (int)m_windowLength; n++){ c@26: if (readPointer < 0 || readPointer >= (int)inputLength) m_blockInput[n] = 0.0f; c@26: else m_blockInput[n] = input[readPointer]; c@26: c@26: readPointer++; c@26: } c@26: c@26: autocorrelation.push_back(processBlock()); c@25: readBlockPointerIndex += m_hopSize; c@25: } c@25: c@25: return autocorrelation; c@25: } c@25: c@26: vector AutocorrelationProcessor::processBlock() const c@25: { c@25: vector autocorrelation; c@25: c@25: int N = m_windowLength/m_lagIncrement; c@25: c@25: for (int lag = 0; lag < N; lag++){ c@25: float sum = 0; c@26: c@26: for (int n = 0; n < (int)m_windowLength-lag; n++){ c@26: sum += m_blockInput[lag]*m_blockInput[n+lag]; c@25: } c@25: autocorrelation.push_back(sum/(2*N + 1 - lag)); c@25: } c@25: c@25: return autocorrelation; c@25: }