# 小波变换简介

## 1. 傅里叶变换的局限性

import numpy as np
import matplotlib.pyplot as plt
from scipy.fftpack import fft

t = np.linspace(0, 1, 400, endpoint=False)
cond = [t<0.25, (t>=0.25)&(t<0.5), t>=0.5]
f1 = lambda t: np.cos(2*np.pi*10*t)
f2 = lambda t: np.cos(2*np.pi*50*t)
f3 = lambda t: np.cos(2*np.pi*100*t)

y1 = np.piecewise(t, cond, [f1, f2, f3])
y2 = np.piecewise(t, cond, [f2, f1, f3])

Y1 = abs(fft(y1))
Y2 = abs(fft(y2))

plt.figure(figsize=(12, 9))
plt.subplot(221)
plt.plot(t, y1)
plt.title('signal_1 in time domain')
plt.xlabel('Time/second')

plt.subplot(222)
plt.plot(range(400), Y1)
plt.title('signal_1 in frequency domain')
plt.xlabel('Frequency/Hz')

plt.subplot(223)
plt.plot(t, y2)
plt.title('signal_2 in time domain')
plt.xlabel('Time/second')

plt.subplot(224)
plt.plot(range(400), Y2)
plt.title('signal_2 in frequency domain')
plt.xlabel('Frequency/Hz')

plt.tight_layout()
plt.show()


## 2. 连续小波变换

ψ a , b ( t ) = 1 a ψ ( t − b a ) \psi_{a,b}(t) = \frac{1}{\sqrt a}\psi\left(\frac{t-b}{a}\right)

f ( t ) f(t) 在缩放因子为 a a 的子空间的投影为
f a ( t ) = ∫ − ∞ + ∞ W f ( a , b ) ψ a , b ∗ ( t ) d b f_a(t) = \int_{-\infty}^{+\infty}W_f(a,b)\psi_{a,b}^*(t)\mathrm db

W f ( a , b ) = ∫ − ∞ + ∞ f ( t ) ψ a , b ∗ ( t ) d t W_f(a, b) = \int_{-\infty}^{+\infty}f(t)\psi_{a,b}^*(t)\mathrm dt
∗ ^* 代表复共轭。

f ( t ) f(t) g ( t ) g(t) 都是平方可积函数，则
∫ − ∞ + ∞ f ( t ) g ∗ ( t ) d t = 1 2 π ∫ − ∞ + ∞ F ( ω ) G ∗ ( ω ) d ω \int_{-\infty}^{+\infty} f(t)g^*(t)\mathrm dt = \frac1{2\pi}\int_{-\infty}^{+\infty}F(\omega)G^*(\omega)\mathrm d\omega

KaTeX parse error: No such environment: split at position 8: \begin{̲s̲p̲l̲i̲t̲}̲ \int_{-\infty}…
f ( t ) f(t) 是信号， g ( t ) = ψ a , b ( t ) g(t)=\psi_{a,b}(t) 是小波函数，则小波变换
W f ( a , b ) = ∫ − ∞ + ∞ f ( t ) ψ a , b ∗ ( t ) d t = 1 2 π ∫ − ∞ + ∞ F ( ω ) Ψ a , b ∗ ( ω ) d ω W_f(a,b) = \int_{-\infty}^{+\infty}f(t)\psi^*_{a,b}(t)\mathrm dt=\frac1{2\pi}\int_{-\infty}^{+\infty}F(\omega)\Psi^*_{a,b}(\omega)\mathrm d\omega

import numpy as np
import matplotlib.pyplot as plt
import pywt

t = np.linspace(0, 1, 400, endpoint=False)
cond = [t<0.25, (t>=0.25)&(t<0.5), t>=0.5]
f1 = lambda t: np.cos(2*np.pi*10*t)
f2 = lambda t: np.cos(2*np.pi*50*t)
f3 = lambda t: np.cos(2*np.pi*100*t)

y1 = np.piecewise(t, cond, [f1, f2, f3])
y2 = np.piecewise(t, cond, [f2, f1, f3])

cwtmatr1, freqs1 = pywt.cwt(y1, np.arange(1, 200), 'cgau8', 1/400)
cwtmatr2, freqs2 = pywt.cwt(y2, np.arange(1, 200), 'cgau8', 1/400)

plt.figure(figsize=(12, 9))
plt.subplot(221)
plt.plot(t, y1)
plt.title('signal_1 in time domain')
plt.xlabel('Time/second')

plt.subplot(222)
plt.contourf(t, freqs1, abs(cwtmatr1))
plt.title('time-frequency relationship of signal_1')
plt.xlabel('Time/second')
plt.ylabel('Frequency/Hz')

plt.subplot(223)
plt.plot(t, y2)
plt.title('signal_2 in time domain')
plt.xlabel('Time/second')

plt.subplot(224)
plt.contourf(t, freqs2, abs(cwtmatr2))
plt.title('time-frequency relationship of signal_2')
plt.xlabel('Time/second')
plt.ylabel('Frequency/Hz')

plt.tight_layout()
plt.show()


## 3. 离散小波变换

Python 中可以使用 pywt.dwt实现离散小波变换：

import numpy as np
import matplotlib.pyplot as plt
import pywt

y = pywt.data.ecg()
x = range(len(y))

ca, cd = pywt.dwt(y, 'db4')
ya = pywt.idwt(ca, None, 'db4') # approximated component
yd = pywt.idwt(None, cd, 'db4') # detailed component

plt.figure(figsize=(12,9))
plt.subplot(311)
plt.plot(x, y)
plt.title('original signal')
plt.subplot(312)
plt.plot(x, ya)
plt.title('approximated component')
plt.subplot(313)
plt.plot(x, yd)
plt.title('detailed component')
plt.tight_layout()
plt.show()


import numpy as np
import matplotlib.pyplot as plt
import pywt

y = pywt.data.ecg()
x = range(len(y))

coeffs = pywt.wavedec(y, 'db4', level=4) # 4阶小波分解

ya4 = pywt.waverec(np.multiply(coeffs, [1, 0, 0, 0, 0]).tolist(), 'db4')
yd4 = pywt.waverec(np.multiply(coeffs, [0, 1, 0, 0, 0]).tolist(), 'db4')
yd3 = pywt.waverec(np.multiply(coeffs, [0, 0, 1, 0, 0]).tolist(), 'db4')
yd2 = pywt.waverec(np.multiply(coeffs, [0, 0, 0, 1, 0]).tolist(), 'db4')
yd1 = pywt.waverec(np.multiply(coeffs, [0, 0, 0, 0, 1]).tolist(), 'db4')

plt.figure(figsize=(12, 12))
plt.subplot(611)
plt.plot(x, y)
plt.title('original signal')
plt.subplot(612)
plt.plot(x, ya4)
plt.title('approximated component in level 4')
plt.subplot(613)
plt.plot(x, yd4)
plt.title('detailed component in level 4')
plt.subplot(614)
plt.plot(x, yd3)
plt.title('detailed component in level 3')
plt.subplot(615)
plt.plot(x, yd2)
plt.title('detailed component in level 2')
plt.subplot(616)
plt.plot(x, yd1)
plt.title('detailed component in level 1')
plt.tight_layout()
plt.show()


## 参考文献

1. 小波与傅里叶分析基础（第二版）， 电子工业出版社，2017.6
2. Wavelet - Wikipedia https://en.wikipedia.org/wiki/Wavelet
3. 知乎专栏 - 时频分析与小波变换 https://zhuanlan.zhihu.com/c_123849464
4. PyWavelets - Wavelet Transforms in Python https://pywavelets.readthedocs.io/en/latest/
• 3
点赞
• 17
收藏
• 打赏
• 6
评论
12-18 4237
01-14 3万+
01-21 7143
10-30 4531
07-05 3144
03-16 2万+
07-03 6295
07-18 1万+
03-22 8274
07-24 573
04-29 3206
08-31 535

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。