How to develop a Spectrum Analyser from a realtime audio?

徘徊边缘 提交于 2019-11-30 00:50:43

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

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!