我正在使用 Accelerate 框架中的 vDSP 在来自网格的二维数组中执行 fft2d 操作。
问题是我得到一个虚部为 0 的数组,它与使用 pylab.fft2 的 python 中的相同操作不匹配。
如果我增加数组大小,结果不会为零但无论如何都不匹配,所以我做错了。
有人可以帮我吗?这是我的第一个堆栈溢出问题,但我现在被困了两个星期。
这是网格(本例为 4x8)
[
[1.80485138784544e-35, 2.61027906966774e-23, 1.26641655490943e-14, 2.06115362243857e-09, 1.1253517471926e-07, 2.06115362243857e-09, 1.26641655490943e-14, 2.61027906966774e-23],
[2.93748211171084e-30, 4.24835425529162e-18, 2.06115362243857e-09, 0.000335462627902512, 0.0183156388887342, 0.000335462627902512, 2.06115362243857e-09, 4.24835425529162e-18],
[1.60381089054866e-28, 2.31952283024359e-16, 1.1253517471926e-07, 0.0183156388887342, 1.0, 0.0183156388887342, 1.1253517471926e-07, 2.31952283024359e-16],
[2.93748211171084e-30, 4.24835425529162e-18, 2.06115362243857e-09, 0.000335462627902512, 0.0183156388887342, 0.000335462627902512, 2.06115362243857e-09, 4.24835425529162e-18]
]
这是 fft2 函数:
func fft2(arr: [[Complex<Double>]]) -> [[Complex<Double>]] {
let nRows = arr.count
let nCols = arr[0].count
let N = nRows * nCols
let radix = FFTRadix(FFT_RADIX2)
let pass = vDSP_Length(Int(log2(Double(N))))
// Create FFTSetup
let setup = vDSP_create_fftsetupD(pass, radix)
// Direction
let dir = FFTDirection(FFT_FORWARD)
// Get real and imag doubles from the [Complex]
// (all imag parts are 0.0 on this example)
let (real, imag) = complex2DArrayToDouble(arr)
// Pack 2d arrays as 1d (function bellow)
var realArray = pack2dArray(real, rows: nRows, cols: nCols)
var imagArray = pack2dArray(imag, rows: nRows, cols: nCols)
// Create the split complex with the packed arrays
var splitComplex = DSPDoubleSplitComplex(
realp: &realArray,
imagp: &imagArray)
let log2n0c = vDSP_Length(Int(log2(Double(nCols))))
let log2n1r = vDSP_Length(Int(log2(Double(nRows))))
let rowStride = vDSP_Stride(nRows)
let colStride = vDSP_Stride(1) // Use all cols
// Perform the fft2d
vDSP_fft2d_zipD(setup, &splitComplex, rowStride, colStride, log2n0c, log2n1r, dir)
// Destroy setup
vDSP_destroy_fftsetupD(setup)
// Pack the 1d arrays on 2d arrays again
let resultReal = unpack2dArray(realArray, rows: nRows, cols: nCols)
let resultImag = unpack2dArray(imagArray, rows: nRows, cols: nCols)
// Ignore this...
return complexFrom2DArray([[Double]](), imag: [[Double]]())
}
最后是我用于将数组从 2d 打包和解包到 1d 的函数
func pack2dArray(arr: [[Double]], rows: Int, cols: Int) -> [Double] {
var resultArray = zeros(rows * cols)
for Iy in 0...cols-1 {
for Ix in 0...rows-1 {
let index = Iy * rows + Ix
resultArray[index] = arr[Ix][Iy]
}
}
return resultArray
}
func unpack2dArray(arr: [Double], rows: Int, cols: Int) -> [[Double]] {
var resultArray = [[Double]](count: rows, repeatedValue: zeros(cols))
for Iy in 0...cols-1 {
for Ix in 0...rows-1 {
let index = Iy * rows + Ix
resultArray[Ix][Iy] = arr[index]
}
}
return resultArray
}
我将不胜感激任何关于此的信息,如果最容易使其像在 python 中一样工作,我可以将其更改为 C 或 Objective-C。
快速结果:
[
[(1.07460475603902+0.0.i), (-1.06348244974363+0.0.i), (1.03663115699765+0.0.i), (-1.00978033088166+0.0.i), (0.998658491216246+0.0.i), (-1.00978033088166+0.0.i), (1.03663115699765+0.0.i), (-1.06348244974363+0.0.i)],
[(-1.03663138619031+0.0.i), (1.02590210946989+0.0.i), (-0.999999662394501+0.0.i), (0.974097665459761+0.0.i), (-0.963368838879988+0.0.i), (0.974097665459761+0.0.i), (-0.999999662394501+0.0.i), (1.02590210946989+0.0.i)],
[(0.998658482971633+0.0.i), (-0.988322230996495+0.0.i), (0.963368617931946+0.0.i), (-0.938415438518917+0.0.i), (0.928079620195301+0.0.i), (-0.938415438518917+0.0.i), (0.963368617931946+0.0.i), (-0.988322230996495+0.0.i)],
[(-1.03663138619031+0.0.i), (1.02590210946989+0.0.i), (-0.999999662394501+0.0.i), (0.974097665459761+0.0.i), (-0.963368838879988+0.0.i), (0.974097665459761+0.0.i), (-0.999999662394501+0.0.i), (1.02590210946989+0.0.i)]
]
Python 结果:
[
[ 1.07460476 +0.00000000e+00j, -1.06348245 +1.98409020e-17j, 1.03663116 +0.00000000e+00j -1.00978033 -1.97866921e-17j, 0.99865849 +0.00000000e+00j -1.00978033 -1.98409020e-17j, 1.03663116 +0.00000000e+00j -1.06348245 +1.97866921e-17j]
[-1.03663139 +0.00000000e+00j, 1.02590211 -1.90819560e-17j, -0.99999966 +0.00000000e+00j, 0.97409767 +1.90819558e-17j, -0.96336884 +0.00000000e+00j, 0.97409767 +1.90819560e-17j, -0.99999966 +0.00000000e+00j, 1.02590211 -1.90819558e-17j]
[ 0.99865848 +0.00000000e+00j, 0.98832223 +1.83230190e-17j, 0.96336862 +0.00000000e+00j, 0.93841544 -1.83772293e-17j, 0.92807962 +0.00000000e+00j, 0.93841544 -1.83230190e-17j, 0.96336862 +0.00000000e+00j, 0.98832223 +1.83772293e-17j]
[-1.03663139 +0.00000000e+00j, 1.02590211 -1.90819560e-17j, -0.99999966 +0.00000000e+00j, 0.97409767 +1.90819558e-17j, -0.96336884 +0.00000000e+00j, 0.97409767 +1.90819560e-17j, -0.99999966 +0.00000000e+00j, 1.02590211 -1.90819558e-17j]
]
提前致以诚挚的问候和感谢!
编辑 1
这是相同代码的 C 版本: http://pastebin.com/C9RPgu68
这是 python 代码: http://pastebin.com/rr4e6rku
最佳答案
不同的输出如
Swift: (-1.06348244974363+0.0.i)
Python: -1.06348245 +1.98409020e-17j
不表示错误的结果。首先,显然是 Swift 代码
使用定点表示,因此 1.98409020 ⋅ 10-17 舍入为 0.0。其次,即使您期望
结果恰好为零,由于精度有限,预计会有一个小的非零值
二进制 float (对于 64 位 Double,大约有 16 个十进制数字)。
关于c - vDSP FFT2d Swift 结果虚部错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33596452/
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie
我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa
这个问题在这里已经有了答案:Arraysmisbehaving(1个回答)关闭6年前。是否应该这样,即我误解了,还是错误?a=Array.new(3,Array.new(3))a[1].fill('g')=>[["g","g","g"],["g","g","g"],["g","g","g"]]它不应该导致:=>[[nil,nil,nil],["g","g","g"],[nil,nil,nil]]
尝试在我的RoR应用程序中实现计数器缓存列时出现错误Unknownkey(s):counter_cache。我在这个问题中实现了模型关联:Modelassociationquestion这是我的迁移:classAddVideoVotesCountToVideos0Video.reset_column_informationVideo.find(:all).eachdo|p|p.update_attributes:videos_votes_count,p.video_votes.lengthendenddefself.downremove_column:videos,:video_vot