swift vDSP_zaspec的自动光谱计算不起作用

falq053o  于 2022-11-28  发布在  Swift
关注(0)|答案(1)|浏览(113)

我尝试计算记录信号的自谱,我使用函数vDSP_zaspec。通常输出数组是真实的。

class  func autoSpectrum (input: [Float])->[Float] {
        
        var real = [Float](input)
        var imaginary = [Float](repeating: 0.0, count : input.count)
        var Output:[Float] = [Float](repeating:0 , count: input.count)
        let length = vDSP_Length(real.count/2)
   
        real.withUnsafeMutableBufferPointer {realBP in
                   imaginary.withUnsafeMutableBufferPointer {imaginaryBP in
                      
                       var splitComplex = DSPSplitComplex(realp: realBP.baseAddress!, imagp: imaginaryBP.baseAddress!)
                       vDSP_zaspec(&splitComplex, &Output, vDSP_Length(input.count))
                    
                   }
               }
    let value = vDSP.rootMeanSquare(Output)
   Output = vDSP.divide(Output, value)

        return Output
    }

我使用500 Hz正弦波进行了测试,输出阵列如下所示:

图表与预期的结果相去甚远...结果看起来像是录制的音频文件的绝对值...
如果有人能帮助我,那就太好了!

bnlyeluc

bnlyeluc1#

vDSP_zaspec返回每个真实的部平方与相应虚部平方之和。Apple documentation将其描述为“计算复向量实部与虚部平方的元素级和”。
下列程式码会计算出相同的结果:

var real: [Float] = [-2, 7, -3]
var imaginary: [Float] = [4, -1, -4]

var vDSPresult: [Float] = [0, 0, 0]
var scalarResult: [Float] = [0, 0, 0]

real.withUnsafeMutableBufferPointer {realBP in
    imaginary.withUnsafeMutableBufferPointer {imaginaryBP in
        var splitComplex = DSPSplitComplex(realp: realBP.baseAddress!,
                                           imagp: imaginaryBP.baseAddress!)
        vDSP_zaspec(&splitComplex,
                    &vDSPresult,
                    vDSP_Length(vDSPresult.count))
        
    }
}

print(vDSPresult)

for i in 0 ..< scalarResult.count {
    scalarResult[i] = pow(real[i], 2) + pow(imaginary[i], 2)
}

print(scalarResult)

相关问题