问题
I am trying to write a program for android to detect frequency using Goertzel algorithm. However when I apply the algorithm on the data that I get from the AudioRecord read() method, the magnitude does not peak on the target frequency (.i.e: it usually peak on low frequency). Am I misunderstanding anything?
protected void detect() {
double[] dbSample = new double[bufferSize];
short[] sample = new short[bufferSize];
max_magnitude = 0;
while(isRecording){
int bufferReadResult = recorder.read(sample,0,bufferSize);
for (int j=0;j<bufferSize&&j<bufferReadResult;j++) {
dbSample[j] = (double)sample[j];
}
}
int freq=0;
while(freq<=20000){
Goertzel g = new Goertzel(RECORDER_SAMPLE_RATE,freq,bufferSize);
g.initGoertzel();
for(int i=0;i<bufferSize;i++){
g.processSample(dbSample[i]);
}
magnitude = Math.sqrt(g.getMagnitudeSquared());
if(magnitude>max_magnitude){
max_magnitude = magnitude;
detect_freq = freq;
}
g.resetGoertzel();
freq+=50;
}
}
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);
}
}
来源:https://stackoverflow.com/questions/40055298/using-goertzel-algorithm-to-detect-frequency