标签:class 关系 private 12px nsf use highlight sky blog
语音和分贝有关系,分贝和傅里叶变换有关系,所以在语音拾取的时候降低语音的敏感度,可以通过过滤指定分贝数拾取语音,可以理解为过滤噪音
private bool IsUserfulVoice(byte[] data) { double[] wave = new double[data.Length / 2]; int h = 0; for (int i = 0; i < wave.Length; i += 2) { wave[h] = (double)BitConverter.ToInt16(data, i); //采样位数为16bit ++h; } double[] res = VoiceTransformer.FourierTransformer.FFTDb(wave); double kk = 0; foreach (double dd in res) { kk += dd; } if (kk < 0) { kk = 0; } if ((kk / res.Length) > 42.0) { return true; } return false; } public static class FourierTransformer { public static double[] FFTDb(double[] source) { int sourceLen = source.Length; int nu = (int)(Math.Log(sourceLen) / Math.Log(2)); int halfSourceLen = sourceLen / 2; int nu1 = nu - 1; double[] xre = new double[sourceLen]; double[] xim = new double[sourceLen]; double[] decibel = new double[halfSourceLen]; double tr, ti, p, arg, c, s; for (int i = 0; i < sourceLen; i++) { xre[i] = source[i]; xim[i] = 0.0f; } int k = 0; for (int l = 1; l <= nu; l++) { while (k < sourceLen) { for (int i = 1; i <= halfSourceLen; i++) { p = BitReverse(k >> nu1, nu); arg = 2 * (double)Math.PI * p / sourceLen; c = (double)Math.Cos(arg); s = (double)Math.Sin(arg); tr = xre[k + halfSourceLen] * c + xim[k + halfSourceLen] * s; ti = xim[k + halfSourceLen] * c - xre[k + halfSourceLen] * s; xre[k + halfSourceLen] = xre[k] - tr; xim[k + halfSourceLen] = xim[k] - ti; xre[k] += tr; xim[k] += ti; k++; } k += halfSourceLen; } k = 0; nu1--; halfSourceLen = halfSourceLen / 2; } k = 0; int r; while (k < sourceLen) { r = BitReverse(k, nu); if (r > k) { tr = xre[k]; ti = xim[k]; xre[k] = xre[r]; xim[k] = xim[r]; xre[r] = tr; xim[r] = ti; } k++; } for (int i = 0; i < sourceLen / 2; i++) { decibel[i] = 10.0 * Math.Log10((float)(Math.Sqrt((xre[i] * xre[i]) + (xim[i] * xim[i])))); } return decibel; } private static int BitReverse(int j, int nu) { int j2; int j1 = j; int k = 0; for (int i = 1; i <= nu; i++) { j2 = j1 / 2; k = 2 * k + j1 - 2 * j2; j1 = j2; } return k; } }
值得注意的是,拾取语音的Byte数组要为2的N次方长度,这个由快速FFT算法决定
引用自:http://www.cnblogs.com/zhuweisky/p/3269896.html
标签:class 关系 private 12px nsf use highlight sky blog
原文地址:http://www.cnblogs.com/hu123rong00/p/7489359.html