如何使用 Goertzel 算法检测频率
Posted
技术标签:
【中文标题】如何使用 Goertzel 算法检测频率【英文标题】:How to detect frequency using Goertzel algorithm 【发布时间】:2019-07-26 08:49:56 【问题描述】:我真的很难弄清楚这一点。本质上,我试图找出通过麦克风播放的频率。据我了解,我需要暴力破解 Goertzel 算法。所以基本上我只是使用 Goertzel 算法尝试每个频率,直到找到正确的频率。但是,我不明白我如何真正知道 Goertzel 算法何时找到了正确的算法。谁能帮帮我。
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
private Button recordButton;
private TextView result;
private AudioRecord recording;
private static final int RECORDER_SAMPLERATE = 10000;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
int bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);
double[] dbSample = new double[bufferSize];
short[] sample = new short[bufferSize];
private int frequency = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recordButton = findViewById(R.id.recordButton);
result = findViewById(R.id.resultTextView);
recordButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
recording = new AudioRecord(MediaRecorder.Audiosource.DEFAULT, RECORDER_SAMPLERATE,
RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, bufferSize);
recording.startRecording();
int bufferReadResult = recording.read(sample, 0, bufferSize);
for (int j = 0; j < bufferSize && j < bufferReadResult; j++)
dbSample[j] = (double) sample[j];
goertzel.processSample(dbSample[j]);
// Is this correct?
magnitude = Math.sqrt(goertzel.getMagnitudeSquared());
if(magnitude > maxMagnitude)
maxMagnitude = magnitude;
System.out.println("Freq is: " + Integer.toString(frequency));
goertzel.resetGoertzel();
frequency += 1;
);
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;
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);
【问题讨论】:
【参考方案1】:您已经特别询问了暴力破解 Goertzel,所以这里有一个带注释的 JUnit 测试,说明了一种合理的方法:
public class TestGoertzel
private float[] freqs;
private Goertzel[] goertzels;
private static final int RECORDER_SAMPLERATE = 10000;
private static final int INPUT_SAMPLES = 256; //Roughly 26 ms of audio. This small array size was
//chosen b/c the number of frequency "bins" is typically related to the number of input samples,
//for engineering applications. If we only check 256 samples of audio, our "DFT" need only include
//128 output "bins". You can resize this to suit, but keep in mind that the processing time will
//increase exponentially.
@Test
public void test()
freqs = new float[INPUT_SAMPLES / 2]; //To prevent frequency-domain aliasing, we cannot test for 256 frequencies; only the first 128.
goertzels = new Goertzel[freqs.length];
for(int n = 0; n < freqs.length; ++n)
freqs[n] = n * RECORDER_SAMPLERATE / INPUT_SAMPLES; //Determine the frequency of a wave that can fit exactly n cycles in a block of audio INPUT_SAMPLES long.
//Create a Goertzel for each frequency "bin":
goertzels[n] = new Goertzel(RECORDER_SAMPLERATE, freqs[n], INPUT_SAMPLES);
goertzels[n].initGoertzel(); //Might as well create them all at the beginning, then "reset" them as necessary.
//This gives you an idea of the quality of output that can be had for a real signal from your
//microphone. The curve is not perfect, but shows the "smearing" characteristic of a wave
//whose frequency does not fall neatly into a single "bin":
testFrequency(1500.0f);
//Uncomment this to see a full unit test:
//for(float freq : freqs)
//
// testFrequency(freq);
//
private void testFrequency(float freqHz)
System.out.println(String.format("Testing input signal of frequency %5.1fHz", freqHz));
short[] audio = generateAudioWave(freqHz, (short) 1000);
short[] magnitudes = detectFrequencies(audio);
for(int i = 0; i < magnitudes.length; ++i)
System.out.println(String.format("%5.1fHz: %d", freqs[i], magnitudes[i]));
private short[] generateAudioWave(float freqHz, short peakAmp)
short[] ans = new short[INPUT_SAMPLES];
float w0 = (float) ((2 * Math.PI) * freqHz / RECORDER_SAMPLERATE);
for(int i = 0; i < ans.length; ++i)
ans[i] = (short) (Math.sin(w0 * i) * peakAmp);
return ans;
private short[] detectFrequencies(short[] audio)
short[] ans = new short[freqs.length];
for(int i = 0; i < goertzels.length; ++i)
Goertzel goertzel = goertzels[i];
goertzel.resetGoertzel();
for(short s : audio)
goertzel.processSample((double) s);
ans[i] = (short) (Math.sqrt(goertzel.getMagnitudeSquared()) * 2 / INPUT_SAMPLES);
return ans;
基本上,对于您读取的每 256 个音频样本,您获取该数组,然后将其运行通过覆盖您感兴趣的频率的 Goertzel 数组(每个 Goertzel 仅测量一个频率)。这给了你一个输出光谱。您可以根据自己的选择来解释该频谱;我把你的问题理解为,“你如何找到输入音频中最响亮的分量的频率?”。在这种情况下,您将搜索detectFrequencies()
的返回值以找出最大幅度。 freqs
的对应成员就是你的答案。
事实是,由于 FFT 卓越的“计算效率”,您可能不想要 Goertzel,您想要 FFT。由于 Goertzel 的速度稍慢(要像 FFT 一样完全覆盖频谱),因此您可能无法实时运行此答案。
顺便说一句,我认为 Android 不支持 10000 的采样率。
【讨论】:
感谢您的回复。但是,我有点困惑。我知道在这种情况下,您是自己产生声音,而不是从麦克风中读取声音。如果是这样的话,你能告诉我它是如何工作的吗?我对detectFrequencies()
以及在录制真实声音时如何使用它感到困惑
如此处所示,该算法适用于 256 个短块。您只需要从麦克风中读取数据,直到您确定 sample
中至少有 256 个样本。然后您可以拨打detectFrequencies( sample )
。从sample
中找出前 256 个样本;他们已经被处理了。然后您可以开始收集接下来的 256 个样本,依此类推。
更正:您需要确保传递给 detectFrequencies
的数组长度为 256 个元素,按照我编写的方式,它会迭代 audio
中的所有样本。因此,您必须一次将 256 个元素从 sample
复制到一个新的 short
数组中。
所以我循环这个recording.read(sample, 0, bufferSize);
256 次?然后我无法检测频率?我想我只是有点糊涂了。
没有。在检测频率之前,您必须能够回答这个问题:“我怎么知道我的阵列中至少有 256 个麦克风样本?” recording.read(...)
可以返回不同的值,具体取决于它实际读取的样本数量。一般来说,它会大于 256(所以,在这种情况下,你已经完成了)。但是如果它返回小于 256,你应该处理这种情况:跟踪你已经阅读的内容,并发出另一个 read
调用,不使用 0 作为第二个参数,而是使用已经在数组。以上是关于如何使用 Goertzel 算法检测频率的主要内容,如果未能解决你的问题,请参考以下文章