Upload 11 files
Browse files- filtered_data/X_test_filtered.npy +3 -0
- filtered_data/X_train_filtered.npy +3 -0
- filtered_data/X_val_filtered.npy +3 -0
- filtered_data/class_distribution.png +3 -0
- filtered_data/class_names.npy +3 -0
- filtered_data/ecg_filtering_results.png +3 -0
- filtered_data/ecg_lead1_samples.png +3 -0
- filtered_data/filter.py +128 -0
- filtered_data/y_test.npy +3 -0
- filtered_data/y_train.npy +3 -0
- filtered_data/y_val.npy +3 -0
filtered_data/X_test_filtered.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0d1634410db97603b59e1bb4221f740526730117ef2b1d55fbee04e96bffeb9f
|
| 3 |
+
size 176840128
|
filtered_data/X_train_filtered.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f4fda7b9ff94d7e614c9541c6c6f4a9e948aec61b022d010f53e4936dc408fb2
|
| 3 |
+
size 1414480128
|
filtered_data/X_val_filtered.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:34dbb38b54b83189b7941673086065bc818d34ca4180b8981d7381588b99f414
|
| 3 |
+
size 176800128
|
filtered_data/class_distribution.png
ADDED
|
Git LFS Details
|
filtered_data/class_names.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:55a079fa8b25ee383fec13c408f58ca33caa665b0eb5109eb658e6cccaa94ac6
|
| 3 |
+
size 272
|
filtered_data/ecg_filtering_results.png
ADDED
|
Git LFS Details
|
filtered_data/ecg_lead1_samples.png
ADDED
|
Git LFS Details
|
filtered_data/filter.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pywt
|
| 3 |
+
import os
|
| 4 |
+
from scipy.signal import butter, filtfilt, iirnotch, medfilt, find_peaks
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
# 1. Load the Data
|
| 7 |
+
|
| 8 |
+
print("Loading datasets...")
|
| 9 |
+
data_train = np.load("X_train.npy")
|
| 10 |
+
data_valid = np.load("X_val.npy")
|
| 11 |
+
data_test = np.load("X_test.npy")
|
| 12 |
+
|
| 13 |
+
print("\n--- Dataset Shapes ---")
|
| 14 |
+
print(f"Train data shape: {data_train.shape}")
|
| 15 |
+
print(f"Validation data shape: {data_valid.shape}")
|
| 16 |
+
print(f"Test data shape: {data_test.shape}")
|
| 17 |
+
print("----------------------\n")
|
| 18 |
+
|
| 19 |
+
os.mkdir("filtered_data")
|
| 20 |
+
|
| 21 |
+
# 2. Define the Processing Functions
|
| 22 |
+
def _remove_baseline_wander(ecg_signal, wavelet='sym8'):
|
| 23 |
+
desired = int(np.ceil(np.log2(100/0.5)))
|
| 24 |
+
max_level = pywt.dwt_max_level(len(ecg_signal), pywt.Wavelet(wavelet).dec_len)
|
| 25 |
+
level = min(desired, max_level)
|
| 26 |
+
|
| 27 |
+
coeffs = pywt.wavedec(ecg_signal, wavelet, level=level)
|
| 28 |
+
coeffs_filt = [coeffs[0]] + [np.zeros_like(c) for c in coeffs[1:]]
|
| 29 |
+
|
| 30 |
+
baseline = pywt.waverec(coeffs_filt, wavelet)
|
| 31 |
+
baseline = baseline[:len(ecg_signal)]
|
| 32 |
+
|
| 33 |
+
return ecg_signal - baseline
|
| 34 |
+
|
| 35 |
+
def _denoise(data):
|
| 36 |
+
# Wavelet transform
|
| 37 |
+
coeffs = pywt.wavedec(data=data, wavelet='db5', level=9)
|
| 38 |
+
cA9, cD9, cD8, cD7, cD6, cD5, cD4, cD3, cD2, cD1 = coeffs
|
| 39 |
+
|
| 40 |
+
# Threshold denoising
|
| 41 |
+
threshold = (np.median(np.abs(cD1)) / 0.6745) * (np.sqrt(2 * np.log(len(cD1))))
|
| 42 |
+
cD1.fill(0)
|
| 43 |
+
cD2.fill(0)
|
| 44 |
+
|
| 45 |
+
for i in range(1, len(coeffs) - 2):
|
| 46 |
+
coeffs[i] = pywt.threshold(coeffs[i], threshold)
|
| 47 |
+
|
| 48 |
+
# Inverse wavelet transform to obtain the denoised signal
|
| 49 |
+
rdata = pywt.waverec(coeffs=coeffs, wavelet='db5')
|
| 50 |
+
return rdata
|
| 51 |
+
|
| 52 |
+
def remove_noise_and_wander(data):
|
| 53 |
+
baseline_wander = _remove_baseline_wander(data)
|
| 54 |
+
cleaned = _denoise(baseline_wander)
|
| 55 |
+
return cleaned
|
| 56 |
+
|
| 57 |
+
# 3. Apply the Filter
|
| 58 |
+
# Assuming your data shape is (num_samples, signal_length).
|
| 59 |
+
# If it's a 1D array of a single continuous signal, remove the list comprehension loop.
|
| 60 |
+
def process_batch(dataset_array):
|
| 61 |
+
print(f"Processing {len(dataset_array)} samples...")
|
| 62 |
+
# Applies the cleaning function to each row/signal in the array
|
| 63 |
+
return [remove_noise_and_wander(signal) for signal in dataset_array]
|
| 64 |
+
|
| 65 |
+
print("\nFiltering Train Data...")
|
| 66 |
+
train_filtered = process_batch(data_train)
|
| 67 |
+
|
| 68 |
+
print("Filtering Validation Data...")
|
| 69 |
+
valid_filtered = process_batch(data_valid)
|
| 70 |
+
|
| 71 |
+
print("Filtering Test Data...")
|
| 72 |
+
test_filtered = process_batch(data_test)
|
| 73 |
+
|
| 74 |
+
np.save("filtered_data/X_train_filtered.npy", train_filtered)
|
| 75 |
+
np.save("filtered_data/X_val_filtered.npy", valid_filtered)
|
| 76 |
+
np.save("filtered_data/X_test_filtered.npy", test_filtered)
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
print("Loading data...")
|
| 80 |
+
data_train_new = np.load("filtered_data/X_train_filtered.npy")
|
| 81 |
+
data_valid_new = np.load("filtered_data/X_val_filtered.npy")
|
| 82 |
+
data_test_new = np.load("filtered_data/X_test_filtered.npy")
|
| 83 |
+
|
| 84 |
+
print("\n--- Dataset Shapes ---")
|
| 85 |
+
print(f"Train data shape: {data_train_new.shape}")
|
| 86 |
+
print(f"Validation data shape: {data_valid_new.shape}")
|
| 87 |
+
print(f"Test data shape: {data_test_new.shape}")
|
| 88 |
+
print("----------------------\n")
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
# --- 3. Plot 5 Samples Before and After ---
|
| 92 |
+
# We assume data_train is a 2D array of shape (num_samples, signal_length)
|
| 93 |
+
num_samples_to_plot = 5
|
| 94 |
+
|
| 95 |
+
# Create a figure with 5 rows and 2 columns
|
| 96 |
+
fig, axes = plt.subplots(nrows=num_samples_to_plot, ncols=2, figsize=(16, 12))
|
| 97 |
+
fig.suptitle('ECG Signal Processing: Before vs. After', fontsize=18, fontweight='bold')
|
| 98 |
+
|
| 99 |
+
for i in range(num_samples_to_plot):
|
| 100 |
+
# Get the original 1D signal for the i-th sample
|
| 101 |
+
original_signal = data_train_new[i]
|
| 102 |
+
|
| 103 |
+
# Process the signal
|
| 104 |
+
filtered_signal = remove_noise_and_wander(original_signal)
|
| 105 |
+
|
| 106 |
+
# Plot Original (Before) - Left Column
|
| 107 |
+
axes[i, 0].plot(original_signal, color='crimson', linewidth=1)
|
| 108 |
+
axes[i, 0].set_title(f"Sample {i+1}: Original (Raw)")
|
| 109 |
+
axes[i, 0].set_ylabel("Amplitude")
|
| 110 |
+
axes[i, 0].grid(True, linestyle='--', alpha=0.6)
|
| 111 |
+
|
| 112 |
+
# Plot Filtered (After) - Right Column
|
| 113 |
+
axes[i, 1].plot(filtered_signal, color='navy', linewidth=1)
|
| 114 |
+
axes[i, 1].set_title(f"Sample {i+1}: Filtered (Denoised & Baseline Removed)")
|
| 115 |
+
axes[i, 1].grid(True, linestyle='--', alpha=0.6)
|
| 116 |
+
|
| 117 |
+
# Only show x-axis label for the bottom-most plots to keep it clean
|
| 118 |
+
if i == num_samples_to_plot - 1:
|
| 119 |
+
axes[i, 0].set_xlabel("Time steps")
|
| 120 |
+
axes[i, 1].set_xlabel("Time steps")
|
| 121 |
+
|
| 122 |
+
# Adjust layout so titles and labels don't overlap
|
| 123 |
+
plt.tight_layout(rect=[0, 0.03, 1, 0.96])
|
| 124 |
+
|
| 125 |
+
# Save the figure as a high-resolution PNG
|
| 126 |
+
plt.savefig("ecg_filtering_results.png", dpi=300, bbox_inches='tight')
|
| 127 |
+
print("Image saved successfully as 'ecg_filtering_results.png'")
|
| 128 |
+
plt.show()
|
filtered_data/y_test.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f69c0bb383bf0c7888d711edc0c56572dd94d7e669addd2eb8d5a6b9f970e101
|
| 3 |
+
size 35496
|
filtered_data/y_train.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:188301990a9cd1db0d4c3612c0ebedb5dfdea6d25211650f5c652e4512a9dfbd
|
| 3 |
+
size 283024
|
filtered_data/y_val.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7efae012c776d98737956b23e052d33fb987b4967733171fb63ad0bf897984db
|
| 3 |
+
size 35488
|