9

I have an EEG signal that I'm interested in analyzing it in both time and frequency domains. I have already used scipy.signal.spectrogram function, but I think using wavelets can yield better results for feature extraction. I tried running the continuous wavelet transform on artificial signal that I created as follows:

fs = 128.0
sampling_period = 1/fs
t = np.linspace(0, 2, 2*fs)
x = chirp(t,10,2,40,'quadratic')

coef, freqs = pywt.cwt(x, np.arange(1,50),'morl', 
sampling_period=sampling_period)

and then I plotted the coef matrix:

plt.matshow(coef)
plt.show()

the resuls for the code above

My question is how can I adjust the scale and time axes?

1 Answer 1

4

The function plt.matshow(coef) does not use the time and and frequency arrays for creating axes (but it creates sample-index based axes).

I suggest using plt.pcolormesh(t, freqs, coef), so the time and frequency are used for the axes. Then you can play with scale – say, put the frequency axis in log scale – and produce something like that:

enter image description here

Here is the code that produced the image, derived from your example:

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

from scipy.signal import chirp

# Define signal
fs = 128.0
sampling_period = 1 / fs
t = np.linspace(0, 2, 2 * fs)
x = chirp(t, 10, 2, 40, 'quadratic')

# Calculate continuous wavelet transform
coef, freqs = pywt.cwt(x, np.arange(1, 50), 'morl',
                       sampling_period=sampling_period)

# Show w.r.t. time and frequency
plt.figure(figsize=(5, 2))
plt.pcolor(t, freqs, coef)

# Set yscale, ylim and labels
plt.yscale('log')
plt.ylim([1, 100])
plt.ylabel('Frequency (Hz)')
plt.xlabel('Time (sec)')
plt.savefig('egg.png', dpi=150)
Sign up to request clarification or add additional context in comments.

Comments

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.