How to develop a Spectrum Analyser from a realtime audio?

后端 未结 3 630
被撕碎了的回忆
被撕碎了的回忆 2020-12-25 08:50

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.setAu         


        
3条回答
  •  南笙
    南笙 (楼主)
    2020-12-25 09:23

    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
    

提交回复
热议问题