C#实现FFT(递归法)的示例代码

2022-07-15,,,

1. c#实现复数类

我们在进行信号分析的时候,难免会使用到复数。但是遗憾的是,c#没有自带的复数类,以下提供了一种复数类的构建方法。

复数相比于实数,可以理解为一个二维数,构建复数类,我们需要实现以下这些内容:

  • 复数实部与虚部的属性
  • 复数与复数的加减乘除运算
  • 复数与实数的加减乘除运算
  • 复数取模
  • 复数取相位角
  • 欧拉公式(即eix+y

c#实现的代码如下:

 public class complex
    {
        double real;
        double imag;
        public complex(double x, double y)   //构造函数
        {
            this.real = x;
            this.imag = y;
        }
        //通过属性实现对复数实部与虚部的单独查看和设置
        public double real
        {
            set { this.real = value; }
            get { return this.real; }
        }
        public double imag
        {
            set { this.imag = value; }
            get { return this.imag; }
        }
        //重载加法
        public static complex operator +(complex c1, complex c2)
        {
            return new complex(c1.real + c2.real, c1.imag + c2.imag);
        }
        public static complex operator +(double c1, complex c2)
        {
            return new complex(c1 + c2.real, c2.imag);
        }
        public static complex operator +(complex c1, double c2)
        {
            return new complex(c1.real + c2, c1.imag);
        }
        //重载减法
        public static complex operator -(complex c1, complex c2)
        {
            return new complex(c1.real - c2.real, c1.imag - c2.imag);
        }
        public static complex operator -(double c1, complex c2)
        {
            return new complex(c1 - c2.real, -c2.imag);
        }
        public static complex operator -(complex c1, double c2)
        {
            return new complex(c1.real - c2, c1.imag);
        }
        //重载乘法
        public static complex operator *(complex c1, complex c2)
        {
            double cr = c1.real * c2.real - c1.imag * c2.imag;
            double ci = c1.imag * c2.real + c2.imag * c1.real;
            return new complex(math.round(cr, 4), math.round(ci, 4));
        }
        public static complex operator *(double c1, complex c2)
        {
            double cr = c1 * c2.real;
            double ci = c1 * c2.imag;
            return new complex(math.round(cr, 4), math.round(ci, 4));
        }
        public static complex operator *(complex c1, double c2)
        {
            double cr = c1.real * c2;
            double ci = c1.imag * c2;
            return new complex(math.round(cr, 4), math.round(ci, 4));
        }

        //重载除法
        public static complex operator /(complex c1, complex c2)
        {
            if (c2.real == 0 && c2.imag == 0)
            {
                return new complex(double.nan, double.nan);
            }
            else
            {
                double cr = (c1.imag * c2.imag + c2.real * c1.real) / (c2.imag * c2.imag + c2.real * c2.real);
                double ci = (c1.imag * c2.real - c2.imag * c1.real) / (c2.imag * c2.imag + c2.real * c2.real);
                return new complex(math.round(cr, 4), math.round(ci, 4));           //保留四位小数后输出
            }
        }
      
        public static complex operator /(double c1, complex c2)
        {
            if (c2.real == 0 && c2.imag == 0)
            {
                return new complex(double.nan, double.nan);
            }
            else
            {
                double cr = c1 * c2.real / (c2.imag * c2.imag + c2.real * c2.real);
                double ci = -c1 * c2.imag / (c2.imag * c2.imag + c2.real * c2.real);
                return new complex(math.round(cr, 4), math.round(ci, 4));           //保留四位小数后输出
            }
        }
      
        public static complex operator /(complex c1, double c2)
        {
            if (c2 == 0)
            {
                return new complex(double.nan, double.nan);
            }
            else
            {
                double cr = c1.real / c2;
                double ci = c1.imag / c2;
                return new complex(math.round(cr, 4), math.round(ci, 4));           //保留四位小数后输出
            }
        }
        //创建一个取模的方法
        public static double abs(complex c)
        {
            return math.sqrt(c.imag * c.imag + c.real * c.real);
        }
        //创建一个取相位角的方法
        public static double angle(complex c)
        {
            return math.round(math.atan2(c.real, c.imag), 6);//保留6位小数输出
        }
        //重载字符串转换方法,便于显示复数
        public override string tostring()
        {
            if (imag >= 0)
                return string.format("{0}+i{1}", real, imag);
            else
                return string.format("{0}-i{1}", real, -imag);
        }
        //欧拉公式
        public static complex exp(complex c)
        {
            double amplitude = math.exp(c.real);
            double cr = amplitude * math.cos(c.imag);
            double ci = amplitude * math.sin(c.imag);
            return new complex(math.round(cr, 4), math.round(ci, 4));//保留四位小数输出
        }
    }

2. 递归法实现fft

以下的递归法是基于奇偶分解实现的。

奇偶分解的原理推导如下:

x(2r)和x(2r+1)都是长度为n/2−1的数据序列,不妨令

则原来的dft就变成了:

于是,将原来的n点傅里叶变换变成了两个n/2点傅里叶变换的线性组合。

但是,n/2点傅里叶变换只能确定n/2个频域数据,另外n/2个数据怎么确定呢?

因为x1(k)和x2(k)周期都是n/2,所以有

从而得到:

综上,我们就可以得到递归法实现fft的流程:

1.对于每组数据,按奇偶分解成两组数据

2.两组数据分别进行傅里叶变换,得到x1(k)和x2(k)

3.总体数据的x(k)由下式确定:

4.对上述过程进行递归

具体代码实现如下:

public complex[] fftre(complex[] c)
{
    int n = c.length;
    complex[] cout = new complex[n];
    if (n == 1)
    {
        cout[0] = c[0];
        return cout;
    }
    else
    {
        double n_2_f = n / 2;
        int n_2 = (int)math.floor(n_2_f);
        complex[] c1 = new complex[n / 2];
        complex[] c2 = new complex[n / 2];
        for (int i = 0; i < n_2; i++)
        {
            c1[i] = c[2 * i];
            c2[i] = c[2 * i + 1];
        }
        complex[] c1out = fftre(c1);
        complex[] c2out = fftre(c2);
        complex[] c3 = new complex[n / 2];
        for (int i = 0; i < n / 2; i++)
        {
            c3[i] = new complex(0, -2 * math.pi * i / n);
        }
        for (int i = 0; i < n / 2; i++)
        {
            c2out[i] = c2out[i] * complex.exp(c3[i]);
        }

        for (int i = 0; i < n / 2; i++)
        {
            cout[i] = c1out[i] + c2out[i];
            cout[i + n / 2] = c1out[i] - c2out[i];
        }
        return cout;
    }
}

3. 补充:窗函数

顺便提供几个常用的窗函数:

  • rectangle
  • bartlett
  • hamming
  • hanning
  • blackman
    public class wdslib
    {
        //以下窗函数均为periodic
        public double[] rectangle(int len)
        {
            double[] win = new double[len];
            for (int i = 0; i < len; i++)
            {
                win[i] = 1;
            }
            return win;
        }

        public double[] bartlett(int len)
        {
            double length = (double)len - 1;
            double[] win = new double[len];
            for (int i = 0; i < len; i++)
            {
                if (i < len / 2) { win[i] = 2 * i / length; }
                else { win[i] = 2 - 2 * i / length; }
            }
            return win;
        }

        public double[] hamming(int len)
        {
            double[] win = new double[len];
            for (int i = 0; i < len; i++)
            {
                win[i] = 0.54 - 0.46 * math.cos(math.pi * 2 * i / len);
            }
            return win;
        }

        public double[] hanning(int len)
        {
            double[] win = new double[len];
            for (int i = 0; i < len; i++)
            {
                win[i] = 0.5 * (1 - math.cos(2 * math.pi * i / len));
            }
            return win;
        }

        public double[] blackman(int len)
        {
            double[] win = new double[len];
            for (int i = 0; i < len; i++)
            {
                win[i] = 0.42 - 0.5 * math.cos(math.pi * 2 * (double)i / len) + 0.08 * math.cos(math.pi * 4 * (double)i / len);
            }
            return win;
        }
    }

以上就是c#实现fft(递归法)的示例代码的详细内容,更多关于c# fft递归法的资料请关注其它相关文章!

《C#实现FFT(递归法)的示例代码.doc》

下载本文的Word格式文档,以方便收藏与打印。