主题:做显示读出信号和计算出频谱的界面
FFT算法程序已经实现,但是要做显示读出信号和计算出频谱的界面。
using System;
using System.IO;
namespace FFT
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class FFT
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
public static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
FileStream fsr,fsw1,fsw2;
fsr=new FileStream("D:/Visual Studio Projects/FFT/sh.dat",FileMode.Open);//读入数据
fsw1=new FileStream("D:/Visual Studio Projects/FFT/shw1.dat",FileMode.CreateNew);//写入模的数据
fsw2=new FileStream("D:/Visual Studio Projects/FFT/shw2.dat",FileMode.CreateNew);//写入幅角数据
BinaryReader rread= new BinaryReader(fsr);
BinaryWriter wwrite1 = new BinaryWriter(fsw1);
BinaryWriter wwrite2 = new BinaryWriter(fsw2);
//文件流
int n=64,k=6;//输入的点数,K满足n=2~k
double[] pr=new double[n]; //采样输入的实部,返回存放FFT的模
double[] pi=new double[n]; //采样输入的虚部,返回存放FFT的幅角
double[] fr=new double[n]; //返回FFT的实部
double[] fi=new double[n]; //返回FFT的虚部
int it,m,iss,i,j,nv,l0;
double p,q,s,vr,vi,poddr,poddi;
for (int h=0;h<n;h++)
{
pr[h]=rread.ReadInt16();
}
for (it=0; it<=n-1; it++)
{ m=it; iss=0;
for (i=0; i<=k-1; i++)
{ j=m/2; iss=2*iss+(m-2*j); m=j;}
fr[it]=pr[iss]; fi[it]=pi[iss];
}
pr[0]=1.0; pi[0]=0.0;
p=6.283185306/(1.0*n);
pr[1]=System.Math.Cos(p); pi[1]=-System.Math.Sin(p);
for (i=2; i<=n-1; i++)
{
p=pr[i-1]*pr[1]; q=pi[i-1]*pi[1];
s=(pr[i-1]+pi[i-1])*(pr[1]+pi[1]);
pr[i]=p-q; pi[i]=s-p-q;
}
for (it=0; it<=n-2; it=it+2)
{
vr=fr[it]; vi=fi[it];
fr[it]=vr+fr[it+1]; fi[it]=vi+fi[it+1];
fr[it+1]=vr-fr[it+1]; fi[it+1]=vi-fi[it+1];
}
m=n/2; nv=2;
for (l0=k-2; l0>=0; l0--)
{
m=m/2; nv=2*nv;
for (it=0; it<=(m-1)*nv; it=it+nv)
for (j=0; j<=(nv/2)-1; j++)
{
p=pr[m*j]*fr[it+j+nv/2];
q=pi[m*j]*fi[it+j+nv/2];
s=pr[m*j]+pi[m*j];
s=s*(fr[it+j+nv/2]+fi[it+j+nv/2]);
poddr=p-q; poddi=s-p-q;
fr[it+j+nv/2]=fr[it+j]-poddr;
fi[it+j+nv/2]=fi[it+j]-poddi;
fr[it+j]=fr[it+j]+poddr;
fi[it+j]=fi[it+j]+poddi;
}
}//FFT变换
for (i=0; i<=n-1; i++)
{
fr[i]=fr[i]/(1.0*n);
fi[i]=fi[i]/(1.0*n);
}
for (i=0; i<=n-1; i++)
{
pr[i]=System.Math.Sqrt(fr[i]*fr[i]+fi[i]*fi[i]);
if (System.Math.Abs(fr[i])<0.000001*System.Math.Abs(fi[i]))
{
if ((fi[i]*fr[i])>0) pi[i]=90.0;
else pi[i]=-90.0;
}
else
pi[i]=System.Math.Atan(fi[i]/fr[i])*360.0/6.283185306;//计算模和幅角
}
for(int h=0;h<n;h++)
{
wwrite1.Write((double)(pr[h]));//模的保存
wwrite2.Write((double)(pi[h]));//幅角的保存
}
fsr.Close();
fsw1.Close();
fsw2.Close();
}
}
}
using System;
using System.IO;
namespace FFT
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class FFT
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
public static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
FileStream fsr,fsw1,fsw2;
fsr=new FileStream("D:/Visual Studio Projects/FFT/sh.dat",FileMode.Open);//读入数据
fsw1=new FileStream("D:/Visual Studio Projects/FFT/shw1.dat",FileMode.CreateNew);//写入模的数据
fsw2=new FileStream("D:/Visual Studio Projects/FFT/shw2.dat",FileMode.CreateNew);//写入幅角数据
BinaryReader rread= new BinaryReader(fsr);
BinaryWriter wwrite1 = new BinaryWriter(fsw1);
BinaryWriter wwrite2 = new BinaryWriter(fsw2);
//文件流
int n=64,k=6;//输入的点数,K满足n=2~k
double[] pr=new double[n]; //采样输入的实部,返回存放FFT的模
double[] pi=new double[n]; //采样输入的虚部,返回存放FFT的幅角
double[] fr=new double[n]; //返回FFT的实部
double[] fi=new double[n]; //返回FFT的虚部
int it,m,iss,i,j,nv,l0;
double p,q,s,vr,vi,poddr,poddi;
for (int h=0;h<n;h++)
{
pr[h]=rread.ReadInt16();
}
for (it=0; it<=n-1; it++)
{ m=it; iss=0;
for (i=0; i<=k-1; i++)
{ j=m/2; iss=2*iss+(m-2*j); m=j;}
fr[it]=pr[iss]; fi[it]=pi[iss];
}
pr[0]=1.0; pi[0]=0.0;
p=6.283185306/(1.0*n);
pr[1]=System.Math.Cos(p); pi[1]=-System.Math.Sin(p);
for (i=2; i<=n-1; i++)
{
p=pr[i-1]*pr[1]; q=pi[i-1]*pi[1];
s=(pr[i-1]+pi[i-1])*(pr[1]+pi[1]);
pr[i]=p-q; pi[i]=s-p-q;
}
for (it=0; it<=n-2; it=it+2)
{
vr=fr[it]; vi=fi[it];
fr[it]=vr+fr[it+1]; fi[it]=vi+fi[it+1];
fr[it+1]=vr-fr[it+1]; fi[it+1]=vi-fi[it+1];
}
m=n/2; nv=2;
for (l0=k-2; l0>=0; l0--)
{
m=m/2; nv=2*nv;
for (it=0; it<=(m-1)*nv; it=it+nv)
for (j=0; j<=(nv/2)-1; j++)
{
p=pr[m*j]*fr[it+j+nv/2];
q=pi[m*j]*fi[it+j+nv/2];
s=pr[m*j]+pi[m*j];
s=s*(fr[it+j+nv/2]+fi[it+j+nv/2]);
poddr=p-q; poddi=s-p-q;
fr[it+j+nv/2]=fr[it+j]-poddr;
fi[it+j+nv/2]=fi[it+j]-poddi;
fr[it+j]=fr[it+j]+poddr;
fi[it+j]=fi[it+j]+poddi;
}
}//FFT变换
for (i=0; i<=n-1; i++)
{
fr[i]=fr[i]/(1.0*n);
fi[i]=fi[i]/(1.0*n);
}
for (i=0; i<=n-1; i++)
{
pr[i]=System.Math.Sqrt(fr[i]*fr[i]+fi[i]*fi[i]);
if (System.Math.Abs(fr[i])<0.000001*System.Math.Abs(fi[i]))
{
if ((fi[i]*fr[i])>0) pi[i]=90.0;
else pi[i]=-90.0;
}
else
pi[i]=System.Math.Atan(fi[i]/fr[i])*360.0/6.283185306;//计算模和幅角
}
for(int h=0;h<n;h++)
{
wwrite1.Write((double)(pr[h]));//模的保存
wwrite2.Write((double)(pi[h]));//幅角的保存
}
fsr.Close();
fsw1.Close();
fsw2.Close();
}
}
}