from scipy import signal
Create a signal with 2 sinusoids at different frequencies and amplitudes
slow_freq = 1e-4; fast_freq = 1e-2; num_samples = 1e5; t = np.arange(num_samples)
s = np.sin(2*np.pi*slow_freq*t) + .1 * np.sin(2*np.pi*fast_freq*t)
plot(s)
[<matplotlib.lines.Line2D at 0xfd76510>]
Power spectrum
p,f=mlab.psd(s,NFFT=len(s), Fs=1)
plot(s[:10000])
[<matplotlib.lines.Line2D at 0x10cec2d0>]
loglog(f, p)
[<matplotlib.lines.Line2D at 0x10cf94d0>]
Filter with a high-pass 5th order Butterworth with cutoff at 1e-3
filter_hz = 1e-3
sampling_frequency = 1
b, a = signal.butter(5, filter_hz / (sampling_frequency/2.), btype='high')
print b,a
[ 0.98988508 -4.94942538 9.89885075 -9.89885075 4.94942538 -0.98988508] [ 1. -4.97966719 9.91887534 -9.87862155 4.91928587 -0.97987246]
sf = signal.lfilter(b, a, s)
Check filtered signal in time and frequency domain
plot(sf)
[<matplotlib.lines.Line2D at 0x11cf3f90>]
plot(sf[:10000])
[<matplotlib.lines.Line2D at 0x11ae26d0>]
pf,ff=mlab.psd(sf,NFFT=len(s), Fs=1)
loglog(f, p, label="original"); loglog(ff, pf, label="filtered"); legend()
<matplotlib.legend.Legend at 0x12e94090>