dsp/lib/plotwav.py

92 lines
2.6 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
from . import wav_read, normalize, averfft, tilter2, smoothfft2, 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)
xs, ys = smoothfft(xs_raw, ys_raw, bw=bw)
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
2015-10-19 20:53:51 -07:00
def plotwavinternal(sm, ss, srate, bw=1, size=8192, smoother=smoothfft2):
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
2015-10-19 20:53:51 -07:00
xs, ys_m = smoother(xs_raw, ys_raw_m, bw=bw)
xs, ys_s = smoother(xs_raw, ys_raw_s, bw=bw)
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 plotwav2(fn, bw=1, size=8192, fix=False,
smoother=smoothfft2, **kwargs):
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)
if s.shape[1] == 2:
ss = monoize(s*np.array((1, -1)))
else:
ss = np.zeros(len(s))
2015-10-19 05:39:37 -07:00
2015-10-19 20:53:51 -07:00
xs, ys_m, ys_s = plotwavinternal(sm, ss, srate, bw, size, smoother)
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)
sf = np.array((smf + ssf, smf - ssf)).T
2015-10-19 05:39:37 -07:00
2015-10-19 20:53:51 -07:00
import ewave
2017-09-21 04:04:22 -07:00
with ewave.open(fno, 'w', sampling_rate=srate,
nchannels=count_channels(sf)) as f:
2015-10-19 20:53:51 -07:00
f.write(sf)
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)