Skip to content

Instantly share code, notes, and snippets.

@pbanaszkiewicz
Last active November 9, 2015 21:44
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 pbanaszkiewicz/c4280b600d0079b31ad3 to your computer and use it in GitHub Desktop.
Save pbanaszkiewicz/c4280b600d0079b31ad3 to your computer and use it in GitHub Desktop.
import numpy as np
import pylab as plt
from scipy.fftpack import fft, ifft, fftfreq
from scipy.io import wavfile
from scipy.signal import butter, filtfilt, lfilter
filename = 'probka2.wav'
filename_out = 'probka2_filtered.wav'
[fs, frames] = wavfile.read(filename)
ts = 1 / fs
nyquist_f = fs / 2
# signal = (frames[:, 0] + frames[:, 1]) / 2
signal = frames[:, 0] # left (?) channel
time = np.arange(0, len(signal) * ts, ts)
# we can use Hz in here when the filter is analog
low = 100
high = 10000
# actual filter
# b, a = butter(2, (low, high), 'bandpass', analog=True)
b, a = butter(2, (0.1, 0.4), 'bandpass')
bandpass = filtfilt(b, a, signal)
# bandpass = lfilter(b, a, signal)
bandpass = bandpass.astype('int16')
# save the results
wavfile.write(filename_out, fs, bandpass)
# Fourier transform of the original signal
freqs = fftfreq(signal.size, ts)
signal_f = fft(signal)
# Fourier of the bandpassed
freqs_band = fftfreq(bandpass.size, ts)
bandpass_f = fft(bandpass)
plt.subplot(221)
plt.plot(time, signal)
plt.subplot(222)
plt.plot(freqs, signal_f)
plt.subplot(223)
plt.plot(time, bandpass)
plt.subplot(224)
plt.plot(freqs_band, bandpass_f)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment