Skip to main content
 首页 » 编程设计

python之matplotlib 中小波分析输出的基本绘图

2024年11月01日9dyllove98

多亏了 python 模块 pywt,我在实践中发现了小波.

我浏览过some examples of the pywt module usage ,但我无法掌握关键步骤:我基本上不知道如何使用 matplotlib 显示小波分析的多维输出。

这是我尝试过的,(给定一个 pyplot ax ax):

import pywt 
 
data_1_dimension_series = [0,0.1,0.2,0.4,-0.1,-0.1,-0.3,-0.4,1.0,1.0,1.0,0]  
# indeed my data_1_dimension_series is much longer 
 
cA, cD = pywt.dwt(data_1_dimension_series, 'haar') 
 
ax.set_xlabel('seconds') 
ax.set_ylabel('wavelet affinity by scale factor') 
 
ax.plot(axe_wt_time, zip(cA,cD)) 

或者还有

data_wt_analysis = pywt.dwt(data_1_dimension_series, 'haar') 
ax.plot(axe_wt_time, data_wt_analysis)  

ax.plot(axe_wt_time, data_wt_analysis) 和 ax.plot(axe_wt_time, zip(cA,cD)) 都不合适,会返回错误。两个抛出 x 和 y 必须具有相同的第一维度

问题是 data_wt_analysis 确实包含多个一维系列,每个系列对应一个小波比例因子。 我当然可以显示与比例因子一样多的图表。但我希望它们都在同一张图中。

使用 matplotlib 我如何才能在一张图表中简单地显示此类数据?

类似于下面的彩色方 block :

请您参考如下方法:

您应该从您感兴趣的数组中提取不同的一维系列,并像在最简单的示例中一样使用 matplotlib

import matplotlib.pyplot as plt 
plt.plot([1,2,3,4]) 
plt.ylabel('some numbers') 
plt.show() 

from doc.

您希望叠加一维图(或线图)。所以,如果你有列表 l1、l2、l3,你会做

import matplotlib.pyplot as plt 
plt.plot(l1) 
plt.plot(l2) 
plt.plot(l3) 
plt.show() 

对于尺度图:我使用的是 imshow()。这不是针对小波,而是相同的 ID:颜色图。

我找到了 this sample对于 imshow() 与小波的使用,没有尝试过

from pylab import * 
import pywt 
import scipy.io.wavfile as wavfile 
 
# Find the highest power of two less than or equal to the input. 
def lepow2(x): 
    return 2 ** floor(log2(x)) 
 
# Make a scalogram given an MRA tree. 
def scalogram(data): 
    bottom = 0 
 
    vmin = min(map(lambda x: min(abs(x)), data)) 
    vmax = max(map(lambda x: max(abs(x)), data)) 
 
    gca().set_autoscale_on(False) 
 
    for row in range(0, len(data)): 
        scale = 2.0 ** (row - len(data)) 
 
        imshow( 
            array([abs(data[row])]), 
            interpolation = 'nearest', 
            vmin = vmin, 
            vmax = vmax, 
            extent = [0, 1, bottom, bottom + scale]) 
 
        bottom += scale 
 
# Load the signal, take the first channel, limit length to a power of 2 for simplicity. 
rate, signal = wavfile.read('kitten.wav') 
signal = signal[0:lepow2(len(signal)),0] 
tree = pywt.wavedec(signal, 'db5') 
 
# Plotting. 
gray() 
scalogram(tree) 
show()