<p>我发现使用matplotlib制作scalegram的最佳方法是使用<code>imshow</code>,类似于<code>specgram</code>的实现。使用矩形很慢,因为必须为每个值创建单独的标志符号。类似地,你不想把东西烘焙成一个统一的NumPy数组,因为你可能会很快耗尽内存,因为你的最高级别大约是你信号的一半。</p>
<p>下面是使用SciPy和<a href="http://www.pybytes.com/pywavelets/" rel="nofollow">PyWavelets</a>的示例:
</p>
<pre><code>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()
</code></pre>
<p>您可能还希望根据级别自适应地缩放值。</p>
<p>这对我很有效。我唯一的问题是matplotlib在层之间创建了一个细细的空间。我还在想办法解决这个问题。</p>
<p>p.S.-尽管这个问题现在已经很老了,但我想我会在这里回答,因为这个页面是在谷歌上出现的,当时我正在寻找一种使用MPL创建scalegrams的方法。</p>