Esempio n. 1
0
    def _test_scale_dependence(std_t_nd0, std_t_d0, mu0, scale0, N0, th):
        """Nondimensional: independent* -- Dimensional: std_t ~ scale.

        *Nondimensional breaks down for low scales (<~3), where freq-domain
        wavelet is trimmed (even beyond mode), deviating center frequency from
        continuous-time counterpart.

        Particularly large `th` per default `(min_decay, max_mult) = (1e6, 2)`
        in `time_resolution`, which reasonably limits the extended wavelet
        duration in time-domain in (paddded) CWT (but this limitation might be
        undue; I'm unsure. https://dsp.stackexchange.com/q/70810/50076).

        _nd here is just an extra division by 'peak' center_frequency.
        """
        scales = 2**(np.arange(16, 81) / 8)  # [4, ..., 1024]
        errs = np.zeros((2, len(scales)))
        wavelet = Wavelet(('morlet', {'mu': mu0}))
        kw = dict(wavelet=wavelet, N=N0, force_int=False)

        for i, scale in enumerate(scales):
            std_t_nd = time_resolution(**kw, scale=scale, nondim=True)
            std_t_d  = time_resolution(**kw, scale=scale, nondim=False)

            errs[0, i] = abs(std_t_nd  - std_t_nd0)
            errs[1, i] = abs((std_t_d / std_t_d0) - (scale / scale0))

        _assert_and_viz(th, errs, 2*[np.log2(scales)], 'log2(scale)',
                        "Time resolution, morlet")
Esempio n. 2
0
    def _test_mu_dependence(std_t_nd0, std_t_d0, mu0, scale0, N0, th):
        """Nondimensional: std_t ~ 1/mu -- Dimensional: independent"""
        mus = np.arange(mu0 + 1, 21)
        errs = np.zeros((2, len(mus)))

        for i, mu in enumerate(mus):
            wavelet = Wavelet(('morlet', {'mu': mu, 'dtype': 'float64'}))
            std_t_nd = time_resolution(wavelet, scale0, N0, nondim=True)
            std_t_d = time_resolution(wavelet, scale0, N0, nondim=False)

            errs[0, i] = abs((std_t_nd / std_t_nd0) - (mu / mu0))
            errs[1, i] = abs(std_t_d - std_t_d0)

        _assert_and_viz(th, errs, 2 * [mus], 'mu', "Time resolution, morlet")
Esempio n. 3
0
    def _test_N_dependence(std_t_nd0, std_t_d0, mu0, scale0, N0, th):
        """Independent

        `th` can be 1e-14 if dropping odd-sampled case where time-domain wavelet
        has suboptimal decay: https://github.com/jonathanlilly/jLab/issues/13
        (also dropping low-sampled case)
        """
        Ns = (np.array([.1, 1 / 3, .5, 2, 4, 9]) * N0).astype('int64')
        errs = np.zeros((2, len(Ns)))
        wavelet = Wavelet(('morlet', {'mu': mu0, 'dtype': 'float64'}))

        for i, N in enumerate(Ns):
            std_t_nd = time_resolution(wavelet, scale0, N, nondim=True)
            std_t_d = time_resolution(wavelet, scale0, N, nondim=False)

            errs[0, i] = abs(std_t_nd - std_t_nd0)
            errs[1, i] = abs(std_t_d - std_t_d0)

        _assert_and_viz(th, errs, [Ns, Ns], 'N', "Time resolution, morlet")
Esempio n. 4
0
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))
Esempio n. 5
0
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')
Esempio n. 6
0
def test_time_resolution():
    """Some thresholds are quite large per center_frequency(, kind='peak') as
    opposed to 'energy', with force_int=True, especially for large scales.
    These are per great deviations from continuous-time counterparts,
    but this is simply a reflection of discretization limitations (trimmed or
    unsmooth bell) and finite precision error.
    """
    def _test_mu_dependence(std_t_nd0, std_t_d0, mu0, scale0, N0, th):
        """Nondimensional: std_t ~ 1/mu -- Dimensional: independent"""
        mus = np.arange(mu0 + 1, 21)
        errs = np.zeros((2, len(mus)))

        for i, mu in enumerate(mus):
            wavelet = Wavelet(('morlet', {'mu': mu}))
            std_t_nd = time_resolution(wavelet, scale0, N0, nondim=True)
            std_t_d  = time_resolution(wavelet, scale0, N0, nondim=False)

            errs[0, i] = abs((std_t_nd / std_t_nd0) - (mu / mu0))
            errs[1, i] = abs(std_t_d - std_t_d0)

        _assert_and_viz(th, errs, 2*[mus], 'mu',
                        "Time resolution, morlet")

    def _test_scale_dependence(std_t_nd0, std_t_d0, mu0, scale0, N0, th):
        """Nondimensional: independent* -- Dimensional: std_t ~ scale.

        *Nondimensional breaks down for low scales (<~3), where freq-domain
        wavelet is trimmed (even beyond mode), deviating center frequency from
        continuous-time counterpart.

        Particularly large `th` per default `(min_decay, max_mult) = (1e6, 2)`
        in `time_resolution`, which reasonably limits the extended wavelet
        duration in time-domain in (paddded) CWT (but this limitation might be
        undue; I'm unsure. https://dsp.stackexchange.com/q/70810/50076).

        _nd here is just an extra division by 'peak' center_frequency.
        """
        scales = 2**(np.arange(16, 81) / 8)  # [4, ..., 1024]
        errs = np.zeros((2, len(scales)))
        wavelet = Wavelet(('morlet', {'mu': mu0}))
        kw = dict(wavelet=wavelet, N=N0, force_int=False)

        for i, scale in enumerate(scales):
            std_t_nd = time_resolution(**kw, scale=scale, nondim=True)
            std_t_d  = time_resolution(**kw, scale=scale, nondim=False)

            errs[0, i] = abs(std_t_nd  - std_t_nd0)
            errs[1, i] = abs((std_t_d / std_t_d0) - (scale / scale0))

        _assert_and_viz(th, errs, 2*[np.log2(scales)], 'log2(scale)',
                        "Time resolution, morlet")

    def _test_N_dependence(std_t_nd0, std_t_d0, mu0, scale0, N0, th):
        """Independent

        `th` can be 1e-14 if dropping odd-sampled case where time-domain wavelet
        has suboptimal decay: https://github.com/jonathanlilly/jLab/issues/13
        (also dropping low-sampled case)
        """
        Ns = (np.array([.1, 1/3, .5, 2, 4, 9]) * N0).astype('int64')
        errs = np.zeros((2, len(Ns)))
        wavelet = Wavelet(('morlet', {'mu': mu0}))

        for i, N in enumerate(Ns):
            std_t_nd = time_resolution(wavelet, scale0, N, nondim=True)
            std_t_d  = time_resolution(wavelet, scale0, N, nondim=False)

            errs[0, i] = abs(std_t_nd - std_t_nd0)
            errs[1, i] = abs(std_t_d  - std_t_d0)

        _assert_and_viz(th, errs, [Ns, Ns], 'N',
                        "Time resolution, morlet")

    mu0 = 5
    scale0 = 10
    N0 = 1024

    wavelet0 = Wavelet(('morlet', {'mu': mu0}))
    std_t_nd0 = time_resolution(wavelet0, scale0, N0, nondim=True)
    std_t_d0  = time_resolution(wavelet0, scale0, N0, nondim=False)

    args = (std_t_nd0, std_t_d0, mu0, scale0, N0)
    _test_mu_dependence(   *args, th=[1e-1, 1e-6])
    _test_scale_dependence(*args, th=[2e-0, 1e-1])
    _test_N_dependence(    *args, th=[1e-1, 1e-8])