コード例 #1
0
ファイル: TWFFT.cs プロジェクト: icprog/FS-SMISCloud
        /// <summary>
        /// 对于时域a[]转换为频率Y(f),参数b为0
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static int FFTData0(double[] a, double[] b, double Fs, out double[] Y, out double[] f)
        {
            try
            {
                int L = a.Length;//采样个数
                if (b == null)
                {
                    b = new double[L];
                }
                int NFFT = TWFFT.funTwo(L);//最靠近大于2的幂数NFFT,有点问题
                Y = new double[NFFT / 2 + 1];
                f = new double[NFFT / 2 + 1];
                TWFFT.FFT(a, b);
#if DEBUG
                FileUtils.StorageDatum(string.Format(@"FFTResult{0}.txt", DateTime.Now.ToString("HHmmss")), a, b);
#endif

                for (int i = 0; i < NFFT / 2 + 1; i++)
                {
                    double j = (double)i / (NFFT / 2);
                    f[i] = Fs / 2 * j;
                }

                for (int i = 0; i < NFFT / 2 + 1; i++)
                {
                    Y[i] = 2 * (double)Math.Sqrt(Math.Pow(a[i], 2) + Math.Pow(b[i], 2)) / L;
                }
                return(NFFT / 2 + 1);
            }
            catch (Exception ee)
            {
                throw ee;
            }
        }
コード例 #2
0
ファイル: TWFFT.cs プロジェクト: icprog/FS-SMISCloud
        /// <summary>
        /// 对于时域a[]转换为频率Y(f),参数b为0
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static int FFTData1(double[] a, double[] b, double Fs, out double[] Y, out double[] f)
        {
            try
            {
                int L    = a.Length;        //采样个数
                int NFFT = TWFFT.funTwo(L); //最靠近大于2的幂数NFFT,有点问题
                Y = new double[NFFT / 2 + 1];
                f = new double[NFFT / 2 + 1];
                double[] ax = new double[NFFT];
                double[] bx = new double[NFFT];
                Array.Copy(a, 0, ax, 0, L);
                if (b != null)
                {
                    Array.Copy(b, 0, bx, 0, L);
                }
                TWFFT.FFT(ax, bx);

                for (int i = 0; i < NFFT / 2 + 1; i++)
                {
                    double j = (double)i / (NFFT / 2);
                    f[i] = Fs / 2 * j;
                }

                for (int i = 0; i < NFFT / 2 + 1; i++)
                {
                    Y[i] = 2 * (double)Math.Sqrt(Math.Pow(ax[i], 2) + Math.Pow(bx[i], 2)) / L;
                }
                return(NFFT / 2 + 1);
            }
            catch (Exception ee)
            {
                throw ee;
            }
        }
コード例 #3
0
ファイル: TWFFT.cs プロジェクト: icprog/FS-SMISCloud
        /// <summary>
        /// 对于时域a[]转换为频率Y(f),参数b为0
        /// 数据总量可大于每次FFT计算的nCount,多次FFT计算求平均
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static bool FFTData(double[] a, double fs, int nCount, out double[] Y, out double[] f)
        {
            try
            {
                int      len      = a.Length;
                int      TotalCnt = 0;
                int      index    = 0;
                double[] tmp      = new double[nCount];
                Y = new double[nCount / 2 + 1];
                while (index < len)
                {
                    int n = 0;
                    for (int i = index; i < index + nCount; i++)
                    {
                        if (i < len)
                        {
                            tmp[n] = a[i];
                        }
                        else
                        {
                            tmp[n] = 0f;
                        }
                        n++;
                    }

                    var b = new double[nCount];
                    TWFFT.FFT(tmp, b);

                    //直流分量
                    Y[0] += (double)Math.Sqrt(Math.Pow(tmp[0], 2) + Math.Pow(b[0], 2)) / nCount;
                    for (int i = 1; i < nCount / 2 + 1; i++)
                    {
                        Y[i] += 2 * (double)Math.Sqrt(Math.Pow(tmp[i], 2) + Math.Pow(b[i], 2)) / nCount;
                    }
                    index += nCount;
                    TotalCnt++;
                }

                for (int i = 0; i < nCount / 2 + 1; i++)
                {
                    Y[i] = Y[i] / TotalCnt;
                }

                f = new double[nCount / 2 + 1];
                for (int i = 0; i < nCount / 2 + 1; i++)
                {
                    double j = (double)i / (nCount / 2);
                    f[i] = fs / 2 * j;
                }
                return(true);
            }
            catch (Exception ee)
            {
                throw ee;
            }
        }