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

  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));//保留四位小数输出

  }

  }