-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgoertzel.c
185 lines (144 loc) · 3.81 KB
/
goertzel.c
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
182
183
184
185
/* Goertzel Algorithm
* http://cms.edn.com/uploads/SourceCode/09banks.txt
*/
#define PI 3.141592653589793
#include <stdio.h>
#include <math.h>
#define FLOATING float
#define SAMPLE unsigned char
#define SAMPLING_RATE 8000.0 //8kHz
#define TARGET_FREQUENCY 941.0 //941 Hz
#define N 205 //Block size
FLOATING coeff;
FLOATING Q1;
FLOATING Q2;
FLOATING sine;
FLOATING cosine;
SAMPLE 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;
FLOATING floatN;
FLOATING omega;
floatN = (FLOATING) 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;
printf("For SAMPLING_RATE = %f", SAMPLING_RATE);
printf(" N = %d", N);
printf(" and FREQUENCY = %f,\n", TARGET_FREQUENCY);
printf("k = %d and coeff = %f\n\n", k, coeff);
ResetGoertzel();
}
/* Call this routine for every sample. */
void ProcessSample(SAMPLE sample)
{
FLOATING Q0;
Q0 = coeff * Q1 - Q2 + (FLOATING) sample;
Q2 = Q1;
Q1 = Q0;
}
/* Basic Goertzel */
/* Call this routine after every block to get the complex result. */
void GetRealImag(FLOATING *realPart, FLOATING *imagPart)
{
*realPart = (Q1 - Q2 * cosine);
*imagPart = (Q2 * sine);
}
/* Optimized Goertzel */
/* Call this after every block to get the RELATIVE magnitude squared. */
FLOATING GetMagnitudeSquared(void)
{
FLOATING result;
result = Q1 * Q1 + Q2 * Q2 - Q1 * Q2 * coeff;
return result;
}
/*** End of Goertzel-specific code, the remainder is test code. */
/* Synthesize some test data at a given frequency. */
void Generate(FLOATING frequency)
{
int index;
FLOATING step;
step = frequency * ((2.0 * PI) / SAMPLING_RATE);
/* Generate the test data */
for (index = 0; index < N; index++)
{
testData[index] = (SAMPLE) (100.0 * sin(index * step) + 100.0);
}
}
/* Demo 1 */
void GenerateAndTest(FLOATING frequency)
{
int index;
FLOATING magnitudeSquared;
FLOATING magnitude;
FLOATING real;
FLOATING imag;
printf("For test frequency %f:\n", frequency);
Generate(frequency);
/* Process the samples */
for (index = 0; index < N; index++)
{
ProcessSample(testData[index]);
}
/* Do the "basic Goertzel" processing. */
GetRealImag(&real, &imag);
printf("real = %f imag = %f\n", real, imag);
magnitudeSquared = real*real + imag*imag;
printf("Relative magnitude squared = %f\n", magnitudeSquared);
magnitude = sqrt(magnitudeSquared);
printf("Relative magnitude = %f\n", magnitude);
/* Do the "optimized Goertzel" processing */
magnitudeSquared = GetMagnitudeSquared();
printf("Relative magnitude squared = %f\n", magnitudeSquared);
magnitude = sqrt(magnitudeSquared);
printf("Relative magnitude = %f\n\n", magnitude);
ResetGoertzel();
}
/* Demo 2 */
void GenerateAndTest2(FLOATING frequency)
{
int index;
FLOATING magnitudeSquared;
FLOATING magnitude;
FLOATING real;
FLOATING imag;
printf("Freq=%7.1f ", frequency);
Generate(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;
printf("rel mag^2=%16.5f ", magnitudeSquared);
magnitude = sqrt(magnitudeSquared);
printf("rel mag=%12.5f\n", magnitude);
ResetGoertzel();
}
int main(void)
{
FLOATING freq;
InitGoertzel();
/* Demo 1 */
GenerateAndTest(TARGET_FREQUENCY - 250);
GenerateAndTest(TARGET_FREQUENCY);
GenerateAndTest(TARGET_FREQUENCY + 250);
/* Demo 2 */
for (freq = TARGET_FREQUENCY - 300; freq <= TARGET_FREQUENCY + 300; freq += 15)
{
GenerateAndTest2(freq);
}
return 0;
}