#include <stdio.h>
#define N 8
void kkfft(double pr[], double pi[], int n, int k, double fr[], double fi[], int l, int il)
void main()
{
double xr[N],xi[N],Yr[N],Yi[N],l=0,il=0
int i,j,n=N,k=3
for(i=0i<Ni++)
{
xr[i]=i
xi[i]=0
}
printf("------FFT------\n")
l=0
kkfft(xr,xi,n,k,Yr,Yi,l,il)
for(i=0i<Ni++)
{
printf("%-11lf + j* %-11lf\n",Yr[i],Yi[i])
}
printf("-----DFFT-------\n")
l=1
kkfft(Yr,Yi,n,k,xr,xi,l,il)
for(i=0i<Ni++)
{
printf("%-11lf + j* %-11lf\n",xr[i],xi[i])
}
getch()
}
void kkfft(double pr[], double pi[], int n, int k, double fr[], double fi[], int l, int il)
{
int it,m,is,i,j,nv,l0
double p,q,s,vr,vi,poddr,poddi
for (it=0it<=n-1it++)
{
m = it
is = 0
for(i=0i<=k-1i++)
{
j = m/2
is = 2*is+(m-2*j)
m = j
}
fr[it] = pr[is]
fi[it] = pi[is]
}
pr[0] = 1.0
pi[0] = 0.0
p = 6.283185306/(1.0*n)
pr[1] = cos(p)
pi[1] = -sin(p)
if (l!=0)
pi[1]=-pi[1]
for (i=2i<=n-1i++)
{
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=0it<=n-2it=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-2l0>=0l0--)
{
m = m/2
nv = 2*nv
for(it=0it<=(m-1)*nvit=it+nv)
for (j=0j<=(nv/2)-1j++)
{
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
}
}
/*逆傅立叶变换*/
if(l!=0)
{
for(i=0i<=n-1i++)
{
fr[i] = fr[i]/(1.0*n)
fi[i] = fi[i]/(1.0*n)
}
}
/*是否计算模和相角*/
if(il!=0)
{
for(i=0i<=n-1i++)
{
pr[i] = sqrt(fr[i]*fr[i]+fi[i]*fi[i])
if(fabs(fr[i])<0.000001*fabs(fi[i]))
{
if ((fi[i]*fr[i])>0)
pi[i] = 90.0
else
pi[i] = -90.0
}
else
pi[i] = atan(fi[i]/fr[i])*360.0/6.283185306
}
}
return
}
用MATLAB进行傅里叶变换用fft()函数来变换,其逆变换用ifft()函数来变换。变换要求X为向量,而不是变量。
根据题主的代码应这样来处理。
>>t=-pi:pi/100:pi
>>x=sin(2*pi*t)
>>y=fft(x) %傅里叶变换
>>plot(abs(y))
x=ifft(y)%傅里叶逆变换
>>plot(t,x)
I0=zeros(256,256)I0(120:130,100:156)=1
subplot(2,3,1),imshow(I0),title('原始图像')
subplot(2,2,2),imshow(log(1+abs(fft2(I0)))),title('直接进行fft2')
I1=I0
F1=fft(I1,[],1)%按列进行傅里叶变换
subplot(2,2,3),imshow(log(1+abs(F1))),title('先按列进行')
F2=fft(F1,[],2)%按行进行傅里叶变换
subplot(2,2,4),imshow(log(1+abs(F2))),title('后按行进行')
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)