DTMF Goertzel 算法不起作用

2024-01-28

因此,我正在打开我在 audacity 中生成的 DTMF 音调的 .raw 文件。我抓住了一个类似于维基百科文章中的罐装 goertzel 算法。但它似乎无法解码正确的数字。

解码后的数字也会根据我传递给算法的 N 值而变化。据我了解,较高的 N 值可以提供更好的准确性,但不应改变正确解码的数字?

这是代码,

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double goertzel(short samples[], double freq, int N) 
{
double s_prev = 0.0;
double s_prev2 = 0.0;    
double coeff, normalizedfreq, power, s;
int i;
normalizedfreq = freq / 8000;
coeff = 2*cos(2*M_PI*normalizedfreq);
for (i=0; i<N; i++) 
{
    s = samples[i] + coeff * s_prev - s_prev2;
    s_prev2 = s_prev;
    s_prev = s;
}
power = s_prev2*s_prev2+s_prev*s_prev-coeff*s_prev*s_prev2;
return power;
}

int main()
{
FILE *fp = fopen("9.raw", "rb");
short *buffer;
float *sample;
int sample_size;
int file_size;
int i=0, x=0;

float frequency_row[] = {697, 770, 852, 941};
float frequency_col[] = {1209, 1336, 1477};
float magnitude_row[4];
float magnitude_col[4];

double result;

fseek(fp, 0, SEEK_END);
file_size = ftell(fp);
fseek(fp, 0, SEEK_SET);

buffer = malloc(file_size);

buffer[x] = getc(fp);
buffer[x] = buffer[x]<<8;
buffer[x] = buffer[x] | getc(fp);

while(!feof(fp))
{
    x++;
    buffer[x] = getc(fp);
    buffer[x] = buffer[x]<<8;
    buffer[x] = buffer[x] | getc(fp);
}

for(i=0; i<x; i++)
{
    //printf("%#x\n", (unsigned short)buffer[i]);
}
for(i=0; i<4; i++)
{
    magnitude_row[i] = goertzel(buffer, frequency_row[i], 8000);
}
for(i=0; i<3; i++)
{
    magnitude_col[i] = goertzel(buffer, frequency_col[i], 8000);
}

x=0;
for(i=0; i<4; i++)
{
    if(magnitude_row[i] > magnitude_row[x])
    x = i;
}
printf("Freq: %f\t Mag: %f\n", frequency_row[x], magnitude_row[x]);

x=0;
for(i=0; i<3; i++)
{
    if(magnitude_col[i] > magnitude_col[x])
    x = i;
}
printf("Freq: %f\t Mag: %f\n", frequency_col[x], magnitude_col[x]);
return 0;
 }

该算法实际上使用起来很棘手,即使对于检测 DTMF 音调这样简单的事情也是如此。它实际上是一个带通滤波器 http://en.wikipedia.org/wiki/Band-pass_filter- 它挑选出以给定频率为中心的频带。这实际上是一件好事 - 你不能指望你的采样音调是exactly您试图检测的频率。

棘手的部分是尝试设置滤波器的带宽 - 将过滤以检测特定音调的频率范围有多大。

参考文献之一维基百科页面 http://en.wikipedia.org/wiki/Goertzel_algorithm就此主题而言 (this one http://focus.ti.com/lit/an/spra168/spra168.pdf准确地说)讨论了在 DSP 中使用 Goertzel 算法实现 DTMF 音调检测。 C 的原理是相同的 - 要获得正确的带宽,您必须使用提供的常量的正确组合。显然没有简单的公式 - 论文提到必须使用暴力搜索 http://en.wikipedia.org/wiki/Brute-force_search,并提供以 8kHz 采样的 DTMF 频率的最佳常数列表。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

DTMF Goertzel 算法不起作用 的相关文章