我有以下代码使用 Swift 和 Accelerate 在坡道上进行 DFT
import Foundation
import Accelerate
let N = 16
var xdtar = UnsafeMutablePointer<Double>.allocate(capacity: N)
var xdtai = UnsafeMutablePointer<Double>.allocate(capacity: N)
xdtar.initialize(to: 0.0, count: N )
xdtai.initialize(to: 0.0, count: N )
var x = DSPDoubleSplitComplex(realp: xdtar, imagp: xdtai)
var ydtar = UnsafeMutablePointer<Double>.allocate(capacity: N)
var ydtai = UnsafeMutablePointer<Double>.allocate(capacity: N)
ydtar.initialize(to: 0.0, count: N )
ydtai.initialize(to: 0.0, count: N )
var y = DSPDoubleSplitComplex(realp: ydtar, imagp: ydtai)
for i in 0..<N {
xdtar[i] = Double(i)
}
let setup = vDSP_DFT_zop_CreateSetupD(nil, vDSP_Length(N),
vDSP_DFT_Direction.FORWARD)
vDSP_DFT_ExecuteD(setup!, x.realp, x.imagp, y.realp, y.imagp)
vDSP_DFT_DestroySetupD(setup)
for i in 0..<N {
print(String(format: "%2d \t-> in \t(%5.4f, %5.4fi)\t out \t(%5.4f, %5.4fi)",
i, xdtar[i], xdtai[i], ydtar[i], ydtai[i]))
}
xdtar.deinitialize(count: N); xdtar.deallocate(capacity: N)
xdtai.deinitialize(count: N); xdtai.deallocate(capacity: N)
ydtar.deinitialize(count: N); ydtar.deallocate(capacity: N)
ydtai.deinitialize(count: N); ydtai.deallocate(capacity: N)
输出是
0 -> in (0.0000, 0.0000i) out (120.0000, 0.0000i)
1 -> in (1.0000, 0.0000i) out (-8.0000, 40.2187i)
2 -> in (2.0000, 0.0000i) out (-8.0000, 19.3137i)
3 -> in (3.0000, 0.0000i) out (-8.0000, 11.9728i)
4 -> in (4.0000, 0.0000i) out (-8.0000, 8.0000i)
5 -> in (5.0000, 0.0000i) out (-8.0000, 5.3454i)
6 -> in (6.0000, 0.0000i) out (-8.0000, 3.3137i)
7 -> in (7.0000, 0.0000i) out (-8.0000, 1.5913i)
8 -> in (8.0000, 0.0000i) out (-8.0000, 0.0000i)
9 -> in (9.0000, 0.0000i) out (-8.0000, -1.5913i)
10 -> in (10.0000, 0.0000i) out (-8.0000, -3.3137i)
11 -> in (11.0000, 0.0000i) out (-8.0000, -5.3454i)
12 -> in (12.0000, 0.0000i) out (-8.0000, -8.0000i)
13 -> in (13.0000, 0.0000i) out (-8.0000, -11.9728i)
14 -> in (14.0000, 0.0000i) out (-8.0000, -19.3137i)
15 -> in (15.0000, 0.0000i) out (-8.0000, -40.2187i)
我认为这是正确的。以上是复杂到复杂的公式。基本上检查下面的代码,这是一个真正到复杂的 FFT。
import Foundation
import Accelerate
let N = 16
let log2N = vDSP_Length(4)
var xdtar = UnsafeMutablePointer<Double>.allocate(capacity: N)
xdtar.initialize(to: 0.0, count: N )
var x = DSPDoubleSplitComplex(realp: xdtar, imagp: xdtar + N/2)
var ydtar = UnsafeMutablePointer<Double>.allocate(capacity: N/2)
var ydtai = UnsafeMutablePointer<Double>.allocate(capacity: N/2)
ydtar.initialize(to: 0.0, count: N )
ydtai.initialize(to: 0.0, count: N )
var y = DSPDoubleSplitComplex(realp: ydtar, imagp: ydtai)
for i in 0..<N {
xdtar[i] = Double(i)
}
let setup = vDSP_create_fftsetupD(log2N, Int32(2))
vDSP_fft_zropD(setup!, &x, vDSP_Stride(1), &y, vDSP_Stride(1), log2N, Int32(1))
vDSP_destroy_fftsetupD(setup)
for i in 0..<N/2 {
print(String(format: "%2d \t-> in \t(%5.4f, %5.4fi)\t out \t(%5.4f, %5.4fi)",
i, x.realp[i], x.imagp[i], ydtar[i], ydtai[i]))
}
xdtar.deinitialize(count: N); xdtar.deallocate(capacity: N)
ydtar.deinitialize(count: N/2); ydtar.deallocate(capacity: N/2)
ydtai.deinitialize(count: N/2); ydtai.deallocate(capacity: N/2)
它的输出是
0 -> in (0.0000, 8.0000i) out (240.0000, -128.0000i)
1 -> in (1.0000, 9.0000i) out (-8.0000, 40.2187i)
2 -> in (2.0000, 10.0000i) out (-8.0000, 19.3137i)
3 -> in (3.0000, 11.0000i) out (-8.0000, 11.9728i)
4 -> in (4.0000, 12.0000i) out (-8.0000, 8.0000i)
5 -> in (5.0000, 13.0000i) out (-8.0000, 5.3454i)
6 -> in (6.0000, 14.0000i) out (-8.0000, 3.3137i)
7 -> in (7.0000, 15.0000i) out (-8.0000, 1.5913i)
所以在我看来,条目 0 中的打包输出应该是 120,-8,但事实并非如此。有人对这里发生的事情有任何建议吗?我正在探索这些功能,因此很容易出错,但足够多的输出是正确的,以至于我不理解打包的条目。