def test_triangsmooth():
    data = [
        3.7352e+06, 1.104e+06, 1.088e+06, 1.0695e+06, 7.1923e+05, 1.2757e+06,
        1.2596e+05, 9.4364e+05, 5.8868e+05, 4.4942e+05, 6.768e+05, 4.0295e+05,
        5.1843e+05, 6.2502e+05, 4.6077e+05, 1.4937e+05, 5.366e+05, 1.4942e+05,
        2.4361e+05, 3.5926e+05
    ]
    # test a smooth function. take a parabola
    win_ratio = 0.04
    smooth = triangsmooth(np.array(data), winlen_ratio=win_ratio)
    assert all([
        smooth[i] <= max(data[i - 1:i + 2])
        and smooth[i] >= min(data[i - 1:i + 2])
        for i in range(1,
                       len(data) - 1)
    ])
    assert np.allclose(smooth,
                       triangsmooth0(np.array(data), win_ratio),
                       rtol=1e-05,
                       atol=1e-08,
                       equal_nan=True)

    data = [x**2 for x in range(115)]
    smooth = triangsmooth(np.array(data), winlen_ratio=win_ratio)
    assert np.allclose(smooth, data, rtol=1e-03, atol=1e-08, equal_nan=True)
    assert np.allclose(smooth,
                       triangsmooth0(np.array(data), win_ratio),
                       rtol=1e-05,
                       atol=1e-08,
                       equal_nan=True)
Example #2
0
def _spectrum(trace, config):
    '''Calculate the spectrum of a trace. Returns the tuple (0, df, values), where
    values depends on the config dict parameters.
    Does not modify the trace in-place
    '''
    taper_max_percentage = config['sn_spectra']['taper']['max_percentage']
    taper_type = config['sn_spectra']['taper']['type']
    if config['sn_spectra']['type'] == 'pow':
        func = powspec  # copies the trace if needed
    elif config['sn_spectra']['type'] == 'amp':
        func = ampspec  # copies the trace if needed
    else:
        # raise TypeError so that if called from within main, the iteration stops
        raise TypeError(
            "config['sn_spectra']['type'] expects either 'pow' or 'amp'")

    df_, spec_ = func(trace,
                      taper_max_percentage=taper_max_percentage,
                      taper_type=taper_type)

    # if you want to implement your own smoothing, change the lines below before 'return'
    # and implement your own config variables, if any
    smoothing_wlen_ratio = config['sn_spectra']['smoothing_wlen_ratio']
    if smoothing_wlen_ratio > 0:
        spec_ = triangsmooth(spec_, winlen_ratio=smoothing_wlen_ratio)

    return (0, df_, spec_)