Adafruit Industries, Essential service and business: NYC – Executive Order 202.6 - Read more.
0

Frequency Analysis/Saving Audio to SD card from Mic
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Frequency Analysis/Saving Audio to SD card from Mic

by dai3504 on Wed Apr 03, 2013 3:48 pm

Hello, I am currently doing a project where I would like to analyze the frequency of the sound of a chain dragging across concrete.

I have this component: http://www.freetronics.com/products/microphone-sound-input-module#.UVxXmKKceSo
Image

connected to an Arduino Duemilanove w/ ATMega 328.

It says it can output Sound Pressure Level (which I am assuming is loudness [dB]) and the raw audio waveform.

When I run an analog read on the MTC output in DEC with this code:
Code: Select all | TOGGLE FULL SIZE
const int micSensor = A0;   // the SPL output is connected to analog pin 0

void setup() {
 Serial.begin(38400);
}

void loop() {
  Serial.println(analogRead(micSensor), DEC);
  delay(10);  // delay to avoid overloading the serial port buffer
}

I am getting numbers such as this:
Code: Select all | TOGGLE FULL SIZE
331
379
369
351
359
347
352
370
319
401
324
343
409
378
351
340
339
384
350
336
393
331
348
360
313
348
334
360
368
317
385
343
320
386
322
374
330
319
344
317
334
360
310
381
342
322
363
313
334
346
317
356
313
312
390
318
347
320
344
359
333
311
382
293
327
345


I wondering if anyone can guide me in trying to save the output of this device to an audio file to an SD card so I can perform further analysis with a program called Audacity (http://audacity.sourceforge.net/). Or if you know of any methods/libraries to get the output in the serial monitor in Hz.

I already have an SD Card module that I have interfaced with the arduino and have successfully been able to read/write to a text file.

Thanks! Please ask if you need clarification.
dai3504
 
Posts: 2
Joined: Wed Apr 03, 2013 3:46 pm

Re: Frequency Analysis/Saving Audio to SD card from Mic

by adafruit_support_rick on Wed Apr 03, 2013 5:16 pm

What you need to do is to understand what all those numbers are, and then you need to understand what kind of numbers you need in order to do your analysis. They are not at all the same kinds of numbers.

What you have are raw analog samples from the microphone. The microphone is going to produce a specific voltage for a specific sound pressure. The analogRead function samples the current voltage level coming from the mic, and converts it to a number between 0 and 1023. With your 10ms delay, you are taking roughly 100 samples per second.

Imagine you are on a beach, standing in water up to your ankles. As a wave goes by, the water level will briefly rise to your knees, and then drop back down to your ankles as the wave passes. Now, imagine you have a ruler taped to your leg. Every second, you write down the current height of the water as measured by the ruler. This is precisely the same thing that your sketch is doing with sound waves.

Now, suppose that the waves are coming along exactly once per second, and you're also taking your measurements exactly once per second. You could easily be in a situation in which you are always taking your measurements exactly between waves. Your notebook entries then would be entirely ankle, ankle, ankle..., and you would never have an entry of 'knee'. Anyone analyzing your data would conclude that there were no waves at the beach that day.

There is a concept called the "Nyquist frequency", which essentially says that, in order to successfully sample any kind of periodic phenomenon, such as waves at a beach or an audio frequency, you have to sample at least twice as fast as the period you're trying to measure. At the beach, you would have to take your measurements twice per second, so that you could guarantee that you would see the top and bottom of each wave (extra credit: think about a situation where even two samples per second would fail to record any wave activity).

To put it another way, at 100 samples per second the highest frequency you can possibly measure will be 50Hz (50 cycles per second). Ordinary speech as heard through a telephone receiver ranges up to around 4000Hz, meaning that you need at least 8000 samples per second to get the quality of a telephone call.

Your rattling chain will likely produce frequencies well into the 20,000Hz range, meaning that at a minimum you would need on the order of 40,000 samples per second to record them.

Maybe you don't need quite that level of fidelity for your project, or maybe you do. I don't know.

For comparison, an ordinary stereo music CD contains 44,100 samples per second each for the left and right channels.

Your samples are 10-bit, meaning they range from 0..1023. A music CD's samples are 16-bit, meaning they range from 0..65535. So, a music CD has 64 times the measurement precision of your microphone samples. This makes a big difference in the accuracy of the sound recording. Going back to the beach example, it's the difference between measuring waves at a resolution of 'ankle' and 'knee', or measuring waves at a resolution of 5mm.

Next, you need to understand that these samples, whether they are 10-bit or 16-bit, 100Hz or 44,100Hz, say absolutely nothing about frequency. To get frequency, you have to run your samples through a mathematical function known as a Fourier Transform. The output of a Fourier Transform will be a graph, with the X axis indicating frequency, and the Y-axis indicating how much of the total wave energy is accounted for by each frequency.

If you had a tuning fork at middle 'C', and you sampled its vibrations for 1 second with your microphone at, say, 4000 samples per second, you would get a set of 4000 numbers going up and down evenly somewhere in between 0 and 1023. If you then ran that sample through a Fourier transform, you would see a mostly horizontal line on your graph, except for a very large peak around 512 Hz (middle 'C').

With 10-bit samples at 4000 samples/second, your peak will be kind of wide and mushy-looking. As you go to a higher number of samples per second, and a higher bit-depth, your peak will become narrower and sharper.

I suggest you do some googling to research sound sampling and analysis a little more deeply before you go on with this project.

adafruit_support_rick
 
Posts: 35095
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: Frequency Analysis/Saving Audio to SD card from Mic

by pburgess on Wed Apr 03, 2013 5:35 pm

The WaveRP library can record audio (8-bit mono) to an SD card:

http://code.google.com/p/waverp/

Determining frequency may be tricky as many sounds are a composite of multiple frequencies. But if you know your test case involves single frequencies, there are a few approaches you can use like watching the peak-to-peak or trough-to-trough intervals, or zero crossings.

pburgess
 
Posts: 4049
Joined: Sun Oct 26, 2008 2:29 am

Re: Frequency Analysis/Saving Audio to SD card from Mic

by arctic_eddie on Wed Apr 03, 2013 6:11 pm

Here's a link to two FFT libraries for the Arduino. Use the one in the middle of the group for V1.0. This FFT uses assembly language to do the transform in integer mode. It covers the range from 75Hz to 4800Hz and uses A0 for input. The output is the magnitude of the spectral components.

http://code.google.com/p/neuroelec/downloads/list

arctic_eddie
 
Posts: 232
Joined: Tue Feb 28, 2012 6:01 pm
Location: St. Petersburg, FL

Re: Frequency Analysis/Saving Audio to SD card from Mic

by dai3504 on Wed Apr 10, 2013 2:47 pm

Arctic_Eddie wrote:Here's a link to two FFT libraries for the Arduino. Use the one in the middle of the group for V1.0. This FFT uses assembly language to do the transform in integer mode. It covers the range from 75Hz to 4800Hz and uses A0 for input. The output is the magnitude of the spectral components.

http://code.google.com/p/neuroelec/downloads/list


Thank you so much, this took me one step further in my project. I have been experimenting with reading and writing values to a .txt file with an SD card reader module I ordered from amazon (http://www.amazon.com/gp/offer-listing/B008B52QQC/ref=sr_1_1_olp?ie=UTF8&qid=1365618949&sr=8-1&keywords=arduino+sd+card&condition=new) and have been successful so far.

Does anyone know if there is a way to save the Fourier Transformed Values so that I can visualize them and interpret the frequencies at a later time?

Here is a snapshot of the Processing Graph Output:
Image
dai3504
 
Posts: 2
Joined: Wed Apr 03, 2013 3:46 pm

Re: Frequency Analysis/Saving Audio to SD card from Mic

by Ineedfrequency on Wed Feb 17, 2016 8:28 pm

dai3504 wrote:
Arctic_Eddie wrote:Here's a link to two FFT libraries for the Arduino. Use the one in the middle of the group for V1.0. This FFT uses assembly language to do the transform in integer mode. It covers the range from 75Hz to 4800Hz and uses A0 for input. The output is the magnitude of the spectral components.

http://code.google.com/p/neuroelec/downloads/list


Thank you so much, this took me one step further in my project. I have been experimenting with reading and writing values to a .txt file with an SD card reader module I ordered from amazon (http://www.amazon.com/gp/offer-listing/B008B52QQC/ref=sr_1_1_olp?ie=UTF8&qid=1365618949&sr=8-1&keywords=arduino+sd+card&condition=new) and have been successful so far.

Does anyone know if there is a way to save the Fourier Transformed Values so that I can visualize them and interpret the frequencies at a later time?

Here is a snapshot of the Processing Graph Output:
Image


Hi, I am trying to do something similar to what you did. I have a sound sensor that gives me analog values but need to use FFT to convert it to frequency. I am pretty new to Arduino, do you mind posting your Arduino code?

Ineedfrequency
 
Posts: 2
Joined: Wed Feb 17, 2016 8:26 pm

Re: Frequency Analysis/Saving Audio to SD card from Mic

by adafruit_support_rick on Thu Feb 18, 2016 12:01 pm


adafruit_support_rick
 
Posts: 35095
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: Frequency Analysis/Saving Audio to SD card from Mic

by Ineedfrequency on Thu Feb 18, 2016 12:59 pm

I have but it is meant for the Teensyduino. I understand you can adjust the code for Arduino but I am very bad at coding for it and am on a deadline. I am using the adafruit microphone + amp and an arduino uno and need to detect 3.3kHz frequency. I understand you can do that with these hardware components and using FFT but don't know exactly how. Was wondering if there is a open source code available that has this done already?

Ineedfrequency
 
Posts: 2
Joined: Wed Feb 17, 2016 8:26 pm

Re: Frequency Analysis/Saving Audio to SD card from Mic

by adafruit_support_rick on Thu Feb 18, 2016 2:13 pm

The Piccolo runs on Arduino. The code is there.

adafruit_support_rick
 
Posts: 35095
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Please be positive and constructive with your questions and comments.