Skip to content

Instantly share code, notes, and snippets.

@brandondube
Created March 20, 2018 20:11
Show Gist options
  • Save brandondube/12d87a55fbeb1bbda8038397f55f6127 to your computer and use it in GitHub Desktop.
Save brandondube/12d87a55fbeb1bbda8038397f55f6127 to your computer and use it in GitHub Desktop.
import timeit
import numpy as np
from numpy.fft import fftshift, ifftshift, fft2
x, y = np.linspace(-1, 1, 128), np.linspace(-1, 1, 128)
xx, yy = np.meshgrid(x, y)
rho, phi = np.sqrt(xx**2 + yy**2), np.arctan2(yy, xx)
phase_arr = rho ** 2 * np.sin(phi)
complex_arr = np.exp(1j * 2 * np.pi * phase_arr)
padded = np.pad(complex_arr, ((64, 64), (64, 64)), mode='constant')
fft_result = fftshift(fft2(ifftshift(padded)))
def bench_trig(rho, phi):
print('trigonometry')
print(timeit.repeat('rho ** 2 * np.sin(phi)', number=1000, globals=globals()))
def bench_realtocomplex(phase_err):
print('real to complex')
print(timeit.repeat('np.exp(1j * 2 * np.pi * phase_arr)', number=1000, globals=globals()))
def bench_fft(padded):
print('fft')
print(timeit.repeat('fftshift(fft2(ifftshift(padded)))', number=1000, globals=globals()))
bench_trig(rho, phi)
bench_realtocomplex(phase_arr)
bench_fft(padded)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment