diff --git a/lib/__init__.py b/lib/__init__.py index 94ac186..352aeb5 100644 --- a/lib/__init__.py +++ b/lib/__init__.py @@ -14,7 +14,6 @@ from .windowing import * from .piir import * import numpy as np -from matplotlib.pylab import show def analog(b, a): import sympy as sym diff --git a/lib/plot.py b/lib/plot.py index a26fc6a..3b1d889 100644 --- a/lib/plot.py +++ b/lib/plot.py @@ -26,20 +26,17 @@ def cleanplot(): return fig, ax def new_response(*args, **kwargs): - fig = plt.figure() - ax = fig.gca() + fig, ax = plt.subplots() response_setup(ax, *args, **kwargs) return fig, ax def new_phase_response(*args, **kwargs): - fig = plt.figure() - ax = fig.gca() + fig, ax = plt.subplots() phase_response_setup(ax, *args, **kwargs) return fig, ax def new_bode(magnitude_offset=0): - fig = plt.figure() - ax1 = fig.gca() + fig, ax1 = plt.subplots() ax2 = ax1.twinx() ymin = -24 + magnitude_offset ymax = 24 + magnitude_offset diff --git a/lib/util.py b/lib/util.py index ea6ef56..333f3d4 100644 --- a/lib/util.py +++ b/lib/util.py @@ -25,9 +25,7 @@ magnitude_x = lambda srate, size: np.arange(0, srate/2, srate/2/size) degrees_clamped = lambda x: ((x*180/np.pi + 180) % 360) - 180 def xsp(precision=4096): - """create #precision log-spaced points from 20 to 20480 Hz""" - # i opt not to use steps or linspace here, - # as the current method is less error-prone for me. + """create #precision log-spaced points from 20 Hz (inclusive) to 20480 Hz (exclusive)""" xs = np.arange(0,precision)/precision return 20*1024**xs @@ -45,7 +43,7 @@ def convolve_each(s, fir, mode='same', axis=0): return np.apply_along_axis(lambda s: sig.fftconvolve(s, fir, mode), axis, s) def count_channels(s): - if len(s.shape) < 2: + if s.ndim < 2: return 1 return s.shape[1] @@ -55,8 +53,7 @@ def monoize(s): existing mono signals are passed through unmodified.""" channels = count_channels(s) if channels != 1: - s = np.sum(s, 1) - s /= channels + s = np.average(s, axis=1) return s def div0(a, b): diff --git a/lib/wav.py b/lib/wav.py index 7aecea0..b729f94 100644 --- a/lib/wav.py +++ b/lib/wav.py @@ -1,12 +1,9 @@ import numpy as np from .util import lament, count_channels -# TODO: don't use wavfile, it breaks on perfectly good files -import scipy.io.wavfile as wav -import ewave - def wav_smart_read(fn): lament('wav_smart_read(): DEPRECATED; use wav_read instead.') + import scipy.io.wavfile as wav # don't use this, it fails to load good files srate, s = wav.read(fn) if s.dtype != np.float64: bits = s.dtype.itemsize*8 @@ -15,12 +12,14 @@ def wav_smart_read(fn): def wav_smart_write(fn, srate, s): lament('wav_smart_write(): DEPRECATED; use wav_write instead.') + import scipy.io.wavfile as wav si = np.zeros_like(s, dtype='int16') bits = si.dtype.itemsize*8 si += np.clip(s*2**(bits - 1), -32768, 32767) wav.write(fn, srate, si) def wav_read(fn): + import ewave with ewave.open(fn) as f: s = f.read() srate = f.sampling_rate @@ -32,6 +31,7 @@ def wav_read(fn): return s, srate def wav_write(fn, s, srate, dtype='h'): + import ewave if dtype in ('b', 'h', 'i', 'l') and np.max(np.abs(s)) > 1: lament('wav_write(): WARNING; clipping') with ewave.open(fn, 'w', srate, dtype, count_channels(s)) as f: diff --git a/lib/windowing.py b/lib/windowing.py index a42c1e6..acd9873 100644 --- a/lib/windowing.py +++ b/lib/windowing.py @@ -43,7 +43,7 @@ blackman_nuttall = _h(0.3635819, 0.4891775, 0.1365995, 0.0106411) blackman_harris = _h(0.35875, 0.48829, 0.14128, 0.01168) nuttall = _h(0.355768, 0.487396, 0.144232, 0.012604) flattop = _h(*_normalize(1, 1.93, 1.29, 0.388, 0.028)) # FTSRS -#flattop_weird = _h(_normalize(1, 1.93, 1.29, 0.388, 0.032)) # ??? wtf +#flattop_weird = _h(*_normalize(1, 1.93, 1.29, 0.388, 0.032)) # ??? wtf flattop_weird = _h(0.2156, 0.4160, 0.2781, 0.0836, 0.0069) # ??? scipy crap hann = _h(0.5, 0.5) hamming_inexact = _h(0.54, 0.46)