# Untitled

a guest
Sep 5th, 2020
58
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # -*- coding: utf-8 -*-
2. import numpy as np
3. import matplotlib.pyplot as plt
4.
5. #%%##########################################################################
6. def _consts(sigma):
7.     cs = (1 + np.exp(-sigma ** 2) - 2 * np.exp(-.75 * sigma ** 2)) ** (-.5)
8.     ks = np.exp(-.5 * sigma ** 2)
9.     return cs, ks
10.
11. def morlet(N, sigma=5):
12.     cs, ks = _consts(sigma)
13.     n = np.linspace(-8, 8, N)
14.     return np.exp(-.5 * n ** 2) * (np.exp(1j * sigma * n) - ks
15.                                    ) * cs * np.pi ** (-.25)
16.
17. def morlet_fourier(N, sigma):
18.     cs, ks = _consts(sigma)
19.     k = np.linspace(0, N - 1, N)
20.     return (np.exp(-.5 * (sigma - k) ** 2) - ks * np.exp(-.5 * k ** 2)
21.             ) * cs * np.pi ** (-.25)
22.
23. def plot(x, complex=False):
24.     plt.plot(x)
25.     if complex:
26.         plt.plot(x.imag)
27.         plt.legend(['real', 'imag'], fontsize=14)
28.     plt.show()
29. #%%##########################################################################
30. N, sigma = 256, 5
31. morl     = morlet(N, sigma)
32. morl_f   = morlet_fourier(N, sigma)
33. morl_fft = np.fft.fft(morl)
34. #%%##########################################################################
35. plot(morl, complex=True)
36. plot(morl_f)
37. plot(morl_fft, complex=True)
38. plot(np.abs(morl_fft))
RAW Paste Data