实施 3D DFT 时无法匹配结果

发布于 2025-01-02 14:46:03 字数 1584 浏览 3 评论 0原文

我正在尝试实现 3D DFT,但遇到了一些麻烦。我认为我应该做的就是只进行 3 个连续的 1D DFT,每个方向一个。假设一维 DFT 是正确的,你能看出这段代码有什么问题吗:

def dft3d(self, real3d, img3d, nx, ny, nz, dir):

    #Transform depth
    for i in range(nx):
        for j in range(ny):
            real = numpy.zeros(nz)
            img = numpy.zeros(nz)
            for k in range(nz):
                real[k] = real3d[i][j][k]
                img[k] = img3d[i][j][k]
            self.dft(real, img, nz, 1) #This was indented too much. It should work now.
            for k in range(nz):
                real3d[i][j][k] = real[k]
                img3d[i][j][k] = img[k]

    #Transform cols
    for k in range(nz):
        for i in range(nx):
            real = numpy.zeros(ny)
            img = numpy.zeros(ny)
            for j in range(ny):
                real[j] = real3d[i][j][k]
                img[j] = img3d[i][j][k]
            self.dft(real, img, ny, 1)
            for j in range(ny):
                real3d[i][j][k] = real[j]
                img3d[i][j][k] = img[j]

    #Transform rows
    for j in range(ny):
        for k in range(nz):
            real = numpy.zeros(nx)
            img = numpy.zeros(nx)
            for i in range(nx):
                real[i] = real3d[i][j][k]
                img[i] = img3d[i][j][k]
            self.dft(real, img, nx, 1)
            for i in range(nx):
                real3d[i][j][k] = real[i]
                img3d[i][j][k] = img[i]

我知道 python 中有内置版本,但我不能使用它们。我只是在 python 中测试我的算法,这样我就可以比较我的算法和内置算法的结果。据我所知,它对于 1D 和 2D 变换都工作得很好,但是一旦我将其扩展到 3D,结果就不再匹配。有谁知道出了什么问题吗?

I am trying to implement a 3D DFT but I am running into some trouble. What I believe I should do is to just do 3 consecutive 1D DFTs, one in each direction. Assuming that the 1D DFT is correct, can you see what is wrong with this code:

def dft3d(self, real3d, img3d, nx, ny, nz, dir):

    #Transform depth
    for i in range(nx):
        for j in range(ny):
            real = numpy.zeros(nz)
            img = numpy.zeros(nz)
            for k in range(nz):
                real[k] = real3d[i][j][k]
                img[k] = img3d[i][j][k]
            self.dft(real, img, nz, 1) #This was indented too much. It should work now.
            for k in range(nz):
                real3d[i][j][k] = real[k]
                img3d[i][j][k] = img[k]

    #Transform cols
    for k in range(nz):
        for i in range(nx):
            real = numpy.zeros(ny)
            img = numpy.zeros(ny)
            for j in range(ny):
                real[j] = real3d[i][j][k]
                img[j] = img3d[i][j][k]
            self.dft(real, img, ny, 1)
            for j in range(ny):
                real3d[i][j][k] = real[j]
                img3d[i][j][k] = img[j]

    #Transform rows
    for j in range(ny):
        for k in range(nz):
            real = numpy.zeros(nx)
            img = numpy.zeros(nx)
            for i in range(nx):
                real[i] = real3d[i][j][k]
                img[i] = img3d[i][j][k]
            self.dft(real, img, nx, 1)
            for i in range(nx):
                real3d[i][j][k] = real[i]
                img3d[i][j][k] = img[i]

I know there are built in versions of this in python, but I can't use those. I'm just testing my algorithm in python so I can compare results of my algorithm and the built in ones. As far as I could tell it worked fine for both 1D and 2D transforms, but once I expanded it to 3D the results no longer match. Does anyone know what is wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聽兲甴掵 2025-01-09 14:46:03

self.dft 的第一个实例缩进得太远。

除此之外,我从提供的代码中看不出任何问题。

附带说明一下,如果您按照代码建议使用 numpy,即使不求助于内置 DFT/FFT,也可以显着简化代码。

例如,您可以索引 3D numpy 数组,如 data3D[i, j, k]。您可以通过执行 data3D[:, j, k]data3D[i, :, k]data3D[:, :, k] 进行切片code> 等,而不是在 for 循环中一次分配一个单独的元素。

The first instance of self.dft is indented too far.

Other than that, I see nothing wrong from the code provided.

As a side note, if you are using numpy as your code suggests, you can simplify your code significantly even without resorting to the built-in DFT/FFT.

For example, you can index a 3D numpy array like data3D[i, j, k]. You can slice by doing data3D[:, j, k], data3D[i, :, k], data3D[:, :, k], etc., instead of assigning individual elements one at a time within a for loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文