[파이썬] scipy 연속 및 이산 웨이블릿 변환

Introduction

In the field of signal processing, wavelet transform plays a significant role in analyzing and decomposing signals or images. Scipy, a popular scientific computing library in Python, provides functions for both continuous and discrete wavelet transforms. In this blog post, we will explore how to perform continuous and discrete wavelet transforms using Scipy.

Continuous Wavelet Transform (CWT)

The continuous wavelet transform (CWT) is used to analyze non-stationary signals over different scales. Scipy provides the scipy.signal.cwt function to perform the CWT.

Here’s an example of how to perform a CWT on a one-dimensional signal:

import numpy as np
import matplotlib.pyplot as plt
from scipy import signal

# Generate a test signal
t = np.linspace(-1, 1, num=200, endpoint=False)
sig = np.cos(2*np.pi*7*t) + signal.gausspulse(t-0.4, fc=2)

# Define the wavelet function
widths = np.arange(1, 31)
cwtmatr = signal.cwt(sig, signal.ricker, widths)

# Plot the time-frequency representation
plt.imshow(cwtmatr, extent=[-1, 1, 1, 31], cmap='PRGn', aspect='auto', vmax=abs(cwtmatr).max(), vmin=-abs(cwtmatr).max())
plt.colorbar()
plt.title('Continuous Wavelet Transform')
plt.xlabel('Time')
plt.ylabel('Scale')
plt.show()

In the above code, we first generate a test signal, which is a combination of a cosine wave and a Gaussian pulse. Then, we define the wavelet function to be used for the CWT, in this case using the Ricker wavelet (signal.ricker). Finally, we call signal.cwt with the signal and wavelet parameters to perform the CWT. The resulting time-frequency representation is plotted using imshow, with the x-axis representing time and the y-axis representing scale.

Discrete Wavelet Transform (DWT)

The discrete wavelet transform (DWT) is used to analyze and decompose signals or images into different frequency bands. Scipy provides the pywt module for performing the DWT.

Here’s an example of how to perform a DWT on a one-dimensional signal:

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

# Generate a test signal
signal_length = 256
t = np.linspace(0, 1, signal_length, endpoint=False)
sig = np.sin(2*np.pi*32*t) + np.sin(2*np.pi*64*t)

# Define the wavelet family and level of decomposition
wavelet = 'db4'
level = 5

# Perform the DWT
coeffs = pywt.wavedec(sig, wavelet, level=level)

# Plot the wavelet coefficients
fig, axs = plt.subplots(level+1, sharex=True)
axs[0].plot(sig)
axs[0].set_ylabel('Original Signal')
for i in range(level):
    axs[i+1].plot(coeffs[i])
    axs[i+1].set_ylabel(f'Level {i+1} Detail Coefficients')
axs[level].set_xlabel('Samples')
plt.suptitle('Discrete Wavelet Transform')
plt.show()

In the above code, we first generate a test signal containing two sine waves of different frequencies. Then, we define the wavelet family and the level of decomposition. We call pywt.wavedec to perform the DWT, which returns a list of wavelet coefficients at each decomposition level. We then plot the original signal and the detail coefficients at each level using plt.subplots.

Conclusion

Scipy provides powerful tools for performing both continuous and discrete wavelet transforms in Python. The continuous wavelet transform (CWT) is useful for analyzing non-stationary signals over different scales, while the discrete wavelet transform (DWT) allows for the decomposition of signals or images into different frequency bands. By utilizing these functions, you can gain insights into the time-frequency properties of your signals or images.