-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
4,551 additions
and
1,139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# local testing outputs | ||
tests/wavanim* | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Show CWT with higher-order Generalized Morse Wavelets on parallel reflect-added | ||
linear chirps, with and without noise, and show GMW waveforms. | ||
""" | ||
import numpy as np | ||
from ssqueezepy import cwt, TestSignals | ||
from ssqueezepy.visuals import viz_cwt_higher_order, viz_gmw_orders | ||
|
||
#%%# CWT with higher-order GMWs ############################################# | ||
N = 1024 | ||
order = 2 | ||
|
||
tsigs = TestSignals() | ||
x, t = tsigs.par_lchirp(N=N) | ||
x += x[::-1] | ||
|
||
for noise in (False, True): | ||
if noise: | ||
x += np.random.randn(len(x)) | ||
Wx_k, scales = cwt(x, 'gmw', order=range(order + 1), average=False) | ||
|
||
viz_cwt_higher_order(Wx_k, scales, 'gmw') | ||
print("=" * 80) | ||
|
||
#%%# Higher-order GMWs ####################################################### | ||
gamma, beta, norm = 3, 60, 'bandpass' | ||
n_orders = 3 | ||
scale = 5 | ||
|
||
viz_gmw_orders(N, n_orders, scale, gamma, beta, norm) |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Shows methods to use for CWT scales selection; also see their docstrings.""" | ||
import numpy as np | ||
from ssqueezepy import ssq_cwt, Wavelet | ||
from ssqueezepy.visuals import imshow, plot | ||
from ssqueezepy.utils import cwt_scalebounds, make_scales, p2up | ||
from ssqueezepy.utils import logscale_transition_idx | ||
|
||
#%%# Helper visual method #################################################### | ||
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) | ||
|
||
#%%# EDIT HERE ############################################################### | ||
# signal length | ||
N = 2048 | ||
# your signal here | ||
t = np.linspace(0, 1, N, endpoint=False) | ||
x = np.cos(2*np.pi * 16 * t) + np.sin(2*np.pi * 64 * t) | ||
|
||
# choose wavelet | ||
wavelet = 'gmw' | ||
# choose padding scheme for CWT (doesn't affect scales selection) | ||
padtype = 'reflect' | ||
|
||
# one of: 'log', 'log-piecewise', 'linear' | ||
# 'log-piecewise' lowers low-frequency redundancy; see | ||
# https://github.com/OverLordGoldDragon/ssqueezepy/issues/29#issuecomment-778526900 | ||
scaletype = 'log-piecewise' | ||
# one of: 'minimal', 'maximal', 'naive' (not recommended) | ||
preset = 'maximal' | ||
# number of voices (wavelets per octave); more = more scales | ||
nv = 32 | ||
# 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)") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# -*- coding: utf-8 -*- | ||
import numpy as np | ||
import scipy.signal as sig | ||
from ssqueezepy import Wavelet, TestSignals | ||
from ssqueezepy.utils import window_resolution | ||
|
||
tsigs = TestSignals(N=512) | ||
#%%# Viz signals ############################################################# | ||
# set `dft` to 'rows' or 'cols' to also plot signals' DFT, along rows or columns | ||
dft = (None, 'rows', 'cols')[0] | ||
tsigs.demo(dft=dft) | ||
|
||
#%%# How to specify `signals` ################################################ | ||
signals = [ | ||
'am-cosine', | ||
('hchirp', dict(fmin=.2)), | ||
('sine:am-cosine', (dict(f=32, phi0=1), dict(amin=.3))), | ||
] | ||
tsigs.demo(signals, N=512) | ||
|
||
#%%# With `dft` ################## | ||
tsigs.demo(signals, dft='rows') | ||
tsigs.demo(signals, dft='cols') | ||
|
||
#%%# Viz CWT & SSQ_CWT with different wavelets ############################### | ||
tsigs = TestSignals(N=512) | ||
wavelets = [Wavelet(('gmw', {'beta': 5})), | ||
Wavelet(('gmw', {'beta': 22}))] | ||
tsigs.wavcomp(wavelets) | ||
|
||
#%%# | ||
tsigs.wavcomp(wavelets, signals=[('#echirp', dict(fmin=.1))], N=512) | ||
|
||
#%%# Viz CWT vs STFT (& SSQ'd) ############################################### | ||
# (N, beta, NW): (512, 42.5, 255); (256, 21.5, 255) | ||
N = 512 | ||
n_fft = N | ||
win_len = n_fft // 2 | ||
tsigs = TestSignals(N=N) | ||
wavelet = Wavelet(('GMW', {'beta': 21.5})) | ||
|
||
NW = win_len//2 - 1 | ||
window = np.abs(sig.windows.dpss(win_len, NW)) | ||
window = np.pad(window, win_len//2) | ||
window_name = 'DPSS' | ||
config_str = '\nNW=%s, win_pad_len=%s' % (NW, len(window) - win_len) | ||
|
||
# ensure `wavelet` and `window` have ~same time & frequency resolutions | ||
print("std_w, std_t, harea\nwavelet: {:.4f}, {:.4f}, {:.8f}" | ||
"\nwindow: {:.4f}, {:.4f}, {:.8f}".format( | ||
wavelet.std_w, wavelet.std_t, wavelet.harea, | ||
*window_resolution(window))) | ||
#%% | ||
tsigs.cwt_vs_stft(wavelet, window, signals='all', N=N, win_len=win_len, | ||
n_fft=n_fft, window_name=window_name, config_str=config_str) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ coverage | |
coveralls | ||
pytest | ||
pytest-cov | ||
pycode | ||
pycode | ||
librosa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.