使用Goertzel算法检测特定频率

时间:2017-02-24 08:58:23

标签: android algorithm audio signal-processing goertzel-algorithm

我只是一名没有数字信号处理经验的大学生,我想创建一个记录音频的Android应用程序,并检测大学作业的特定目标频率。我是在Goertzel算法的帮助下完成的。 所以这是与我用作参考的完全相同的问题的链接。 Using Goertzel algorithm to detect frequency 此外,此链接作为Goertzel算法的主要参考。 http://www.embedded.com/design/configurable-systems/4024443/The-Goertzel-Algorithm 正如在这个链接中提到的那样,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类

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

如果有人可以帮助我,告诉我哪里出错,并指出正确的方向,我真的很感激。

2 个答案:

答案 0 :(得分:3)

您始终将计算出的幅度添加到a1数组中,但永远不会从该容器中删除任何内容。相应地,如果在某个时刻幅度超过阈值,那么遍历 all a1处理程序中onClick元素的循环将继续找到该元素并设置{ {1}}。我建议您在flag完成处理后清除该容器:

onClick

答案 1 :(得分:0)

您不断将数据汇总到您的Goertzel滤波器积分中,而不会将下一个数据块的总和重置为零(例如,在您选择的等效DFT宽度的N个样本之后),或者通过减去滑动的历史值窗口方法。