How to calculate sound frequency in android?

我怕爱的太早我们不能终老 提交于 2019-11-29 20:30:52
ymn

Your problem was solved here EDIT: archived here. Also you can analyze the frequency by using FFT.

EDIT: FFTBasedSpectrumAnalyzer (example code, the link from the comment)

Ashish Wadatkar

Thanks for Reply I have done this by using sample on
http://som-itsolutions.blogspot.in/2012/01/fft-based-simple-spectrum-analyzer.html

Just modify your code for to calculate sound frequency by using below method

 // sampleRate = 44100 

public static int calculate(int sampleRate, short [] audioData){

    int numSamples = audioData.length;
    int numCrossing = 0;
    for (int p = 0; p < numSamples-1; p++)
    {
        if ((audioData[p] > 0 && audioData[p + 1] <= 0) || 
            (audioData[p] < 0 && audioData[p + 1] >= 0))
        {
            numCrossing++;
        }
    }

    float numSecondsRecorded = (float)numSamples/(float)sampleRate;
    float numCycles = numCrossing/2;
    float frequency = numCycles/numSecondsRecorded;

    return (int)frequency;
}

The other answers show how to display a spectrogram. I think the question is how to detect a change in fundamental frequency. This is asked so often on Stack Exchange I wrote a blog entry (with code!) about it:

http://blog.bjornroche.com/2012/07/frequency-detection-using-fft-aka-pitch.html

Admittedly, the code is in C, but I think you'll find it easy to port.

In short, you must:

  1. low-pass the input signal so that higher frequency overtones are not mistaken for the fundamental frequency (this may not appear to be an issue in your application, since you are just looking for a change in pitch, but I recommend doing it anyway for reasons that are too complex to go into here).

  2. window the signal, using a proper windowing function. To get the most responsive output, you should overlap the windows, which I don't do in my sample code.

  3. Perform an FFT on the data in each window, and calculate the frequency using the index of maximum absolute peak value.

Keep in mind for your application where you probably want to detect the change in pitch accurately and quickly, the FFT method I describe may not be sufficient. You have two options:

  1. There are techniques for increasing the specificity of the pitch tracking using phase information, not just the absolute peak.

  2. Use a time-domain method based on autocorrelation. Yin is an excellent choice. (google for "yin pitch tracking")

Here is a link to the code mentioned. There's also some other useful code there.

https://github.com/gast-lib/gast-lib/blob/master/library/src/root/gast/audio/processing/ZeroCrossing.java

Here's the deal with ZeroCrossings:

It is inaccurate at determining frequency precisely based on recorded audio on an Android. That said, it is still useful for giving your app a general sense that the sound it is hearing is a constant singing tone, versus just noise.

The code here seems to work quite well for determining frequency, (if you can translate it from C# to java) http://code.google.com/p/yaalp/

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