2

I am discovering wavelets in practice thanks to the python module pywt.

I have browsed some examples of the pywt module usage, but I could not grasp the essential step: I don't know how to display the multidimensionnal output of a wavelet analysis with matplotlib, basically.

This is what I tried, (given one pyplot axe 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))

or also

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

Both ax.plot(axe_wt_time, data_wt_analysis) and ax.plot(axe_wt_time, zip(cA,cD)) are not appropriate and returns error. Both throws x and y must have the same first dimension

The thing is data_wt_analysis does contain several 1D series, one for each wavelet scale factor. I surely could display as many graphs as there are scale factors. But I want them all in the same graph.

How could I simply display such data, in only one graph, with matplotlib ?

Something like the colourful square below:

enter image description here

1 Answer 1

1

You should extract the different 1D series from your array of interest, and use matplotlib as in most simple example

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

from doc.

You wish to superimpose 1D plots (or line plots). So, if you have lists l1, l2, l3, you will do

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

For a scalogram: what i used was imshow(). This was not for wavelets, but same ID: a colormap.

I have found this sample for use of imshow() with wavelets, didn t try thought

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()
Sign up to request clarification or add additional context in comments.

4 Comments

but in such case, all will have the same y=0 origin. I'm gonna add an image for what I want. I have read it's called a scalograme.
I have added an image corresponding.
well I also have gone through this sample, but I don't understand all the steps. And I don't know what is a MRA tree...
it is what you get out of tree = pywt.wavedec(signal, 'db5'), you should try this method and see the format of output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.