scipy.ndimage.fourier\u过滤器似乎只适用于数组中索引为0,0,:的向量

2024-04-24 22:32:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在三维阵列的第三个轴上实现移动平均。由于移动窗口可能相当大,我想在傅里叶域中进行,以获得合适的速度。但它似乎只适用于arr[0,0,:]中的向量。你知道吗

下面是一个带有测试用例的最小示例。第一个断言通过,第二个断言不通过。你知道吗

import numpy as np
import scipy.ndimage as sni

def moving_average_in_ft(arr, size, axis=-1):
    """Perform moving average of given size along given axis of arr
    Performed in Fourier domain.
    Returns a complex result.
    """
    arr_ft = np.fft.fft(arr)
    nd_size = [1,] * arr.ndim # all other axes do not average
    nd_size[axis] = size
    return np.fft.ifft(sni.fourier_uniform(arr_ft, nd_size))


def test_moving_average_in_ft_and_time():
    a = np.random.rand(2, 2, 20)
    size = 9
    a_filt_ft = moving_average_in_ft(a, size).real
    a_filt_time = sni.uniform_filter(a, (1, 1, size), mode='wrap')
    # for the first ensemble it works
    ensamble_1 = (0, 0, slice(None))
    np.testing.assert_allclose(a_filt_ft[ensamble_1], a_filt_time[ensamble_1], atol=0.01)
    # but all the other ensembles don't
    np.testing.assert_allclose(a_filt_ft, a_filt_time, atol=0.1)

Tags: infftsizetimenp断言averagesni