import matplotlib.pyplot as plt
import numpy as np
import numpy.fft as fft
import dascore as dc
from dascore.units import Hz
def plot_spectrum(x, T, ax, phase=False):
= np.angle(fft.fft(x))
fftphase = np.abs(fft.fft(x))
fftsig = fftsig.size
fftlen = fftsig[0 : int(fftlen / 2) + 1]
fftsig = fftphase[0 : int(fftlen / 2) + 1]
fftphase = np.linspace(0, 0.5 / T, fftsig.size)
freqvec if not phase:
ax.plot(freqvec, fftsig)"frequency [Hz]")
ax.set_xlabel("Amplitude (|H(w)|)")
ax.set_ylabel(else:
ax.plot(freqvec, fftphase)"frequency [Hz]")
ax.set_xlabel("Phase (radians)")
ax.set_ylabel(
= dc.get_example_patch("dispersion_event")
patch = patch.resample(time=(200 * Hz))
patch
= patch.whiten(smooth_size=3, time = (10,50))
white_patch
= plt.subplots(3, 2, figsize=(10, 7))
fig, ((ax1, ax2), (ax3, ax4), (ax5, ax6)) 50, :])
ax1.plot(patch.data["Original data, distance = 50 m")
ax1.set_title(50, :])
ax2.plot(white_patch.data["Whitened data, distance = 50 m")
ax2.set_title(
50, :], 1 / 200, ax3)
plot_spectrum(patch.data["Original data, distance = 50 m")
ax3.set_title(50, :], 1 / 200, ax4)
plot_spectrum(white_patch.data["Whitened data, distance = 50 m")
ax4.set_title(
50, :], 1 / 200, ax5, phase=True)
plot_spectrum(patch.data["Original data, distance = 50 m")
ax5.set_title(50, :], 1 / 200, ax6, phase=True)
plot_spectrum(white_patch.data["Whitened data, distance = 50 m")
ax6.set_title(
plt.tight_layout() plt.show()
whiten
whiten(
patch: Patch ,
smooth_size: float | None[float, None] = None,
tukey_alpha: float = 0.1,
**kwargs ,
)-> ‘PatchType’
Band-limited signal whitening.
Parameters
Parameter | Description |
---|---|
patch | Patch to transform. Has to have dimensions of time and distance. |
smooth_size |
Size (in Hz) of moving average window, used to compute the spectrum before whitening. If no value is inputted, smoothing is over the entire spectrum. |
tukey_alpha |
Alpha parameter for Tukey window applied as windowing to the smoothed spectrum within the required frequency range. By default its value is 0.1. See more details at https://docs.scipy.org/doc/scipy/reference /generated/scipy.signal.windows.tukey.html |
**kwargs |
Used to specify the dimension and frequency, wavelength, or equivalent limits. If no input is provided, whitening is also the last axis with frequency band of [0,Nyquist] |
Note
The FFT result is divided by the smoothed spectrum before inverting back to time-domain signal. The phase is not changed.
A tukey window (fixed) is applied to window the smoothed spectrum within the frequency range of interest. Be aware of its effect and consider enlarging the frequency range according to the tukey_alpha parameter.
Amplitude is NOT preserved
Example
```