I tested it with a slightly modified version of the library which I've attached to this post. I've added code to toggle the output of digital pin 4 so that you can test it yourself. When you do the test, you should modify your code so that it does:
while(1)goertzel.sample(sensorPin); //Will take n samples
This will force it to loop in the sample routine all the time, otherwise you won't get an accurate frequency reading. Then whatever frequency you get on your frequency counter, you use double that value as the sampling frequency.
It is best to leave the counter code in there all the time because removing it will speed up the loop a bit which knocks the sampling frequency out of whack again.
Changes to the library:
- added code to initialize and toggle pin 4 so that a frequency counter can be used to determine the sampling rate
- the library was storing the sampled values as bytes - not integers!
- offset the sample by 512 so that the Goertzel algorithm then uses values in the range -512 to 511
Also I don't believe you can just change the sampling rate without changing "N".
Yes you can. The two values are independent. N determines the bandwidth of the detector. Larger N gives smaller bandwidth. For example with a sampling frequency of 9000 and N=45 the bandwidth will be 200Hz (9000/45). Change N to 90 and the bandwidth will be 100Hz.
NOTE that the maximum value of N allowed by the library is 160. If you exceed that value your sketch will just crash in mysterious ways.
Pete
Goertzel.cpp (3.29 KB)
Goertzel.h (1.29 KB)