Open nonchip opened 3 years ago
untested, but this seems to be a 1-function version of this algo achieving this:
#define ADC_MIDPOINT 512
#if F_CPU == 16000000L
#define SAMPLE_FREQ 8900
#else
#define SAMPLE_FREQ 4400
#endif
float goertzel(int samples[], int num_samples, float freq){
float Q1 = 0;
float Q2 = 0;
float coeff = 2.0 * cos((2.0 * PI * freq) / SAMPLE_FREQ);
for (int i = 0; i < num_samples; i++) {
float Q0 = coeff * Q1 - Q2 + (float) (samples[i] - ADC_MIDPOINT);
Q2 = Q1;
Q1 = Q0;
}
return sqrt(Q1*Q1 + Q2*Q2 - coeff*Q1*Q2);
}
since the constructor doesn't seem to have to prepare much except for do a single formula to get the
coeff
value, i don't see the need to attach a fixed target frequency to the instance.currently one would have to instantiate multiple objects and have them each reach the analog pin N times to e.g. distinguish between multiple frequencies, something that'll slow down the effective sample rate.