| import numpy as np |
| import pywt |
| import os |
| from scipy.signal import butter, filtfilt, iirnotch, medfilt, find_peaks |
| import matplotlib.pyplot as plt |
| |
|
|
| print("Loading datasets...") |
| data_train = np.load("X_train.npy") |
| data_valid = np.load("X_val.npy") |
| data_test = np.load("X_test.npy") |
|
|
| print("\n--- Dataset Shapes ---") |
| print(f"Train data shape: {data_train.shape}") |
| print(f"Validation data shape: {data_valid.shape}") |
| print(f"Test data shape: {data_test.shape}") |
| print("----------------------\n") |
|
|
| os.mkdir("filtered_data") |
|
|
| |
| def _remove_baseline_wander(ecg_signal, wavelet='sym8'): |
| desired = int(np.ceil(np.log2(100/0.5))) |
| max_level = pywt.dwt_max_level(len(ecg_signal), pywt.Wavelet(wavelet).dec_len) |
| level = min(desired, max_level) |
| |
| coeffs = pywt.wavedec(ecg_signal, wavelet, level=level) |
| coeffs_filt = [coeffs[0]] + [np.zeros_like(c) for c in coeffs[1:]] |
| |
| baseline = pywt.waverec(coeffs_filt, wavelet) |
| baseline = baseline[:len(ecg_signal)] |
| |
| return ecg_signal - baseline |
|
|
| def _denoise(data): |
| |
| coeffs = pywt.wavedec(data=data, wavelet='db5', level=9) |
| cA9, cD9, cD8, cD7, cD6, cD5, cD4, cD3, cD2, cD1 = coeffs |
|
|
| |
| threshold = (np.median(np.abs(cD1)) / 0.6745) * (np.sqrt(2 * np.log(len(cD1)))) |
| cD1.fill(0) |
| cD2.fill(0) |
| |
| for i in range(1, len(coeffs) - 2): |
| coeffs[i] = pywt.threshold(coeffs[i], threshold) |
|
|
| |
| rdata = pywt.waverec(coeffs=coeffs, wavelet='db5') |
| return rdata |
|
|
| def remove_noise_and_wander(data): |
| baseline_wander = _remove_baseline_wander(data) |
| cleaned = _denoise(baseline_wander) |
| return cleaned |
|
|
| |
| |
| |
| def process_batch(dataset_array): |
| print(f"Processing {len(dataset_array)} samples...") |
| |
| return [remove_noise_and_wander(signal) for signal in dataset_array] |
|
|
| print("\nFiltering Train Data...") |
| train_filtered = process_batch(data_train) |
|
|
| print("Filtering Validation Data...") |
| valid_filtered = process_batch(data_valid) |
|
|
| print("Filtering Test Data...") |
| test_filtered = process_batch(data_test) |
|
|
| np.save("filtered_data/X_train_filtered.npy", train_filtered) |
| np.save("filtered_data/X_val_filtered.npy", valid_filtered) |
| np.save("filtered_data/X_test_filtered.npy", test_filtered) |
|
|
|
|
| print("Loading data...") |
| data_train_new = np.load("filtered_data/X_train_filtered.npy") |
| data_valid_new = np.load("filtered_data/X_val_filtered.npy") |
| data_test_new = np.load("filtered_data/X_test_filtered.npy") |
|
|
| print("\n--- Dataset Shapes ---") |
| print(f"Train data shape: {data_train_new.shape}") |
| print(f"Validation data shape: {data_valid_new.shape}") |
| print(f"Test data shape: {data_test_new.shape}") |
| print("----------------------\n") |
|
|
|
|
| |
| |
| num_samples_to_plot = 5 |
|
|
| |
| fig, axes = plt.subplots(nrows=num_samples_to_plot, ncols=2, figsize=(16, 12)) |
| fig.suptitle('ECG Signal Processing: Before vs. After', fontsize=18, fontweight='bold') |
|
|
| for i in range(num_samples_to_plot): |
| |
| original_signal = data_train_new[i] |
| |
| |
| filtered_signal = remove_noise_and_wander(original_signal) |
| |
| |
| axes[i, 0].plot(original_signal, color='crimson', linewidth=1) |
| axes[i, 0].set_title(f"Sample {i+1}: Original (Raw)") |
| axes[i, 0].set_ylabel("Amplitude") |
| axes[i, 0].grid(True, linestyle='--', alpha=0.6) |
| |
| |
| axes[i, 1].plot(filtered_signal, color='navy', linewidth=1) |
| axes[i, 1].set_title(f"Sample {i+1}: Filtered (Denoised & Baseline Removed)") |
| axes[i, 1].grid(True, linestyle='--', alpha=0.6) |
| |
| |
| if i == num_samples_to_plot - 1: |
| axes[i, 0].set_xlabel("Time steps") |
| axes[i, 1].set_xlabel("Time steps") |
|
|
| |
| plt.tight_layout(rect=[0, 0.03, 1, 0.96]) |
|
|
| |
| plt.savefig("ecg_filtering_results.png", dpi=300, bbox_inches='tight') |
| print("Image saved successfully as 'ecg_filtering_results.png'") |
| plt.show() |