mth5.timeseries.ts_filters

time series filters

Classes

RemoveInstrumentResponse

Remove instrument response from the given channel response filter

Functions

butter_bandpass(lowcut, highcut, sample_rate[, order])

Butterworth bandpass filter using scipy.signal

butter_bandpass_filter(data, lowcut, highcut, fs[, order])

low_pass(data, low_pass_freq, cutoff_freq, sample_rate)

zero_pad(input_array[, power, pad_fill])

adaptive_notch_filter(bx[, df, notches, notchradius, ...])

Module Contents

mth5.timeseries.ts_filters.butter_bandpass(lowcut, highcut, sample_rate, order=5)[source]

Butterworth bandpass filter using scipy.signal

Transforms band corners to angular frequencies

Parameters:
  • lowcut (float) – low cut frequency in Hz (3dB point)

  • highcut (float) – high cut frequency in Hz (3dB point)

  • sample_rate (float) – Sample rate

  • order (int, optional) – Butterworth order, defaults to 5

Returns:

SOS scipy.signal format

Return type:

scipy.signal.SOS?

mth5.timeseries.ts_filters.butter_bandpass_filter(data, lowcut, highcut, fs, order=5)[source]
Parameters:
  • data (np.ndarray) – 1D time series data

  • lowcut (float) – low cut frequency in Hz

  • highcut (float) – high cut frequency in Hz

  • fs (float) – Sample rate

  • order (int, optional) – Butterworth order, defaults to 5

Returns:

filtered data

Return type:

np.ndarray

mth5.timeseries.ts_filters.low_pass(data, low_pass_freq, cutoff_freq, sample_rate)[source]
Parameters:
  • data (np.ndarray) – 1D time series data

  • low_pass_freq (float) – low pass frequency in Hz

  • cutoff_freq (float) – cut off frequency in Hz

  • sampling_rate (float) – Sample rate in samples per second

Returns:

lowpass filtered data

Return type:

np.ndarray

mth5.timeseries.ts_filters.zero_pad(input_array, power=2, pad_fill=0)[source]
Parameters:
  • input_array (np.ndarray) – 1D array

  • power – base power to used to pad to, defaults to 2 which is optimal

for the FFT :type power: int, optional :param pad_fill: fill value for padded values, defaults to 0 :type pad_fill: float, optional :return: zero padded array :rtype: np.ndarray

class mth5.timeseries.ts_filters.RemoveInstrumentResponse(ts, time_array, sample_interval, channel_response, **kwargs)[source]

Remove instrument response from the given channel response filter

The order of operations is important (if applied):

  1. detrend

  2. zero mean

  3. zero pad

  4. time window

  5. frequency window

  6. remove response

  7. undo time window

  8. bandpass

Parameters:
  • ts (np.ndarray((N,) , dtype=float)) – time series data to remove response from

  • time_array (np.ndarray((N,) , dtype=np.datetime[ns])) – time index that corresponds to the time series

  • sample_interval (float) – seconds per sample (time interval between samples)

  • channel_response – Channel response filter with all filters

included to convert from counts to physical units :type channel_response: class:mt_metadata.timeseries.filters.ChannelResponse`

kwargs

Parameters:
  • plot (boolean, default True) – to plot the calibration process [ False | True ]

  • detrend (boolean, default True) – Remove linar trend of the time series

  • zero_mean (boolean, default True) – Remove the mean of the time series

  • zero_pad (boolean, default True) – pad the time series to the next power of 2 for efficiency

  • t_window (string, default None) – Time domain window name see scipy.signal.windows for options

  • t_window_params – Time domain window parameters, parameters can be

found in scipy.signal.windows :type t_window_params: dictionary :param f_window: Frequency domain window name see scipy.signal.windows for options :type f_window: string, default None :param f_window_params: Frequency window parameters, parameters can be found in scipy.signal.windows :type f_window_params: dictionary :param bandpass: bandpass frequency and order {“low”:, “high”:, “order”:,} :type bandpass: dictionary

logger[source]
ts[source]
time_array[source]
sample_interval[source]
channel_response[source]
plot = False[source]
detrend = True[source]
zero_mean = True[source]
zero_pad = True[source]
t_window = None[source]
t_window_params[source]
f_window = None[source]
f_window_params[source]
bandpass[source]
fig = None[source]
nrows = None[source]
subplot_dict[source]
include_decimation = False[source]
include_delay = False[source]
static get_window(window, window_params, size)[source]

Get window from scipy.signal

Parameters:
  • window (string) – name of the window

  • window_params (dictionary) – dictionary of window parameters

  • size (integer) – number of points in the window

Returns:

window function

Return type:

class:scipy.signal

apply_detrend(ts)[source]

Detrend time series using scipy.detrend(‘linear’)

Parameters:

ts (np.ndarray) – input time series

Returns:

detrended time series

Return type:

np.ndarray

apply_zero_mean(ts)[source]

Remove the mean from the time series

Parameters:

ts (np.ndarray) – input time series

Returns:

zero mean time series

Return type:

np.ndarray

apply_zero_pad(ts)[source]

zero pad to power of 2, at the end of the time series to make the FFT more efficient

Parameters:

ts (np.ndarray) – input time series

Returns:

zero padded time series

Return type:

np.ndarray

apply_t_window(ts)[source]

Apply a window in the time domain. Get the available windows from scipy.signal.windows

Parameters:

ts (np.ndarray) – input time series

Returns:

windowed time series

Return type:

np.ndarray

apply_f_window(data)[source]

Apply a frequency domain window. Get the available windows from scipy.signal.windows

Need to create a window twice the size of the input because we are only taking the rfft which gives just half the spectra and then take only half the window

Parameters:

data (np.ndarray) – input spectra

Returns:

windowed spectra

Return type:

np.ndarray

apply_bandpass(ts)[source]

apply a bandpass filter to the calibrated data

Parameters:

ts (np.ndarray) – calibrated time series

Returns:

bandpassed time series

Return type:

np.ndarray

remove_instrument_response(operation='divide', include_decimation=None, include_delay=None, filters_to_remove=[])[source]

Remove instrument response following the recipe provided

Returns:

calibrated time series

Return type:

np.ndarray

mth5.timeseries.ts_filters.adaptive_notch_filter(bx, df=100, notches=[50, 100], notchradius=0.5, freqrad=0.9, rp=0.1, dbstop_limit=5.0)[source]
Parameters:
  • bx (np.ndarray) – time series to filter

  • df (float, optional) – sample rate in samples per second, defaults to 100

  • notches (list, optional) – list of frequencies to locate notches at in Hz, defaults to [50, 100]

  • notchradius (float, optional) – notch radius, defaults to 0.5

  • freqrad (float, optional) – radius to search for a peak at the notch frequency, defaults to 0.9

  • rp (float, optional) – ripple of Chebyshev type 1 filter, lower numbers means less ripples, defaults to 0.1

  • dbstop_limit (float, optional) – limits the difference between the peak at the notch and surrounding spectra. Any difference above dbstop_limit will be filtered, anything less will not, defaults to 5.0

Returns:

notch filtered data

Return type:

np.ndarray

Returns:

list of notch frequencies

Return type:

list

Example

>>> import RemovePeriodicNoise_Kate as rmp
>>> # make a variable for the file to load in
>>> fn = r"/home/MT/mt01_20130101_000000.BX"
>>> # load in file, if the time series is not an ascii file
>>> # might need to add keywords to np.loadtxt or use another
>>> # method to read in the file
>>> bx = np.loadtxt(fn)
>>> # create a list of frequencies to filter out
>>> freq_notches = [50, 150, 200]
>>> # filter data
>>> bx_filt, filt_lst = rmp.adaptiveNotchFilter(bx, df=100.
>>> ...                                         notches=freq_notches)
>>> #save the filtered data into a file
>>> np.savetxt(r"/home/MT/Filtered/mt01_20130101_000000.BX", bx_filt)

Notes

Most of the time the default parameters work well, the only thing you need to change is the notches and perhaps the radius. I would test it out with a few time series to find the optimum parameters. Then make a loop over all you time series data. Something like

>>> import os
>>> dirpath = r"/home/MT"
>>> #make a director to save filtered time series
>>> save_path = r"/home/MT/Filtered"
>>> if not os.path.exists(save_path):
>>>     os.mkdir(save_path)
>>> for fn in os.listdir(dirpath):
>>>     bx = np.loadtxt(os.path.join(dirpath, fn)
>>>     bx_filt, filt_lst = rmp.adaptiveNotchFilter(bx, df=100.
>>>     ...                                         notches=freq_notches)
>>>     np.savetxt(os.path.join(save_path, fn), bx_filt)