Skip to content

Instantly share code, notes, and snippets.

@fasiha
Created August 29, 2019 03:30
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 fasiha/bc3d7f6d21d2a66fbc4bb8ac8aa18929 to your computer and use it in GitHub Desktop.
Save fasiha/bc3d7f6d21d2a66fbc4bb8ac8aa18929 to your computer and use it in GitHub Desktop.
How to zero-pad nicely symmetric vectors for FFT, so FFT of real symmetric inputs is (to machine precision) real
def Fsympad(h, m=None, inputOriginMiddle=True):
if inputOriginMiddle:
h = fft.ifftshift(np.array(h))
else:
h = np.array(h)
if not m:
return fft.fftshift(fft.fft(h))
n = h.size
assert m >= n
# odd-length case: just insert zeros
if n % 2 == 1:
nfirst = int(np.ceil(n / 2))
padded = np.hstack((h[:nfirst], np.zeros(m - n), h[nfirst:]))
return fft.fftshift(fft.fft(padded))
# even-length input: split the anti-DC component in half
if m == n:
padded = h
else:
ncut = int(n / 2)
padded = np.hstack((h[:ncut], [h[ncut] / 2], np.zeros(m - n - 1), [h[ncut] / 2], h[ncut + 1:]))
return fft.fftshift(fft.fft(padded))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment