修改一下主楼的代码(转录错误,根据网上原稿改正,原稿是不让复制的):
蝶形算法代码
Sub 蝶形算法(xr() As Double)
Dim l As Long, le As Long, le1 As Long, n As Long, r As Long, p As Long, q As Long, m As Byte
Dim wr As Double, w1 As Double, wlr As Double, wl1 As Double, tr As Double, t1 As Double
Dim pi As Double, t As Double
Dim xi()
n = UBound(xr) '求数组大小,其值必须是2的幂
m = 0
l = 2
pi = 3.14159265358979
Do
l = l + l
m = m + 1
Loop Until l > n
n = l / 2
ReDim xi(n - 1)
l = 1
Do
le = 2 ^ l
le1 = le / 2
wr = 1
wi = 0
t=pi/le1 '这一步缺少t值,补上了
w1r = Cos(t)
w1i = -Sin(t)
r = 0
Do
p = r
Do
q = p + le1
tr = xr(q) * wr - xi(q) * wi
ti = xr(q) * wi + xi(q) * wr
xr(q) = xr(p) - tr
xi(q) = xi(p) - ti
xr(p) = xr(p) + tr
xi(p) = xi(p) + ti
p = p + le
Loop Until p > n - 1
wr = wr * w1r - wi * w1i
wi = wr * w1i + wi * w1r '这一步用+法
r = r + 1
Loop Until r > le1 - 1
l = l + 1
Loop Until l > m
For i = 0 To n - 1 '仅输出模
xr(i) = Sqr(xr(i) ^ 2 + xi(i) ^ 2)
Next
End Sub
蝶形算法代码
Sub 蝶形算法(xr() As Double)
Dim l As Long, le As Long, le1 As Long, n As Long, r As Long, p As Long, q As Long, m As Byte
Dim wr As Double, w1 As Double, wlr As Double, wl1 As Double, tr As Double, t1 As Double
Dim pi As Double, t As Double
Dim xi()
n = UBound(xr) '求数组大小,其值必须是2的幂
m = 0
l = 2
pi = 3.14159265358979
Do
l = l + l
m = m + 1
Loop Until l > n
n = l / 2
ReDim xi(n - 1)
l = 1
Do
le = 2 ^ l
le1 = le / 2
wr = 1
wi = 0
t=pi/le1 '这一步缺少t值,补上了
w1r = Cos(t)
w1i = -Sin(t)
r = 0
Do
p = r
Do
q = p + le1
tr = xr(q) * wr - xi(q) * wi
ti = xr(q) * wi + xi(q) * wr
xr(q) = xr(p) - tr
xi(q) = xi(p) - ti
xr(p) = xr(p) + tr
xi(p) = xi(p) + ti
p = p + le
Loop Until p > n - 1
wr = wr * w1r - wi * w1i
wi = wr * w1i + wi * w1r '这一步用+法
r = r + 1
Loop Until r > le1 - 1
l = l + 1
Loop Until l > m
For i = 0 To n - 1 '仅输出模
xr(i) = Sqr(xr(i) ^ 2 + xi(i) ^ 2)
Next
End Sub