Trying to use a Goertzel library for a sketch to detect two frequencies, 50 and 55Hz although I don’t know if it can resolve them.
The Goertzel library was obtained from:
https://github.com/jacobrosenthal/Goertzel
When I use the example detect.ino from this site, I get “nan” for “magnitude”.
The sketch from different example code is…
/*
Released into the public domain.
*/
#include <Goertzel.h>
int sensorPin = 0;
int led = 13;
// ideally an integer of SAMPLING_FREQUENCY/N to center the bins around your content so if you're
// looking for 700hz, frequencies below and above it equally contribute. Read up on Kevin's article
// for more info.
// Nyquist says the highest frequency we can target is SAMPLING_FREQUENCY/2
const float TARGET_FREQUENCY = 700;
const float TARGET_FREQUENCY2 = 425;
// if you're trying to detect several different drum hits all within low frequency like
// ~100-200hz you'll need a small bin size like 25 or 50 to distinguish them.
// If however you're just trying to find ANY bass hit you might want something
// basically equal to frequency youre looking for like ~100
// If Im detecting a frequency much higher with no care about nearby tones, like 2000hz
// Ill set to a round divisor like 200 So 1900 to 2100 could trigger, but not less or more
// Max is 200 as we have limited ram in the Arduino, and sampling longer would make us less
// responsive anyway
const int N = 100;
// This is what will trigger the led. Its INCREDIBLY squishy based on volume of your source,
// frequency, etc. You'll just need to get in your environment and look at the serial console
// to start. Then pick something that triggers pleasantly to your eye.
const float THRESHOLD = 4000;
// Again, the highest frequency we can target is SAMPLING_FREQUENCY/2. So Since Arduino is
// relatively slow in terms of audio, we sample literally as fast as we can
// This is generally around ~8900hz for a 16mhz Arduino and 4400hz for an 8mhz Arduino.
// User nicola points out these rates are for stock arduino firmware and that on a board
// by board basis you can juice the adc rates. For Arduino Uno you could move that rate up to
// 22khz by adding somthing like this to your setup:
// _SFR_BYTE(ADCSRA) |= _BV(ADPS2); // Set ADPS2
// _SFR_BYTE(ADCSRA) &= ~_BV(ADPS1); // Clear ADPS1
// _SFR_BYTE(ADCSRA) &= ~_BV(ADPS0); // Clear ADPS0
const float SAMPLING_FREQUENCY = 8900;
Goertzel goertzel = Goertzel(TARGET_FREQUENCY, N, SAMPLING_FREQUENCY);
void setup(){
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop()
{
goertzel.sample(sensorPin);
goertzel.Goertzel(TARGET_FREQUENCY, N, SAMPLING_FREQUENCY);
float magnitude = goertzel.detect(); //check them for target_freq
goertzel.Goertzel(TARGET_FREQUENCY2, N, SAMPLING_FREQUENCY);
float magnitude2 = goertzel.detect(); //check them for target_freq
if(magnitude>THRESHOLD || magnitude2>THRESHOLD) //if you're getting false hits or no hits adjust this
digitalWrite(led, HIGH); //if found, enable led
else
digitalWrite(led, LOW); //if not found, or lost, disable led
static char buffer[256]; // make sure this is big enough!!!
snprintf(buffer, sizeof(buffer), "Magnitude(%dHz) %d\n", (u16)TARGET_FREQUENCY,(u16)magnitude);
Serial.print(buffer);
snprintf(buffer, sizeof(buffer), "Magnitude(%dHz) %d\n", (u16)TARGET_FREQUENCY2,(u16)magnitude2);
Serial.print(buffer);
delay(400);
}
When compiled (using the same Goertzel library) gives…
GoertzelMultiTone.ino: In function 'void loop()':
GoertzelMultiTone:54: error: invalid use of 'Goertzel::Goertzel'
GoertzelMultiTone:56: error: invalid use of 'Goertzel::Goertzel'
What does “invalid use of ‘Goertzel::Goertzel’” mean?
Also, is there another source of a Goertzel library and code examples? Am I naive in expecting that code examples should just work?