From: Harvie Date: Tue, 31 Jul 2012 20:02:39 +0000 (+0200) Subject: Goertzel tone detection software X-Git-Url: https://git.harvie.cz/?a=commitdiff_plain;h=4b43521aa25cb694f319315c689cb749cb607e43;p=mirrors%2FPrograms.git Goertzel tone detection software --- diff --git a/c/goertzel/Makefile b/c/goertzel/Makefile new file mode 100644 index 0000000..6c16c5a --- /dev/null +++ b/c/goertzel/Makefile @@ -0,0 +1,2 @@ +goertzel: goertzel.c + gcc goertzel.c -o goertzel -lm diff --git a/c/goertzel/goertzel.c b/c/goertzel/goertzel.c new file mode 100644 index 0000000..bcf7a91 --- /dev/null +++ b/c/goertzel/goertzel.c @@ -0,0 +1,76 @@ +#include +#include + +/* + Usage examples + arecord | ./goertzel + sox input.mp3 -b 8 -c 1 -r 8000 -t wav - | ./goertzel +*/ + +float goertzel_mag(int numSamples,int TARGET_FREQUENCY,int SAMPLING_RATE, float* data) +{ + /* + On lower samplerates and frame sizes this may perform sub-optimally. Eg.: + When set to detect 440Hz (at 8000Hz samplerate and ~4000 samples) + it actually detects something around 438,3Hz rather than 400Hz... + If you can't increase samplerate way around this is just to increase sensitivity. + */ + + int k,i; + float floatnumSamples; + float omega,sine,cosine,coeff,q0,q1,q2,magnitude,real,imag; + + float scalingFactor = numSamples / 2.0; + + floatnumSamples = (float) numSamples; + k = (int) (0.5 + ((floatnumSamples * TARGET_FREQUENCY) / SAMPLING_RATE)); + omega = (2.0 * M_PI * k) / floatnumSamples; + sine = sin(omega); + cosine = cos(omega); + coeff = 2.0 * cosine; + q0=0; + q1=0; + q2=0; + + for(i=0; i