Skip to content

Instantly share code, notes, and snippets.

@andrewgiessel
Created November 21, 2013 20:58
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 andrewgiessel/7589513 to your computer and use it in GitHub Desktop.
Save andrewgiessel/7589513 to your computer and use it in GitHub Desktop.
fft based filtering with FIR windows!
import numpy as np
import scipy.signal
import matplotlib.pyplot as plt
n=256
data = np.random.random(n)
data_fft = np.fft.fft(data)
# see also : http://mpastell.com/2010/01/18/fir-with-scipy/
# low pass
a = scipy.signal.firwin(256, cutoff=0.5, window='hanning')
# high pass
b = scipy.signal.firwin(256, cutoff=0.3, window='hanning');
b[n/2] = b[n/2] + 1
# combine
d = - (a+b);
d[n/2] = d[n/2] + 1
# fft, filter, ifft
H = np.fft.fft(b)
data_filt = np.fft.ifft(data_fft*H) # note that you could rotate this around for a 2d window
plt.plot(data)
plt.plot(data_filt)
# you can also filter really easily with scipy.signal.filtfilt()
data_filt = scipy.signal.filtfilt(b=d, a=[1], x=data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment