<jupyter_start><jupyter_text>Generate figures for Lecture 5.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pywt
L = 1
N_grid = 2**10
grid = np.linspace(0, 1, N_grid)


fourier_ex = np.sin(10*np.pi*grid)

gabor_ex = np.exp( - ( (1/(2*0.025))*(grid- 0.5)**2 ) )*fourier_ex

plt.plot(grid, fourier_ex, grid, gabor_ex)
plt.xlabel('t')
plt.show()<jupyter_output><empty_output><jupyter_text>Example scalogram computed on the [El Nino dataset](http://paos.colorado.edu/research/wavelets/wave_idl) of quarterly sea surface measurements from 1871 to 1991.<jupyter_code>import pandas as pd

dataset = "http://paos.colorado.edu/research/wavelets/wave_idl/sst_nino3.dat"
df_nino = pd.read_table(dataset)
N = df_nino.shape[0]

t0=1871 # initial year
dt=0.25 # data measured 4 times each year
time = np.arange(0, N) * dt + t0
data = df_nino.values.squeeze()

fig, ax = plt.subplots(figsize=(15, 10))


ax.plot(time, data)
ax.set_xlabel('Year', fontsize=18)
ax.set_ylabel('Temperature', fontsize=18)
plt.show()
scales = np.arange(1, 128)

[wc, freq] = pywt.cwt(data, scales, 'gaus3', dt)

power = wc**2
period = 1. / freq

levels = [0.0625, 0.125, 0.25, 0.5, 1, 2, 4, 8]
contourlevels = np.log2(levels)
    
fig, ax = plt.subplots(figsize=(15, 10))
im = ax.contourf(time, np.log2(period), np.log2(power), contourlevels, extend='both')
    
ax.set_title('Power Spectrum of Wavelet Transform', fontsize=20)    
ax.set_xlabel('Time', fontsize=18)
ax.set_ylabel('Period (Years)', fontsize=18)

yticks = 2**np.arange(np.ceil(np.log2(period.min())), np.ceil(np.log2(period.max())))
ax.set_yticks(np.log2(yticks))
ax.set_yticklabels(yticks)
    
cbar_ax = fig.add_axes([0.95, 0.5, 0.03, 0.25])
fig.colorbar(im, cax=cbar_ax, orientation="vertical")
plt.show()<jupyter_output><empty_output><jupyter_text>A little interactive demo to play around and get a feel for how the wavelet transform sees different scales.<jupyter_code>def g(x, kk, ss, tt): 

  val = np.exp( - ( (1/(2*(ss**2)))*(x- tt)**2 ) )*np.sin(kk*np.pi*x)

  return val


k = 300
s = 0.05
t = 0.25

g_vals = g(grid, k, s, t) # + g(grid, k/2, s/2, t/2) 

fig, ax = plt.subplots(figsize=(15, 10))

ax.plot(grid, g_vals)
ax.set_xlabel('t', fontsize=18)
plt.show()
print(pywt.wavelist(kind='continuous')) # in case you want to change the wavelet
g_scales = np.arange(1, 64)
dt = L/N_grid
[gwc, gwf] = pywt.cwt(g_vals, scales, 'cmor', sampling_period=dt)

fig, ax = plt.subplots(figsize=(15, 10))
ax.contourf(grid, gwf, np.abs(gwc), extend='both')

ax.set_xlabel('t', fontsize=18)
ax.set_ylabel('freq (Hz)', fontsize=18)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17547892720306513, 'fraction_numerical': 0.050957854406130265, 'mean_word_length': 5.129107981220657, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '490460', 'license': 'MIT', 'n_tokens_mistral': 1219, 'n_tokens_neox': 1111, 'n_words': 234, 'path': 'course_notes/AMATH582-Lec5.ipynb', 'repo_name': 'raph651/Amath-582-Data-Analysis', 'size': '2610'}