Skip to content

Instantly share code, notes, and snippets.

@fasiha
Created September 7, 2023 05:56
Show Gist options
  • Save fasiha/84c25d8a608ee515ab0395a0aca95e57 to your computer and use it in GitHub Desktop.
Save fasiha/84c25d8a608ee515ab0395a0aca95e57 to your computer and use it in GitHub Desktop.
Demo code on how to use FFT to sinc-interpolate (upsample). (You probably should just use scipy.signal.resample which will filter and window the signal to avoid the ringing that this FFT-only method invariably yields, unless you know what you're doing.)
import numpy as np
from numpy.fft import fft, ifft
def fftUpsample(a, factor):
a_f = fft(a)
n = len(a)
assert n % 2 == 0, "odd case is harder"
padded = np.zeros(int(len(a) * factor), dtype=a_f.dtype)
padded[:n // 2 + 1] = a_f[:n // 2 + 1]
remaining = n - (n // 2 + 1)
padded[-remaining:] = a_f[n // 2 + 1:]
padded[n // 2] /= 2
padded[-remaining - 1] = padded[n // 2]
return factor * ifft(padded)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment