我只是一名大一毕业的大学生,没有数字信号处理方面的经验,我想制作一个可记录音频并检测大学分配的特定目标频率的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);
}
}
如果有人能帮助我并告诉我我出了什么问题并为我指明正确的方向,我将不胜感激。
最佳答案
您总是将计算出的幅度添加到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();
}