【发布时间】:2019-08-02 08:39:55
【问题描述】:
我正在为 CoreML 开发特征工程管道,我需要对我的数据执行 FFT。问题是Accelerate框架的结果和NumPy FFT的结果不一样。
斯威夫特:
public func fft(_ input: [Double]) -> [Double] {
var real = [Double](input)
var imaginary = [Double](repeating: 0.0, count: input.count)
var splitComplex = DSPDoubleSplitComplex(realp: &real, imagp: &imaginary)
let length = vDSP_Length(floor(log2(Float(input.count))))
let radix = FFTRadix(kFFTRadix2)
let weights = vDSP_create_fftsetupD(length, radix)
vDSP_fft_zipD(weights!, &splitComplex, 1, length, FFTDirection(FFT_FORWARD))
var magnitudes = [Double](repeating: 0.0, count: input.count)
vDSP_zvmagsD(&splitComplex, 1, &magnitudes, 1, vDSP_Length(input.count))
var normalizedMagnitudes = [Double](repeating: 0.0, count: input.count)
vDSP_vsmulD(sqrt(magnitudes), 1, [2.0 / Double(input.count)], &normalizedMagnitudes, 1, vDSP_Length(input.count))
vDSP_destroy_fftsetupD(weights)
return normalizedMagnitudes
}
Python:
def fft(series: pd.Series):
f = np.fft.fft(series)
fa = np.abs(f)
return pd.Series(fa)
我对每种方法都使用相同的 100 个值。
我猜这与规范化部分有关,但我什至不确定两个数组是否包含相同的内容,例如:
- 索引 0:零频率项
- 索引 1-50:正幅度
- 索引 50-99:负震级
我只对正数感兴趣。
编辑:
这是 NumPy 图:
这是加速情节:
希望有人可以帮忙:)
【问题讨论】:
标签: python swift numpy fft coreml