def _test_N_dependence(wc0, mu0, scale0, N0, th): """Independent""" Ns = (np.array([.25, .5, 2, 4, 9]) * N0).astype('int64') errs = np.zeros(len(Ns)) wavelet = Wavelet(('morlet', {'mu': mu0, 'dtype': 'float64'})) for i, N in enumerate(Ns): wc = center_frequency(wavelet, scale=scale0, N=N, kind='energy') errs[i] = abs(wc - wc0) _assert_and_viz(th, errs, Ns, 'N', "Center frequency (energy), morlet")
def _test_mu_dependence(wc0, mu0, scale0, N0, th): """wc ~ mu""" mus = np.arange(mu0 + 1, 21) errs = np.zeros(len(mus)) for i, mu in enumerate(mus): wavelet = Wavelet(('morlet', {'mu': mu})) wc = center_frequency(wavelet, scale=scale0, N=N0, kind='energy') errs[i] = abs((wc / wc0) - (mu / mu0)) _assert_and_viz(th, errs, mus, 'mu', "Center frequency (energy), morlet")
def test_utils(): os.environ['SSQ_GPU'] = '0' _ = utils.buffer(np.random.randn(20), 4, 1) wavelet = Wavelet(('morlet', {'mu': 6})) _ = wavelets.center_frequency(wavelet, viz=1) _ = wavelets.freq_resolution(wavelet, viz=1, scale=3, force_int=0) _ = wavelets.time_resolution(wavelet, viz=1) xh = np.random.randn(128) xhs = np.zeros(xh.size) wavelets._aifftshift_even(xh, xhs) wavelets._afftshift_even(xh, xhs) _ = utils.padsignal(xh, padlength=len(xh) * 2, padtype='symmetric') _ = utils.padsignal(xh, padlength=len(xh) * 2, padtype='wrap') x2d = np.random.randn(4, 64) _ = utils.padsignal(x2d, padlength=96, padtype='symmetric') g = np.ones((128, 200)) utils.unbuffer(g, xh, 1, n_fft=len(xh), N=None, win_exp=0) utils.unbuffer(g, xh, 1, n_fft=len(xh), N=g.shape[1], win_exp=2) scales = utils.process_scales('log', 1024, Wavelet()) _ = utils.find_downsampling_scale(Wavelet(), scales, method='any', viz_last=1) _ = utils.find_downsampling_scale(Wavelet(), scales, method='all') #### errors / warnings ################################################### pass_on_error(utils.find_max_scale, 1, 1, -1, -1) pass_on_error(utils.cwt_scalebounds, 1, 1, preset='etc', min_cutoff=0) pass_on_error(utils.cwt_scalebounds, 1, 1, min_cutoff=-1) pass_on_error(utils.cwt_scalebounds, 1, 1, min_cutoff=.2, max_cutoff=.1) pass_on_error(utils.cwt_scalebounds, 1, 1, cutoff=0) pass_on_error(utils.cwt_utils._assert_positive_integer, -1, 'w') pass_on_error(utils.infer_scaletype, 1) pass_on_error(utils.infer_scaletype, np.array([1])) pass_on_error(utils.infer_scaletype, np.array([1., 2., 5.])) pass_on_error(utils._process_fs_and_t, 1, np.array([1]), 2) pass_on_error(utils._process_fs_and_t, 1, np.array([1., 2, 4]), 3) pass_on_error(utils._process_fs_and_t, -1, None, 1) pass_on_error(utils.make_scales, 128, scaletype='banana') pass_on_error(utils.padsignal, np.random.randn(3, 4, 5))
def _test_scale_dependence_high(wc0, mu0, scale0, N0, th): """wc ~ 1/scale High `scale` subject to more significant discretization error in frequency domain. """ scales = 2**(np.arange(53, 81) / 8) # [90.5, ..., 1024] errs = np.zeros(len(scales)) wavelet = Wavelet(('morlet', {'mu': mu0})) for i, scale in enumerate(scales): wc = center_frequency(wavelet, scale=scale, N=N0, kind='energy') errs[i] = abs((wc / wc0) - (scale0 / scale)) _assert_and_viz(th, errs, np.log2(scales), 'log2(scale)', "Center frequency (energy), morlet | High scales")
def _test_scale_dependence(wc0, mu0, scale0, N0, th): """wc ~ 1/scale For small `scale`, the bell is trimmed and the (energy) center frequency is no longer at mode/peak (both of which are also trimmed), but is less; we don't test these to keep code clean. """ scales = 2**(np.arange(16, 53) / 8) # [4, ..., 90.5] errs = np.zeros(len(scales)) wavelet = Wavelet(('morlet', {'mu': mu0})) for i, scale in enumerate(scales): wc = center_frequency(wavelet, scale=scale, N=N0, kind='energy') errs[i] = abs((wc / wc0) - (scale0 / scale)) _assert_and_viz(th, errs, np.log2(scales), 'log2(scale)', "Center frequency (energy), morlet")
def test_utils(): _ = buffer(np.random.randn(20), 4, 1) wavelet = Wavelet(('morlet', {'mu': 6})) _ = center_frequency(wavelet, viz=1) _ = freq_resolution(wavelet, viz=1, scale=3, force_int=0) _ = time_resolution(wavelet, viz=1) xh = np.random.randn(128) xhs = np.zeros(xh.size) _aifftshift_even(xh, xhs) _afftshift_even(xh, xhs) _ = padsignal(xh, padlength=len(xh) * 2, padtype='symmetric') _ = padsignal(xh, padlength=len(xh) * 2, padtype='wrap') g = np.ones((128, 200)) unbuffer(g, xh, 1, n_fft=len(xh), N=None, win_exp=0) unbuffer(g, xh, 1, n_fft=len(xh), N=g.shape[1], win_exp=2) #### errors / warnings ################################################### _pass_on_error(find_max_scale, 1, 1, -1, -1) _pass_on_error(cwt_scalebounds, 1, 1, preset='etc', min_cutoff=0) _pass_on_error(cwt_scalebounds, 1, 1, min_cutoff=-1) _pass_on_error(cwt_scalebounds, 1, 1, min_cutoff=.2, max_cutoff=.1) _pass_on_error(cwt_scalebounds, 1, 1, cutoff=0) _pass_on_error(_assert_positive_integer, -1, 'w') _pass_on_error(_infer_scaletype, 1) _pass_on_error(_infer_scaletype, np.array([1])) _pass_on_error(_infer_scaletype, np.array([1., 2., 5.])) _pass_on_error(_process_fs_and_t, 1, np.array([1]), 2) _pass_on_error(_process_fs_and_t, 1, np.array([1., 2, 4]), 3) _pass_on_error(_process_fs_and_t, -1, None, 1) _pass_on_error(make_scales, 128, scaletype='banana')
def test_energy_center_frequency(): """If kind='energy' passes, long shot for 'peak' to fail, so just test former; would still be interesting to investigate 'peak' vs 'energy', esp. at scale extrema. """ def _test_mu_dependence(wc0, mu0, scale0, N0, th): """wc ~ mu""" mus = np.arange(mu0 + 1, 21) errs = np.zeros(len(mus)) for i, mu in enumerate(mus): wavelet = Wavelet(('morlet', {'mu': mu})) wc = center_frequency(wavelet, scale=scale0, N=N0, kind='energy') errs[i] = abs((wc / wc0) - (mu / mu0)) _assert_and_viz(th, errs, mus, 'mu', "Center frequency (energy), morlet") def _test_scale_dependence(wc0, mu0, scale0, N0, th): """wc ~ 1/scale For small `scale`, the bell is trimmed and the (energy) center frequency is no longer at mode/peak (both of which are also trimmed), but is less; we don't test these to keep code clean. """ scales = 2**(np.arange(16, 53) / 8) # [4, ..., 90.5] errs = np.zeros(len(scales)) wavelet = Wavelet(('morlet', {'mu': mu0})) for i, scale in enumerate(scales): wc = center_frequency(wavelet, scale=scale, N=N0, kind='energy') errs[i] = abs((wc / wc0) - (scale0 / scale)) _assert_and_viz(th, errs, np.log2(scales), 'log2(scale)', "Center frequency (energy), morlet") def _test_scale_dependence_high(wc0, mu0, scale0, N0, th): """wc ~ 1/scale High `scale` subject to more significant discretization error in frequency domain. """ scales = 2**(np.arange(53, 81) / 8) # [90.5, ..., 1024] errs = np.zeros(len(scales)) wavelet = Wavelet(('morlet', {'mu': mu0})) for i, scale in enumerate(scales): wc = center_frequency(wavelet, scale=scale, N=N0, kind='energy') errs[i] = abs((wc / wc0) - (scale0 / scale)) _assert_and_viz(th, errs, np.log2(scales), 'log2(scale)', "Center frequency (energy), morlet | High scales") def _test_N_dependence(wc0, mu0, scale0, N0, th): """Independent""" Ns = (np.array([.25, .5, 2, 4, 9]) * N0).astype('int64') errs = np.zeros(len(Ns)) wavelet = Wavelet(('morlet', {'mu': mu0})) for i, N in enumerate(Ns): wc = center_frequency(wavelet, scale=scale0, N=N, kind='energy') errs[i] = abs(wc - wc0) _assert_and_viz(th, errs, Ns, 'N', "Center frequency (energy), morlet") mu0 = 5 scale0 = 10 N0 = 1024 wavelet0 = Wavelet(('morlet', {'mu': mu0})) wc0 = center_frequency(wavelet0, scale=scale0, N=N0, kind='energy') args = (wc0, mu0, scale0, N0) _test_mu_dependence( *args, th=1e-7) _test_scale_dependence( *args, th=1e-14) _test_scale_dependence_high(*args, th=1e-1) _test_N_dependence( *args, th=1e-14)