DFT result in Swift is different than that of MATLAB

前端 未结 2 1080
别那么骄傲
别那么骄傲 2021-01-21 06:32
import Cocoa
import Accelerate

let filePath = Bundle.main.path(forResource: \"sinusoid\", ofType: \"txt\")
let contentData = FileManager.default.contents(atPath: filePa         


        
相关标签:
2条回答
  • 2021-01-21 07:05

    The lengths of your 2 FFT are different, and so, of course the results won't match. You are also passing different amounts of data to your 2 FFTs.

    Print out the FFT lengths and the input data vector to debug your code. Make sure the inputs match before comparing results.

    Also, Apple's Accelerate/vDSP FFTs can use lengths other than just powers of 2 (lengths with factors of 3 or 5 are also allowed).

    Also, note that Matlab indexes arrays starting at 1, not 0, as is more typical in C and Swift functions.

    0 讨论(0)
  • 2021-01-21 07:10

    Indeed, the problem with the FFT result mismatch was due to the input size mismatch. Restricting input to be specific multiples of powers of 2 greatly constrains the FFT usage in the Accelerate framework. One suggestion was to pad the input with 0s until it is of an appropriate length. Whether you pad with 0s or truncate the input such that the size is a specific multiple of a power of 2, the results from the Accelerate framework will be different than the results from a program such as MATLAB. The solution to this is to perform a chirp-z transform as mentioned in a link specified by Martin R. The chirp-z transform itself yields identical results to the FFT and may be performed on inputs of arbitrary size.

    0 讨论(0)
提交回复
热议问题