Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to develop a Spectrum Analyser from a realtime audio?

I am developing an app that get a source audio from mic in realtime, with no file storage. Basically, I use:

mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile("/dev/null");

How I can do a spectrum graphic from this realtime audio, with no files?

All post that I read are analyzing a buffered file.

like image 364
Víctor Martín Avatar asked Aug 17 '13 22:08

Víctor Martín


1 Answers

yes it can be done.

All you need is a fast FFT algorithm !

First decide the frequency resolution that you want, for example you can set the sample rate from your mic at 8000hz, now choose one chunk size like 1024 or 2048 to capture from your mic.

If you choose 2048 points and sample rate 8000, do you will have a frequency resolution = 3.9063 (8000 /2048).

Apply one window function over your 2048 points, then apply FFT and get magnitude !

Remember of Nyquist theorem sample rate = 8000 / 2 = 4000, now do you know your FFT can get frequencies between 3.9063 Hz at 4000 Hz.

FFT Bin of corresponding frequencies:

1 -> 3,90625  hz    
2 -> 7,8125  hz    
3 -> 11,71875 hz    
...    
1024 -> 4000 hz    
...    
2048 - > 8000 hz

For it you need just the first half values of FFT, for this case 1024.

Now if you plot this data from your FFT, you will have a spectrum !

EDIT

Pseudo code:

#construct one hanning window Function
Chunk = 2048;
windowed = [Chunk];
hanning = [Chunk];
for i 1:Chunk:
      hanning[i] = ((1 - cos(i*2*pi/Chunk-1))/2)

#start capture from Mic
while true:

    #into values capture 2048 points from your mic
    values=dataFromMic(Chunk);
    #Apply Window hanning = multiply window function(hanning) over your 2048 points
    for i 1:Chunk:
            windowed[i] = values[i] * hanning[i]
    #Apply FFT 
    fftData=fft(windowed);
    #Get Magnitude (linear scale) of first half values
    Mag=abs(fftData(1:Chunk/2))
    # update/show results
    plot(Mag)

end
like image 67
ederwander Avatar answered Sep 22 '22 11:09

ederwander