gpt4 book ai didi

ios - Apple FFT 给出不一致的结果

转载 作者:行者123 更新时间:2023-12-02 10:13:20 30 4
gpt4 key购买 nike

我使用原生 Apple 类实现了 FFT 算法。我直接从他们的网站上获取了代码:

https://developer.apple.com/documentation/accelerate/vdsp/fast_fourier_transforms/finding_the_component_frequencies_in_a_composite_sine_wave

尽管如此,当我运行代码时,它每次都会提供不同的结果。我创建了一个单元测试,它重复运行它并比较单元测试失败时结果是否相同。我唯一的猜测是这是一个内存问题。这是我能想象每次结果都会不同的唯一方法。

import Foundation
import Accelerate

class AppleFFT{
var windowSize = 512
var n = vDSP_Length(512)
var halfN = Int(512 / 2)
var fftSetUp : FFTSetup?
var log2n : vDSP_Length?
init(windowSize: Int){
self.windowSize = windowSize
n = vDSP_Length(windowSize)
halfN = Int(n / 2)
initialize()
}
private init(){
initialize()
}
func initialize(){
log2n = vDSP_Length(log2(Float(n)))
if log2n == nil { return }
fftSetUp = vDSP_create_fftsetup(log2n!, FFTRadix(kFFTRadix2))

}
func process(signal : [Float], n: vDSP_Length) ->DSPSplitComplex{
let window = vDSP.window(ofType: Float.self,
usingSequence: .hanningDenormalized,
count: Int(n),
isHalfWindow: false)

let signal2 = vDSP.multiply(signal, window)
let observed: [DSPComplex] = stride(from: 0, to: Int(n), by: 2).map {
return DSPComplex(real: signal[$0],
imag: signal[$0.advanced(by: 1)])
}

var forwardInputReal = [Float](repeating: 0, count: halfN)
var forwardInputImag = [Float](repeating: 0, count: halfN)

var forwardInput = DSPSplitComplex(realp: &forwardInputReal,
imagp: &forwardInputImag)

vDSP_ctoz(observed, 2,
&forwardInput, 1,
vDSP_Length(halfN))

//Create some empty arrays we can put data into
var forwardOutputReal = [Float](repeating: 0, count: halfN)
var forwardOutputImag = [Float](repeating: 0, count: halfN)
var forwardOutput = DSPSplitComplex(realp: &forwardOutputReal,
imagp: &forwardOutputImag)

//Perform actual fft, placing results in forwardOutput
vDSP_fft_zrop(fftSetUp!,
&forwardInput, 1,
&forwardOutput, 1,
log2n!,
FFTDirection(kFFTDirection_Forward))

//Do cheap analysis to figure out original frequencies
let componentFrequencies = forwardOutputImag.enumerated().filter {
$0.element < -1
}.map {
return $0.offset
}
return forwardOutput
}
}

import XCTest
import Accelerate

class testAppleFFT: XCTestCase {

func testFFTConsistency(){
let signal = genSignalWith(frequencies:[100, 500], numSamples: 512, sampleRate: 44100)
let fft = AppleFFT(windowSize: 512)
let complex1 = fft.process(signal: signal , n: 512)
for i in 0..<10{
print("i = \(i)")
let complex2 = fft.process(signal: signal, n: 512)
var complex1realp = complex1.realp
var complex1imagp = complex1.imagp
var complex2realp = complex2.realp
var complex2imagp = complex2.imagp
for j in 0..<512 {
let r1 = complex1realp.pointee
let i1 = complex1imagp.pointee
let r2 = complex2realp.pointee
let i2 = complex2imagp.pointee
XCTAssert(abs(r1 - r2) < 0.00001)
XCTAssert(abs(i1 - i2) < 0.00001)
if !(abs(r1 - r2) < 0.00001){
print(" error: i: \(i) j: \(j) r1: \(r1) r2: \(r2)")
}
if !(abs(i1 - i2) < 0.00001){
print(" error: index: \(i) i1: \(i1) i2: \(i2)")
}
complex1realp = complex1realp.advanced(by: 1)
complex1imagp = complex1imagp.advanced(by: 1)
complex2realp = complex2realp.advanced(by: 1)
complex2imagp = complex2imagp.advanced(by: 1)

}
}
}
func genSignalWith(frequencies: [Float], numSamples: Int, sampleRate: Float, amplitudes: [Float] = []) -> [Float]{
var sig : [Float] = []
for t in 0..<numSamples{
var sum : Float = 0.0
for i in 0..<frequencies.count{
let f = frequencies[i]
var a : Float = 1.0
if(amplitudes.count > i){
a = amplitudes[i]
}
let thisValue = sin(Float(t) / sampleRate * 2 * .pi * f)
sum += thisValue
}
sig.append(sum)
}
return sig
}
}

最佳答案

这个:

var forwardInput = DSPSplitComplex(realp: &forwardInputReal,
imagp: &forwardInputImag)
vDSP_ctoz(observed, 2, &forwardInput, 1, vDSP_Length(halfN))

没有做你想做的事。它的问题有点微妙,特别是如果您有 C 或 C++ 背景。 swift 中的数组与 C 或 C++ 中的数组不同;特别是,它们在内存中没有固定地址。它们是 Swift 可能选择移动的对象。当您在 Swift 中工作时,这很好,但有时当您需要与 C 函数交互时(尤其是想要在函数调用之间保留指针的 C 类型,正如您所注意到的那样),有时会带来痛苦。

当您调用DSPSplitComplex(realp: &forwardInputReal, ...)时,&隐式创建 UnsafeMutablePointer<Float>纪念forwardInputReal ,但是该指针仅在调用 init 期间有效。当您经过 forwardInputvDSP_ctoz ,指针已经超出范围并且不再有效,因此您正在调用未定义的行为。特别是,编译器可以假设对 vDSP_ctoz 的调用不修改 forwardInputReal 的内容或forwardInputImag ,因为该函数没有收到指向其内容的有效指针。

解决这个问题的最佳方法是更加明确:

forwardInputReal.withUnsafeMutableBufferPointer { r in
forwardInputImag.withUnsafeMutableBufferPointer { i in
var splitComplex = DSPSplitComplex(realp: r.baseAddress!, imagp: i.baseAddress!)
vDSP_ctoz(observed, 2, &splitComplex, 1, vDSP_Length(halfN))
}
}
// forwardInput[Real,Imag] now contain the de-interleaved data.
// splitComplex is out-of-scope and cannot be used, so the invalid pointers
// are discarded.

有一些事情可以让这件事变得更容易。

首先,有一个 change coming to the Swift compiler这将为您诊断此错误。

其次,我们可以将我展示的小舞蹈包装成一些方便的函数:

/// De-interleave the real and imaginary parts of a complex buffer into two
/// new Float arrays.
func ctoz<T>(_ data: T) -> (real: [Float], imag: [Float])
where T: AccelerateBuffer, T.Element == DSPComplex {
var imag = [Float]()
let real = [Float](unsafeUninitializedCapacity: data.count) { r, n in
imag = [Float](unsafeUninitializedCapacity: data.count) { i, n in
ctoz(data, real: &r, imag: &i)
n = data.count
}
n = data.count
}
return (real, imag)
}

/// De-interleave the real and imaginary parts of a complex buffer into two
/// caller-provided Float buffers.
///
/// - Precondition: data, real, and imag must all have the same length.
func ctoz<T, U, V>(_ data: T, real: inout U, imag: inout V)
where T: AccelerateBuffer, T.Element == DSPComplex,
U: AccelerateMutableBuffer, U.Element == Float,
V: AccelerateMutableBuffer, V.Element == Float
{
precondition(data.count == real.count && data.count == imag.count)
real.withUnsafeMutableBufferPointer { r in
imag.withUnsafeMutableBufferPointer { i in
var split = DSPSplitComplex(realp: r.baseAddress!, imagp: i.baseAddress!)
data.withUnsafeBufferPointer { d in
vDSP_ctoz(d.baseAddress!, 2, &split, 1, vDSP_Length(data.count))
}
}
}
}

定义了这些便利函数后,您可以执行以下操作:

var forwardInputReal = [Float](repeating: 0, count: halfN)
var forwardInputImag = [Float](repeating: 0, count: halfN)
ctoz(observed, real: &forwardInputReal, imag: &forwardInputImag)

甚至:

let (forwardInputReal, forwardInputImag) = ctoz(data)

我将与 vDSP 团队联系,看看我们是否无法在未来版本的框架中添加类似的内容,这样您就不需要自己编写它。

关于ios - Apple FFT 给出不一致的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58332930/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com