ios - DFT 结果使用来自 Swift 的 vDSP 来自实值输入(Surge 实现)

标签 ios swift signal-processing fft frequency

我在 DFT 函数的 Surge 实现中产生了不兼容的结果;我正在使用 Surge 实现,这对我来说似乎很简单。

当我计算虚拟数据的幅度时,我得到:

var xx = [1.0,0.0,1.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0]

{0.6, 0.082842712474619, 0.2, 0.482842712474619, 0.2, 0.482842712474619, 0.2, 0.082842712474619, 0.0, 0.2}

如果它在一个域中是实数,则它在另一个域中是共轭对称的”之后似乎是错误的

当我做同样的事情时:

在 Mathematica 中:

x = {1., 0., 1., 0., 0., 1., 0., 0., 0., 1.}
Norm /@ Fourier[x, FourierParameters -> {-1, 1}]

{0.4, 0.117557, 0.154336, 0.190211, 0.214896, 0., 0.214896, 0.190211, 0.154336, 0.117557}

在 R 中:

x = c(1,0,1,0,0,1,0,0,0,1)
abs(fft(x)/length(x))

"0.400000" "0.117557" "0.154336" "0.190211" "0.214896" "0.000000" "0.214896" "0.190211" "0.154336" "0.117557"

同时使用 JTransforms:

double[] x = { 1., 0., 1., 0., 0., 1., 0., 0., 0., 1. };
double[] output = new double[x.length];
DoubleFFT_1D fftDo = new DoubleFFT_1D(x.length);
double[] fft = new double[x.length * 2];
System.arraycopy(x, 0, fft, 0, x.length);
fftDo.realForwardFull(fft);

0.4 0.11755705045849463 0.1543361918426817 0.19021130325903074 0.21489611417496346 0.0 0.21489611417496346 0.19021130325903074 0.1543361918426817 0.11755705045849463

Surge 中的 Swift 实现是:

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
} 

我是否漏掉了一些明显的东西?

谢谢

最佳答案

看起来您的 Swift/vDSP 代码假定的长度是 2 的幂。如果您尝试使用 N = 16 而不是 N = 10,那么您应该会得到正确的结果,该结果将与其他 FFT 实现相匹配。

许多 FFT 实现仅支持 2 的幂长度 - 当您需要使用不同的大小时,典型的方法是用零填充输入数据直到下一个 2 的幂。这给出了有效的结果,尽管频域数据被有效地插值到更大的点数(它似乎具有比没有实际数据点提供的频率分辨率更高的频率分辨率,但这只是插值的结果)。一个额外的好处是,当使用 2 的幂时,FFT 实现通常是最有效的,并且使用 2 的填充幂 FFT 通常比更短的任意大小更快。

关于ios - DFT 结果使用来自 Swift 的 vDSP 来自实值输入(Surge 实现),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39615176/

相关文章:

c - FFT 频率区间和 PIC32

audio - 如何实时处理音频?

iphone - 完全困惑在哪里更改 openCV 中的代码

ios - 如何更新 UIPageViewController 页面控件计数

ios - 设置accessibilityElementsHidden是否递归工作?

ios - 更改 Root View Controller 不会在同一窗口上释放以前的 View Controller

ios - CollectionView 在新帖子后复制数据

ios - Firebase 通知未在 iOS 应用程序中显示警报或横幅和角标(Badge)应用程序图标

signal-processing - 高通DSP : hexagon-sim with command line arguments

ios - CGRect 在 iOS 中与 Rect() 相交