Home > Net >  The results of FFT after IFFT didn't restore to the original signal?
The results of FFT after IFFT didn't restore to the original signal?

Time:03-13

 
using System;

The namespace ConsoleApp34
{
Class Program
{
The static void Main (string [] args)
{
//initialization time domain data
Complex [] WT=new Complex [8].
Complex [] IWT=new Complex [8].
Complex [] TD2FD=new Complex [8].
for (int i=0; I & lt; TD2FD. Length; I++)
{
Complex CPX=new Complex ();
CPX. Re=I;
CPX. Im=1;
TD2FD [I]=CPX;

//FFT rotation factor lookup table
Complex cpx_wt=new Complex ();
Float Angle=(float) (I * math.h PI * 2/TD2FD. Length);
Cpx_wt. Re=(float) Math. Cos (Angle);
Cpx_wt. Im=- (float) Math. Sin (Angle);
WT=cpx_wt [I];

//IFFT rotation factor lookup table
Complex cpx_iwt=new Complex ();
Angle=(float) (I * math.h PI * 2/TD2FD. Length);
Cpx_iwt. Re=(float) Math. Cos (Angle);
Cpx_iwt. Im=(float) Math. Sin (Angle);
IWT [I]=cpx_iwt;
}

Console. WriteLine (" -- -- -- -- -- - the original signal -- -- -- -- -- ");
Print (TD2FD);

Console. WriteLine (" -- -- -- -- -- FFI -- -- -- -- -- ");
FFT (TD2FD, WT);
Print (TD2FD);

IFFT (TD2FD, IWT);
Print (TD2FD);

The Console. The Read ();
}

Public static void FFT (Complex [] TD2FD, Complex [] WT)
{
Int power=(int) Math. The Log (TD2FD Length, 2);
Int butterfly;
Int p, s;
Complex x1, x2, wt;

For (int k=0; K & lt; The power; K++)//series
{
Console. WriteLine (" the first grade {0}, {1} group ", k, 1 & lt; for (int j=0; J & lt; 1 {
Butterfly=1 & lt; <(power - k);//there are several elements in each group
//computation involved in butterfly operations of the two elements of index
P=j * butterfly;
S=p + butterfly/2;
Console. WriteLine (" butterfly={0}, p={1}, s={2} ", butterfly, p, s);
for (int i=0; I & lt; Butterfly/2; I++)//butterfly operations
{
The Console. Write (" ({0} {1} x wtIdx={2}) ", I + p, I + s, I * (1 & lt; X1=TD2FD [I + p];
X2=TD2FD [I + s];
Wt=wt [I * (1 & lt; & lt; k)];
TD2FD [I + p]=x1 + x2 * wt;
TD2FD [I] + s=x1 - x2 * wt;
}
Console. WriteLine ();
}
}

//reorder
for (int i=0; I & lt; TD2FD. Length; I++)
{
Int r=BitReverse (I);
If (r & gt; I)
{
Complex t=TD2FD [r].
TD2FD [r]=TD2FD [I];
TD2FD [I]=t;
}
}
}

//print
Private static void Print (Complex [] TD2FD)
{
for (int i=0; I & lt; TD2FD. Length; I++)
{
Console. WriteLine (" (re={0}, im={1}) ", TD2FD [I] re, TD2FD [I]. Im);
}
Console. WriteLine ();
}

Public static void IFFT (Complex [] FD2TD, Complex [] WT)
{
Console. WriteLine (" - IFFT - ");
//do FFT transform
FFT (FD2TD, WT);
//real N
for (int i=0; I & lt; FD2TD. Length; I++)
FD2TD [I]. Re/=FD2TD. Length;
}

Static int BitReverse (int x)
{
//0 1 2 3 4 5 6 7 decimal
//000 001 010 011 100 101 110 111 decimal corresponding binary
//000 100 010 110 001 101 011 111 yards of a reverse
//0 4 2 1 3 5 6 7 yards after an inversion of the corresponding decimal
Int [] table=new int [8] {0, 4, 2, 6, 1, 5, 3, 7};
Return the table [x];
}
}

//define the plural
Public class Complex
{
Public float re;//real department
Public float im;//imaginary part

Public static Complex operator + (Complex LHS, Complex RHS)
{
Complex result=new Complex ();
Result. The re=LHS. Re + RHS. Re;
Result. Im=LHS. Im + RHS. Im;
return result;
}

Public static Complex operator - (Complex LHS, Complex RHS)
{
Complex result=new Complex ();
Result. The re=LHS. Re - RHS. Re;
Result. Im=LHS. Im - RHS. Im;
return result;
}

Public static Complex operator * (Complex LHS, Complex RHS)
{
Complex result=new Complex ();
Result. The re=LHS. Re * RHS in re;
Result. Im=LHS. Im * RHS in im.
return result;
}

Public static Complex operator * (float LHS, Complex RHS)
{
Complex result=new Complex ();
Result. The re=LHS * RHS. Re;
Result. Im=LHS * RHS. Im;
return result;
}

Public static Complex operator * (Complex LHS, float RHS)
{
Complex result=new Complex ();
Result. The re=LHS. Re * RHS;
Result. Im=LHS. Im * RHS;
return result;
}
}
}

  •  Tags:  
  • C#
  • Related