# how to use FFT library

I am wondering how to use this library.

http://wiki.openmusiclabs.com/wiki/ArduinoFFT

It compiles and outputs binary data as expected. But I wanted to use it to say determine the dominant frequency. Can it do that, and if so, how?

But I wanted to use it to say determine the dominant frequency

So you search through those output values for the biggest one. The index ( or bin ) you find the biggest value represents the dominant frequency in the spectrum.

thanks, I did try that, but failed. But if you think that is the way its done, I'll give it another go. How do I determine which freq is aligned with with bin?

I used this library

successfully. But, there were occasional incorrect frequencies logged. And so I was looking for something better, though the problem may be rectified via software.

Using a guitar I wanted to find the dominant frequency, but some problems were an occasional octave difference, and also if there was any fret or string buzzing, some incorrect frequencies came through.

I tried all the different windows but they seemed about the same. I am using a 2 pole filter with a cutoff starting at 1.5KHz

any help would be great. thanks.

Using a guitar I wanted to find the dominant frequency, but some problems were an occasional octave difference,

Yes you will get that due to the nature of the guitar sound, the fundamental frequency is not always the loudest one, so the octave will be off. There is not a lot you can do about it with an FFT because it is just measuring the signal. The fact it is not what you want is not the fault of the measurement technique.

How do I determine which freq is aligned with with bin?

It is the sample rate. The top bin corresponds to half the sample rate, then next a quarter and so on down.

I see. Then if not FFT, what is the best way of determining the fundamental frequency on a guitar?

Then if not FFT, what is the best way of determining the fundamental frequency on a guitar?

This is not a simple problem, and of course, "best" depends on your requirements.

The FFT is a good way to get an overview of the spectrum of sounds produced by an instrument, but accurate determination of frequencies is difficult for various reasons: your microphone and audio input have limited frequency response, the Arduino is slow and does not have much memory for sampling, the analog to digital converter has limited accuracy, etc.

Autocorrelation functions tend to work better than FFT for accurate frequency determination, but are limited by the same problems mentioned above.

Google will be happy to show you more on the topic.

tim77777:
Then if not FFT, what is the best way of determining the fundamental frequency on a guitar?

The YIN algorithm (PDF link) works very well. It uses autocorrelation and “a number of modifications that combine to prevent errors”. The authors claim “error rates are about three times lower than the best competing methods”. While that was written in 2001, I’d imagine YIN could be still be considered the best way today.

But YIN is far too much for 8 bit AVR chips. It’s probably impractical even on Cortex-M0+, but can be done on the faster 32 bit boards. You did ask for “the best way”…

There’s an implementation in the Teensy audio library. Here’s the example sketch to analyze guitar samples.

Teensy 3.2 is the minimum hardware to run this code. The faster 3.5 & 3.6 boards give more CPU time if you want to do anything else, since YIN uses nearly all the CPU power of a 96 MHz Cortex-M4.

I used the simple autocorrelation and it works better than fft for my application. Does autocorrelation of guitar signals require heavy input filtering to remove harmonics, like when using fft?

"Heavy input filtering" is required any time you sample analog data.

It has nothing to do with what happens to those data afterwards.

You must not allow components with frequencies higher than (sample frequency)/2 in your input signal or you will get spurious and sometimes completely uninterpretable output. Look up "sampling theorem", "Nyquist limit", "aliasing" etc.

thanks, but I am referring to filtering out the harmonics which are large enough sometimes to override the fundamental.

The harmonics are irrelevant if you are measuring the fundamental.

An extremely important fact about the world is that with complex signals, the different frequency components are completely independent of each other. The components simply add to produce the resultant. Nothing "overrides".

Look up the Superposition Principle.

tim77777:
thanks, but I am referring to filtering out the harmonics which are large enough sometimes to override the fundamental.

But if you know what harmonics you are going to filter out then you know the octave you are wanting to look at.

Hi

I understand that the thread is a bit older and that's why I am hoping that someone may have found a solution already.

I need to find all the frequencies in an analog input through MEMS microphone module connected to ESP32.

I've managed to calculate the dB level but not sure how to find all the frequencies.

any help would be appreciated.

Thanks

tim77777:
I used the simple autocorrelation and it works better than fft for my application. Does autocorrelation of guitar signals require heavy input filtering to remove harmonics, like when using fft?

You take the autocorrelation, then the spectrum of that, and you'll get a strong signal at the fundamental even if the original signal is all harmonics and no fundamental!!

Brings back memories of my college Signal and Systems Analysis classes. If I recall correctly, the Fourier Transform of the autocorrelation function is the power spectral density.

MarkT:
You take the autocorrelation, then the spectrum of that, and you'll get a strong signal at the fundamental even if the original signal is all harmonics and no fundamental!!

Whoops, made a mistake there, you square the auto-correlation before taking its FFT to get the fundamental showing up strongly... Otherwise you've only the PSD as gfvalvo points out.