Go Down

Topic: Finding frequency using a simple sound sensor such as sparkfun SEN-12642. Ideas? (Read 5619 times) previous topic - next topic

Ajith1212

Hi everyone. I am using arduino UNO. I am new to it and trying to figure out the circuit and codes for calculating frequency.

Grumpy_Mike

What sort of signal do you want to measure?
A sin wave is simple and will give you a simple answer.
Natural sounds are complex and are a mixture of frequencies that change over the duration of the sound. For that you need something called an FFT.


Ajith1212

Well, I want to find the frequency of "a note" while playing Harmonica.

Grumpy_Mike

Well, I want to find the frequency of "a note" while playing Harmonica.
Oh, not at all simple or indeed reliably possible.

Best bet is to use an FFT and then look at the results returned and try and work out the note. One approach is to look for the lowest large harmonic and assume it is that but it is not always.

DVDdoug

Like most instruments and most real-world sounds, when you play a note on a harmonica multiple harmonics & overtones are produced.    And as you probably know, you're normally playing a chord (more than one note at a time).    The "trick' is finding the fundamental in that complex sound.

The only good news is that a harmonica plays a limited set of notes (depending on the key & scale) so if you know what notes a particular harmonica can play that will minimize errors.

Wikipedia has some information about FFT, and there is an Arduino FFT library, but I've never used it.

Quote
such as sparkfun SEN-12642
Use the "audio output" into an Arduino analog-input.

Ajith1212

Thanks for your inputs. I have no previous experience with FFT, but I will learn and try figure something out.

pjrc

I want to find the frequency of "a note" while playing Harmonica.
You want the YIN algorithm, not FFT.  The bad news is Arduino Uno is far to slow to run the YIN algorithm in real time.  In fact, it uses most of the computation power of Teensy 3.2 running at 96 MHz, but does work quite well.

If you would upload a few high quality sound clips of your harmonica playing different notes (with permission to copy the sound file into the NoteFrequency example code) I would be happy to test them on a Teensy 3.2 and post the results here.

Ajith1212

Hey, I used a technique that requires a Mono Input. The source -

http://www.instructables.com/id/Arduino-Audio-Input/

http://www.instructables.com/id/Arduino-Frequency-Detection/.

I am currently using a function generator to give in the frequency. I will later try a Mono Mic and see if it works well and finds the frequency(fundamental) of different notes of Harmonica. But for now, it calculates the frequency well enough(from the function generator).

I am open to pieces of advice if you guys have any on how to proceed further. :smiley:  :smiley:

Grumpy_Mike

Did you do what Paul asked here https://forum.arduino.cc/index.php?topic=483201.msg3299296#msg3299296
What were the results?

Quote
I am currently using a function generator to give in the frequency.
As I said before that is simple, real sounds are much more complex.

Remember anything with instructables in the URL is 99% sure to be crap. That one is simplistic and won't help you. Unless you have an oscilloscope you are just groping in the dark.
Many people have tried to do this sort of thing before and not succeeded fully.

DVDdoug

Quote
I will later try a Mono Mic and see if it works well...
For experimental/development purposes, record the harmonica (with your laptop or phone, etc.)   Then you can connect the headphone-output in place of the function generator and play-back the recording to test your software before you add a mic or any more hardware.

A recording is actually "easier" (for the software) because the levels are adjustable & repeatable and the noise is consistent, etc.  But, but I'd work on the software before building the hardware.

pjrc

My offer is still open.  If you make a couple good quality recordings of your harmonica, and if you allow those sounds to be copied (eg, "open source" or "creative commons"), I will put them into the frequency analysis example code and share the results here.  That way you can see the results before buying more powerful hardware capable of running the YIN algorithm.

Johan_Ha

In this thread I explained a very simple and rudimentary way to determine the direction of a sound when recorded with two microphones. It was about comparing the left channel and the right channel signal and finding out how much they were time shifted from each other.

A similar simple approach can be used to find out the frequency of a tone in one single monophonic recording. It's kind of the YIN algorithm very much simplified, or a small part of it. If there's an audible pitch of a sound, say C4 or middle C, there's aparticular wave length that can be found. Say you have 1000 samples of sound, sampled at 44.1 kHz, which means the 1000 sample sound lasts 22.6 milliseconds. It's very short, but it might contain some 10 wavelengths, if your tone is around 400 Hz. You just have to find the length of a wavelength to calculate the frequency of the tone. If you can assume your frequency is in the range 200 Hz - 800 Hz, you could start with 800 Hz. That would have a wavelength of 55 samples. Take sample #0 and subtract it from sample #55. Square the difference. Add it to a sum. Advance to sample #1 and sample #56 and continue, until you've summed the squared difference of all samples in two assumed consequtive waves. Save the sum. Then start all over with samples #0 and #56. Save this sum. This assumed wavelength would correspond to 787.5 Hz. After counting the sums of the squared differencies of all possible wavelengths from 56 samples to 224 samples, you can pick the one with the least sum (You have to divide each sum with the number of samples to make them comparable). The least sum corresponds to the frequency of your tone.

To add accuracy, run through the whole 1000 sample long sound, find all waves and get an average of them, discarding waves that deviate too much from an average - they are just noisy.
____________________

If you ask for help and write 'u' instead of 'you' because you think it's convenient, I will write 'no' instead of 'yes'. For same reasons.

Grumpy_Mike


Ajith1212

For experimental/development purposes, record the harmonica (with your laptop or phone, etc.)   Then you can connect the headphone-output in place of the function generator and play-back the recording to test your software before you add a mic or any more hardware.

Thanks. Well, I did that. I recorded samples of my harmonica on my phone. Plugged a jack and attached that to my circuit. It works kind of fine. It calculates the frequency with an error of 20 Hz at most.

I only needed an approximate estimate as my project gives commands based on a particular range of frequency. And it seems to work better than I thought it would.

My offer is still open.  If you make a couple good quality recordings of your harmonica, and if you allow those sounds to be copied (eg, "open source" or "creative commons"), I will put them into the frequency analysis example code and share the results here.  That way you can see the results before buying more powerful hardware capable of running the YIN algorithm.
Well, I do like that idea. But the thing is I am given a budget to work on and can't afford any new hardware. So, I am trying to make do with what I have.

If you guys have any suggestion regarding the intensity of signals coming in, i.e. calculating the ones above a certain level. (for the code) To help run commands only when something is played. (sort of) If that can be figured out then maybe it'd get much simpler.

pjrc

Well, I do like that idea. But the thing is I am given a budget to work on and can't afford any new hardware. So, I am trying to make do with what I have.
I'm trying to understand, you don't actually have the harmonica?  Or you don't have a computer or phone that can record a short sound clip?

Go Up