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@25
|
51 int N = m_windowLength/m_lagIncrement;
|
c@25
|
52
|
c@25
|
53 for (int lag = 0; lag < N; lag++){
|
c@25
|
54 float sum = 0;
|
c@26
|
55
|
c@26
|
56 for (int n = 0; n < (int)m_windowLength-lag; n++){
|
c@26
|
57 sum += m_blockInput[lag]*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 } |