使用Goertzel算法如何检测频率

3
我很难理解这个问题的意思。基本上我正在尝试找出通过麦克风播放的频率。据我了解,我需要通过暴力破解高尔泽算法来实现。所以基本上我只需使用高尔泽算法尝试每一个频率,直到找到正确的频率为止。然而,我并不知道如何知道高尔泽算法何时找到正确的算法。可以有人帮帮我吗?
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private Button recordButton;
    private TextView result;

    private AudioRecord recording;
    private static final int RECORDER_SAMPLERATE = 10000;
    private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
    private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
    int bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);
    double[] dbSample = new double[bufferSize];
    short[] sample = new short[bufferSize];
    private int frequency = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recordButton = findViewById(R.id.recordButton);
        result = findViewById(R.id.resultTextView);
        recordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recording = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, RECORDER_SAMPLERATE,
                                            RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, bufferSize);
                recording.startRecording();
                int bufferReadResult = recording.read(sample, 0, bufferSize);


                for (int j = 0; j < bufferSize && j < bufferReadResult; j++) {
                    dbSample[j] = (double) sample[j];
                    goertzel.processSample(dbSample[j]);
                }

                // Is this correct?
                magnitude = Math.sqrt(goertzel.getMagnitudeSquared());
                if(magnitude > maxMagnitude){
                    maxMagnitude = magnitude;
                    System.out.println("Freq is: " + Integer.toString(frequency));
                }
                goertzel.resetGoertzel();
                frequency += 1;
            }
        });

    }
}

Goertzel.java

public class Goertzel {
    private float samplingRate;
    private float targetFrequency;
    private long n;
    private double coeff, Q1, Q2;
    private double sine, cosine;

    public Goertzel(float samplingRate, float targetFrequency, long inN) {
        this.samplingRate = samplingRate;
        this.targetFrequency = targetFrequency;
        n = inN;
    }

    public void resetGoertzel() {
        Q1 = 0;
        Q2 = 0;
    }

    public void initGoertzel() {
        int k;
        float floatN;
        double omega;
        floatN = (float) n;
        k = (int) (0.5 + ((floatN * targetFrequency) / samplingRate));
        omega = (2.0 * Math.PI * k) / floatN;
        sine = Math.sin(omega);
        cosine = Math.cos(omega);
        coeff = 2.0 * cosine;
        resetGoertzel();
    }

    public void processSample(double sample) {
        double Q0;
        Q0 = coeff * Q1 - Q2 + sample;
        Q2 = Q1;
        Q1 = Q0;
    }

    public double[] getRealImag(double[] parts) {
        parts[0] = (Q1 - Q2 * cosine);
        parts[1] = (Q2 * sine);
        return parts;
    }

    public double getMagnitudeSquared() {
        return (Q1 * Q1 + Q2 * Q2 - Q1 * Q2 * coeff);
    }
}
1个回答

0

你问到了关于暴力破解高兹尔的问题,这里提供一个带注释的 JUnit 测试,展示了一种合理的方法:

public class TestGoertzel
{
    private float[] freqs;
    private Goertzel[] goertzels;
    private static final int RECORDER_SAMPLERATE = 10000;
    private static final int INPUT_SAMPLES = 256;   //Roughly 26 ms of audio. This small array size was
        //chosen b/c the number of frequency "bins" is typically related to the number of input samples,
        //for engineering applications. If we only check 256 samples of audio, our "DFT" need only include
        //128 output "bins". You can resize this to suit, but keep in mind that the processing time will
        //increase exponentially.

    @Test
    public void test()
    {
        freqs = new float[INPUT_SAMPLES / 2];   //To prevent frequency-domain aliasing, we cannot test for 256 frequencies; only the first 128.

        goertzels = new Goertzel[freqs.length];

        for(int n = 0; n < freqs.length; ++n)
        {
            freqs[n] = n * RECORDER_SAMPLERATE / INPUT_SAMPLES;     //Determine the frequency of a wave that can fit exactly n cycles in a block of audio INPUT_SAMPLES long.

            //Create a Goertzel for each frequency "bin":
            goertzels[n] = new Goertzel(RECORDER_SAMPLERATE, freqs[n], INPUT_SAMPLES);
            goertzels[n].initGoertzel();        //Might as well create them all at the beginning, then "reset" them as necessary.
        }

        //This gives you an idea of the quality of output that can be had for a real signal from your
        //microphone. The curve is not perfect, but shows the "smearing" characteristic of a wave
        //whose frequency does not fall neatly into a single "bin":
        testFrequency(1500.0f);

        //Uncomment this to see a full unit test:
        //for(float freq : freqs)
        //{
        //  testFrequency(freq);
        //}
    }

    private void testFrequency(float freqHz)
    {
        System.out.println(String.format("Testing input signal of frequency %5.1fHz", freqHz));
        short[] audio = generateAudioWave(freqHz, (short) 1000);

        short[] magnitudes = detectFrequencies(audio);

        for(int i = 0; i < magnitudes.length; ++i)
        {
            System.out.println(String.format("%5.1fHz: %d", freqs[i], magnitudes[i]));
        }
    }

    private short[] generateAudioWave(float freqHz, short peakAmp)
    {
        short[] ans = new short[INPUT_SAMPLES];

        float w0 = (float) ((2 * Math.PI) * freqHz / RECORDER_SAMPLERATE);

        for(int i = 0; i < ans.length; ++i)
        {
            ans[i] = (short) (Math.sin(w0 * i) * peakAmp);
        }

        return ans;
    }


    private short[] detectFrequencies(short[] audio)
    {
        short[] ans = new short[freqs.length];

        for(int i = 0; i < goertzels.length; ++i)
        {
            Goertzel goertzel = goertzels[i];
            goertzel.resetGoertzel();

            for(short s : audio)
            {
                goertzel.processSample((double) s);
            }

            ans[i] = (short) (Math.sqrt(goertzel.getMagnitudeSquared()) * 2 / INPUT_SAMPLES);
        }

        return ans;
    }
}

基本上,对于每读入256个音频样本,您需要将该数组传递给一组覆盖您感兴趣的频率的Goertzel数组(每个Goertzel仅测量一个频率)。这会给您一个输出频谱。您可以根据自己的选择来解释该频谱;我理解您的问题是“如何找到输入音频中最响亮的成分的频率?”在这种情况下,您需要搜索detectFrequencies()的返回值以获取最大幅度。相应的freqs成员就是您的答案。

事实上,您可能不需要使用Goertzel算法,而是需要FFT,因为FFT具有更高的“计算效率”。由于Goertzel算法要比FFT慢一些(为了完全覆盖频谱),您可能无法实时运行此答案。

顺便说一句,在Android上不支持10000的采样率。


谢谢您的回复。然而,我有点困惑了。我理解在这种情况下,您是自己生成声音而不是从麦克风中读取声音。如果是这种情况,您能否向我展示它是如何工作的?我对detectFrequencies()感到困惑,当真实声音被记录时,我该如何使用它。 - Zoey Malkov
如下所示,该算法适用于256个短块。您只需要从麦克风中读取数据,直到确保“sample”中至少有256个样本为止。然后,您可以调用“detectFrequencies(sample)”。将这前256个样本移出“sample”,因为它们已经被处理过了。然后,您可以开始收集下一个256个样本,以此类推。 - greeble31
更正一下:你需要确保传递给 detectFrequencies 的数组长度为 256,因为我写的方式是迭代 audio 中的所有样本。所以你需要每次从 sample 中复制 256 个元素到一个新的 short 数组中。 - greeble31
那我需要循环执行 recording.read(sample, 0, bufferSize); 这个操作256次吗?然后才能检测频率?我觉得我更加困惑了。 - Zoey Malkov
在你能够检测频率之前,你必须要回答这个问题:“如何知道我的数组中至少有256个麦克风样本?” recording.read(...) 可以返回不同的值,这取决于它实际读取了多少个样本。通常情况下,它会大于256(所以,在这种情况下,你已经完成了)。但如果它返回少于256个样本,你应该处理这种情况:跟踪你已经读取了什么,并发出另一个 read 调用,不使用0作为第二个参数,而是使用数组中已有内容的大小。 - greeble31
我建议您在尝试解决此问题之前,先熟悉一下 read() 的用法以及音频流的一般知识。 - greeble31

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接