|
| 1 | +""" |
| 2 | +Chirp CWT with Ricker |
| 3 | +======================= |
| 4 | +
|
| 5 | +In this example, we analyze a chirp signal with a Ricker (a.k.a. Mexican Hat wavelet) |
| 6 | +""" |
| 7 | + |
| 8 | +# Configure JAX to work with 64-bit floating point precision. |
| 9 | +from jax.config import config |
| 10 | +config.update("jax_enable_x64", True) |
| 11 | + |
| 12 | +# %% |
| 13 | +# Let's import necessary libraries |
| 14 | +import numpy as np |
| 15 | +import jax.numpy as jnp |
| 16 | +# CR.Sparse libraries |
| 17 | +import cr.sparse as crs |
| 18 | +import cr.sparse.wt as wt |
| 19 | +# Utilty functions to construct sinusoids |
| 20 | +import cr.sparse.dsp.signals as signals |
| 21 | +# Plotting |
| 22 | +import matplotlib.pyplot as plt |
| 23 | + |
| 24 | +# %% |
| 25 | +# Test signal generation |
| 26 | +# ------------------------------ |
| 27 | +# Sampling frequency in Hz |
| 28 | +fs = 100 |
| 29 | +# Signal duration in seconds |
| 30 | +T = 10 |
| 31 | +# Initial instantaneous frequency for the chirp |
| 32 | +f0 = 1 |
| 33 | +# Final instantaneous frequency for the chirp |
| 34 | +f1 = 4 |
| 35 | +# Construct the chirp signal |
| 36 | +t, x = signals.chirp(fs, T, f0, f1, initial_phase=0) |
| 37 | +# Plot the chirp signal |
| 38 | +fig, ax = plt.subplots(figsize=(12, 4)) |
| 39 | +ax.plot(t, x) |
| 40 | +ax.grid('on') |
| 41 | + |
| 42 | +# %% |
| 43 | +# Power spectrum |
| 44 | +# ------------------------------ |
| 45 | + |
| 46 | +# Compute the power spectrum |
| 47 | +f, sxx = crs.power_spectrum(x, dt=1/fs) |
| 48 | +# Plot the power spectrum |
| 49 | +fig, ax = plt.subplots(1, figsize=(12,4)) |
| 50 | +ax.plot(f, sxx) |
| 51 | +ax.grid('on') |
| 52 | +ax.set_xlabel('Frequency (Hz)') |
| 53 | +ax.set_ylabel('Power') |
| 54 | +# %% |
| 55 | +# As expected, the power spectrum is able to identify the |
| 56 | +# frequencies in the zone 1Hz to 4Hz in the chirp. |
| 57 | +# However, the spectrum is unable to localize the |
| 58 | +# changes in frequency over time. |
| 59 | + |
| 60 | +# %% |
| 61 | +# Ricker/Mexican Hat Wavelet |
| 62 | +# ------------------------------ |
| 63 | +wavelet = wt.build_wavelet('mexh') |
| 64 | +# generate the wavelet function for the range of time [-8, 8] |
| 65 | +psi, t_psi = wavelet.wavefun() |
| 66 | +# plot the wavelet |
| 67 | +fig, ax = plt.subplots(figsize=(12, 4)) |
| 68 | +ax.plot(t_psi, psi) |
| 69 | +ax.grid('on') |
| 70 | + |
| 71 | +# %% |
| 72 | +# Wavelet Analysis |
| 73 | +# ------------------------------ |
| 74 | +# select a set of scales for wavelet analysis |
| 75 | +# voices per octave |
| 76 | +nu = 8 |
| 77 | +scales = wt.scales_from_voices_per_octave(nu, jnp.arange(32)) |
| 78 | +# Compute the wavelet analysis |
| 79 | +output = wt.cwt(x, scales, wavelet) |
| 80 | +# Identify the frequencies for the analysis |
| 81 | +frequencies = wt.scale2frequency(wavelet, scales) * fs |
| 82 | +# Plot the analysis |
| 83 | +cmap = plt.cm.seismic |
| 84 | +fig, ax = plt.subplots(1, figsize=(10,10)) |
| 85 | + |
| 86 | +title = 'Wavelet Transform (Power Spectrum) of signal' |
| 87 | +ylabel = 'Frequency (Hz)' |
| 88 | +xlabel = 'Time' |
| 89 | + |
| 90 | +power = (abs(output)) ** 2 |
| 91 | +levels = [0.0625, 0.125, 0.25, 0.5, 1, 2, 4, 8] |
| 92 | +contourlevels = np.log2(levels) |
| 93 | + |
| 94 | +im = ax.contourf(t, jnp.log2(frequencies), jnp.log2(power), contourlevels, extend='both',cmap=cmap) |
| 95 | + |
| 96 | +ax.set_title(title, fontsize=20) |
| 97 | +ax.set_ylabel(ylabel, fontsize=18) |
| 98 | +ax.set_xlabel(xlabel, fontsize=18) |
| 99 | + |
| 100 | +yticks = 2**np.arange(np.ceil(np.log2(frequencies.min())), np.ceil(np.log2(frequencies.max()))) |
| 101 | +ax.set_yticks(np.log2(yticks)) |
| 102 | +ax.set_yticklabels(yticks) |
| 103 | +ylim = ax.get_ylim() |
| 104 | + |
0 commit comments