dsp/lib/plotwav.py

88 lines
2.5 KiB
Python
Raw Normal View History

2015-10-19 05:39:37 -07:00
# this is a bunch of crap that should really be reduced to one or two functions
2019-01-02 06:45:12 -08:00
from . import wav_read, wav_write
from . import normalize, averfft, tilter2, smoothfft4, firize
2015-10-30 04:04:36 -07:00
from . import new_response, magnitude_x, convolve_each, monoize, count_channels
2015-10-19 05:39:37 -07:00
import numpy as np
2017-09-21 04:04:22 -07:00
def plotfftsmooth(s, srate, ax=None, bw=1, tilt=None, size=8192,
window=0, raw=False, **kwargs):
2015-10-19 05:39:37 -07:00
sm = monoize(s)
2015-10-30 04:04:36 -07:00
xs_raw = magnitude_x(srate, size)
2015-10-19 05:39:37 -07:00
ys_raw = averfft(sm, size=size, mode=window)
ys_raw -= tilter2(xs_raw, tilt)
2019-01-02 06:45:12 -08:00
xs, ys = smoothfft4(ys_raw, bw)
2015-10-19 05:39:37 -07:00
2015-10-19 20:53:51 -07:00
if ax:
2017-09-21 04:04:22 -07:00
if raw:
ax.semilogx(xs_raw, ys_raw, **kwargs)
2015-10-19 20:53:51 -07:00
ax.semilogx(xs, ys, **kwargs)
2015-10-19 05:39:37 -07:00
return xs, ys
2017-09-21 04:04:22 -07:00
2019-01-02 06:45:12 -08:00
def plotwavinternal(sm, ss, srate, bw=1, size=8192):
2015-10-30 04:04:36 -07:00
xs_raw = magnitude_x(srate, size)
2015-10-19 20:53:51 -07:00
ys_raw_m = averfft(sm, size=size)
ys_raw_s = averfft(ss, size=size)
2015-10-19 05:39:37 -07:00
# tilting beforehand is negligible besides lowest frequencies, but eh
2015-10-19 20:53:51 -07:00
ys_raw_m -= tilter2(xs_raw, 'np2')
ys_raw_s -= tilter2(xs_raw, 'np2s')
2015-10-19 05:39:37 -07:00
2015-10-19 20:53:51 -07:00
if bw <= 0:
return xs_raw, xs_raw_m, xs_raw_s
2015-10-19 05:39:37 -07:00
2019-01-02 06:45:12 -08:00
xs, ys_m = smoothfft4(ys_raw_m, bw=bw, srate=srate)
xs, ys_s = smoothfft4(ys_raw_s, bw=bw, srate=srate)
2015-10-19 05:39:37 -07:00
2015-10-19 20:53:51 -07:00
return xs, ys_m, ys_s
2015-10-19 05:39:37 -07:00
2017-09-21 04:04:22 -07:00
2019-01-02 06:45:12 -08:00
def plotwav2(fn, bw=1, size=8192, fix=False, **kwargs):
2015-10-19 20:53:51 -07:00
s, srate = wav_read(fn)
2015-10-19 05:39:37 -07:00
2015-10-19 20:53:51 -07:00
s, rms = normalize(s, srate)
sm = monoize(s)
2019-01-02 06:45:12 -08:00
if s.ndim > 1 and s.shape[1] == 2:
2015-10-19 20:53:51 -07:00
ss = monoize(s*np.array((1, -1)))
else:
ss = np.zeros(len(s))
2015-10-19 05:39:37 -07:00
2019-01-02 06:45:12 -08:00
xs, ys_m, ys_s = plotwavinternal(sm, ss, srate, bw, size)
2015-10-19 05:39:37 -07:00
2015-10-19 20:53:51 -07:00
side_gain = np.average(ys_s) - np.average(ys_m)
2015-10-19 05:39:37 -07:00
2015-10-19 20:53:51 -07:00
if fix:
fno = fn[:-4]+"-proc.wav"
2015-10-19 05:39:37 -07:00
2015-10-19 20:53:51 -07:00
fir_m = firize(xs, -ys_m, srate=srate)
fir_s = firize(xs, -ys_s, srate=srate)
smf = convolve_each(sm/8, fir_m, mode='same')
ssf = convolve_each(ss/8, fir_s, mode='same')
ssf *= 10**(side_gain/20)
2019-01-02 06:45:12 -08:00
sf = np.c_[smf + ssf, smf - ssf]
2015-10-19 05:39:37 -07:00
2019-01-02 06:45:12 -08:00
wav_write(fno, sf, srate, dtype='f')
2015-10-19 20:53:51 -07:00
print('wrote '+fno)
2015-10-19 05:39:37 -07:00
2015-10-19 20:53:51 -07:00
return xs, ys_m, ys_s
2015-10-19 05:39:37 -07:00
2017-09-21 04:04:22 -07:00
2015-10-19 20:53:51 -07:00
def pw2(fn, label=None, bw=1/6, **kwargs):
2015-10-19 05:39:37 -07:00
fno = fn[:-4]+"-proc.wav"
2015-10-19 20:53:51 -07:00
xs, ys_m, ys_s = plotwav2(fn, fix=True, bw=bw, **kwargs)
xs, ys_m, ys_s = plotwav2(fno, fix=False, bw=bw, **kwargs)
2015-10-19 05:39:37 -07:00
2015-10-19 20:53:51 -07:00
fig, ax = new_response(-18, 18)
2017-09-21 04:04:22 -07:00
ax.set_title(
'averaged magnitudes of normalized songs with tilt and smoothing')
2015-10-19 20:53:51 -07:00
label = label or fn
ax.semilogx(xs, ys_m + 0, label=label+' (mid)')
ax.semilogx(xs, ys_s + 9, label=label+' (side)')
2015-10-19 05:39:37 -07:00
ax.legend(loc=8)
2019-01-02 06:45:12 -08:00
return fig, ax