0
点赞
收藏
分享

微信扫一扫

220502-连续小波变换CWT中[scale, wavename]以及[contouf, pcolormesh]对信号分析的影响


影响CWT图像的几个要素

  1. 基函数wavename的选择
  2. Scale的设定
  3. Plot的方式
  4. cmap的选择

Demo 1: CWT with different wavelets

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

aa = []
for i in range(200):
    aa.append(np.sin(0.3*np.pi*i))
for i in range(200):
    aa.append(np.sin(0.13*np.pi*i))
for i in range(200):
    aa.append(np.sin(0.05*np.pi*i))
t = np.arange(0, 600, 1.0)

fig, ax = plt.subplots(4,7, figsize=(7*3, 4*3))
# ax = ax.ravel()

gs = ax[0, 0].get_gridspec()
# remove the underlying axes
for x in ax[0, :]:
    x.remove()
axbig = fig.add_subplot(gs[0, :])
axbig.plot(t,aa)
axbig.set_title('Signals')

for idx, wavename in enumerate(pywt.wavelist(kind='continuous')):
    m, n = divmod(idx,7)
    if 'cmor' in wavename:
        wavename = 'cmor3-3'
    if 'fbsp' in wavename:
        wavename = 'fbsp1-1.5-1.0' 
    if 'shan' in wavename:
        wavename = 'shan1.5-1.0'
    sampling_rate = 1024
    # wavename = 'cgau8'
    totalscal = 256
    fc = pywt.central_frequency(wavename)
    cparam = 2 * fc * totalscal
    scales = cparam / np.arange(totalscal, 1, -1)
    [cwtmatr, frequencies] = pywt.cwt(aa, scales, wavename, 1.0 / sampling_rate)
    ax[m+1, n].contourf(t, frequencies, abs(cwtmatr), cmap='jet')
    ax[m+1, n].set_title('{} {}'.format(wavename, cwtmatr.shape))
    
    # ax[idx].ylabel(u"freq(Hz)")
    # ax[idx].xlabel(u"time(s)")
plt.tight_layout()

220502-连续小波变换CWT中[scale, wavename]以及[contouf, pcolormesh]对信号分析的影响_python

Demo 2: CWT with different scales (Pcolormesh vs. Contour)

fig, ax = plt.subplots(2,6, figsize=(9*3, 3*3))
# ax = ax.ravel()

wavename = 'cgau8'
for idx, totalscal in enumerate([32, 64, 128, 256, 512, 1024]):
    m,n = divmod(idx,6)
    fc = pywt.central_frequency(wavename)
    cparam = 2 * fc * totalscal
    scales = cparam / np.arange(totalscal, 1, -1)
    [cwtmatr, frequencies] = pywt.cwt(aa, scales, wavename, 1.0 / sampling_rate)
    # * pcolormesh
    ax[m, n].pcolormesh(t, frequencies, np.abs(cwtmatr), shading='gouraud', cmap='jet') 
    ax[m, n].set_title('Scale={}, Shape={}: Pcolormesh'.format(totalscal, cwtmatr.shape))
    # * contourf
    ax[m+1, n].contourf(t, frequencies, abs(cwtmatr), cmap='jet')
    ax[m+1, n].set_title('Scale={}, Shape={}: '.format(totalscal, cwtmatr.shape))
    # ax[idx].set_xlim(0,2)
plt.tight_layout()

220502-连续小波变换CWT中[scale, wavename]以及[contouf, pcolormesh]对信号分析的影响_机器学习_02

Demo 3: CWT with different wavelets

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

fs = 1024
fft_size = 1024
step = fft_size/16
time=2
t = np.arange(0, time, 1/fs) # 
# sweep = signal.chirp(t, f0=100, t1=time, f1=0.8*sampling_rate/2, method='logarithmic')
aa = signal.chirp(t, f0=30, t1=time, f1=500, method='quadratic')

fig, ax = plt.subplots(4,7, figsize=(7*3, 4*3))
# ax = ax.ravel()

gs = ax[0, 0].get_gridspec()
# remove the underlying axes
for x in ax[0, :]:
    x.remove()
axbig = fig.add_subplot(gs[0, :])
axbig.plot(t,aa)
axbig.set_title('Signals')

for idx, wavename in enumerate(pywt.wavelist(kind='continuous')):
    m, n = divmod(idx,7)
    if 'cmor' in wavename:
        wavename = 'cmor3-3'
    if 'fbsp' in wavename:
        wavename = 'fbsp1-1.5-1.0' 
    if 'shan' in wavename:
        wavename = 'shan1.5-1.0'
    sampling_rate = 1024
    # wavename = 'cmor3'
    totalscal = 256
    fc = pywt.central_frequency(wavename)
    cparam = 2 * fc * totalscal
    scales = cparam / np.arange(totalscal, 1, -1)
    [cwtmatr, frequencies] = pywt.cwt(aa, scales, wavename, 1.0 / sampling_rate)
    ax[m+1, n].contourf(t, frequencies, abs(cwtmatr), cmap='jet')
    ax[m+1, n].set_title('{} {}'.format(wavename, cwtmatr.shape))
    
    # ax[idx].ylabel(u"freq(Hz)")
    # ax[idx].xlabel(u"time(s)")
plt.tight_layout()

220502-连续小波变换CWT中[scale, wavename]以及[contouf, pcolormesh]对信号分析的影响_人工智能_03

Demo 4: CWT with different scales (Pcolormesh vs. Contour)

fig, ax = plt.subplots(2,6, figsize=(9*3, 3*3))
# ax = ax.ravel()

wavename = 'cmor3-3'
for idx, totalscal in enumerate([32, 64, 128, 256, 512, 1024]):
    m,n = divmod(idx,6)
    fc = pywt.central_frequency(wavename)
    cparam = 2 * fc * totalscal
    scales = cparam / np.arange(totalscal, 1, -1)
    [cwtmatr, frequencies] = pywt.cwt(aa, scales, wavename, 1.0 / sampling_rate)
    # * pcolormesh
    ax[m, n].pcolormesh(t, frequencies, np.abs(cwtmatr), shading='gouraud', cmap='jet') 
    ax[m, n].set_title('Scale={}, Shape={}: Pcolormesh'.format(totalscal, cwtmatr.shape))
    # * contourf
    ax[m+1, n].contourf(t, frequencies, abs(cwtmatr), cmap='jet')
    ax[m+1, n].set_title('Scale={}, Shape={}: '.format(totalscal, cwtmatr.shape))
    # ax[idx].set_xlim(0,2)
plt.tight_layout()

220502-连续小波变换CWT中[scale, wavename]以及[contouf, pcolormesh]对信号分析的影响_机器学习_04

举报

相关推荐

0 条评论