【问题标题】:1d Fourier convolution python一维傅里叶卷积python
【发布时间】:2020-12-16 00:04:28
【问题描述】:

问题

我想使用傅里叶变换编写一个非常简单的一维卷积。我的代码没有给出预期的结果。

代码

import numpy as np
import scipy
def fftconvolve(x, y):
    ''' Perso method to do FFT convolution'''
    fftx = np.fft.fft(x)
    ffty = np.fft.fft(y)
    fftc = fftx * ffty
    c = np.fft.ifft(fftc)
    return c.real

square = [0,0,0,1,1,1,0,0,0,0] # Example array

output = fftconvolve(square, square)
output2 = scipy.signal.fftconvolve(square, square, mode='same')

结果

图中显示了使用 scipy(橙色)和我的函数(蓝色)进行的卷积。

问题

为什么两个输出不同?

【问题讨论】:

标签: python fft convolution


【解决方案1】:

如果您使用“完整”模式而不是“相同”模式,您会更接近!

import numpy as np
import scipy
def fftconvolve(x, y):
    ''' Perso method to do FFT convolution'''
    fftx = np.fft.fft(x)
    ffty = np.fft.fft(y)
    fftc = fftx * ffty
    c = np.fft.ifft(fftc)
    return c.real

square = [0,0,0,1,1,1,0,0,0,0] # Example array

output = fftconvolve(square, square)
output2 = scipy.signal.fftconvolve(square, square, mode='full')

【讨论】:

  • 事实上,我不确定这是不是真的。如果您绘制结果:import matplotlib.pyplot as plt plt.plot(output) plt.plot(output2, '--') 您会注意到输出的长度不同。此外,两者的开始(情节左侧)非常不同。或者我不明白为什么这是等价的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-19
  • 1970-01-01
  • 1970-01-01
  • 2021-07-12
相关资源
最近更新 更多