Skip to content

Instantly share code, notes, and snippets.

@carlosdelfino
Created July 31, 2014 03:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlosdelfino/81398b305bc792f77b01 to your computer and use it in GitHub Desktop.
Save carlosdelfino/81398b305bc792f77b01 to your computer and use it in GitHub Desktop.
analisando o uso do Algoritimo Goertzel, as anotações aqui presentes são para uso no artigo referente ao uso do Algortimo com Arduino. todos foram obtidos no link: http://netwerkt.wordpress.com/2011/08/25/goertzel-filter/
double goertzelFilter(int samples[], double freq, int N) {
double s_prev = 0.0;
double s_prev2 = 0.0;
double coeff,normalizedfreq,power,s;
int i;
normalizedfreq = freq / SAMPLEFREQUENCY;
coeff = 2*cos(2*M_PI*normalizedfreq);
for (i=0; i<N; i++) {
s = samples[i] + coeff * s_prev - s_prev2;
s_prev2 = s_prev;
s_prev = s;
}
power = s_prev2*s_prev2+s_prev*s_prev-coeff*s_prev*s_prev2;
return power;
}
double RTgoertzelFilter(int sample, double freq) {
static double s_prev = 0.0;
static double s_prev2 = 0.0;
static double totalpower = 0.0;
static int N = 0;
double coeff,normalizedfreq,power,s;
normalizedfreq = freq / SAMPLEFREQUENCY;
coeff = 2*cos(2*M_PI*normalizedfreq);
s = sample + coeff * s_prev - s_prev2;
s_prev2 = s_prev;
s_prev = s;
N++;
power = s_prev2*s_prev2+s_prev*s_prev-coeff*s_prev*s_prev2;
totalpower += sample*sample;
if (totalpower == 0) totalpower=1;
return power / totalpower / N;
}
double tandemRTgoertzelFilter(int sample, double freq) {
static double s_prev[2] = {0.0,0.0};
static double s_prev2[2] = {0.0,0.0};
static double totalpower[2] = {0.0,0.0};
static int N=0;
double coeff,normalizedfreq,power,s;
int active;
static int n[2] = {0,0};
normalizedfreq = freq / SAMPLEFREQUENCY;
coeff = 2*cos(2*M_PI*normalizedfreq);
s = sample + coeff * s_prev[0] - s_prev2[0];
s_prev2[0] = s_prev[0];
s_prev[0] = s;
n[0]++;
s = sample + coeff * s_prev[1] - s_prev2[1];
s_prev2[1] = s_prev[1];
s_prev[1] = s;
n[1]++;
N++;
active = (N / RESETSAMPLES) & 0x01;
if (n[1-active] >= RESETSAMPLES) { // reset inactive
s_prev[1-active] = 0.0;
s_prev2[1-active] = 0.0;
totalpower[1-active] = 0.0;
n[1-active]=0;
}
totalpower[0] += sample*sample;
totalpower[1] += sample*sample;
power = s_prev2[active]*s_prev2[active]+s_prev[active]
* s_prev[active]-coeff*s_prev[active]*s_prev2[active];
return power / (totalpower[active]+1e-7) / n[active];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment