geocat.comp.fourier_filters.fourier_filter

Contents

geocat.comp.fourier_filters.fourier_filter#

geocat.comp.fourier_filters.fourier_filter(signal, frequency, cutoff_frequency_low=0, cutoff_frequency_high=0, time_axis=0, low_pass=False, high_pass=False, band_pass=False, band_block=False)#

Filter a dataset by frequency. This function allows for low_pass, high_pass, band_pass, or band_block filtering of the data’s frequency representation.

Parameters:
  • signal (numpy.ndarray, xarray.DataArray) – n-dimensional dataset

  • frequency (float) – sample frequency of dataset

  • cutoff_frequency_low (float, optional) – low frequency for cutting fourier transform, used by low_pass, band_pass, band_block. Defaults to 0.

  • cutoff_frequency_high (float, optional) – high frequency for cutting fourier transform, used by low_pass, band_pass, band_block. Defaults to 0.

  • time_axis (int, optional) – the time axis of the data set. Defaults to 0.

  • low_pass (bool, optional) – runs a low_pass filter on the data if set to True. Defaults to False.

  • high_pass (bool, optional) – runs a high_pass filter on the data if set to True. Defaults to False.

  • band_pass (bool, optional) – runs a band_pass filter on the data if set to True. Defaults to False.

  • band_block (bool, optional) – runs a band_block filter on the data if set to True. Defaults to False.

Returns:

return_signal (numpy.ndarray, xarray.DataArray) – signal with specified filters applied

Examples

Example 1: The tidal cycle needs to be removed from a 10/hr oceanic dataset

>>> from geocat.comp import fourier_filter
>>> import matplotlib.pyplot as plt
>>> from mpl_toolkits.mplot3d import Axes3D
>>> import numpy as np
>>> import pandas as pd
>>> import xarray as xr
>>> dataset = xr.DataArray(pd.read_csv('CO-OPS_9415020_wl.csv'))
>>> xr_data = dataset.loc[:,'Verified (ft)']
>>> data_freq = 10 #points per hour
>>> tide_freq1 = 1/(1*12.4206) #tides per hour
>>> tide_freq2 = 1/(2*12.4206) #tides per hour
>>> res = data_freq/(len(xr_data))
>>> cflow1 = tide_freq1 - res*5
>>> cfhigh1 = tide_freq1 + res*5
>>> cflow2 = tide_freq2 - res*5
>>> cfhigh2 = tide_freq2 + res*5
>>> fig, ax = plt.subplots(1,
...                        1,
...                        dpi=100,
...                        figsize=(8,4),
...                        constrained_layout=True)
>>> no_tide = xr_data
>>> ax.plot(no_tide[2000:3000])
>>> no_tide = fourier_filter(no_tide,
...                          data_freq,
...                          cutoff_frequency_low=cflow1,
...                          cutoff_frequency_high=cfhigh1,
...                          band_block=True)
>>> ax.plot(no_tide[2000:3000])
>>> no_tide = fourier_filter(no_tide,
...                          data_freq,
...                          cutoff_frequency_low=cflow2,
...                          cutoff_frequency_high=cfhigh2,
...                          band_block=True)
>>> ax.plot(no_tide[2000:3000])
>>> fig.show()
>>> fig, axs = plt.subplots(2,
...                         1,
...                         dpi=100,
...                         figsize=(8,4),
...                         constrained_layout=True)
>>> axs[0].set_title('real')
>>> axs[0].plot(np.real(np.fft.fft(xr_data)[1:100]))
>>> axs[0].plot(np.real(np.fft.fft(no_tide)[1:100]))
>>> axs[1].set_title('imag')
>>> axs[1].plot(np.imag(np.fft.fft(xr_data)[1:100]))
>>> axs[1].plot(np.imag(np.fft.fft(no_tide)[1:100]))
>>> fig.show()
>>> fig, axs = plt.subplots(2,
...                         1,
...                         dpi=100,
...                         figsize=(8,4),
...                         constrained_layout=True)
>>> start = 0
>>> end = -1
>>> axs[0].set_title('real')
>>> axs[0].plot(np.real(xr_data)[start:end])
>>> axs[0].plot(np.real(no_tide)[start:end])
>>> axs[1].set_title('imag')
>>> axs[1].plot(np.imag(xr_data)[start:end])
>>> axs[1].plot(np.imag(no_tide)[start:end])
>>> fig.show()