Showing posts with label FFT. Show all posts
Showing posts with label FFT. Show all posts

Friday, 12 February 2016

Arduino Signal Experiment #2 FFT Analysis using Arduino

From the previous blog, we were able to see signals in the  time domain. Now, we will try to analyze things in frequency domain. I will be using this FFT Library  http://wiki.openmusiclabs.com/wiki/ArduinoFFT  . To start with, I created three different sine waves on Audacity (I will be sticking to sine waves to avoid the unwanted harmonics of a square wave):


The three different sine waves are: 200Hz, 625Hz and 850Hz. I also opened the built-in Frequency Analyzer of Audacity for us to have an idea of what those 3 frequencies are.

This is the code that I wrote for Arduino  http://codepad.org/UnFPb87k

On the code, notice that I used 450usec on the delay. This value is ideal for the 3 target frequencies that I will generate from Audacity and this is the explanation: The FFT bin size is 256. The output array will be FFT bin size divided by 2 (256/2 = 128). In this case, my array dimension is from 0 to 127.
@ array 0,  index * (1/T) / binsize = 0 * (1/450usec) / 256 = 0 Hz
@ array 1,  1 * (1/450usec) / 256 = 8.680555555555555 Hz
@ array 2,  2 * (1/450usec) / 256 = 17.36111111111111 Hz
@ array 127,  127 * (1/450usec) / 256 = 1102.4305555555554 Hz

Which means, I will be measuring only up to 1102 Hz.

Now, after I played the tones and measured it using the Arduino program, I tried to plot the values using OpenOffice Calc:


We can clearly see that the FFT works as shown by the rise on the magnitude on 200Hz, 625Hz and 850Hz. :)

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. :)