Wavelet transform + python

wavelet transform

Fourier transform—>Short-time Fourier transform—>Wavelet transform
Fourier transform can analyze the frequency spectrum of a signal, but it has limitations for non-stationary processes (non-stationary signals whose frequency changes with time).
The short-time Fourier transform decomposes the entire time-domain process into countless small processes of equal length, and each small process is approximately stable. After Fourier transform, it is known what frequency appears at which time point. However, if the STFT window is too long or too short, there are problems. If the window is too narrow, the signal in the window is too short, which will lead to inaccurate frequency analysis and poor frequency resolution; if the window is too wide, the time domain is not fine enough, and the time resolution is low. Unable to meet the frequency requirements of non-stationary signal changes,
the wavelet transform directly replaces the infinitely long trigonometric function basis of the Fourier transform with a finitely long attenuating wavelet basis, which can not only obtain frequency but also locate time.

1. Continuous wavelet transform

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

path = 'E:\AudioClassification-Pytorch-master\mel/audio/1_001-200.wav'
y, sr = librosa.load(path, sr=16000)
wavename = 'morl'
totalscal = 4  # totalscal是对信号进行小波变换时所用尺度序列的长度(通常需要预先设定好)
fc = pywt.central_frequency(wavename)  # 计算小波函数的中心频率
cparam = 2 * fc * totalscal  # 常数c
scales = cparam / np.arange(totalscal, 1, -1)  # 为使转换后的频率序列是一等差序列,尺度序列必须取为这一形式(也即小波尺度)
[cwtmatr, frequencies] = pywt.cwt(y, scales, wavename, 1.0 / sr)
t = np.arange(0, y.shape[0]/sr, 1.0/sr)
plt.contourf(t, frequencies, abs(cwtmatr))
plt.ylabel(u"freq(Hz)")
plt.xlabel(u"time(s)")
plt.subplots_adjust(hspace=0.4)  # 调整边距和子图的间距 hspace为子图之间的空间保留的高度,平均轴高度的一部分
plt.title = ("小波时频图")
plt.show()

The wavelet time-frequency diagram when totalscal=4 and the
insert image description here
time-frequency diagram when totalscal=256
insert image description here
can be seen that the size of the wavelet transform scale has a relatively large impact on the time-frequency diagram.
If the features extracted by the continuous wavelet transform are added to the neural network for training words can be used

wavename = 'morl'
totalscal = 256
fc = pywt.central_frequency(wavename)  # 计算小波函数的中心频率
cparam = 2 * fc * totalscal  # 常数c
scales = cparam / np.arange(totalscal, 1, -1)  # 为使转换后的频率序列是一等差序列,尺度序列必须取为这一形式(也即小波尺度)
[cwtmatr, frequencies] = pywt.cwt(y, scales, wavename, 1.0 / sr)
features = np.array(cwtmatr)
mean = np.mean(features, 0, keepdims=True)
std = np.std(features, 0, keepdims=True)
features = (features - mean) / (std + 1e-5)
features = features.astype('float32')

2. Discrete wavelet transform

Guess you like

Origin blog.csdn.net/weixin_48983346/article/details/126543551