L
ll710915
Unregistered / Unconfirmed
GUEST, unregistred user!
我最近用delphi做了一fft的测试程序,以sin为测试对象,大家知道无论多少点的FFT,对sin
而言只有两个值,我根据算法编了一程序,检查了很多编,但结果是只有8点是对的,16点、32点、64点
128点都不对,但图形关于N/2对称,这是什么原因啊?请帮忙查找原因!现将代码贴出:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,math, Buttons, TeEngine, Series, ExtCtrls, TeeProcs,
Chart;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
Chart1: TChart;
Series1: TBarSeries;
BitBtn1: TBitBtn;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
var
xr,xi:array[0..31] of real;
rcf,icf:array[0..15] of real;//旋转因子
B, P:integer;
rpartKB,ipartKB:real;//
kk:array[0..31] of real;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var LH,N1,n,i,j,k,o:integer;
t:real;
begin
n:=32 ;
LH:=n div 2;
j:=LH;
N1:=N-2;
for i:=0 to n-1 do
begin
//xr:=0;
xr:=sin(i*3.1415926535*2/n);
//memo1.Text:=memo1.Text+'xr('+inttostr(i+1)+')'+'='+floattostr(xr)+';';
xi:=0;
end;
//xr[1]:=1;
//xr[0]:=1;
//xr[2]:=1;
//xr[3]:=1;
for i:=1 to N1 do //倒序
begin
if i<j then
begin
t:=xr;
xr:=xr[j];
xr[j]:=t;
end;
k:=LH;
if (j>=k) then
begin
j:=j-k;
k:=(k div 2);
end;
j:=j+k;
end;
for o:=0 to 15 do
begin
rCf[o]:=cos(o*3.14159*2/n);
iCf[o]:=sin(o*3.14159*2/n);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var l, i,j,k,tme,n, m:integer;
begin
n:=32;
m:=5;
for L:=1 to m do
begin
B:=trunc((power(2,(L-1))+0.5));
for j:=0 to B-1 do
begin
P:=J*(trunc(power(2,(M-L))+0.5));
memo2.Text:=memo2.Text+inttostr(p)+' ';
k:=j;
while (k<=n-1) do
begin
rPartKB:=xr[k+B]*rCf[P]-xi[k+B]*iCf[P];
iPartKB:=xi[k+B]*rCf[P]+xr[k+B]*iCf[P];
xr[k+B]:=xr[k]-rPartKB;
xi[k+B]:=xi[k]-iPartKB;
xr[k]:=xr[k]+rPartKB;
xi[k]:=xi[k]+iPartKB;
k:=k+trunc(power(2,L)+0.5);
end;
end;
end;
for i:= 0 to 31 do
kk:=sqrt(xr*xr+xi*xi);
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var i:integer;
begin
for i:=0 to 31 do
series1.Add(kk,'',clred);
end;
end.
而言只有两个值,我根据算法编了一程序,检查了很多编,但结果是只有8点是对的,16点、32点、64点
128点都不对,但图形关于N/2对称,这是什么原因啊?请帮忙查找原因!现将代码贴出:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,math, Buttons, TeEngine, Series, ExtCtrls, TeeProcs,
Chart;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
Chart1: TChart;
Series1: TBarSeries;
BitBtn1: TBitBtn;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
var
xr,xi:array[0..31] of real;
rcf,icf:array[0..15] of real;//旋转因子
B, P:integer;
rpartKB,ipartKB:real;//
kk:array[0..31] of real;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var LH,N1,n,i,j,k,o:integer;
t:real;
begin
n:=32 ;
LH:=n div 2;
j:=LH;
N1:=N-2;
for i:=0 to n-1 do
begin
//xr:=0;
xr:=sin(i*3.1415926535*2/n);
//memo1.Text:=memo1.Text+'xr('+inttostr(i+1)+')'+'='+floattostr(xr)+';';
xi:=0;
end;
//xr[1]:=1;
//xr[0]:=1;
//xr[2]:=1;
//xr[3]:=1;
for i:=1 to N1 do //倒序
begin
if i<j then
begin
t:=xr;
xr:=xr[j];
xr[j]:=t;
end;
k:=LH;
if (j>=k) then
begin
j:=j-k;
k:=(k div 2);
end;
j:=j+k;
end;
for o:=0 to 15 do
begin
rCf[o]:=cos(o*3.14159*2/n);
iCf[o]:=sin(o*3.14159*2/n);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var l, i,j,k,tme,n, m:integer;
begin
n:=32;
m:=5;
for L:=1 to m do
begin
B:=trunc((power(2,(L-1))+0.5));
for j:=0 to B-1 do
begin
P:=J*(trunc(power(2,(M-L))+0.5));
memo2.Text:=memo2.Text+inttostr(p)+' ';
k:=j;
while (k<=n-1) do
begin
rPartKB:=xr[k+B]*rCf[P]-xi[k+B]*iCf[P];
iPartKB:=xi[k+B]*rCf[P]+xr[k+B]*iCf[P];
xr[k+B]:=xr[k]-rPartKB;
xi[k+B]:=xi[k]-iPartKB;
xr[k]:=xr[k]+rPartKB;
xi[k]:=xi[k]+iPartKB;
k:=k+trunc(power(2,L)+0.5);
end;
end;
end;
for i:= 0 to 31 do
kk:=sqrt(xr*xr+xi*xi);
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var i:integer;
begin
for i:=0 to 31 do
series1.Add(kk,'',clred);
end;
end.