Created
July 13, 2018 18:15
-
-
Save christian-blades-cb/47d1237e4aea445417ff9d449c0b0fbf to your computer and use it in GitHub Desktop.
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 wave | |
import numpy as np | |
# band-pass filter https://plot.ly/python/fft-filters/ | |
def bandpass(lowfreq, highfreq, framerate, frames): | |
# fL = 3900 / framerate | |
# fH = 4200 / framerate | |
fL = lowfreq / framerate | |
fH = highfreq / framerate | |
b = 0.08 | |
N = int(np.ceil((4/b))) | |
if not N % 2: # no odd numbers | |
N += 1 | |
n = np.arange(N) | |
# low-pass filter | |
hlpf = np.sinc(2 * fH * (n - (N - 1) / 2.)) | |
hlpf *= np.blackman(N) | |
hlpf = hlpf / np.sum(hlpf) | |
# high-pass filter | |
hhpf = hlpf = np.sinc(2 * fL * (n - (N - 1) / 2.)) | |
hlpf *= np.blackman(N) | |
hlpf = hlpf / np.sum(hhpf) | |
hhpf = -hhpf | |
hhpf[int((N - 1) / 2)] += 1 | |
h = np.convolve(hlpf, hhpf) | |
new_signal = np.convolve(frames, h) | |
return new_signal | |
fd = wave.open('/home/cblades/beeping_clip.aiff', 'rb') | |
n_samples = fd.getnframes() | |
framerate = fd.getframerate() | |
samplewidth = fd.getsampwidth() | |
print(samplewidth) | |
channels = fd.getnchannels() | |
assert(channels == 1) | |
frames = fd.readframes(n_samples) | |
frames = np.fromstring(frames, dtype='<i2') | |
fd.close() | |
# filter out anything not in ~4000Hz range | |
new_signal = bandpass(3900, 4200, framerate, frames) | |
out = wave.open('filtered_beeping.wav', 'wb') | |
out.setnchannels(channels) | |
out.setsampwidth(samplewidth) | |
out.setframerate(framerate) | |
out.writeframes(new_signal.tobytes()) | |
out.close() | |
fd.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment