Chrisi
Chrisi

Reputation: 51

FFT analysis with JavaScript: How to find a specific frequency?

I've the following problem: I analyse audio data using javascript and FFT. I can already write the FFT data into an array:

audioCtx = new AudioContext();
analyser = audioCtx.createAnalyser();
source = audioCtx.createMediaElementSource(audio);
source.connect(analyser);
analyser.connect(audioCtx.destination);
analyser.fftSize = 64;

var frequencyData = new Uint8Array(analyser.frequencyBinCount);

Every time I want to have new data I call:

analyser.getByteFrequencyData(frequencyData);

The variable "audio" is a mp3 file defined in HTML:

<audio id="audio" src="test.mp3"></audio>

So far so good.

My problem now is that I want to check if the current array "frequencyData" includes a specific frequency. For example: I place a 1000 Hz signal somewhere in the mp3 file and want to get a notification if this part of the mp3 file is currently in the array "frequencyData".

In a first step it would help me to solve the problem when the important part of the mp3 file only contains a 1000 Hz signal. In a second step I would also like to find the part if there is an overlay with music.

Upvotes: 1

Views: 3869

Answers (1)

jaket
jaket

Reputation: 9341

frequencyData is an array of amplitudes and each element of the array basically represents a range of frequencies. The size of each range is defined by the sample rate divided by the number of FFT points, 64 in your case. So if your sample rate was 48000 and your FFT size is 64 then each element covers a range of 48000/64 = 750 Hz. That means frequencyData[0] are the frequencies 0Hz-750Hz, frequencyData[1] is 750Hz-1500Hz, and so on. In this example the presence of a 1kHz tone would be seen as a peak in the first bin. Also, with such a small FFT you probably noticed that the resolution is very coarse. If you want to increase the frequency resolution you'll need to do a larger FFT.

Upvotes: 3

Related Questions