Created
August 29, 2019 03:30
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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