使用 Goertzel 算法检测特定频率

Detecting a specific frequency using Goertzel algorithm

我只是一名没有数字信号处理经验的大四学生,我想制作一个 android 应用程序来记录音频并检测大学作业的特定目标频率。我是在 Goertzel 算法的帮助下这样做的。 所以这是 link 与我用作参考的问题完全相同。 Using Goertzel algorithm to detect frequency 此外,此 link 作为 Goertzel 算法的主要参考。 http://www.embedded.com/design/configurable-systems/4024443/The-Goertzel-Algorithm 正如这个 link 中提到的,Goertzel 算法在目标频率处达到峰值然后再次下降,但对我来说,幅度在目标频率处确实变得非常高但之后不会下降。是缓冲区大小问题还是某个阈值频率问题。我真的不确定。

这是我的代码:- MainActivity

package abc.com.goertzel;


import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
private static Button recordButton;
private static Button stopButton;
private static TextView textView2;
private static final int RECORDER_SAMPLERATE = 44100;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
private AudioRecord recorder;
private boolean isRecording = false;
double magnitude=0;
int freq=15000;
int bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,
        RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);

ArrayList<Double> a1 = new ArrayList<Double>();
ArrayList<Double> a2 = new ArrayList<Double>();

double[] dbSample = new double[bufferSize];
short[] sample = new short[bufferSize];
Goertzel g = new Goertzel(RECORDER_SAMPLERATE, freq, bufferSize);


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

    recordButton = (Button) findViewById(R.id.recordButton);
    stopButton = (Button) findViewById(R.id.stopButton);
    textView2= (TextView) findViewById(R.id.textView2);

    System.out.println("Hello Hi "+ bufferSize);

    recordButton.setOnClickListener(new View.OnClickListener(){

        public void onClick (View v)
        {
            textView2.setText("  ");

            recorder = new AudioRecord(MediaRecorder.AudioSource.VOICE_RECOGNITION,
                RECORDER_SAMPLERATE, RECORDER_CHANNELS,
                RECORDER_AUDIO_ENCODING, bufferSize);
            recorder.startRecording();
            isRecording = true;
            g.initGoertzel();

            new Thread(){
                public void run(){
                    while( isRecording )
                    {
                        int bufferReadResult = recorder.read(sample, 0, bufferSize);
                        //System.out.println(" BufferRead " + bufferReadResult);
                        //System.out.println("Sample length " + sample.length);

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

                        for (int i = 0; i < bufferSize; i++) {
                            g.processSample(dbSample[i]);
                        }
                        magnitude = Math.sqrt(g.getMagnitudeSquared());
                        System.out.println("magnitude " + magnitude);

                        a1.add(magnitude);

                        g.resetGoertzel();

                    }
                }
            }.start();
        }
    });


    stopButton.setOnClickListener(new View.OnClickListener() {
        public void onClick (View view) {

            //      try{
            isRecording = false;
            recorder.stop();
            recorder.release();
            recorder = null;

            System.out.println(a1);

            int flag=0;
            for(int j=0;j<a1.size();j++)
            {
                double b= (a1.get(j));
                if(b>24000)
                {
                    a2.add(b);
                }
            }
            System.out.println(a2);
            for (int counter = 0; counter < a1.size(); counter++)
            {  
                double d = (a1.get(counter));
                if (d > 17000) {
                    flag=1;
                    break;
                }
                else
                {
                    flag=0;
                }
            }
            if(flag==1)
            {
                textView2.setText("Frequency of " + freq + "  detected ");
            }
            else{
                textView2.setText("Frequency of " + freq + " not detected ");
            }
        }
    });
}

Goertzel.java class

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;
        //sine = Math.sin(2 * Math.PI * (targetFrequency / samplingRate));
        //cosine = Math.cos(2 * Math.PI * (targetFrequency / samplingRate));
        //coeff = 2 * cosine;
    }

    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);
    }
}

如果有人能帮助我并指出我哪里出错并指出正确的方向,我将不胜感激。

您不断地将数据求和到您的 Goertzel 滤波器集成中,而没有将下一个数据块的总和重置为零(例如,在您选择的等效 DFT 宽度的每 N 个样本之后),或者减去历史值滑动 window 方法。

您总是将计算出的星等添加到您的 a1 数组中,但永远不要从该容器中删除任何内容。相应地,如果在某个时候幅度超过阈值,遍历 all onClick 处理程序中 a1 的元素的循环将继续找到该元素并且设置 flag。我建议您在 onClick:

中完成处理后清除该容器
public void onClick (View view) {
    ...
    // Detect frequency above threshold
    flag = 0;
    for (int counter = 0; counter < a1.size(); counter++) {
        double d = (a1.get(counter));
        if (d > 17000) {
            flag=1;
            break;
        }
    }
    // Do something in case frequency is detected
    if (flag) {
    }
    ...
    // Clear a1 to get rid of data that we just processed
    a1.clear();
}