使用Goertzel算法检测频率

时间:2016-10-15 05:24:47

标签: java android signal-processing goertzel-algorithm

我正在尝试使用Goertzel算法编写一个用于检测频率的程序。然而,当我将算法应用于我从AudioRecord read()方法获得的数据时,幅度不会在目标频率上达到峰值(.i.e:它通常在低频上达到峰值)。我误解了什么吗?

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

}

0 个答案:

没有答案