gpt4 book ai didi

ios - 在 Accelerate vDSP 上进行离散傅里叶变换后进行逆离散傅里叶变换不会产生原始值

转载 作者:搜寻专家 更新时间:2023-11-01 06:11:46 27 4
gpt4 key购买 nike

我有一系列离散值。所以,我没有输入部分的虚数值。我正在对这些值进行离散傅里叶变换并执行离散傅里叶逆变换,以获取相同的值。这个想法是为了测试来自 Accelerate Framework 的 vDSP 是否按预期工作,因为文档是一堆废话,你必须自己弄清楚。

考虑以下代码

  lazy var DFTSetupForward: vDSP_DFT_Setup = {
guard let setup = vDSP_DFT_zop_CreateSetupD(
nil,
vDSP_Length(self.numSamples),
vDSP_DFT_Direction.FORWARD) else {
fatalError("can't create vDSP_DFT_Setup")
}
return setup
}()

lazy var DFTSetupInverse: vDSP_DFT_Setup = {
guard let setup = vDSP_DFT_zop_CreateSetupD(
nil,
vDSP_Length(self.numSamples),
vDSP_DFT_Direction.INVERSE) else {
fatalError("can't create vDSP_DFT_Setup")
}
return setup
}()
func discreteFourierTransform (_ valores:[Double] = []) -> ([Double], [Double]) {
let numeroDados = valores.count

let inputImag = Array<Double>(repeating:0.0, count:numeroDados)
var outputReal = Array<Double>(repeating:0.0, count:numeroDados)
var outputImag = Array<Double>(repeating:0.0, count:numeroDados)

vDSP_DFT_ExecuteD(DFTSetupForward, valores, inputImag, &outputReal, &outputImag)
return (outputReal, outputImag)
}


// faz a DISCRETE FOURIER TRANSFORM DOUBLE
// a saída corresponde aos vetores reais e imaginários
func discreteFourierTransformInverse (_ valoresReais:[Double] = [],
_ valoresImaginarios:[Double] = []) -> ([Double], [Double]) {
let numeroDados = valoresReais.count

var outputReal = Array<Double>(repeating:0.0, count:numeroDados)
var outputImag = Array<Double>(repeating:0.0, count:numeroDados)

vDSP_DFT_ExecuteD(DFTSetupInverse, valoresReais, valoresImaginarios, &outputReal, &outputImag)
return (outputReal, outputImag)
}

和后来的这些电话

let (outputDFTreal, outputDFTImaginario ) = self.discreteFourierTransform(normalizedData)

let (sinalRealX, sinalImaginarioX ) = self.discreteFourierTransformInverse(outputDFTreal, outputDFTImaginario)

或者换句话说,我从第一个 let 中获取傅里叶变换的结果,并将它们注入(inject)到傅里叶逆变换中。我希望 sinalRealX 等于原始数据,在这种情况下是 normalizedData 并且还希望 signalImaginarioX 全部为零。

但值(value)观完全不同!

这是怎么回事?

最佳答案

您不应期望 IDFT 返回标准化值。您应该期望它返回原始(预归一化)值,我相信它会返回(在计算错误范围内):

let fft = FFT()
let data = Array(stride(from: 0.0, to: Double(fft.numSamples), by: 1))
let normalizedData = data.map{ $0/Double(fft.numSamples) }
let (outputDFTreal, outputDFTImaginario ) = fft.discreteFourierTransform(normalizedData)
let (sinalRealX, sinalImaginarioX ) = fft.discreteFourierTransformInverse(outputDFTreal, outputDFTImaginario)
print(data)
print(sinalRealX)
print(sinalImaginarioX)

let zeros = Array(repeating: 0.0, count: data.count)
func avgerr(_ a: [Double], _ b: [Double]) -> Double {
return zip(a, b).map { abs($0 - $1) }.reduce(0, +) / Double(a.count)
}

avgerr(data, sinalRealX) // 2.442490654175344e-15
avgerr(zeros, sinalImaginarioX) // 2.602442788260593e-15

关于ios - 在 Accelerate vDSP 上进行离散傅里叶变换后进行逆离散傅里叶变换不会产生原始值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54853222/

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