| 1 |
import numpy as np |
| 2 |
import pywt |
| 3 |
import matplotlib.pyplot as plt |
| 4 |
|
| 5 |
wav = pywt.ContinuousWavelet('cmor1.5-1.0') |
| 6 |
|
| 7 |
|
| 8 |
print("Continuous wavelet will be evaluated over the range [{}, {}]".format( |
| 9 |
wav.lower_bound, wav.upper_bound)) |
| 10 |
|
| 11 |
width = wav.upper_bound - wav.lower_bound |
| 12 |
|
| 13 |
scales = [1, 2, 3, 4, 10, 15] |
| 14 |
|
| 15 |
max_len = int(np.max(scales)*width + 1) |
| 16 |
t = np.arange(max_len) |
| 17 |
fig, axes = plt.subplots(len(scales), 2, figsize=(12, 6)) |
| 18 |
for n, scale in enumerate(scales): |
| 19 |
|
| 20 |
|
| 21 |
int_psi, x = pywt.integrate_wavelet(wav, precision=10) |
| 22 |
step = x[1] - x[0] |
| 23 |
j = np.floor( |
| 24 |
np.arange(scale * width + 1) / (scale * step)) |
| 25 |
if np.max(j) >= np.size(int_psi): |
| 26 |
j = np.delete(j, np.where((j >= np.size(int_psi)))[0]) |
| 27 |
j = j.astype(np.int_) |
| 28 |
|
| 29 |
|
| 30 |
int_psi /= np.abs(int_psi).max() |
| 31 |
|
| 32 |
|
| 33 |
filt = int_psi[j][::-1] |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
nt = len(filt) |
| 39 |
t = np.linspace(-nt//2, nt//2, nt) |
| 40 |
axes[n, 0].plot(t, filt.real, t, filt.imag) |
| 41 |
axes[n, 0].set_xlim([-max_len//2, max_len//2]) |
| 42 |
axes[n, 0].set_ylim([-1, 1]) |
| 43 |
axes[n, 0].text(50, 0.35, 'scale = {}'.format(scale)) |
| 44 |
|
| 45 |
f = np.linspace(-np.pi, np.pi, max_len) |
| 46 |
filt_fft = np.fft.fftshift(np.fft.fft(filt, n=max_len)) |
| 47 |
filt_fft /= np.abs(filt_fft).max() |
| 48 |
axes[n, 1].plot(f, np.abs(filt_fft)**2) |
| 49 |
axes[n, 1].set_xlim([-np.pi, np.pi]) |
| 50 |
axes[n, 1].set_ylim([0, 1]) |
| 51 |
axes[n, 1].set_xticks([-np.pi, 0, np.pi]) |
| 52 |
axes[n, 1].set_xticklabels([r'$-\pi$', '0', r'$\pi$']) |
| 53 |
axes[n, 1].grid(True, axis='x') |
| 54 |
axes[n, 1].text(np.pi/2, 0.5, 'scale = {}'.format(scale)) |
| 55 |
|
| 56 |
axes[n, 0].set_xlabel('time (samples)') |
| 57 |
axes[n, 1].set_xlabel('frequency (radians)') |
| 58 |
axes[0, 0].legend(['real', 'imaginary'], loc='upper left') |
| 59 |
axes[0, 1].legend(['Power'], loc='upper left') |
| 60 |
axes[0, 0].set_title('filter') |
| 61 |
axes[0, 1].set_title(r'|FFT(filter)|$^2$') |