SIGNAL PROCESSING
CASE
18BMC303J Biomedical signal Processing
Download the Normal ECG signal and Abnormal ECG from MIT database and add randomly generated noise to it and apply Butterworth high pass filter to remove the low frequency noise. Then extract the statistical features from the filtered signal of both Normal ECG signal and Abnormal ECG and Tabulate it. Then fed these features as input to any Neural network classifiers and classify the signals.
PROCEDURE
Loading ECG Signals:
loading normal and abnormal ECG signals from the MIT database. specify the filenames as '100m.mat' for normal and '16265m.mat' for abnormal.
Adding Noise:
Gaussian noise is added to both the normal and abnormal ECG signals. This simulates real-world scenarios where ECG signals can be corrupted by noise during acquisition.
Butterworth High-Pass Filter:
designed a Butterworth high-pass filter to remove low-frequency noise from the noisy ECG signals. The filter parameters include the order and cutoff frequency.
Statistical Feature Extraction:
extraction of statistical features (mean, median, and standard deviation) from both the filtered normal and abnormal ECG signals. These features can be used for further analysis and classification.
Data Visualization:
creation four subplots to visualize the noisy and filtered ECG signals for both normal and abnormal cases. This helps in comparing the original and processed signals.
Neural Network Classification:
generate example data for training a neural network. It's important to replace this example data with your actual ECG feature data.
The code splits the data into training and testing sets using a specified split ratio.
create a feedforward neural network with two hidden layers and set training options such as the number of epochs and learning rate.
The neural network is trained on the training data.
Predictions are made on the test set, and accuracy is calculated to evaluate the network's performance.
Additional Neural Network Example:
There is an additional example of training and testing a neural network with different parameters. You define the network architecture and use the Levenberg-Marquardt backpropagation algorithm for training.
Confusion Matrix and Accuracy:
calculate a confusion matrix to assess the performance of the neural network in classifying ECG signals. The accuracy is reported as a percentage.
ensure that you have the necessary MATLAB toolbox for signal processing and neural network development.
Training: Train the neural network using your training dataset. Use the train function, which takes the network object, training data, and training options as input. Training options include the number of epochs, mini-batch size, and more.
Validation: Validate the trained network on a separate validation dataset to monitor its performance and avoid overfitting. MATLAB's training process will provide validation results.
Testing: Evaluate the network's performance on a testing dataset to assess its generalization.
Performance Analysis and Fine-Tuning: Analyze the performance metrics and, if necessary, fine-tune the network architecture, hyperparameters, or data preprocessing steps to improve performance.
Save the Trained Model: Save the trained neural network model using the save function for later use.
Deployment: Deploy the trained network to make predictions on new, unseen data. You can load the saved model using the load function.
CODE
% Load normal ECG signal (replace 'rec_1m.mat' with the actual filename)
normal_ecg_struct = load('100m.mat');
normal_ecg = normal_ecg_struct.val; % Extract the ECG data
% Load abnormal ECG signal (replace '100m.mat' with the actual filename)
abnormal_ecg_struct = load('16265m.mat');
abnormal_ecg = abnormal_ecg_struct.val; % Extract the ECG data
% Sampling frequency (adjust to match your data)
Fs = 1000; % Assuming a sampling frequency of 1000 Hz
% Create time vectors for the x-axis
t_normal = (0:length(normal_ecg) - 1) / Fs;
t_abnormal = (0:length(abnormal_ecg) - 1) / Fs;
% Add Gaussian noise to both normal and abnormal ECG signals
mean_noise = 0;
std_dev_noise = 0.1;
noisy_normal_ecg = normal_ecg + std_dev_noise * randn(size(normal_ecg)) + mean_noise;
noisy_abnormal_ecg = abnormal_ecg + std_dev_noise * randn(size(abnormal_ecg)) + mean_noise;
% Define filter parameters
order = 6; % Filter order
cutoff_frequency = 0.5; % Cutoff frequency (in Hz)
% Design the Butterworth filter
[b, a] = butter(order, cutoff_frequency / (Fs / 2), 'high');
% Apply the filter to the noisy normal and abnormal ECG signals
filtered_normal_ecg = filter(b, a, noisy_normal_ecg);
filtered_abnormal_ecg = filter(b, a, noisy_abnormal_ecg);
% Extract statistical data (e.g., mean, median, standard deviation)
mean_normal = mean(filtered_normal_ecg);
median_normal = median(filtered_normal_ecg);
std_dev_normal = std(filtered_normal_ecg);
mean_abnormal = mean(filtered_abnormal_ecg);
median_abnormal = median(filtered_abnormal_ecg);
std_dev_abnormal = std(filtered_abnormal_ecg);
% Display statistical data
fprintf('Statistical Data for Normal ECG:\n');
fprintf('Normal Mean: %.4f\n', mean_normal);
fprintf('Normal Median: %.4f\n', median_normal);
fprintf('Normal Standard Deviation: %.4f\n\n', std_dev_normal);
fprintf('Statistical Data for Abnormal ECG:\n');
fprintf('Abnormal Mean: %.4f\n', mean_abnormal);
fprintf('Abnormal Median: %.4f\n', median_abnormal);
fprintf('Abnormal Standard Deviation: %.4f\n', std_dev_abnormal);
% Plot the original and filtered ECG signals for normal and abnormal cases
figure;
subplot(2, 2, 1);
plot(t_normal, noisy_normal_ecg);
title('Noisy Normal ECG Signal');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(2, 2, 2);
plot(t_normal, filtered_normal_ecg);
title('Filtered Normal ECG Signal (Butterworth)');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(2, 2, 3);
plot(t_abnormal, noisy_abnormal_ecg);
title('Noisy Abnormal ECG Signal');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(2, 2, 4);
plot(t_abnormal, filtered_abnormal_ecg);
title('Filtered Abnormal ECG Signal (Butterworth)');
xlabel('Time (s)');
ylabel('Amplitude');
linkaxes; % Link the x-axes for easier comparison
% Generating example data
rng(42); % Set random seed for reproducibility
X = randn(100, 10); % Replace with actual input features
y = randi([0, 1], 100, 1); % Replace with actual labels (0 or 1)
% Split the data into training and testing sets
splitRatio = 0.8;
splitIdx = round(splitRatio * size(X, 1));
X_train = X(1:splitIdx, :);
y_train = y(1:splitIdx);
X_test = X(splitIdx+1:end, :);
y_test = y(splitIdx+1:end);
% Create a feedforward neural network
net = feedforwardnet([10, 5]); % Two hidden layers with 10 and 5 neurons
% Set up training options
net.trainParam.epochs = 100;
net.trainParam.lr = 0.01;
% Train the neural network
net = train(net, X_train', y_train');
% Make predictions on the test set
predictions = sim(net, X_test') > 0.5;
% Evaluate the performance
accuracy = sum(predictions == y_test) / numel(y_test);
disp(['Test Accuracy: ', num2str(accuracy * 100), '%']);
% Assuming you have a trained neural network 'net' and test data X_test, y_test
% Test the neural network
y_pred = round(net(X_test')');
% Evaluate the performance
accuracy = sum(y_pred == y_test) / numel(y_test);
fprintf('Test Accuracy: %.2f%%\n', accuracy * 100);
% Assuming you have a dataset with features (X) and labels (y)
% Replace this with your actual data loading process
% Sample data (replace this with your actual data)
rng(42); % For reproducibility
X = randn(100, 10); % Replace with your feature data
y = randi([0, 1], 100, 1); % Replace with your label data (0 or 1 for binary classification)
% Split the data into training and testing sets
train_ratio = 0.8;
num_samples = size(X, 1);
num_train_samples = round(train_ratio * num_samples);
idx = randperm(num_samples);
X_train = X(idx(1:num_train_samples), :);
y_train = y(idx(1:num_train_samples));
X_test = X(idx(num_train_samples+1:end), :);
y_test = y(idx(num_train_samples+1:end));
% Define the neural network architecture
hiddenLayerSize = 64;
net = feedforwardnet(hiddenLayerSize, 'trainlm'); % 'trainlm' is the Levenberg-Marquardt backpropagation algorithm
% Train the neural network
net = train(net, X_train', y_train');
% Test the neural network
y_pred = round(net(X_test')');
% Evaluate the performance
conf_matrix = confusionmat(y_test, y_pred);
accuracy = sum(diag(conf_matrix)) / sum(conf_matrix(:));
% Print the results
fprintf('Confusion Matrix:\n');
disp(conf_matrix);
fprintf('Test Accuracy: %.2f%%\n', accuracy * 100);
fprintf('Accuracy: %.2f%%\n', accuracy * 100);