Created
November 21, 2013 20:58
-
-
Save andrewgiessel/7589513 to your computer and use it in GitHub Desktop.
fft based filtering with FIR windows!
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
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