私はデジタル信号処理の経験のない大学の最終学年で、大学の課題のためにオーディオを録音し、特定のターゲット周波数を検出する Android アプリケーションを作成したいと考えています。Goertzel アルゴリズムの助けを借りてそうしています。したがって、これは私が参照として使用したのとまったく同じ質問があるリンクです。 Goertzel アルゴリズムを使用して周波数を検出する また、このリンクは Goertzel アルゴリズムの主なリファレンスです。 http://www.embedded.com/design/configurable-systems/4024443/The-Goertzel-アルゴリズム このリンクで述べたように、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);
}
}
誰かが私を助けて、どこが間違っているのかを教えてくれ、正しい方向に向けてくれたら本当にありがたいです.