对不起该帖子的长度。我想说明一下我尝试过的以及我想要完成的事情。
基本上我要做的是在C#中编写VOIP网络测试程序。我使用Ozeki VOIP SIP C#SDK编写了所有VOIP代码。基本上它的作用是客户端进行服务器端选择的VOIP呼叫。客户端播放WAV文件,服务器端记录它。我从audiocheck.net生成了一个音调文件。我以8000Hz和16bit的采样率生成了一个3000Hz的wav文件,其正弦波形为5秒。这就是客户端所扮演的角色。我随意选择了频率,因此可以随时改变。我想要做的是让服务器端对文件进行简单的分析,以确定可以通过丢包,延迟等引入的噪声量。
AudioProcessor.cs是一个C#类,它打开一个WAV文件并读取头信息。由于文件是16位波形,我使用“两个补码”(感谢http://www.codeproject.com/Articles/19590/WAVE-File-Processor-in-C)将每个2字节帧读入数组。例如,我有:
0:0
1:-14321
2:17173
3:-9875
4:0
5:9875
6:-17175
7:14319
8:0
9:-14321
10:17173
11:-9875
代码是:
Console.WriteLine("Audio: Filename: " + fileName);
FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(stream);
int chunkID = reader.ReadInt32();
int fileSize = reader.ReadInt32();
int riffType = reader.ReadInt32();
int fmtID = reader.ReadInt32();
int fmtSize = reader.ReadInt32();
int fmtCode = reader.ReadInt16();
int channels = reader.ReadInt16();
int sampleRate = reader.ReadInt32();
int fmtAvgBPS = reader.ReadInt32();
int fmtBlockAlign = reader.ReadInt16();
int bitDepth = reader.ReadInt16();
if (fmtSize == 18)
{
// Read any extra values
int fmtExtraSize = reader.ReadInt16();
reader.ReadBytes(fmtExtraSize);
}
int dataID = reader.ReadInt32();
int dataSize = reader.ReadInt32();
Console.WriteLine("Audio: file size: " + fileSize.ToString());
Console.WriteLine("Audio: sample rate: " + sampleRate.ToString());
Console.WriteLine("Audio: channels: " + channels.ToString());
Console.WriteLine("Audio: bit depth: " + bitDepth.ToString());
Console.WriteLine("Audio: fmtAvgBPS: " + fmtAvgBPS.ToString());
Console.WriteLine("Audio: data id: " + dataID.ToString());
Console.WriteLine("Audio: data size: " + dataSize.ToString());
int frames = 8 * (dataSize / bitDepth) / channels;
int frameSize = dataSize / frames;
double timeLength = ((double)frames / (double)sampleRate);
Console.WriteLine("Audio: frames: " + frames.ToString());
Console.WriteLine("Audio: frame size: " + frameSize.ToString());
Console.WriteLine("Audio: Time length: " + timeLength.ToString());
// byte[] soundData = reader.ReadBytes(dataSize);
// Convert to two-complement
short[] frameData = new short[frames];
for (int i = 0; i < frames; i++)
{
short snd = reader.ReadInt16();
if (snd != 0)
snd = Convert.ToInt16((~snd | 1));
frameData[i] = snd;
}
下一步是计算噪声量,或者说有多少非3000Hz信号。基于研究,我最初尝试使用Goertzel滤波器来检测特定频率。它似乎用于检测手机DTMF。这个方法是我试过的一个实现。
public static double Calculate(short[] samples, double freq)
{
double s_prev = 0.0;
double s_prev2 = 0.0;
double coeff,normalizedfreq,power,s;
int i;
normalizedfreq = freq / (double)SAMPLING_RATE;
coeff = 2.0*Math.Cos(2.0*Math.PI*normalizedfreq);
for (i=0; i<samples.Length; 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;
}
我会调用函数传递1秒样本:
short[] sampleData = new short[4000];
Array.Copy(frameData,sampleData,4000);
for (int i = 1; i < 11; i++)
{
Console.WriteLine(i * 1000 + ": " + Goertzel2.Calculate(sampleData, i * 1000));
}
输出结果为:
1000: 4297489869.04579
2000: 19758026000000
3000: 1.17528628051013E+15
4000: 0
5000: 1.17528628051013E+15
6000: 19758026000000
7000: 4297489869.04671
8000: 4000000
9000: 4297489869.04529
10000: 19758026000000
3000Hz似乎有最大的数字,但5000也是如此。我不知道这些数字是否准确。如果这是有用的话,我会针对较小的样本运行,例如1/10秒以试图检测我将解释为噪声的变化。
我还研究了陷波滤波器或FFT。我不确定接下来最好的步骤是什么。我不需要任何复杂的东西。我只想粗略地计算输出wav文件的多少是噪声。如上所述,我是用C#编写的,但我可以从C,C ++,Python和Java中移植代码。
编辑:这是我更新的代码。
计算每个频率的总功率
// Number of frequencies that are half of the sample rate to scan
int _frequencyGranularity = 2000;
// Number of frames to use to create a sample for the filter
int _sampleSize = 4000;
int frameCount = 0;
while(frameCount + _sampleSize < frameData.Length)
{
// Dictionary to store the power level at a particular frequency
Dictionary<int, double> vals = new Dictionary<int, double>(_frequencyGranularity);
double totalPower = 0;
for (int i = 1; i <= _frequencyGranularity; i++)
{
// Only process up to half of the sample rate as this is the Nyquist limit
// http://stackoverflow.com/questions/20864651/calculating-the-amount-of-noise-in-a-wav-file-compared-to-a-source-file
int freq = i * wave.SampleRate / 2 / _frequencyGranularity;
vals[freq] = Goertzel.Calculate(frameData, frameCount, _sampleSize, wave.SampleRate, freq);
totalPower += vals[freq];
}
// Calculate the percentange of noise by subtracting the percentage of power at the desided frequency of 3000 from 100.
double frameNoisePercentange = (100 - (vals[3000] / totalPower * 100));
logger.Debug("Frame: " + frameCount + " Noise: " + frameNoisePercentange);
noisePercentange += frameNoisePercentange;
frameCount += _sampleSize;
}
double averageNoise = (noisePercentange / (int)(frameCount/_sampleSize));
更新了Goertzel方法
public static double Calculate(short[] sampleData, int offset, int length, int sampleRate, double searchFreq)
{
double s_prev = 0.0;
double s_prev2 = 0.0;
double coeff,normalizedfreq,power,s;
int i;
normalizedfreq = searchFreq / (double)sampleRate;
coeff = 2.0*Math.Cos(2.0*Math.PI*normalizedfreq);
for (i=0; i<length; i++)
{
s = sampleData[i+offset] + 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;
}
答案 0 :(得分:0)
建立粗略估计噪声的一种方法是计算信号峰值的标准偏差。
鉴于你知道预期的频率,你可以将信号分成一个波长的块,即如果你的信号是3KHz而你的采样率是16KHz,那么你的块大小是5.3333个样本,每个块找到最高value,然后对于该值序列,找到stddev。
或者,您可以为每个块跟踪最小值和最大值,然后在整个样本中,找到最小值和最大值的平均值,以及最小值的范围(即最小值的最高值和最低值),然后SNR是〜(mean_max-mean_min)/(min_range)