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 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(): 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)
np.cos(2 * np.pi * f * (_t(i, i + 1, N) + phi)) for i, f in enumerate(freqs) ]) #%%## Configure, compute, plot ############################################### wavelet = ('morlet', {'mu': 5}) f, N = 12, 512 x = cos_f([f], N=N) Wx, scales, *_ = cwt(x, wavelet, fs=N) #%%# Show, print max row imshow(Wx, abs=1, yticks=scales, title="f=%d, N=%d" % (f, N), show=1, cmap='bone') mxidx = np.where(np.abs(Wx) == np.abs(Wx).max())[0][0] print("Max row idx:", mxidx, flush=True) #%%# Plot aroundI max row idxs = slice(mxidx - 30, mxidx + 20) Wxz = Wx[idxs] imshow(Wxz, abs=1, title="abs(CWT), zoomed", show=0, cmap='bone') plt.axhline(30, color='r') plt.show() #%%## Animate rows ########################################################### def row_anim(Wxz, idxs, scales, superposed=False):
# downsampling factor for higher scales (used only if `scaletype='log-piecewise'`) downsample = 4 # show this many of lowest-frequency wavelets show_last = 20 #%%## Make scales ############################################################ # `cwt` uses `p2up`'d N internally M = p2up(N)[0] wavelet = Wavelet(wavelet, N=M) min_scale, max_scale = cwt_scalebounds(wavelet, N=len(x), preset=preset) scales = make_scales(N, min_scale, max_scale, nv=nv, scaletype=scaletype, wavelet=wavelet, downsample=downsample) #%%# Visualize scales ######################################################## viz(wavelet, scales, scaletype, show_last, nv) wavelet.viz('filterbank', scales=scales) #%%# Show applied ############################################################ Tx, Wx, ssq_freqs, scales, *_ = ssq_cwt(x, wavelet, scales=scales, padtype=padtype) imshow(Wx, abs=1, title="abs(CWT)") imshow(Tx, abs=1, title="abs(SSQ_CWT)")
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) #%%# Estimate inversion ridge ############################################### bw, slope, offset = .035, .45, .45 Cs, freqband = lin_band(Tx, slope, offset, bw, norm=(0, 4e-1)) #%%########################################################################### xrec = issq_cwt(Tx, kw['wavelet'], Cs, freqband)[0] plot(xo) plot(xrec, show=1) axof = np.abs(rfft(xo)) axrecf = np.abs(rfft(xrec)) plot(axof) plot(axrecf, show=1)
# print(Wx) print(len(Wx)) # print(Wx[1]) # print(len(Wx[1])) # for wx in Wx: # print(wx) # print("________________") # fig, ax = plt.subplots() # ax.plot(t, wx) # ax.set(xlabel='time (s)', ylabel='Frequency', # title='About as simple as it gets, folks') # ax.grid() # plt.show() imshow(Wx, abs=2, yticks=scales, title="f=%d, N=%d" % (f, N), show=1) # mxidx = np.where(np.abs(Wx) == np.abs(Wx).max())[0][0] # print("Max row idx:", mxidx, flush=True) #%%# Plot aroundI max row # idxs = slice(mxidx - 30, mxidx + 20) # Wxz = Wx[idxs] # imshow(Wxz, abs=1, title="abs(CWT), zoomed", show=0) # plt.axhline(30, color='r') # plt.show() #%%## Animate rows ########################################################### # def row_anim(Wxz, idxs, scales, superposed=False): # mx = np.max(np.abs(Wxz)) # for scale, row in zip(scales[idxs], Wxz): # if row.max() == Wxz.max():
# -*- coding: utf-8 -*- """Experimental feature example.""" if __name__ != '__main__': raise Exception("ran example file as non-main") import numpy as np from ssqueezepy import TestSignals, ssq_cwt, Wavelet from ssqueezepy.visuals import imshow from ssqueezepy.experimental import phase_ssqueeze #%% x = TestSignals(N=2048).par_lchirp()[0] x += x[::-1] wavelet = Wavelet() Tx0, Wx, _, scales, *_ = ssq_cwt(x, wavelet, get_dWx=1) Tx1, *_ = phase_ssqueeze(Wx, wavelet=wavelet, scales=scales, flipud=1) adiff = np.abs(Tx0 - Tx1) print(adiff.mean(), adiff.max(), adiff.sum()) #%% # main difference near boundaries; see `help(trigdiff)` w/ `rpadded=False` imshow(Tx1, abs=1)
def cos_f(freqs, N=128, phi=0): return np.concatenate([ np.cos(2 * np.pi * f * (_t(i, i + 1, N) + phi)) for i, f in enumerate(freqs) ]) #%%## Configure, compute, plot ############################################### wavelet = ('morlet', {'mu': 5}) f, N = 12, 512 x = cos_f([f], N=N) Wx, scales, *_ = cwt(x, wavelet, fs=N) #%%# Show, print max row imshow(Wx, abs=1, yticks=scales, title="f=%d, N=%d" % (f, N), show=1) mxidx = np.where(np.abs(Wx) == np.abs(Wx).max())[0][0] print("Max row idx:", mxidx, flush=True) #%%# Plot aroundI max row idxs = slice(mxidx - 30, mxidx + 20) Wxz = Wx[idxs] imshow(Wxz, abs=1, title="abs(CWT), zoomed", show=0) plt.axhline(30, color='r') plt.show() #%%## Animate rows ########################################################### def row_anim(Wxz, idxs, scales, superimposed=False): mx = np.max(np.abs(Wxz)) for scale, row in zip(scales[idxs], Wxz):
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(_Tx, norm=(0, 4e-1), **pkw) #%%# Estimate inversion ridge ############################################### bw, slope, offset = .035, -.46, -1.8 Cs, freqband = lin_band(Tx, slope, offset, bw, norm=(0, 4e-1)) #%%########################################################################### xrec = issq_cwt(Tx, kw['wavelet'], Cs, freqband)[0] plot(xo) plot(xrec, show=1) axof = np.abs(rfft(xo)) axrecf = np.abs(rfft(xrec)) plot(axof) plot(axrecf, show=1)