代码之家  ›  专栏  ›  技术社区  ›  F. Win

集成2D矢量场阵列(反向np梯度)

  •  3
  • F. Win  · 技术社区  · 7 年前

    我想积分一个2D数组,所以基本上是反转梯度算子。

    shape = (60, 60)
    sampling = 1
    k_mesh = np.meshgrid(np.fft.fftfreq(shape[0], sampling), np.fft.fftfreq(shape[1], sampling))
    

    然后我将向量场构造为复数arreay(x向量=实部,y向量=虚部):

    k = k_mesh[0] + 1j * k_mesh[1]
    

    例如,真实的部分如下所示 enter image description here

    k_grad = np.gradient(k, sampling)
    

    def freq_array(shape, sampling):
    
        f_freq_1d_y = np.fft.fftfreq(shape[0], sampling[0])
        f_freq_1d_x = np.fft.fftfreq(shape[1], sampling[1])
        f_freq_mesh = np.meshgrid(f_freq_1d_x, f_freq_1d_y)
        f_freq = np.hypot(f_freq_mesh[0], f_freq_mesh[1])
    
        return f_freq
    
    
    def int_2d_fourier(arr, sampling):
        freqs = freq_array(arr.shape, sampling)
    
        k_sq = np.where(freqs != 0, freqs**2, 0.0001)
        k = np.meshgrid(np.fft.fftfreq(arr.shape[0], sampling), np.fft.fftfreq(arr.shape[1], sampling))
    
        v_int_x = np.real(np.fft.ifft2((np.fft.fft2(arr[1]) * k[0]) / (2*np.pi * 1j * k_sq)))
        v_int_y = np.real(np.fft.ifft2((np.fft.fft2(arr[0]) * k[0]) / (2*np.pi * 1j * k_sq)))
    
        v_int_fs = v_int_x + v_int_y
        return v_int_fs
    
    
    k_int = int_2d_fourier(k, sampling)
    

    不幸的是,结果在以下位置不是很准确 k 具有突变,如下图所示,该图显示了 K k_int .

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  1
  •   F. Win    7 年前

    然而,numpy的梯度函数计算二阶精度的中心差,这意味着梯度本身已经是近似值。

    长话短说:上面提出的集成功能工作正常!