Go Down

hongp

Jul 04, 2013, 09:43 pmLast Edit: Jul 05, 2013, 02:38 am by hongp Reason: 1
Hello, people,

I've been trying to figure out how to use arduino fft library provided by musical lab for the past two weeks.
I couldn't find any decent documentations on how to use this library. Maybe I should say I really couldn't understand the things in the fft_read_me.txt. I have no where to find working sample code to read. Please help me out.

Here is my problem:
After I upload the first fft sample code provided in arduino 1.0.5 example, it outputs some weird character in the serial monitor. I was expecting some numbers even if they're in dB scale. Do you guys have any ideas what is going here? For all the projects that I looked over, it seemed the output was hooked up to processing to be visualized in graphs. What if I just want the frequency itself as a number? let's say I use a oscilloscope with 2.5DC offset and generate a sine wave with 100 Hz at a sampling frequency of 38.5kHz. How do I go about extracting this 100Hz from the output of the fft?
Here is what I found from the readings:
# of bins = # of samples;
use the first half bins because the second part is the mirror image of the first part;
frequency = (sampling frequency / # samples) * bin #
Output available from fft library: fft_lin_out8; fft_mag_lin; fft_mag_log. but they seemed to look like the magnitude of the waveform, not the frequency component which I'm interested in. For the testing purpose, all I wanted to do is to input a sine wave ranges from 10hz to 20khz, and I want to have those frequency outputed to serial port, or store it in a variable or array where I have access to it, I will use it to make a auto tuner.
Here is the code:
Code: [Select]
`/*/*fft_adc.pdeguest openmusiclabs.com 8.18.12example sketch for testing the fft library.it takes in data on ADC0 (Analog0) and processes themwith the fft. the data is sent out over the serialport at 115.2kb.  there is a pure data patch forvisualizing the data.*/#define LOG_OUT 1 // use the log output function#define FFT_N 256 // set to 256 point fft#include <FFT.h> // include the libraryvoid setup() {  Serial.begin(115200); // use the serial port  TIMSK0 = 0; // turn off timer0 for lower jitter  ADCSRA = 0xe5; // set the adc to free running mode  ADMUX = 0x40; // use adc0  DIDR0 = 0x01; // turn off the digital input for adc0}void loop() {  while(1) { // reduces jitter    cli();  // UDRE interrupt slows this way down on arduino1.0    for (int i = 0 ; i < 512 ; i += 2) { // save 256 samples      while(!(ADCSRA & 0x10)); // wait for adc to be ready      ADCSRA = 0xf5; // restart adc      byte m = ADCL; // fetch adc data      byte j = ADCH;      int k = (j << 8) | m; // form into an int      k -= 0x0200; // form into a signed int      k <<= 6; // form into a 16b signed int      fft_input[i] = k; // put real data into even bins      fft_input[i+1] = 0; // set odd bins to 0    }    fft_window(); // window the data for better frequency response    fft_reorder(); // reorder the data before doing the fft    fft_run(); // process the data in the fft    fft_mag_log(); // take the output of the fft    sei();    Serial.write(255); // send a start byte    Serial.write(fft_log_out, 128); // send out the data  }}`

Thank you guys for the help in advance. If I can get this project to work, I will produce a full document and post it to here.

UPDATE 1:
To print out the number from fft_log_out, use a for loop and Serial.print() to print out each element in the array. Thank you all for the help. But I still have more questions on the second page!

AWOL

#1
Jul 04, 2013, 10:15 pm
Quote
it outputs some weird character in the serial monitor. I was expecting some numbers

I don't see where you set the baud rate, and anyway you're printing raw data with "write", not "print"
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

johnwasser

#2
Jul 04, 2013, 10:17 pm
The code is using Serial.write() to send binary bytes.  To make numbers human readable, use Serial.print().
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

hongp

#3
Jul 04, 2013, 10:23 pm

I don't see where you set the baud rate, and anyway you're printing raw data with "write", not "print"

I didn't copy the complete code. sorry about that. I copied and pasted it again.
The baud rate is 115200. I also tried 9600.

The code is from the example. I've tried to use serial.print.
but I always get this error:

E:\programming\arduino-1.0.2\hardware\arduino\cores\arduino/Print.h:59: note: candidates are: size_t Print::print(unsigned char, int) <near match>
E:\programming\arduino-1.0.2\hardware\arduino\cores\arduino/Print.h:60: note:                 size_t Print::print(int, int) <near match>
E:\programming\arduino-1.0.2\hardware\arduino\cores\arduino/Print.h:61: note:                 size_t Print::print(unsigned int, int) <near match>
E:\programming\arduino-1.0.2\hardware\arduino\cores\arduino/Print.h:62: note:                 size_t Print::print(long int, int) <near match>
E:\programming\arduino-1.0.2\hardware\arduino\cores\arduino/Print.h:63: note:                 size_t Print::print(long unsigned int, int) <near match>

AWOL

#4
Jul 04, 2013, 10:25 pm
It's unlikely that you'd want to print binary data as a C string, so print it a sample at a time, in a for loop.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

hongp

#5
Jul 04, 2013, 10:26 pm

The code is using Serial.write() to send binary bytes.  To make numbers human readable, use Serial.print().

Is it possible to process these binary bytes to get a useful frequency component out? Please point me to the right direction. I don't mind to read more code or concepts.

I tried to use serial.print.
but I always get the error:

E:\programming\arduino-1.0.2\hardware\arduino\cores\arduino/Print.h:59: note: candidates are: size_t Print::print(unsigned char, int) <near match>
E:\programming\arduino-1.0.2\hardware\arduino\cores\arduino/Print.h:60: note:                 size_t Print::print(int, int) <near match>
E:\programming\arduino-1.0.2\hardware\arduino\cores\arduino/Print.h:61: note:                 size_t Print::print(unsigned int, int) <near match>
E:\programming\arduino-1.0.2\hardware\arduino\cores\arduino/Print.h:62: note:                 size_t Print::print(long int, int) <near match>
E:\programming\arduino-1.0.2\hardware\arduino\cores\arduino/Print.h:63: note:                 size_t Print::print(long unsigned int, int) <near match>

AWOL

#6
Jul 04, 2013, 10:28 pm
There is no "print" overload that prints a byte buffer as numeric.
Use a for loop to print each sample.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

hongp

#7
Jul 04, 2013, 10:30 pm

It's unlikely that you'd want to print binary data as a C string, so print it a sample at a time, in a for loop.

Can you elaborate a little more please? I'm brand new to arduino, and not that much programming experience.
If I were to print one sample at time, will I be able to print out the frequency or just magnitude of the sample?

Thanks.

hongp

#8
Jul 04, 2013, 10:33 pm

There is no "print" overload that prints a byte buffer as numeric.
Use a for loop to print each sample.

Thank you all so much! I got some numbers out. Let me test it!

johnwasser

#9
Jul 04, 2013, 10:56 pm
The results of the FFT will be a list of amplitudes at a set of frequencies.  The frequency of each sample depends on the sample frequency.

For each 'bin' N the frequency is N * Sample_Rate / FFT_Size

Your sample rate is whatever the free-runing rate of the ADC is.  I don't remember offhand.  It should be in the datasheet.  Let's call it 70 KHz.

Bin 0 frequency = 0 * 70,000/256 = 0 (DC offset)
Bin 1 frequency = 1 * 70,000/256 = 273.43 Hz (this will also be the step size in your frequency)
Bin 2 frequency = 2 * 70,000/256 = 546.875
Bin 3 frequency = 3 * 70,000/256 = 820.3125
...
Bin 127 frequency = 127 * 70,000/256 = 34,726.56

The top frequency is half the sample rate.
The sample step size is the sample rate divided by the the sample size.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

hongp

#10
Jul 04, 2013, 10:58 pm
So I think I can print out the magnitude in dB. Not sure  if it is correct or not, but it feels really good to have some numbers coming out.

How do I go about extracting the frequency?
Just to learn FFT, I tried out kissFFT in C++ a couple days ago. I filled up an array with single sine function, and I passed the array to kissfft, then I output all the numbers from the first half of the bin. The array was set to have size the same as number of samples, so that the bin number = frequency for testing purpose. The output I had was all zero except the bin where the frequency of sine function is.

Can I do the same to fft library in arduino? I tried it. but a lot of the outputs aren't zero. Not sure if it's correct. Please point out any possible error.
Here is the code I used to test fft library:
Code: [Select]
`#define LOG_OUT 1 // use the log output function#define FFT_N 256 // set to 256 point fft#include <FFT.h> // include the libraryvoid setup() {  Serial.begin(9600); // use the serial port}void loop() {  // put your main code here, to run repeatedly:   int k = 0;   int t = 0;    for (int i = 0 ; i < 512 ; i += 2) { // save 256 samples      k = 127*sin(2*3.14 * 100 * t );      t++;      fft_input[i] = k; // put real data into even bins      fft_input[i+1] = 0; // set odd bins to 0    }    fft_window(); // window the data for better frequency response    fft_reorder(); // reorder the data before doing the fft    fft_run(); // process the data in the fft    fft_mag_log(); // take the output of the fft    for(int i = 0; i < 128; i++){          Serial.println(fft_log_out[i]);    }    delay(1000); }`

hongp

#11
Jul 04, 2013, 11:06 pm

The results of the FFT will be a list of amplitudes at a set of frequencies.  The frequency of each sample depends on the sample frequency.

For each 'bin' N the frequency is N * Sample_Rate / FFT_Size

Your sample rate is whatever the free-runing rate of the ADC is.  I don't remember offhand.  It should be in the datasheet.  Let's call it 70 KHz.

Bin 0 frequency = 0 * 70,000/256 = 0 (DC offset)
Bin 1 frequency = 1 * 70,000/256 = 273.43 Hz (this will also be the step size in your frequency)
Bin 2 frequency = 2 * 70,000/256 = 546.875
Bin 3 frequency = 3 * 70,000/256 = 820.3125
...
Bin 127 frequency = 127 * 70,000/256 = 34,726.56

The top frequency is half the sample rate.
The sample step size is the sample rate divided by the the sample size.

Great explanation! Thank you so much! that clears out a lot of confusion!
So I want to test out fft library by filling up the bins with numbers from one single sine function, but I get many bins with nonzero output. Am I supposed to get only one bin with non-zero number? Because that bin has to be the frequency of the sine function scaled in the way you explained here.
Here is the code I used to test:
Code: [Select]
`#define LOG_OUT 1 // use the log output function#define FFT_N 256 // set to 256 point fft#include <FFT.h> // include the libraryvoid setup() {  Serial.begin(9600); // use the serial port}void loop() {  // put your main code here, to run repeatedly:    int k = 0;   int t = 0;    for (int i = 0 ; i < 512 ; i += 2) { // save 256 samples      k = 127*sin(2*3.14 * 100 * t );      t++;      fft_input[i] = k; // put real data into even bins      fft_input[i+1] = 0; // set odd bins to 0    }    fft_window(); // window the data for better frequency response    fft_reorder(); // reorder the data before doing the fft    fft_run(); // process the data in the fft    fft_mag_log(); // take the output of the fft    for(int i = 0; i < 128; i++){          Serial.println(fft_log_out[i]);    }    delay(1000); }`

johnwasser

#12
Jul 04, 2013, 11:37 pm
If the frequency of the sine wave matches one of the bins then you should get a strong signal in that bin and little or no signal in the other bins.  If the frequency is between bins you should get signals in the two bins on either side.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Magician

#13
Jul 04, 2013, 11:44 pm
Quote
If the frequency of the sine wave matches one of the bins then you should get a strong signal in that bin and little or no signal in the other bins.  If the frequency is between bins you should get signals in the two bins on either side.

True, if "float" math involved, better 64-bits. For 8/16-bits integer microprocessor w/o floating coprocessor truncation error would get into scene, don't expect clean output.
To OP: What kind of arduino board do you have?
What you are trying to achieve, resolution, freq. range, nature of the input signal?

hongp

#14
Jul 05, 2013, 12:10 am

Quote
If the frequency of the sine wave matches one of the bins then you should get a strong signal in that bin and little or no signal in the other bins.  If the frequency is between bins you should get signals in the two bins on either side.

True, if "float" math involved, better 64-bits. For 8/16-bits integer microprocessor w/o floating coprocessor truncation error would get into scene, don't expect clean output.
To OP: What kind of arduino board do you have?
What you are trying to achieve, resolution, freq. range, nature of the input signal?

I'm using arduino UNO. Frequency range = human detectable range; input signal = instrument sound, but for testing purpose, it is oscilloscope with 2.5DC offset, 4Vpp, frequency ranges from 10hz to the limit when output frequency is not accurate anymore, hopefully close to 20khz;
I'm going to make a frequency auto-tuner. I've seen people make the guitar frequency tuner, mine is different, but the concept is the same.

Do you guys know what the free running sampling frequency is? I'm really new to arduino, did a few search. Didn't find the one correspond to UNO.
Also, if you have a link to a tutorial that explains what those parameters are, it would be great!

Go Up

Please enter a valid email to subscribe