Víctor Martín
Víctor Martín

Reputation: 3450

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.

Upvotes: 9

Views: 32590

Answers (3)

There is an open source Android spectrum analyzer on Github which computers FFT on audio from the microphone and displays a 2D spectrogram.

The Spectrum Analyzer project is found in the v2.x/Showcase app

You can see a video of it in action at https://youtu.be/yU05fsgOYO4

You can see a video with build instructions here: https://youtu.be/tVgn30uss2k?t=1m37s

enter image description here

The charting is provided by the SciChart Android chart library which is a commercial control, but the source code to read the mic, compute the FFT and spectrogram is free & open source under MIT License.

As a disclosure: I am the tech lead on the SciChart project

Upvotes: 8

somenath mukhopadhyay
somenath mukhopadhyay

Reputation: 1558

I have developed an open source FFT based spectrum analyzer. Please have a look at

http://som-itsolutions.blogspot.in/2012/01/fft-based-simple-spectrum-analyzer.html.

You can also get the source code from

https://github.com/sommukhopadhyay/FFTBasedSpectrumAnalyzer

Hope this will help you.

Upvotes: 3

ederwander
ederwander

Reputation: 3478

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

Upvotes: 9

Related Questions