【问题标题】:calculating the amount of noise in a wav file compared to a source file计算 wav 文件与源文件相比的噪声量
【发布时间】:2014-01-01 00:52:23
【问题描述】:

对不起,帖子的长度。我想说明我已经尝试过和想要完成的工作。

基本上我想做的是用 C# 编写一个 VOIP 网络测试器。我已经使用 Ozeki VOIP SIP C# SDK 编写了所有的 VOIP 代码。本质上,它所做的是客户端发出服务器端接听的 VOIP 呼叫。客户端播放WAV文件,服务器端录制。我从 audiocheck.net 生成了一个音调文件。我以 8000Hz 和 16 位的采样率生成了一个 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;
}

【问题讨论】:

    标签: c# audio filter voip wav


    【解决方案1】:

    对噪声进行粗略估计的一种方法是计算信号峰值的标准偏差。

    鉴于您知道预期的频率,您可以将信号分成一个波长的块,即如果您的信号是 3KHz 并且您的采样率为 16KHz,那么您的块大小是 5.3333 个样本,对于每个块找到最高的值,然后对于该值序列,找到标准差。

    或者,您可以为每个块跟踪最小值和最大值,然后在整个样本上,找到最小值和最大值的平均值,以及最小值的范围(即最小值的最高和最低值)然后SNR 为 ~ (mean_max - mean_min) / (min_range)

    【讨论】:

    • 我的采样率为 8Khz,这意味着每个波长我会有 2.66 个采样。使用较低频率在每个波长获得更多样本会更好吗?可能更好的是对 2 个波长使用 5 个样本,使用我原始帖子中的值会产生 12120.788 的标准偏差。我不确定如何处理这个号码。我会看看你的第二种方法,看看我能计算出什么。
    • 是的,8KHz 通道上的 3KHz 信号正在接近奈奎斯特极限 - 这解释了使用 Goertzel 在 5KHz 处的峰值,您看到的是反射......,只有低于 4KHz 的值有意义
    • 这就是我需要知道的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    相关资源
    最近更新 更多