Pages: 1 [2] 3   Go Down
Author Topic: Please help with arduino fft library  (Read 4166 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

It makes sense.
Can you look at my test code to see if I coded it correctly? I think the sine wave I used has 256 samples = #bins. And the frequency is set at 100hz.
I should get some numbers in bin#100, but I got zero in it, and a lot of other numbers in most of the bins.

Here is the output:
bin #: 0   log mag: 32
bin #: 1   log mag: 43
bin #: 2   log mag: 39
bin #: 3   log mag: 43
bin #: 4   log mag: 35
bin #: 5   log mag: 39
bin #: 6   log mag: 39
bin #: 7   log mag: 39
bin #: 8   log mag: 30
......

bin #: 84   log mag: 27
bin #: 85   log mag: 8
bin #: 86   log mag: 19
bin #: 87   log mag: 19
bin #: 88   log mag: 0
bin #: 89   log mag: 19
bin #: 90   log mag: 0
bin #: 91   log mag: 8
bin #: 92   log mag: 19
bin #: 93   log mag: 0
bin #: 94   log mag: 8
bin #: 95   log mag: 8
bin #: 96   log mag: 8
bin #: 97   log mag: 8
bin #: 98   log mag: 8
bin #: 99   log mag: 8
bin #: 100   log mag: 0
bin #: 101   log mag: 8
bin #: 102   log mag: 8
bin #: 103   log mag: 0
bin #: 104   log mag: 0
bin #: 105   log mag: 0
bin #: 106   log mag: 0
bin #: 107   log mag: 8
« Last Edit: July 04, 2013, 05:29:28 pm by hongp » Logged

Offline Offline
Edison Member
*
Karma: 31
Posts: 1417
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't think your test code is generating a useful sine wave. Have a look at the code I reference in this thread:
http://forum.arduino.cc/index.php?topic=96562.0

Pete
Logged

Where are the Nick Gammons of yesteryear?

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 179
Posts: 8064
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you look at my test code to see if I coded it correctly? I think the sine wave I used has 256 samples = #bins. And the frequency is set at 100hz.
I should get some numbers in bin#100, but I got zero in it, and a lot of other numbers in most of the bins.

What is the (simulated) sample rate in Hz?  In order to get 100 Hz to be in Bin 100 you have to have sampleRate == sampleCount.

100 * sampleRate (Hz) / sampleCount = 100 (Hz)

Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Montreal
Online Online
Edison Member
*
Karma: 23
Posts: 2486
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'm using arduino UNO.
You hardly can get FFT-256 on UNO, check on memory specification. 512 only with 8-bits word size.

 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;
Free running arduno samples ~9 kHz, upper limit 4.5 kHz, you have to use your own sampling subroutine with tweaking ADC registers.

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.
You can't get better than 4 Hz resolution, too pure for instrument tunning.

You 'd be better with http://interface.khm.de/index.php/lab/experiments/frequency-measurement-library/
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'm using arduino UNO.
You hardly can get FFT-256 on UNO, check on memory specification. 512 only with 8-bits word size.

 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;
Free running arduno samples ~9 kHz, upper limit 4.5 kHz, you have to use your own sampling subroutine with tweaking ADC registers.

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.
You can't get better than 4 Hz resolution, too pure for instrument tunning.

You 'd be better with http://interface.khm.de/index.php/lab/experiments/frequency-measurement-library/
I have access to other arduinos. Most of them. Can you suggest one that has the juice to get the job done?
I saw a tutorial before, that a girl was able to set the sampling rate to 38.5kHz. I was hoping that's the rate I will use.
When you say the resolution is too pure for instrument tunning, is that good or bad?  4Hz difference sounds very reasonable.
How did you find out free running sampling frequency is 9 kHz for UNO? Can you post a link here, please? I want to know where or how to find those infos. Also, I looked at arduino reference page, UNO has 16 MHz ceramic resonator. Why does it only sample at a max rate of 9kHz? Can you teach me how to figure it out?
Thanks
« Last Edit: July 04, 2013, 07:45:24 pm by hongp » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


What is the (simulated) sample rate in Hz?  In order to get 100 Hz to be in Bin 100 you have to have sampleRate == sampleCount.

100 * sampleRate (Hz) / sampleCount = 100 (Hz)



This is the code I used to generate sine wave data:
Code:
  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
  }
I have 256 samples = sampleCount; I have no idea what the simulated sampling rate is. How can I find out? I thought all I need was to filled up the array with numbers. Please help!
     If I change " k = 127*sin(2*3.14 * 100 * t ) " to "k = 127*sin(2*3.14 * 100 * 256 * t )" would that make a difference?
     I thought I understood a little about this stuff. Now I'm really confused.
    Thank you
Logged

Montreal
Online Online
Edison Member
*
Karma: 23
Posts: 2486
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Can you suggest one that has the juice to get the job done?
More memory, Mega - 8K 'd make 4x difference in resolution, compare to UNO.
I saw a tutorial before, that a girl was able to set the sampling rate to 38.5kHz. I was hoping that's the rate I will use.
Rate is not an obstacle, memory is.
When you say the resolution is too pure for instrument tunning, is that good or bad?  4Hz difference sounds very reasonable.
Do your research, I think it's about 1%, 100Hz note 'd requre 1Hz
http://en.wikipedia.org/wiki/Piano_key_frequencies
http://coolarduino.wordpress.com/2011/09/03/arduino-musical-note-recognition-pushing-the-limits/
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't think your test code is generating a useful sine wave. Have a look at the code I reference in this thread:
http://forum.arduino.cc/index.php?topic=96562.0

Pete
Hey, Pete, I posted a question back in your original post. Please take a look at it. Thanks
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Can you suggest one that has the juice to get the job done?
More memory, Mega - 8K 'd make 4x difference in resolution, compare to UNO.
I saw a tutorial before, that a girl was able to set the sampling rate to 38.5kHz. I was hoping that's the rate I will use.
Rate is not an obstacle, memory is.
When you say the resolution is too pure for instrument tunning, is that good or bad?  4Hz difference sounds very reasonable.
Do your research, I think it's about 1%, 100Hz note 'd requre 1Hz
http://en.wikipedia.org/wiki/Piano_key_frequencies
http://coolarduino.wordpress.com/2011/09/03/arduino-musical-note-recognition-pushing-the-limits/


Thank you for the help! I'm reading the links you posted here.
How did you find out free running sampling frequency is 9 kHz for UNO? Can you post a link here, please? I want to know where or how to find those infos. Also, I looked at arduino reference page, UNO has 16 MHz ceramic resonator. Why does it only sample at a max rate of 9kHz? Can you teach me how to figure it out?
Why would you think sampling rate is not a problem? Even if I used UNO, it still isn't a problem or you meant that if I use mega, It won't be a problem anymore? It looks like mega has the same cpu speed as UNO, it does have a lot bigger memory. You think the memory is a problem, is it because I need to get better frequency resolutions?
thanks
« Last Edit: July 04, 2013, 07:57:52 pm by hongp » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 179
Posts: 8064
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

From the ATmega data sheet:

"By default, the successive approximation circuitry requires an input clock frequency between 50 kHz and 200 kHz to get maximum resolution. If a lower resolution than 10 bits is needed, the input clock frequency to the ADC can be higher than 200 kHz to get a higher sample rate."

"To get full resolution you need to run the ADC at 15,000 samples per second or slower."

"You can get 76,900 samples per second at reduced resolution."

In free-running mode it takes 13.5 cycles to per sample.  The pre-scaler options are:

2 = 8 MHz = 592,592 samples per second (too fast)
4 = 4 MHz = 296,296 samples per second (too fast)
8 = 2 MHz = 148,148 samples per second (too fast)
16 = 1 MHz = 74,074 samples per second (low resolution)
32 = 500 KHz = 37,037 samples per second (reduced resolution)
64 = 250 KHz = 18,518 samples per second (reduced resolution)
128 = 125 KHz = 9,259 samples per second (full resolution)

Looks like your loose a couple of digits of accuracy if you run at 1 MHz (74,074 samples per second)
Absolute accuracy, ADC clock = 200 kHz: 2 LSB
Absolute accuracy, ADC clock = 1 MHz: 4.5 LSB
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Can you suggest one that has the juice to get the job done?
More memory, Mega - 8K 'd make 4x difference in resolution, compare to UNO.
I saw a tutorial before, that a girl was able to set the sampling rate to 38.5kHz. I was hoping that's the rate I will use.
Rate is not an obstacle, memory is.
When you say the resolution is too pure for instrument tunning, is that good or bad?  4Hz difference sounds very reasonable.
Do your research, I think it's about 1%, 100Hz note 'd requre 1Hz
http://en.wikipedia.org/wiki/Piano_key_frequencies
http://coolarduino.wordpress.com/2011/09/03/arduino-musical-note-recognition-pushing-the-limits/


I see your point. accuracy is very important for piano tuning, maybe for all the musical tuning. From the wiki page, piano only produces frequencies from 27.5hz up 4186hz, which means I need to have at lease 8372hz sampling  rate to do this type of tuning. I read that sometime it requires a little bit higher than twice of highest frequency to get the optimum result somewhere. But that's a lot lower than what I intended to set at.
According to john from reply #24, if I set pre-scaler = 128, I can have a sample rate of  9259 hz and full-resolution. The frequency is enough for piano tuning, right? but for resoolution, I hope full-resolution is at least 10bits, do I still need larger memory?

Thank you.
« Last Edit: July 04, 2013, 10:26:49 pm by hongp » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

From the ATmega data sheet:

"By default, the successive approximation circuitry requires an input clock frequency between 50 kHz and 200 kHz to get maximum resolution. If a lower resolution than 10 bits is needed, the input clock frequency to the ADC can be higher than 200 kHz to get a higher sample rate."

"To get full resolution you need to run the ADC at 15,000 samples per second or slower."

"You can get 76,900 samples per second at reduced resolution."

In free-running mode it takes 13.5 cycles to per sample.  The pre-scaler options are:

2 = 8 MHz = 592,592 samples per second (too fast)
4 = 4 MHz = 296,296 samples per second (too fast)
8 = 2 MHz = 148,148 samples per second (too fast)
16 = 1 MHz = 74,074 samples per second (low resolution)
32 = 500 KHz = 37,037 samples per second (reduced resolution)
64 = 250 KHz = 18,518 samples per second (reduced resolution)
128 = 125 KHz = 9,259 samples per second (full resolution)

Looks like your loose a couple of digits of accuracy if you run at 1 MHz (74,074 samples per second)
Absolute accuracy, ADC clock = 200 kHz: 2 LSB
Absolute accuracy, ADC clock = 1 MHz: 4.5 LSB
Hey, john, thank you for the great explanation.
I found out that I don't need to go all the way up to 20kHz for musical tuning. For piano, I need to have at least 8372 hz sampling rate. According to your calculation, I can use pre-scaler 128 and get the full resolution. Magician from reply #18 pointed that out! which is a great news for me.

Could you also help me out on set the simulation sampling frequency rate, please?

"I have 256 samples = sampleCount; I have no idea what the simulated sampling rate is. How can I find out? I thought all I need was to filled up the array with numbers. Please help!
     If I change " k = 127*sin(2*3.14 * 100 * t ) " to "k = 127*sin(2*3.14 * 100 * 256 * t )" would that make a difference?
     I thought I understood a little about this stuff. Now I'm really confused."

Thank you
Logged

Montreal
Online Online
Edison Member
*
Karma: 23
Posts: 2486
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How did you find out free running sampling frequency is 9 kHz for UNO? Can you post a link here, please? I want to know where or how to find those infos. Also, I looked at arduino reference page, UNO has 16 MHz ceramic resonator. Why does it only sample at a max rate of 9kHz? Can you teach me how to figure it out?
Download datasheet AtMega328 from atmel.com look into ADC section, you will find all information

Why would you think sampling rate is not a problem? Even if I used UNO, it still isn't a problem or you meant that if I use mega, It won't be a problem anymore? It looks like mega has the same cpu speed as UNO, it does have a lot bigger memory. You think the memory is a problem, is it because I need to get better frequency resolutions?
Exactly, don't confuse ADC resolution (which is least of your worry, as low as 8-bit o'k), and FFT resolution, that defined as Freq. range / Bin numbers. Having 4 kHz sampling and / 512-fft = 8 Hz. Paradox is  , you need to lower sampling rate, to get better resolution! (I mention this in musical note recognition post). Other parameter, increasing fft-size, isn't an option , except you have Due board.

Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 179
Posts: 8064
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The wikipedia article shows note frequencies with six significant digits.  In the low notes that's a resolution of 0.0001 Hz.  To get that resolution at 9,259 samples per second you would need an FFT size of 92.5 million samples (185 megabytes). That is not going to happen on an Arduino in any reasonable amount of time.

Perhaps you can use zero crossings to measure the frequency.  Have you looked at any frequency counter sketches?

http://interface.khm.de/index.php/lab/experiments/arduino-frequency-counter-library/
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How did you find out free running sampling frequency is 9 kHz for UNO? Can you post a link here, please? I want to know where or how to find those infos. Also, I looked at arduino reference page, UNO has 16 MHz ceramic resonator. Why does it only sample at a max rate of 9kHz? Can you teach me how to figure it out?

Download datasheet AtMega328 from atmel.com look into ADC section, you will find all information


This is what I found in the datasheet:
"Using the ADC Interrupt Flag as a trigger source makes the ADC start a new conversion as soon
as the ongoing conversion has finished. The ADC then operates in Free Running mode, constantly
sampling and updating the ADC Data Register."
It doesn't talk about how fast the free running mode is. Did you use some conversion factor? I'm completely new to micro-controllers. Look like I have a lot reading to do. Can you suggest a good place to start?



Why would you think sampling rate is not a problem? Even if I used UNO, it still isn't a problem or you meant that if I use mega, It won't be a problem anymore? It looks like mega has the same cpu speed as UNO, it does have a lot bigger memory. You think the memory is a problem, is it because I need to get better frequency resolutions?

Exactly, don't confuse ADC resolution (which is least of your worry, as low as 8-bit o'k), and FFT resolution, that defined as Freq. range / Bin numbers. Having 4 kHz sampling and / 512-fft = 8 Hz. Paradox is  , you need to lower sampling rate, to get better resolution! (I mention this in musical note recognition post). Other parameter, increasing fft-size, isn't an option , except you have Due board.


I I think arduino FFT library only provide max # of bin of 256 to filled in the real data. It doesn't go up to 512. I was definitely confused by ADC resolution and FFT resolution. Thank you for clear that out. Amanda from instructable.com was able to turn down ADC resolution to 8 bit in order to speed up the sampling rate, but you're saying to lower it down to get better resolution, and I can clearly see your point. If I were to lower it down, how would I be able to cover all the frequency range? from 27hz to 4186hz.
I read your blog about pushing arduino to the limit. That was a very impressive project you made! A few questions after reading your blog:
1. How could you have 1024 bins for the FFT? The one comes with arduino IDE is limited to 512 bins as the max. Half of the bins are filled with the data taken from analog input, the other half is set to zero manually. Do you use different library?
2.The shadow masking technique you used, is it setting a range for the detected frequencies to fall in, and then light up corresponding LED even though the resolution limited to 7.8hz?
3. If you already pushed arduino mega to the limit in that project, does it mean I have to either get a Due board or call off my project, or do something similar to yours just to understand how micro-controllers works? What if I use multiple mega boards? having each of them detecting different frequency ranges?
4.From arduino FFT documentation, it suggests to set imaginary part to zero if the samples are taken from analog input. Is that what you did?

I know it's a lot of questions, but I'm really into micro-controller now, especially this topic. Please bear with me if I ask something that is too trivial.
Thank you
 

« Last Edit: July 05, 2013, 05:46:17 pm by hongp » Logged

Pages: 1 [2] 3   Go Up
Jump to: