Example #1
0
        /// <summary>
        /// 用倒谱获得频谱包络
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private static double[] GetEnvelopeFD(double[] data)
        {
            int n = data.Length;

            double[] data1  = new double[n];
            double[] data1i = new double[n];
            for (int i = 0; i < n; i++)
            {
                data1[i] = Math.Log(data[i]);
            }
            for (int i = 0; i < n; i++)
            {
                data1i[i] = 0;
            }
            TWFFT.FFT(data1, data1i);
            double therehold = 100;

            double[] data2  = new double[n];
            double[] data2i = new double[n];
            // 低通滤波
            for (int i = 0; i < n; i++)
            {
                if (i < therehold || i > n - therehold)
                {
                    data2[i]  = data1[i];
                    data2i[i] = data1i[i];
                }
                else
                {
                    data2[i]  = 0;
                    data2i[i] = 0;
                }
            }
            //for (int i = 0; i < n; i++) data2i[i] = 0;
            TWFFT.IFFT(data2, data2i);
            var res = FToneAnalysis.MiddleFilter(data2, 3);

            for (int i = 0; i < n; i++)
            {
                res[i] = Math.Pow(Math.E, res[i]);
            }
            return(res);
        }
Example #2
0
        public static Image getFFTImage(int[] data)
        {
            int w = 2000;
            int h = 1000;

            Bitmap   bitmap = new Bitmap(w, h);
            Graphics g      = Graphics.FromImage(bitmap);

            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            //draw fft
            int n = 4096;

            double[] datas   = new double[n];
            double[] datas2  = new double[n];
            double[] datamod = new double[n];
            //if (data.Length <= n) return null;

            for (int i = 0; i < n; i++)
            {
                datas2[i] = 0;
                if (i > data.Length - 1)
                {
                    datas[i] = 0;
                }
                else
                {
                    datas[i] = data[i];
                }
            }
            TWFFT.FFT(datas, datas2);
            for (int i = 0; i < n; i++)
            {
                datamod[i] = Math.Sqrt(datas[i] * datas[i] + datas2[i] * datas2[i]);
            }
            int time = 2;

            for (int i = 1; i < n / time; i++)
            {
                g.DrawLine(new Pen(Color.Black, 1),
                           new Point((int)((double)(i - 1) / n * time * bitmap.Width), (int)(h - datamod[i - 1] / w)),
                           new Point((int)((double)i / n * time * bitmap.Width), (int)(h - datamod[i] / w)));
            }

            double[] datad  = new double[n];
            double[] datad2 = new double[n];
            for (int i = 0; i < n; i++)
            {
                datad2[i] = 0;
            }
            for (int i = 0; i < n; i++)
            {
                datad[i] = Math.Log(datamod[i]);
            }
            for (int i = 1; i < n / time; i++)
            {
                g.DrawLine(new Pen(Color.Red, 1),
                           new Point((int)((double)(i - 1) / n * time * bitmap.Width), (int)(h - datad[i - 1] * 50)),
                           new Point((int)((double)i / n * time * bitmap.Width), (int)(h - datad[i] * 50)));
            }

            TWFFT.FFT(datad, datad2);
            for (int i = 0; i < n; i++)
            {
                datamod[i] = Math.Log(Math.Sqrt(datad[i] * datad[i] + datad2[i] * datad2[i]));
            }

            double therehold = 100;

            double[] rdata  = new double[n];
            double[] rdata2 = new double[n];
            for (int i = 0; i < n; i++)
            {
                if (i < therehold || i > n - therehold)
                {
                    rdata[i] = datad[i];
                }
                else
                {
                    rdata[i] = 0;
                }
            }
            for (int i = 0; i < n; i++)
            {
                rdata2[i] = datad2[i];
            }
            TWFFT.IFFT(rdata, rdata2);
            for (int i = 1; i < n / time; i++)
            {
                g.DrawLine(new Pen(Color.Blue, 1),
                           new Point((int)((double)(i - 1) / n * time * bitmap.Width), (int)(h - rdata[i - 1] * 50)),
                           new Point((int)((double)i / n * time * bitmap.Width), (int)(h - rdata[i] * 50)));
            }



            return(bitmap);
        }