从earlier question继续,我的目标是从C#中检测WAV文件中的DTMF音调。但是,我真的很难理解如何做到这一点。
我了解DTMF使用频率的组合,并且可以使用Goertzel算法...以某种方式使用。我捕获了Goertzel代码段,并尝试将.WAV文件插入其中(使用NAudio读取文件,该文件是8KHz单声道16位PCM WAV):
using (WaveFileReader reader = new WaveFileReader(@"dtmftest_w.wav"))
{
byte[] buffer = new byte[reader.Length];
int read = reader.Read(buffer, 0, buffer.Length);
short[] sampleBuffer = new short[read/2];
Buffer.BlockCopy(buffer, 0, sampleBuffer, 0, read/2);
Console.WriteLine(CalculateGoertzel(sampleBuffer,8000,16));
}
public static double CalculateGoertzel(short[] sample, double frequency, int samplerate)
{
double Skn, Skn1, Skn2;
Skn = Skn1 = Skn2 = 0;
for (int i = 0; i < sample.Length; i++)
{
Skn2 = Skn1;
Skn1 = Skn;
Skn = 2 * Math.Cos(2 * Math.PI * frequency / samplerate) * Skn1 - Skn2 + sample[i];
}
double WNk = Math.Exp(-2 * Math.PI * frequency / samplerate);
return 20 * Math.Log10(Math.Abs((Skn - WNk * Skn1)));
}
我知道我在做什么是错误的:我假设我应该遍历缓冲区,并且一次只计算一小块的Goertzel值-这是正确的吗?
其次,我不太了解Goertzel方法的输出说明了什么:返回了一个double(例如:
210.985812
),但是我不知道将其等同于DTMF音的存在和值。音频文件。我到处都在寻找答案,包括this答案中引用的库;不幸的是,代码here似乎不起作用(如站点评论中所述)。 TAPIEx提供了一个商业图书馆;我已经尝试了他们的评估库,它确实满足了我的需要-但是他们没有回复电子邮件,这使我对实际购买他们的产品感到谨慎。
我很清楚自己可能在不知道确切问题的情况下正在寻找答案,但是最终我所需要的只是在.WAV文件中找到DTMF音调的方法。我是在正确的路线上吗?如果没有,有人能指出我正确的方向吗?
编辑:使用@Abbondanza的代码作为基础,并基于(可能根本上是错误的)假设,我需要滴入音频文件的一小部分,现在我有了这个(非常粗糙,仅是概念证明) ) 代码:
const short sampleSize = 160;
using (WaveFileReader reader = new WaveFileReader(@"\\mac\home\dtmftest.wav"))
{
byte[] buffer = new byte[reader.Length];
reader.Read(buffer, 0, buffer.Length);
int bufferPos = 0;
while (bufferPos < buffer.Length-(sampleSize*2))
{
short[] sampleBuffer = new short[sampleSize];
Buffer.BlockCopy(buffer, bufferPos, sampleBuffer, 0, sampleSize*2);
var frequencies = new[] {697.0, 770.0, 852.0, 941.0, 1209.0, 1336.0, 1477.0};
var powers = frequencies.Select(f => new
{
Frequency = f,
Power = CalculateGoertzel(sampleBuffer, f, 8000)
});
const double AdjustmentFactor = 1.05;
var adjustedMeanPower = AdjustmentFactor*powers.Average(result => result.Power);
var sortedPowers = powers.OrderByDescending(result => result.Power);
var highestPowers = sortedPowers.Take(2).ToList();
float seconds = bufferPos / (float)16000;
if (highestPowers.All(result => result.Power > adjustedMeanPower))
{
// Use highestPowers[0].Frequency and highestPowers[1].Frequency to
// classify the detected DTMF tone.
switch (Convert.ToInt32(highestPowers[0].Frequency))
{
case 1209:
switch (Convert.ToInt32(highestPowers[1].Frequency))
{
case 697:
Console.WriteLine("1 pressed at " + bufferPos + " (" + seconds + "s)");
break;
case 770:
Console.WriteLine("4 pressed at " + bufferPos + " (" + seconds + "s)");
break;
case 852:
Console.WriteLine("7 pressed at " + bufferPos + " (" + seconds + "s)");
break;
case 941:
Console.WriteLine("* pressed at " + bufferPos);
break;
}
break;
case 1336:
switch (Convert.ToInt32(highestPowers[1].Frequency))
{
case 697:
Console.WriteLine("2 pressed at " + bufferPos + " (" + seconds + "s)");
break;
case 770:
Console.WriteLine("5 pressed at " + bufferPos + " (" + seconds + "s)");
break;
case 852:
Console.WriteLine("8 pressed at " + bufferPos + " (" + seconds + "s)");
break;
case 941:
Console.WriteLine("0 pressed at " + bufferPos + " (" + seconds + "s)");
break;
}
break;
case 1477:
switch (Convert.ToInt32(highestPowers[1].Frequency))
{
case 697:
Console.WriteLine("3 pressed at " + bufferPos + " (" + seconds + "s)");
break;
case 770:
Console.WriteLine("6 pressed at " + bufferPos + " (" + seconds + "s)");
break;
case 852:
Console.WriteLine("9 pressed at " + bufferPos + " (" + seconds + "s)");
break;
case 941:
Console.WriteLine("# pressed at " + bufferPos + " (" + seconds + "s)");
break;
}
break;
}
}
else
{
Console.WriteLine("No DTMF at " + bufferPos + " (" + seconds + "s)");
}
bufferPos = bufferPos + (sampleSize*2);
}
这是在Audacity中查看的示例文件;我已经添加了被按下的DTMF按键-
而且...几乎可以正常工作。从上面的文件中,直到几乎恰好3秒钟,我都看不到任何DTMF,但是,我的代码报告:
9 pressed at 1920 (0.12s)
1 pressed at 2880 (0.18s)
* pressed at 3200
1 pressed at 5120 (0.32s)
1 pressed at 5440 (0.34s)
7 pressed at 5760 (0.36s)
7 pressed at 6080 (0.38s)
7 pressed at 6720 (0.42s)
5 pressed at 7040 (0.44s)
7 pressed at 7360 (0.46s)
7 pressed at 7680 (0.48s)
1 pressed at 8000 (0.5s)
7 pressed at 8320 (0.52s)
...直到到达3秒,然后开始稳定下来以得到正确的答案:按下了
1
:7 pressed at 40000 (2.5s)
# pressed at 43840 (2.74s)
No DTMF at 44800 (2.8s)
1 pressed at 45120 (2.82s)
1 pressed at 45440 (2.84s)
1 pressed at 46080 (2.88s)
1 pressed at 46720 (2.92s)
4 pressed at 47040 (2.94s)
1 pressed at 47360 (2.96s)
1 pressed at 47680 (2.98s)
1 pressed at 48000 (3s)
1 pressed at 48960 (3.06s)
4 pressed at 49600 (3.1s)
1 pressed at 49920 (3.12s)
1 pressed at 50560 (3.16s)
1 pressed at 51520 (3.22s)
1 pressed at 52160 (3.26s)
4 pressed at 52480 (3.28s)
如果我将
AdjustmentFactor
提升到1.2以上,我将几乎没有被发现。我感觉到我快要走了,但是有人可以看到我在想什么吗?
EDIT2:上面的测试文件可用here。上例中的
adjustedMeanPower
是47.6660450354638
,功效是:最佳答案
CalculateGoertzel()
返回提供的样本内选定频率的功率。
为每个DTMF频率(697、770、852、941、1209、1336和1477 Hz)计算此功率,对所得功率进行排序并选择最高的两个。如果两者都超过某个阈值,则说明已检测到DTMF音。
用作阈值的方式取决于 sample 的信噪比(SNR)。首先,只需计算所有Goerzel值的平均值,然后将该平均值乘以一个因子(例如2或3),然后检查两个最高的Goerzel值是否都高于该值即可。
这是一个代码段,以更正式的方式表达我的意思:
var frequencies = new[] {697.0, 770.0, 852.0, 941.0, 1209.0, 1336.0, 1477.0};
var powers = frequencies.Select(f => new
{
Frequency = f,
Power = CalculateGoerzel(sample, f, samplerate)
});
const double AdjustmentFactor = 1.0;
var adjustedMeanPower = AdjustmentFactor * powers.Average(result => result.Power);
var sortedPowers = powers.OrderByDescending(result => result.Power);
var highestPowers = sortedPowers.Take(2).ToList();
if (highestPowers.All(result => result.Power > adjustedMeanPower))
{
// Use highestPowers[0].Frequency and highestPowers[1].Frequency to
// classify the detected DTMF tone.
}
以
AdjustmentFactor
的1.0
开头。如果您从测试数据中得到误报(即您在不应有任何DTMF音调的样本中检测到DTMF音调),请继续增加它直到误报停止。更新#1
我在wave文件上尝试了您的代码,并进行了一些调整:
我在Goertzel计算之后实现了可枚举(对于性能很重要):
var powers = frequencies.Select(f => new
{
Frequency = f,
Power = CalculateGoertzel(sampleBuffer, f, 8000)
// Materialize enumerable to avoid multiple calculations.
}).ToList();
我没有将调整后的均值用于阈值化。我只是使用
100.0
作为阈值:if (highestPowers.All(result => result.Power > 100.0))
{
...
}
我将样本大小增加了一倍(我相信您使用了
160
):int sampleSize = 160 * 2;
我修正了您的DTMF分类。我使用嵌套字典来捕获所有可能的情况:
var phoneKeyOf = new Dictionary<int, Dictionary<int, string>>
{
{1209, new Dictionary<int, string> {{1477, "?"}, {1336, "?"}, {1209, "?"}, {941, "*"}, {852, "7"}, {770, "4"}, {697, "1"}}},
{1336, new Dictionary<int, string> {{1477, "?"}, {1336, "?"}, {1209, "?"}, {941, "0"}, {852, "8"}, {770, "5"}, {697, "2"}}},
{1477, new Dictionary<int, string> {{1477, "?"}, {1336, "?"}, {1209, "?"}, {941, "#"}, {852, "9"}, {770, "6"}, {697, "3"}}},
{ 941, new Dictionary<int, string> {{1477, "#"}, {1336, "0"}, {1209, "*"}, {941, "?"}, {852, "?"}, {770, "?"}, {697, "?"}}},
{ 852, new Dictionary<int, string> {{1477, "9"}, {1336, "8"}, {1209, "7"}, {941, "?"}, {852, "?"}, {770, "?"}, {697, "?"}}},
{ 770, new Dictionary<int, string> {{1477, "6"}, {1336, "5"}, {1209, "4"}, {941, "?"}, {852, "?"}, {770, "?"}, {697, "?"}}},
{ 697, new Dictionary<int, string> {{1477, "3"}, {1336, "2"}, {1209, "1"}, {941, "?"}, {852, "?"}, {770, "?"}, {697, "?"}}}
}
然后使用以下方式检索电话 key :
var key = phoneKeyOf[(int)highestPowers[0].Frequency][(int)highestPowers[1].Frequency];
结果不是完美的,但是有些可靠。
更新#2
我想我已经解决了问题,但现在无法自己解决。您无法将目标频率直接传递给
CalculateGoertzel()
。必须对其进行归一化,以使其位于DFT箱的中央。在计算功效时,请尝试以下方法:var powers = frequencies.Select(f => new
{
Frequency = f,
// Pass normalized frequenzy
Power = CalculateGoertzel(sampleBuffer, Math.Round(f*sampleSize/8000.0), 8000)
}).ToList();
另外,您必须将
205
用作sampleSize
才能最大程度地减少错误。更新#3
我重新编写了原型(prototype),以使用NAudio的
ISampleProvider
接口(interface),该接口(interface)返回归一化的样本值(范围[-1.0; 1.0]中的float
)。我也从头开始重写了CalculateGoertzel()
。它仍然没有进行性能优化,但是在频率之间产生了明显得多的功率差异。当我运行您的测试数据时,不会再有误报了。我强烈建议您看一下:http://pastebin.com/serxw5nG更新#4
我创建了一个GitHub project和two NuGet packages来检测实时(捕获)音频和预录制音频文件中的DTMF音调。
关于c# - 从WAV文件解码DTMF,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34092954/