I am trying to apply some signal processing techniques to local ECG data that is retrieved from PhysioNet. I managed to plot the original data but after applying the techniques and starting to plot the new signals, I get this error:
Exception has occurred: ValueError
x and y must have same first dimension, but have shapes (1500,) and (12,)
File "D:\16th_UGRF\Project_Parts\Mobile_Application\DSP_with_Python\DSP_4.py", line 71, in <module>
ax1.plot(t, signal)
ValueError: x and y must have same first dimension, but have shapes (1500,) and (12,)
I tried changing the time vector to fit the new denoised signals but it is the same error.
You can find Here an example of the data.
import numpy as np
import wfdb
import matplotlib.pyplot as plt
from scipy.signal import butter, filtfilt, medfilt
import pywt
# Create the figure
fig, ax = plt.subplots(figsize=(17, 17))
# Add the axes/subplots using subplot2grid
ax1 = plt.subplot2grid(shape=(4, 4), loc=(0, 0))
ax2 = plt.subplot2grid(shape=(4, 4), loc=(0, 1))
ax3 = plt.subplot2grid(shape=(4, 4), loc=(0, 2))
ax4 = plt.subplot2grid(shape=(4, 4), loc=(0, 3))
ax5 = plt.subplot2grid(shape=(4, 4), loc=(1, 0))
ax6 = plt.subplot2grid(shape=(4, 4), loc=(1, 1))
ax7 = plt.subplot2grid(shape=(4, 4), loc=(1, 2))
ax8 = plt.subplot2grid(shape=(4, 4), loc=(1, 3))
ax9 = plt.subplot2grid(shape=(4, 4), loc=(2, 0))
ax10 = plt.subplot2grid(shape=(4, 4), loc=(2, 1))
ax11 = plt.subplot2grid(shape=(4, 4), loc=(2, 2))
ax12 = plt.subplot2grid(shape=(4, 4), loc=(2, 3))
ax13 = plt.subplot2grid(shape=(4, 4), loc=(3, 0), colspan=4)
try:
# Load WFDB record into a wfdb.Record object
record = wfdb.rdrecord(r'D:\16th_UGRF\Project_Parts\Mobile_Application\DSP_with_Python\Raw_Electrical_Signal_Data\a-large-scale-12-lead-electrocardiogram-database-for-arrhythmia-study-1.0.0\WFDBRecords\01\010\JS00001')
# Define the new signal names and custom order
new_signal_names = ['I', 'II', 'III', 'aVR', 'aVL', 'aVF', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6']
custom_order = [0, 3, 6, 9, 1, 4, 7, 10, 2, 5, 8, 11]
# Get the sampling frequency
fs = record.fs
# Extract the first three seconds of all 12 signals
duration = 3 # seconds
num_samples = int(duration * fs)
t = (1/fs) * np.arange(num_samples) # Time vector for the first three seconds
# Get the number of available signals (columns)
num_signals = record.p_signal.shape[1]
# Extract the 12 lead signals (transpose to have time in the rows)
y = record.p_signal.T
# Filter the signal data
fcutlow = 0.5 # Lower cutoff frequency in Hz
fcuthigh = 40 # Upper cutoff frequency in Hz
b, a = butter(1, [fcutlow / (fs/2), fcuthigh / (fs/2)], 'bandpass')
filtered_signal = np.zeros_like(y)
for i in range(num_signals):
filtered_signal[:, i] = filtfilt(b, a, y[:, i])
# Amplify the filtered signal data
amplified_signal = 2 * filtered_signal
# Apply baseline correction to each lead of the amplified signal data
baseline_corrected_signal = np.zeros_like(amplified_signal)
for i in range(num_signals):
baseline_corrected_signal[:, i] = amplified_signal[:, i] - medfilt(amplified_signal[:, i], int(fs/4))
# Apply wavelet denoising to each lead of the baseline-corrected signal data
denoised_signal = np.zeros_like(baseline_corrected_signal)
for i in range(num_signals):
denoised_signal[:, i] = pywt.threshold(baseline_corrected_signal[:, i], value=0.1, mode='soft')
# ploting the signals
# signal I
signal = denoised_signal[:num_samples, 0]
ax1.plot(t, signal)
ax1.set_title(f'Signal I')
ax1.grid(True)
# signal aVR
signal = record.p_signal[:num_samples, 3]
ax2.plot(t, signal)
ax2.set_title(f'Signal aVR')
ax2.grid(True)
# signal V1
signal = record.p_signal[:num_samples, 6]
ax3.plot(t, signal)
ax3.set_title(f'Signal V1')
ax3.grid(True)
# signal V4
signal = record.p_signal[:num_samples, 9]
ax4.plot(t, signal)
ax4.set_title(f'Signal V4')
ax4.grid(True)
# signal II
signal = record.p_signal[:num_samples, 1]
ax5.plot(t, signal)
ax5.set_title(f'Signal II')
ax5.grid(True)
# signal aVL
signal = record.p_signal[:num_samples, 4]
ax6.plot(t, signal)
ax6.set_title(f'Signal aVL')
ax6.grid(True)
# signal V2
signal = record.p_signal[:num_samples, 7]
ax7.plot(t, signal)
ax7.set_title(f'Signal V2')
ax7.grid(True)
# signal V5
signal = record.p_signal[:num_samples, 10]
ax8.plot(t, signal)
ax8.set_title(f'Signal V5')
ax8.grid(True)
# signal III
signal = record.p_signal[:num_samples, 2]
ax9.plot(t, signal)
ax9.set_title(f'Signal III')
ax9.grid(True)
# signal aVF
signal = record.p_signal[:num_samples, 5]
ax10.plot(t, signal)
ax10.set_title(f'Signal AVF')
ax10.grid(True)
# signal V3
signal = record.p_signal[:num_samples, 8]
ax11.plot(t, signal)
ax11.set_title(f'Signal V3')
ax11.grid(True)
# signal V6
signal = record.p_signal[:num_samples, 11]
ax12.plot(t, signal)
ax12.set_title(f'Signal V6')
ax12.grid(True)
# signal II
signal = record.p_signal[:num_samples, 1]
ax13.plot(t, signal)
ax13.set_title(f'Signal II')
ax13.grid(True)
except FileNotFoundError as e:
print(f"Error: {e}")
plt.subplots_adjust(wspace=0.4, hspace=0.4)
plt.tight_layout()
plt.show()