📄 complex.cs
字号:
/*
* Filter functions, a part of DSProcessing library.
*
* based on: AForge.NET
* adress: http://code.google.com/p/aforge/
* author: Andrew Kirillov
*
* modifyed under GNU GPL by Jan Sova
* a part of DSProcessing library
* -------------------------------------------------------------------------
*
* DSProcessing - C#/C++ library of signal processing, speech processing,
* and communications classes and functions
*
* Copyright (C) 2007-2008
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* -------------------------------------------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace DSProcessing
{
/// <summary>
/// Complex number - struktura pro reprezentaci komplexniho cisla
/// </summary>
/// <remarks>The class encapsulate complex number and provides
/// basic complex operators.</remarks>
public struct Complex
{
/// <summary>
/// Real part of the complex number
/// </summary>
public double Re;
/// <summary>
/// Imaginary part of the complex number
/// </summary>
public double Im;
/// <summary>
/// Represents complex zero
/// </summary>
///
/// <remarks>Represents complex number with both real and imaginary
/// parts equal to zero.</remarks>
///
public static Complex Zero
{
get { return new Complex(0, 0); }
}
/// <summary>
/// Magnitude value of the complex number
/// </summary>
public double Magnitude
{
get { return System.Math.Sqrt(Re * Re + Im * Im); }
}
/// <summary>
/// Phase value of the complex number
/// </summary>
public double Phase
{
get { return System.Math.Atan(Im / Re); }
}
/// <summary>
/// Squared magnitude value of the complex number
/// </summary>
public double SquaredMagnitude
{
get { return (Re * Re + Im * Im); }
}
/// <summary>
/// Initializes a new instance of the <see cref="Complex"/> class
/// </summary>
///
/// <param name="re">Real part</param>
/// <param name="im">Imaginary part</param>
public Complex(double re, double im)
{
this.Re = re;
this.Im = im;
}
/// <summary>
/// Initializes a new instance of the <see cref="Complex"/> class
/// </summary>
///
/// <param name="c">Source complex number</param>
public Complex(Complex c)
{
this.Re = c.Re;
this.Im = c.Im;
}
/// <summary>
/// Returns string representation of the complex number
/// </summary>
///
/// <returns>String representation of the complex number</returns>
public override string ToString()
{
return String.Format("{0}{1}{2}i", Re, ((Im < 0) ? '-' : '+'), Math.Abs(Im));
}
/// <summary>
/// Addition operator
/// </summary>
///
/// <param name="a">Left complex operand</param>
/// <param name="b">Right complex operand</param>
///
/// <returns>Result complex number</returns>
public static Complex operator +(Complex a, Complex b)
{
return new Complex(a.Re + b.Re, a.Im + b.Im);
}
/// <summary>
/// Subtraction operator
/// </summary>
///
/// <param name="a">Left complex operand</param>
/// <param name="b">Right complex operand</param>
///
/// <returns>Result complex number</returns>
public static Complex operator -(Complex a, Complex b)
{
return new Complex(a.Re - b.Re, a.Im - b.Im);
}
/// <summary>
/// Multiplication operator
/// </summary>
///
/// <param name="a">Left complex operand</param>
/// <param name="b">Right complex operand</param>
///
/// <returns>Result complex number</returns>
public static Complex operator *(Complex a, Complex b)
{
return new Complex(
a.Re * b.Re - a.Im * b.Im,
a.Re * b.Im + a.Im * b.Re);
}
/// <summary>
/// Division operator
/// </summary>
///
/// <param name="a">Left complex operand</param>
/// <param name="b">Right complex operand</param>
///
/// <returns>Result complex number</returns>
public static Complex operator /(Complex a, Complex b)
{
double divider = b.Re * b.Re + b.Im * b.Im;
if (divider == 0)
throw new DivideByZeroException();
return new Complex(
(a.Re * b.Re + a.Im * b.Im) / divider,
(a.Im * b.Re - a.Re * b.Im) / divider);
}
/// <summary>
/// Create complex field from two doubles field:
/// out[n].Re = real[n]
/// out[n].Im = img[n]
/// </summary>
///
/// <param name="real">real parts</param>
/// <param name="img">img parts</param>
///
/// <returns></returns>
public static Complex[] makeFrom(double[] real, double[] img)
{
Complex[] returnValue = new Complex[img.Length];
for(int i =0; i< real.Length; i++)
{
returnValue[i].Re = real[i];
returnValue[i].Im = img[i];
}
return returnValue;
}
/// <summary>
/// Return field with real parts from input complex field.
/// </summary>
///
/// <param name="data">input complex field</param>
///
/// <returns></returns>
public static double[] real(Complex[] data)
{
double[] returnValue = new double[data.Length];
for (int i = 0; i < data.Length; i++)
{
returnValue[i] = data[i].Re;
}
return returnValue;
}
/// <summary>
/// Create complex field from double field:
/// out[n].Re = data[n]
/// out[n].Im = 0
/// </summary>
///
/// <param name="data">real parts</param>
///
/// <returns></returns>
public static Complex[] makeRealPart(double[] data)
{
Complex[] returnValue = new Complex[data.Length];
for (int i = 0; i < data.Length; i++)
{
returnValue[i].Re = data[i];
returnValue[i].Im = 0;
}
return returnValue;
}
/// <summary>
/// Return double abs field from complex field:
/// out[n] = data[n].Magnitude
/// </summary>
///
/// <param name="data">field of complex numbers</param>
///
/// <returns></returns>
public static double[] AbsField(Complex[] data)
{
double[] returnValue = new double[data.Length];
for (int i = 0; i < data.Length; i++)
{
returnValue[i] = data[i].Magnitude;
}
return returnValue;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -