Module2 Wavelet - igheyas/WaveletTransformation GitHub Wiki
-
Understand how scale and translation control time–frequency resolution
-
State the admissibility condition ensuring perfect reconstruction
-
Build intuition for why wavelets localize energy in time and scale
Populat Continuous Wavelets
Python Code
Step1: List your installed continuous wavelets:
import pywt
print(pywt.wavelist(kind='continuous'))
# wavelet_experiment.py
import numpy as np
import matplotlib.pyplot as plt
import pywt
from scipy.signal import chirp
# 1. Generate a toy non‐stationary signal: a linear chirp + noise
fs = 1000 # sampling frequency (Hz)
T = 1.0 # duration (seconds)
t = np.linspace(0, T, int(fs * T), endpoint=False)
signal = chirp(t, f0=20, f1=200, t1=T, method='linear') \
+ 0.3 * np.random.randn(len(t))
# 2. Choose scales and wavelets for CWT
scales = np.arange(1, 128)
wavelets = ['morl', 'mexh', 'gaus1', 'cgau1'] # replaced 'paul1' with 'cgau1'
# 3. Compute and plot the scalogram for each wavelet
for wname in wavelets:
coefs, freqs = pywt.cwt(signal, scales, wname, sampling_period=1/fs)
plt.figure(figsize=(10, 4))
plt.imshow(
np.abs(coefs),
extent=[t[0], t[-1], scales[-1], scales[0]],
cmap='jet',
aspect='auto'
)
plt.title(f'CWT Scalogram – {wname}')
plt.xlabel('Time [s]')
plt.ylabel('Scale')
plt.tight_layout()
plt.show()
# signal is 1-D
length = signal.shape[0]
# or simply
length = len(signal)
print(f"signal length = {length}")
Output
# Assuming `coefs` is a 2D NumPy array:
import numpy as np
# Example: coefs = np.random.randn(127, 1000)
# Get its shape
rows, cols = coefs.shape
print(f"Number of rows: {rows}")
print(f"Number of columns: {cols}")
# Or in one line:
print(f"coefs has shape {coefs.shape[0]}×{coefs.shape[1]} (rows×columns)")
Output
# signal is 1-D
length = signal.shape[0]
# or simply
length = len(signal)
length1 = len(freqs)
length2 = len(t)
print(f"signal length = {length}")
print(f"freqs length = {length1}")
print(f"t length = {length2}")
Output
Change the scale
# wavelet_experiment.py
import numpy as np
import matplotlib.pyplot as plt
import pywt
from scipy.signal import chirp
# 1. Generate a toy non‐stationary signal: a linear chirp + noise
fs = 1000 # sampling frequency (Hz)
T = 1.0 # duration (seconds)
t = np.linspace(0, T, int(fs * T), endpoint=False)
signal = chirp(t, f0=20, f1=200, t1=T, method='linear') \
+ 0.3 * np.random.randn(len(t))
# 2. Choose scales and wavelets for CWT
scales = np.arange(1, 228)
wavelets = ['morl', 'mexh', 'gaus1', 'cgau1'] # replaced 'paul1' with 'cgau1'
# 3. Compute and plot the scalogram for each wavelet
for wname in wavelets:
coefs, freqs = pywt.cwt(signal, scales, wname, sampling_period=1/fs)
plt.figure(figsize=(10, 4))
plt.imshow(
np.abs(coefs),
extent=[t[0], t[-1], scales[-1], scales[0]],
cmap='jet',
aspect='auto'
)
plt.title(f'CWT Scalogram – {wname}')
plt.xlabel('Time [s]')
plt.ylabel('Scale')
plt.tight_layout()
plt.show()
# wavelet_experiment.py
import numpy as np
import matplotlib.pyplot as plt
import pywt
from scipy.signal import chirp
# 1. Generate a toy non‐stationary signal: a linear chirp + noise
fs = 1000 # sampling frequency (Hz)
T = 1.0 # duration (seconds)
t = np.linspace(0, T, int(fs * T), endpoint=False)
signal = chirp(t, f0=20, f1=200, t1=T, method='linear') \
+ 0.3 * np.random.randn(len(t))
# 2. Choose scales and wavelets for CWT
scales = np.arange(1, 1000)
wavelets = ['morl', 'mexh', 'gaus1', 'cgau1'] # replaced 'paul1' with 'cgau1'
# 3. Compute and plot the scalogram for each wavelet
for wname in wavelets:
coefs, freqs = pywt.cwt(signal, scales, wname, sampling_period=1/fs)
plt.figure(figsize=(10, 4))
plt.imshow(
np.abs(coefs),
extent=[t[0], t[-1], scales[-1], scales[0]],
cmap='jet',
aspect='auto'
)
plt.title(f'CWT Scalogram – {wname}')
plt.xlabel('Time [s]')
plt.ylabel('Scale')
plt.tight_layout()
plt.show()