-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautocorrelation.cpp
More file actions
84 lines (65 loc) · 1.48 KB
/
autocorrelation.cpp
File metadata and controls
84 lines (65 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "autocorrelation.h"
#include <vector>
AutoCorrelation::AutoCorrelation()
{
freq = 0;
}
void AutoCorrelation::analyse(std::array<float, bufferSize> wave, int max_samples)
{
float sum = 0;
float norm = 0;
/*
int pd_state = 0;
int thresh = 0;
int period = 0;
*/
std::vector<float> ac_data;
for (int i = 0; i < bufferSize/2; i++)
{
sum = 0;
for(int j = 0; j < bufferSize/2; j++)
{
//if(j+i > max_samples) break;
sum += wave[j]*wave[j+i];
}
if(i == 0)
norm = sum;
sum /= norm;
ac_data.push_back(sum);
// Peak Detect
/*
if (pd_state == 2 && (sum-sum_old) <=0)
{
period = i;
pd_state = 3;
}
if (pd_state == 1 && (sum > thresh) && (sum-sum_old) > 0)
pd_state = 2;
if (!i && pd_state == 0)
{
thresh = sum * 0.5;
pd_state = 1;
}
*/
}
float max = ac_data[0]*0.5;
int max_i = 0;
bool first_peak = true;
for(int i = max_samples/2; i < ac_data.size(); i++)
{
if(ac_data[i] > max)
{
if(first_peak == false)
{
max = ac_data[i];
max_i = i;
}
else first_peak = false;
}
}
// Frequency identified in Hz
if(max_i != 0)
freq = (float)sampleRate/max_i;
else
freq = 0;
}