def test_spectrum():
    freq = np.logspace(-2, 2, 101)
    moment = spectrum.moment_from_magnitude(6.7)
    mod = spectrum.model((moment, 150), freq, 10, kappa=0.035)
    np.testing.assert_allclose(mod[0], 0.21764373, atol=1e-5)
    np.testing.assert_allclose(mod[50], 113.5025146, atol=1e-5)
    np.testing.assert_allclose(mod[-1], 0.0032295, atol=1e-5)
def snr_check(tr,
              mag,
              threshold=3.0,
              min_freq='f0',
              max_freq=5.0,
              f0_options={
                  'stress_drop': 10,
                  'shear_vel': 3.7,
                  'ceiling': 2.0,
                  'floor': 0.1
              }):
    """
    Check signal-to-noise ratio.

    Requires noise/singal windowing to have succeeded.

    Args:
        tr (StationTrace):
            Trace of data.
        threshold (float):
            Threshold SNR value.
        min_freq (float or str):
            Minimum frequency for threshold to be exeeded. If 'f0', then the
            Brune corner frequency will be used.
        max_freq (float):
            Maximum frequency for threshold to be exeeded.
        bandwidth (float):
            Konno-Omachi smoothing bandwidth parameter.
        f0_options (dict):
            Dictionary of f0 options (see config file).

    Returns:
        trace: Trace with SNR check.
    """
    if tr.hasCached('snr'):
        snr_dict = tr.getCached('snr')
        snr = np.array(snr_dict['snr'])
        freq = np.array(snr_dict['freq'])

        # If min_freq is 'f0', then compute Brune corner frequency
        if min_freq == 'f0':
            min_freq = brune_f0(moment_from_magnitude(mag),
                                f0_options['stress_drop'],
                                f0_options['shear_vel'])
            if min_freq < f0_options['floor']:
                min_freq = f0_options['floor']
            if min_freq > f0_options['ceiling']:
                min_freq = f0_options['ceiling']

        # Check if signal criteria is met
        mask = (freq >= min_freq) & (freq <= max_freq)
        if np.any(mask):
            min_snr = np.min(snr[mask])
        else:
            min_snr = 0

        if min_snr < threshold:
            tr.fail('Failed SNR check; SNR less than threshold.')
    snr_conf = {
        'threshold': threshold,
        'min_freq': min_freq,
        'max_freq': max_freq
    }
    tr.setParameter('snr_conf', snr_conf)
    return tr
Exemple #3
0
def snr_check(
    tr,
    mag,
    threshold=3.0,
    min_freq="f0",
    max_freq=5.0,
    f0_options={
        "stress_drop": 10,
        "shear_vel": 3.7,
        "ceiling": 2.0,
        "floor": 0.1
    },
):
    """
    Check signal-to-noise ratio.

    Requires noise/singal windowing to have succeeded.

    Args:
        tr (StationTrace):
            Trace of data.
        threshold (float):
            Threshold SNR value.
        min_freq (float or str):
            Minimum frequency for threshold to be exeeded. If 'f0', then the
            Brune corner frequency will be used.
        max_freq (float):
            Maximum frequency for threshold to be exeeded.
        bandwidth (float):
            Konno-Omachi smoothing bandwidth parameter.
        f0_options (dict):
            Dictionary of f0 options (see config file).

    Returns:
        trace: Trace with SNR check.
    """
    if tr.hasCached("snr"):
        snr_dict = tr.getCached("snr")
        snr = np.array(snr_dict["snr"])
        freq = np.array(snr_dict["freq"])

        # If min_freq is 'f0', then compute Brune corner frequency
        if min_freq == "f0":
            min_freq = brune_f0(
                moment_from_magnitude(mag),
                f0_options["stress_drop"],
                f0_options["shear_vel"],
            )
            if min_freq < f0_options["floor"]:
                min_freq = f0_options["floor"]
            if min_freq > f0_options["ceiling"]:
                min_freq = f0_options["ceiling"]

        # Check if signal criteria is met
        mask = (freq >= min_freq) & (freq <= max_freq)
        if np.any(mask):
            min_snr = np.min(snr[mask])
        else:
            min_snr = 0

        if min_snr < threshold:
            tr.fail("Failed SNR check; SNR less than threshold.")
    snr_conf = {
        "threshold": threshold,
        "min_freq": min_freq,
        "max_freq": max_freq
    }
    tr.setParameter("snr_conf", snr_conf)
    return tr