2

我只是一名在数字信号处理方面没有经验的大四学生,我想制作一个 android 应用程序来记录音频并检测大学作业的特定目标频率。我是在 Goertzel 算法的帮助下这样做的。所以这是与我用作参考的完全相同的问题的链接。 使用 Goertzel 算法检测频率 另外,此链接作为 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);
    }
}

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

4

2 回答 2

3

您总是将计算出的幅度添加到您的a1数组中,但从不从该容器中删除任何内容。相应地,如果在某个时候幅度超过阈值,遍历处理程序中所有元素a1的循环onClick将继续查找该元素并设置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();
}
于 2017-02-24T22:52:07.993 回答
0

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

于 2017-02-24T20:22:33.110 回答