1. 程式人生 > >語音和分貝數關系

語音和分貝數關系

byte[] math .com ++ convert ret each reverse uri

語音和分貝有關系,分貝和傅裏葉變換有關系,所以在語音拾取的時候降低語音的敏感度,可以通過過濾指定分貝數拾取語音,可以理解為過濾噪音

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

語音和分貝數關系