# # filtermatch.py # # # Some very rough tests to estimate a highpass filter # from step response. # # The "10hz" WAVs are a 10 Hz square wave cut to begin at the first high sample. # The "pop" WAVs have a single positive pop from 0 to high somewhere in the middle. # # This program tries random variations of filter settings to try and best match # the given input. The results aren't good enough to be directly usable # but with some probing and changing of parameters it can find some # good information to use as a starting point for modelling the highpass. # import wave import struct import matplotlib.pyplot as pyplot import math import random import scipy.signal sr = 0 def normalize(w, trim=0): m = max(max(w[trim:]),-min(w[trim:])) r = 1 if m != 0: r = 1/m return [x*r for x in w] def ideal_square(w): o = [] for v in w: if v > 0: o.append(1) else: o.append(-1) return o def ideal_step(w,t): o = [] s = 0 for v in w: if v > t: s = 1 o.append(s) return o def highpass(w,cutoff,trim=0): rc = 1 / (2 * math.pi * cutoff) alpha = rc / (rc + (1 / sr)) o = [] pw = w[0] po = w[0] for vw in w: vo = alpha * ( po + vw - pw ) o.append(vo) pw = vw po = vo return normalize(o,trim) def highpass_butter(w,cutoff,order,trim=0): global sr bcut = cutoff / (sr * 0.5) (b,a) = scipy.signal.butter(order,bcut,"highpass") return scipy.signal.lfilter(b, a, w) def loadwave(filename): w = wave.open(filename,"rb") global sr sr = w.getframerate() assert(w.getnchannels() == 1) # "Mono only." assert(w.getsampwidth() == 2) # "16-bit only." wb = w.readframes(w.getnframes()) ws = [] for i in range(0,len(wb),2): ws.append(struct.unpack_from("