def viz(wavelet, scales, scaletype, show_last, nv): plot(scales, show=1, title="scales | scaletype=%s, nv=%s" % (scaletype, nv)) if scaletype == 'log-piecewise': extra = ", logscale_transition_idx=%s" % logscale_transition_idx( scales) else: extra = "" print("n_scales={}, max(scales)={:.1f}{}".format(len(scales), scales.max(), extra)) psih = wavelet(scale=scales) last_psihs = psih[-show_last:] # find xmax of plot least_large = last_psihs[0] mx_idx = np.argmax(least_large) last_nonzero_idx = np.where( least_large[mx_idx:] < least_large.max() * .1)[0][0] last_nonzero_idx += mx_idx + 2 plot(last_psihs.T[:last_nonzero_idx], color='tab:blue', show=1, title="Last %s largest scales" % show_last)
def viz(x, Tf, ridge_idxs, yticks=None, ssq=False, transform='cwt', show_x=True): if show_x: plot(x, title="x(t)", show=1, xlabel="Time [samples]", ylabel="Signal Amplitude [A.U.]") ylabel = ("Frequency scales [1/Hz]" if (transform == 'cwt' and not ssq) else "Frequencies [Hz]") title = "abs({}{}) w/ ridge_idxs".format("SSQ_" if ssq else "", transform.upper()) ikw = dict(abs=1, cmap='turbo', yticks=yticks, title=title) pkw = dict(linestyle='--', color='k', xlabel="Time [samples]", ylabel=ylabel, xlims=(0, Tf.shape[1])) imshow(Tf, **ikw, show=0) plot(ridge_idxs, **pkw, show=1)
def test_visuals(): os.environ['SSQ_GPU'] = '0' x = np.random.randn(10) visuals.hist(x, show=1, stats=1) y = x * (1 + 1j) visuals.plot(y, complex=1, c_annot=1, vlines=1, ax_equal=1, xticks=np.arange(len(y)), yticks=y) visuals.plot(y, abs=1, vert=1, dx1=1, ticks=0) visuals.scat(x, vlines=1, hlines=1) visuals.scat(y, complex=1, ticks=0) visuals.plotscat(y, show=1, xlims=(-1, 1), dx1=1, ylabel="5") visuals.plots([y, y], tight=1, show=1) visuals.plots([y, y], nrows=2) visuals.plots([y, y], ncols=2) g = np.random.randn(4, 4) visuals.imshow(g * (1 + 2j), complex=1) visuals.imshow(g, ridge=1, ticks=0) pass_on_error(visuals.plot, None, None) pass_on_error(visuals.wavelet_tf, 'morlet', notext=True)
def test_visuals(): x = np.random.randn(10) hist(x, show=1, stats=1) y = x * (1 + 1j) plot(y, complex=1, c_annot=1, vlines=1, ax_equal=1, xticks=np.arange(len(y)), yticks=y) plot(y, abs=1, vert=1, dx1=1, ticks=0) scat(x, vlines=1, hlines=1) scat(y, complex=1, ticks=0) plotscat(y, show=1, xlims=(-1, 1), dx1=1, ylabel="5") plots([y, y], tight=1, show=1) plots([y, y], nrows=2) plots([y, y], ncols=2) g = np.random.randn(4, 4) imshow(g * (1 + 2j), complex=1) imshow(g, ridge=1, ticks=0) _pass_on_error(plot, None, None)
def _maybe_viz(Wx, xo, xrec, title, err): if not VIZ: return mx = np.abs(Wx).max() if 'SSQ' in title: Wx = np.pad(np.flipud(Wx), [[5], [0]]) mx = .1*mx else: mx = .9*mx imshow(Wx, abs=1, norm=(0, mx), cmap='jet', show=1, title=title) plot(xo, title="Original vs reconstructed | MAD/RMS=%.4f" % err) plot(xrec, show=1)
#%% row_anim(Wxz, idxs, scales) #%%## Superimpose #### row_anim(Wxz, idxs, scales, superposed=True) #%%## Synchrosqueeze Tx, _, ssq_freqs, *_ = ssq_cwt(x, wavelet, t=_t(0, 1, N)) #%% imshow(Tx, abs=1, title="abs(SSWT)", yticks=ssq_freqs, show=1) #%%# Damped pendulum example ################################################ N, w0 = 4096, 25 t = _t(0, 6, N) s = np.exp(-t) * np.cos(w0 * t) w = np.linspace(-40, 40, N) S = (1 + 1j * w) / ((1 + 1j * w)**2 + w0**2) #%%# Plot #### plot(s, title="s(t)", show=1) plot(w, np.abs(S), title="abs(FT(s(t)))", show=1) #%%# Now SSWT ## wavelet = ('morlet', {'mu': 5}) Tx, *_ = ssq_cwt(s, wavelet, t=t) #%%# 'cheat' a little; could use boundary wavelets instead (not implemented) aTxz = np.abs(Tx)[:, len(t) // 8:] imshow(aTxz, abs=1, title="abs(SSWT(s(t)))", show=1, cmap='bone') #%% mxidx = np.where(np.abs(aTxz) == np.abs(aTxz).max())[0][0] plot(aTxz[mxidx], title="max row of abs(SSWT(s(t)))", show=1)
#%%## Configure signal ####################################################### N = 2048 noise_var = 6 # noise variance; compare error against = 12 x, ts = echirp(N) x *= (1 + .3 * cos_f([1], N)) # amplitude modulation xo = x.copy() np.random.seed(4) x += np.sqrt(noise_var) * np.random.randn(len(x)) #### Show signal & its global spectrum ####################################### axf = np.abs(rfft(x)) plot(xo) scat(xo, s=8, show=1) plot(x) scat(x, s=8, show=1) plot(axf, show=1) #%%# Synchrosqueeze ########################################################## kw = dict(wavelet=('morlet', {'mu': 4.5}), nv=32, scales='log') Tx, *_ = ssq_cwt(x, t=ts, **kw) Wx, *_ = cwt(x, t=ts, **kw) #%%# Visualize ############################################################### pkw = dict(abs=1, cmap='bone') _Tx = np.pad(Tx, [[4, 4]]) # improve display of top- & bottom-most freqs imshow(Wx, **pkw) imshow(np.flipud(_Tx), norm=(0, 4e-1), **pkw)