Showing posts with label goertzel. Show all posts
Showing posts with label goertzel. Show all posts

Friday, 12 February 2016

Arduino Signal Experiment #3-B: Goertzel using Arduino

On my last blog post, I discussed the library that I will be using on this blog post. We will be using the library that I forked and modified. Same as before, we will be using a PC speaker output going straight to an Arduino Mega. This is the arduino code that I used http://codepad.org/ezlp9tKT .

On Audacity, I generated two sine waves - 425Hz and 700Hz.


On Audacity, we can mute the channels to be  able to verify if the Goertzel algorithm works.  

Experiment 1 - Both Channels muted
Magnitude(425Hz): 15
Magnitude(700Hz): 11 

Experiment 2 - 700Hz Active / 425Hz Muted
Magnitude(425Hz) 234
Magnitude(700Hz) 5984 

Experiment 3 - 700Hz Muted / 425Hz Active
Magnitude(425Hz) 6299
Magnitude(700Hz) 152 

Experiment 4 - 700Hz and 425Hz Active
Magnitude(425Hz) 4328
Magnitude(700Hz) 4243

If I adjust the master volume of my laptop, the Goertzel algorithm reports the change of magnitude. To conclude, we can see that the Goertzel Algorithm can detect and distinguish the target frequencies.

Special Thanks to jacobrosenthal.

Arduino Signal Experiment #3-A: Goertzel using Arduino

This is the third series of the Arduino Signal Experiment. Now we will be dwelving with Goertzel Algorithm. Before we go to the actual experiment (which I will be posting on the next blog), I will discuss the Goertzel library that we will be using this time. I searched for an Goertzel Library and I found this https://github.com/jacobrosenthal/Goertzel and from my initial experiment, this pretty worked well, however, I faced some hurdles when doing multiple tone detection. I therefore checked and forked the source code. After that, I did some modifications:
  • I added a ChangeParameter to be able to dynamically change the parameters, thus no need to re-sample again. This contains the code  that was initially on the constructor.
  • All the global variables on the cpp files were pushed to the header files, making them class level variables. This way, if I choose to make two instances of Goertzel, then those variables will be instantiated. But since I already used a ChangeParameter, this second option may be rarely used.
 You can get the forked source code here: https://github.com/fwswdev/Goertzel

Arduino Signal Experiment #1 Sine Wave Input to Arduino using Laptop Speaker

Most of us who would like to do actual experiments for FFT or Goertzel algorithms using Arduino will need a simple signal generator to be fed into the analog pins of Arduino. We could buy some cheap signal generators from Ebay or Aliexpress, but there is a cheaper alternative that is already in front of you.... your PC or Laptop's Speaker output. In this blog post, I will show a step-by-step guide on how to use this.

Our PC or Laptop's speaker output has a DC offset of 0V(due to AC coupling)  as seen on majority of audio designs. This means that the sine wave can reach down to negative voltage which is not an ideal input to Arduino since it will not be able to measure the negative voltages. To resolve this, the sine wave must have a DC offset to be able to read the positive and negative peak of the sine wave. Here is a simple circuit to do that:


On the simulation, we can see that the input V_speakerInput was offset to Vcc/2. On the actual circuit, the R4 must be removed since it was only placed here to act as a dummy load (some simulators have trouble simulating circuit without load). The R1 and R2 can range from 1K to 3K. The R3 can range from 100 to 470 ohms. Here is the actual circuit:


I am using the great Arduino Mega 2560 and I soldered the surface mount resistor and capacitor on a separate bare PCB board. As you notice, there is only 1 wire from the TRRS audio jack that I used. This is because I omitted the ground wire to avoid ground loop. I will just simply use the USB's ground as the main ground source of the circuit.

Next part is the software to generate the sine wave ----->  the great open source Audacity. This is a beautiful piece of software which I use most of the time for music recording/composition. I never expected  to use this someday on my engineering work as well. :) Going to back to our project, once you run Audacity, go to Generate menu > Tone  and you can see that:
  • You can generate Sine/Sawtooth/Squarewave
  • You can set the amplitude and time duration
  • You can even produce and mix different tones and play them all together!

For the Arduino, here is the code that I am using currently using to verify if Arduino can read the sine wave (with DC offset) http://codepad.org/infZ2OLV

To prove that it works, I generated a sine wave tone on Audacity at 400Hz. I captured it using Arduino and graphed it:


Voila! A sine wave with DC offset. :)