Identifying and rejecting bad waveforms

Hello, I am currently using Obspy to download data from the GEONET FDSN server for New Zealand data in order to calculate magnitude. I noticed some extremely high magnitudes for some of the data and soon discovered it is being caused by bad waveforms.

Does anyone have a suggestion for rejecting or correcting waveforms of this nature?

That is pretty horrible - I wouldn’t expect to try and correct these data, and would probably try to remove them.

In this case you might want to look for single sample differences and reject data with simple sample differences larger than some moving threshold:

import numpy as np
from obspy import Trace

def glitch_catcher(
    tr: Trace, 
    window_length: float, 
    threshold: float
   Catch glitches - returns True if a glitch is found.

        Trace to look for glitches in
        Window length to compute average differences over in seconds
        Threshold multiplier of average differences to declare a glitch

    window_length = int(window_length * tr.stats.sampling_rate)
    # Using a window length to try and avoid removing earthquake signals...

    diffs = np.abs([0:-1] -[1:])
    diff_moving = np.cumsum(diffs)
    diff_moving = diff_moving[window_length:] - diff_moving[0:-window_length:]
    diff_moving  = diff_moving / window_length
    # Extend diff_moving to the same length as diffs - not a great way to do this!
    diff_moving = np.concatenate(
        [np.array([diff_moving.mean()] * (len(diffs) - len(diff_moving))), 
    if np.any(diffs > diff_moving * threshold):
        print(f"Found large differences at {np.where(diffs > diff_moving * threshold)}")
        return True
    return False

Running this for your data catches those glitches, but I haven’t tried it extensively! I think it is very sensitive to threshold and window length and should be used with caution, but may help…

Hi Calum (this is Jesse), thanks for the feedback! I’ll give that a try :slight_smile:

It might be better to compare the differences to the window before and after the glitch to ensure that it isn’t the start of a high-amplitude event?