gpt4 book ai didi

c# - 计算与源文件相比的wav文件中的噪声量

转载 作者:行者123 更新时间:2023-12-03 02:14:35 26 4
gpt4 key购买 nike

抱歉,帖子的长度。我想举例说明我已经尝试过并且想要完成的工作。

本质上,我想做的是用C#编写VOIP网络测试仪。我已经使用Ozeki VOIP SIP C#SDK编写了所有VOIP代码。本质上,它所做的是客户端发出VOIP call ,服务器端接听。客户端播放WAV文件,服务器端记录该文件。我已经从audiocheck.net生成了一个音调文件。我以8000Hz和16位的采样率生成了一个5秒的正弦波形的3000Hz wav文件。这就是客户扮演的角色。我任意选择频率,以便可以随时更改。然后,我要做的是让服务器端对文件进行简单分析,以确定噪声量,该噪声量可能是由于数据包丢失,延迟等导致的。

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 s)进行测试,以尝试检测出我认为是噪声的变化。

我还研究了陷波滤波器或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;
}

最佳答案

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

假设您知道预期的频率,则可以将信号分成一个波长的块,即,如果信号为3KHz,采样率为16KHz,则块大小为5.3333个样本,每个块都找到最大值,然后对于该值序列,找到stddev。

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

关于c# - 计算与源文件相比的wav文件中的噪声量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20864651/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com