oceans.filters

oceans.filters.fft_lowpass(signal, low, high)[source]

Performs a low pass filer on the series. low and high specifies the boundary of the filter.

>>> from oceans.filters import fft_lowpass
>>> import matplotlib.pyplot as plt
>>> t = np.arange(500)  # Time in hours.
>>> x = 2.5 * np.sin(2 * np.pi * t / 12.42)
>>> x += 1.5 * np.sin(2 * np.pi * t / 12.0)
>>> x += 0.3 * np.random.randn(len(t))
>>> filtered = fft_lowpass(x, low=1 / 30, high=1 / 40)
>>> fig, ax = plt.subplots()
>>> (l1,) = ax.plot(t, x, label="original")
>>> (l2,) = ax.plot(t, filtered, label="filtered")
>>> legend = ax.legend()
oceans.filters.lanc(numwt, haf)[source]

Generates a numwt + 1 + numwt lanczos cosine low pass filter with -6dB (1/4 power, 1/2 amplitude) point at haf

Parameters:
  • numwt (int) – number of points

  • haf (float) – frequency (in ‘cpi’ of -6dB point, ‘cpi’ is cycles per interval. For hourly data cpi is cph,

Examples

>>> from oceans.filters import lanc
>>> import matplotlib.pyplot as plt
>>> t = np.arange(500)  # Time in hours.
>>> h = 2.5 * np.sin(2 * np.pi * t / 12.42)
>>> h += 1.5 * np.sin(2 * np.pi * t / 12.0)
>>> h += 0.3 * np.random.randn(len(t))
>>> wt = lanc(96 + 1 + 96, 1.0 / 40)
>>> low = np.convolve(wt, h, mode="same")
>>> high = h - low
>>> fig, (ax0, ax1) = plt.subplots(nrows=2)
>>> _ = ax0.plot(high, label="high")
>>> _ = ax1.plot(low, label="low")
>>> _ = ax0.legend(numpoints=1)
>>> _ = ax1.legend(numpoints=1)
oceans.filters.md_trenberth(x)[source]

Returns the filtered series using the Trenberth filter as described on Monthly Weather Review, vol. 112, No. 2, Feb 1984.

Input data: series x of dimension 1Xn (must be at least dimension 11) Output data: y = md_trenberth(x) where y has dimension 1X(n-10)

Examples

>>> from oceans.filters import md_trenberth
>>> import matplotlib.pyplot as plt
>>> t = np.arange(500)  # Time in hours.
>>> x = 2.5 * np.sin(2 * np.pi * t / 12.42)
>>> x += 1.5 * np.sin(2 * np.pi * t / 12.0)
>>> x += 0.3 * np.random.randn(len(t))
>>> filtered = md_trenberth(x)
>>> fig, ax = plt.subplots()
>>> (l1,) = ax.plot(t, x, label="original")
>>> pad = [np.NaN] * 5
>>> (l2,) = ax.plot(t, np.r_[pad, filtered, pad], label="filtered")
>>> legend = ax.legend()
oceans.filters.medfilt1(x, L=3)[source]

Median filter for 1d arrays.

Performs a discrete one-dimensional median filter with window length L to input vector x. Produces a vector the same size as x. Boundaries are handled by shrinking L at edges; no data outside of x is used in producing the median filtered output.

Parameters:
  • x (array_like) – Input 1D data

  • L (integer) – Window length

Returns:

xout – Numpy 1d array of median filtered result; same size as x

Return type:

array_like

Examples

>>> from oceans.filters import medfilt1
>>> import matplotlib.pyplot as plt
>>> # 100 pseudo-random integers ranging from 1 to 100, plus three large
>>> # outliers for illustration.
>>> x = np.r_[
...     np.ceil(np.random.rand(25) * 100),
...     [1000],
...     np.ceil(np.random.rand(25) * 100),
...     [2000],
...     np.ceil(np.random.rand(25) * 100),
...     [3000],
...     np.ceil(np.random.rand(25) * 100),
... ]
>>> L = 2
>>> xout = medfilt1(x=x, L=L)
>>> ax = plt.subplot(211)
>>> l1, l2 = ax.plot(x), ax.plot(xout)
>>> ax.grid(True)
>>> y1min, y1max = np.min(xout) * 0.5, np.max(xout) * 2.0
>>> leg1 = ax.legend(["x (pseudo-random)", "xout"])
>>> t1 = ax.set_title(
...     '''Median filter with window length %s.
...                   Removes outliers, tracks remaining signal)'''
...     % L
... )
>>> L = 103
>>> xout = medfilt1(x=x, L=L)
>>> ax = plt.subplot(212)
>>> (l1, l2,) = ax.plot(
...     x
... ), ax.plot(xout)
>>> ax.grid(True)
>>> y2min, y2max = np.min(xout) * 0.5, np.max(xout) * 2.0
>>> leg2 = ax.legend(["Same x (pseudo-random)", "xout"])
>>> t2 = ax.set_title(
...     '''Median filter with window length %s.
...                   Removes outliers and noise'''
...     % L
... )
>>> ax = plt.subplot(211)
>>> lims1 = ax.set_ylim([min(y1min, y2min), max(y1max, y2max)])
>>> ax = plt.subplot(212)
>>> lims2 = ax.set_ylim([min(y1min, y2min), max(y1max, y2max)])
oceans.filters.pl33tn(x, dt=1.0, T=33.0, mode='valid', t=None)[source]

Computes low-passed series from x using pl33 filter, with optional sample interval dt (hours) and filter half-amplitude period T (hours) as input for non-hourly series.

The PL33 filter is described on p. 21, Rosenfeld (1983), WHOI Technical Report 85-35. Filter half amplitude period = 33 hrs., half power period = 38 hrs. The time series x is folded over and cosine tapered at each end to return a filtered time series xf of the same length. Assumes length of x greater than 67.

Can input a DataArray and use dask-supported for lazy execution. In that use case, dt is ignored and calculated from the input DataArray. cf-xarray is also required.

Examples

>>> from oceans.filters import pl33tn
>>> import matplotlib.pyplot as plt
>>> t = np.arange(500)  # Time in hours.
>>> x = 2.5 * np.sin(2 * np.pi * t / 12.42)
>>> x += 1.5 * np.sin(2 * np.pi * t / 12.0)
>>> x += 0.3 * np.random.randn(len(t))
>>> filtered_33 = pl33tn(x, dt=4.0)  # 33 hr filter
>>> filtered_33d3 = pl33tn(x, dt=4.0, T=72.0)  # 3 day filter
>>> fig, ax = plt.subplots()
>>> (l1,) = ax.plot(t, x, label="original")
>>> pad = [np.NaN] * 8
>>> (l2,) = ax.plot(t, np.r_[pad, filtered_33, pad], label="33 hours")
>>> pad = [np.NaN] * 17
>>> (l3,) = ax.plot(t, np.r_[pad, filtered_33d3, pad], label="3 days")
>>> legend = ax.legend()
oceans.filters.smoo1(datain, window_len=11, window='hanning')[source]

Smooth the data using a window with requested size.

Parameters:
  • datain (array_like) – input series

  • window_len (int) – size of the smoothing window; should be an odd integer

  • window (str) – window from ‘flat’, ‘hanning’, ‘hamming’, ‘bartlett’, ‘blackman’. flat window will produce a moving average smoothing.

Returns:

data_out – smoothed signal

Return type:

array_like

See also

scipy.signal.lfilter

Notes

original from: https://scipy-cookbook.readthedocs.io/items/SignalSmooth.html This method is based on the convolution of a scaled window with the signal. The signal is prepared by introducing reflected copies of the signal (with the window size) in both ends so that transient parts are minimized in the beginning and end part of the output signal.

Examples

>>> from oceans.filters import smoo1
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> time = np.linspace(-4, 4, 100)
>>> series = np.sin(time)
>>> noise_series = series + np.random.randn(len(time)) * 0.1
>>> data_out = smoo1(series)
>>> ws = 31
>>> ax = plt.subplot(211)
>>> _ = ax.plot(np.ones(ws))
>>> windows = ["flat", "hanning", "hamming", "bartlett", "blackman"]
>>> for w in windows[1:]:
...     _ = eval("plt.plot(np." + w + "(ws) )")
>>> _ = ax.axis([0, 30, 0, 1.1])
>>> leg = ax.legend(windows)
>>> _ = plt.title("The smoothing windows")
>>> ax = plt.subplot(212)
>>> (l1,) = ax.plot(series)
>>> (l2,) = ax.plot(noise_series)
>>> for w in windows:
...     _ = plt.plot(smoo1(noise_series, 10, w))
...
>>> l = ["original signal", "signal with noise"]
>>> l.extend(windows)
>>> leg = ax.legend(l)
>>> _ = plt.title("Smoothing a noisy signal")

TODO: window parameter can be the window itself (i.e. an array) instead of a string.

oceans.filters.smoo2(A, hei, wid, kind='hann', badflag=-9999, beta=14)[source]

Calculates the smoothed array ‘As’ from the original array ‘A’ using the specified window of type ‘kind’ and shape (‘hei’, ‘wid’).

Usage: As = smoo2(A, hei, wid, kind=’hann’, badflag=-9999, beta=14)

Parameters:
  • A (2D array) – Array to be smoothed.

  • hei (integer) – Window height. Must be odd and greater than or equal to 3.

  • wid (integer) – Window width. Must be odd and greater than or equal to 3.

  • kind (string, optional) – Refer to Numpy for details about each window type.

  • badflag (float, optional) – The bad data flag. Elements of the input array ‘A’ holding this value are ignored.

  • beta (float, optional) – Shape parameter for the kaiser window.

Returns:

  • As (2D array) – The smoothed array.

  • André Palóczy Filho (paloczy@gmail.com)

  • April 2012

oceans.filters.weim(x, N, kind='hann', badflag=-9999, beta=14)[source]

Calculates the smoothed array ‘xs’ from the original array ‘x’ using the specified window of type ‘kind’ and size ‘N’. ‘N’ must be an odd number.

Usage: xs = weim(x, N, kind=’hann’, badflag=-9999, beta=14)

Parameters:
  • x (1D array) – Array to be smoothed.

  • N (integer) – Window size. Must be odd.

  • kind (string, optional) –

  • type. (Refer to Numpy for details about each window) –

  • badflag (float, optional) – The bad data flag. Elements of the input array ‘A’ holding this value are ignored.

  • beta (float, optional) – Shape parameter for the kaiser window. For windows other than the kaiser window, this parameter does nothing.

Returns:

  • xs (1D array) – The smoothed array.

  • —————————————

  • André Palóczy Filho (paloczy@gmail.com) June 2012