-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgorithm.cpp
181 lines (139 loc) · 3.47 KB
/
Algorithm.cpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//Listing 1 A Goertzel implementation
// Last update 26/08/2023
#define PI 3.14
#define SAMPLING_RATE 8000.0 //8kHz
#define TARGET_FREQUENCY 1000.0 //941 Hz
#define N 300 //Block size
int X = 0, Y;
unsigned ADCvalue;
float temp, temp2;
float max = 0;
float max_frq = 0;
double coeff;
double Q1;
double Q2;
double sine;
double cosine;
unsigned char testData[N];
/* Call this routine before every "block" (size=N) of samples. */
void ResetGoertzel(void)
{
Q2 = 0;
Q1 = 0;
}
/* Call this once, to precompute the constants. */
void InitGoertzel(void)
{
int k;
double floatN;
double omega;
floatN = (double)N;
k = (int)(0.5 + ((floatN * TARGET_FREQUENCY) / SAMPLING_RATE));
omega = (2.0 * PI * k) / floatN;
sine = sin(omega);
cosine = cos(omega);
coeff = 2.0 * cosine;
ResetGoertzel();
}
/* Call this routine for every sample. */
void ProcessSample(unsigned char sample)
{
double Q0;
Q0 = coeff * Q1 - Q2 + (double)sample;
Q2 = Q1;
Q1 = Q0;
}
/* Basic Goertzel */
/* Call this routine after every block to get the complex result. */
void GetRealImag(double *realPart, double *imagPart)
{
*realPart = (Q1 - Q2 * cosine);
*imagPart = (Q2 * sine);
}
/*** End of Goertzel-specific code, the remainder is test code. */
/* Synthesize some test data at a given frequency. */
void sin_wave(double frequency)
{
int index;
double step;
step = frequency * ((2.0 * PI) / SAMPLING_RATE);
/* Generate the test data */
for (index = 0; index < N; index++)
{
testData[index] = (unsigned char)(25.0 * sin(index * step) + 25.0);
ResetGoertzel();
}
}
void dumped_sin_wave(double frequency)
{
int index;
double step;
step = frequency * ((2.0 * PI) / SAMPLING_RATE);
/* Generate the test data */
for (index = 0; index < N; index++)
{
testData[index] = (unsigned char)(25.0 * exp(-(double)index * 0.007) * sin(index * step) + 25.0);
ResetGoertzel();
}
}
void sin_wave_with_changing_freq(double frequency)
{
int index;
double step;
double frequency1;
double f_0 = frequency/100.0;
/* Generate the test data */
for (index = 0; index < N; index++)
{
frequency1 = frequency + 5*sin(PI*index);
step = frequency1 * ((2.0 * PI) / N);
testData[index] = (unsigned char)(25.0 * sin(index * step) + 25.0);
ResetGoertzel();
}
}
/* Demo */
void GenerateAndTest(double frequency)
{
int index;
double magnitudeSquared;
double magnitude;
double real;
double imag;
//sin_wave(frequency);
//dumped_sin_wave(frequency);
sin_wave_with_changing_freq(frequency);
/* Process the samples. */
for (index = 0; index < N; ++index)
{
ProcessSample(testData[index]);
}
/* Do the "standard Goertzel" processing. */
GetRealImag(&real, &imag);
magnitudeSquared = real * real + imag * imag;
magnitude = sqrt(magnitudeSquared);
temp = magnitude;// / 120.0;
Serial.println((int)temp);
if(temp < max)
{
max = temp;
max_frq = frequency;
}
ResetGoertzel();
}
void setup() {
Serial.begin(115200);
}
void loop() {
double freq;
InitGoertzel();
for (freq = TARGET_FREQUENCY - 200; freq <= TARGET_FREQUENCY + 200; freq = freq + 1)
{
GenerateAndTest(freq);
}
//sin_wave_with_changing_freq(TARGET_FREQUENCY);
//for(int i = 0; i<300; i++)
// Serial.println(testData[i]);
//Serial.println("Max freq at: ");
//Serial.print(max_frq);
while (1);
}