Skip to content

Instantly share code, notes, and snippets.

@agmarrugo
Forked from thearn/fft_convolution.py
Created June 20, 2014 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agmarrugo/d9239243442a6ba0cea9 to your computer and use it in GitHub Desktop.
Save agmarrugo/d9239243442a6ba0cea9 to your computer and use it in GitHub Desktop.
from numpy.fft import fft, ifft, fft2, ifft2, fftshift
import numpy as np
def fft_convolve2d(x,y):
""" 2D convolution, using FFT"""
fr = fft2(x)
fr2 = fft2(np.flipud(np.fliplr(y)))
m,n = fr.shape
cc = np.real(ifft2(fr*fr2))
cc = np.roll(cc, -m/2+1,axis=0)
cc = np.roll(cc, -n/2+1,axis=1)
return cc
def fft_convolve1d(x,y): #1d cross correlation, fft
""" 1D convolution, using FFT """
fr=fft(x)
fr2=fft(np.flipud(y))
cc=np.real(ifft(fr*fr2))
return fftshift(cc)
if __name__ == "__main__":
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment