Reputation: 11
I have code for my wavelet transform using a morlet wave. I am using a pandas dataframe which has 4 emg columns within it. Here is my code
import numpy as np
import matplotlib.pyplot as plt
import pywt
# List of column names for which you want to generate the Morlet wavelet transform
columns_to_process = ['VL_R', 'VM_R', 'VM_L', 'VL_L']
# Parameters for the Morlet wavelet transform
fs = 100 # Sample rate of EEG data
freqs = np.arange(1, 50, 1) # Adjust the frequency range as needed
scales = fs / freqs # Scales for Morlet wavelets
# Create subplots for each column
num_columns = len(columns_to_process)
fig, axs = plt.subplots(num_columns, 1, figsize=(10, 4 * num_columns))
for i, column in enumerate(columns_to_process):
# Apply a moving average for smoothing
smoothed_data = df_cleaned[column].rolling(window=10).mean()
# Generate Morlet wavelet transform
coefficients, frequencies = pywt.cwt(smoothed_data, scales, 'morl')
# Plot Morlet wavelet transform
im = axs[i].imshow(np.abs(coefficients), extent=[0, len(smoothed_data)/fs, freqs.min(), freqs.max()], aspect='auto', cmap='jet')
axs[i].set_title(f'{column} Morlet Wavelet Transform')
axs[i].set_ylabel('Frequency [Hz]')
axs[i].set_xlabel('Time [sec]')
# Add colorbar
cbar = plt.colorbar(im, ax=axs[i])
cbar.set_label('Magnitude')
plt.tight_layout()
plt.show()
When I run this, I get something that looks like this
Is this currently graphing scale and mislabeling it as frequency on the graph? If so, how do I change this code to look right and use frequency instead. Sorry I am new to coding and am trying to use this for research
Upvotes: 0
Views: 43
Reputation: 4633
Good catch.
As you guessed, the frequency axis is flipped upside down. You can figure this out by taking a look at the cone of influence on the left-hand side.
As per the mathworks documentation [edge-effect artefacts] are effects in the scalogram that arise from areas where the stretched wavelets extend beyond the edges of the observation interval. The lower the frequency, the more stretched the wavelet becomes, and the wider the cone of influence: so low frequencies should show up in the upper left corner.
You may fix this by changing the origin
parameter in imshow from the default upper
to lower
:
im = axs[i].imshow(np.abs(coefficients), extent=[0, len(smoothed_data)/fs, freqs.min(), freqs.max()], aspect='auto', cmap='jet', origin = 'lower')
.
Upvotes: 0