PROJECT REPORT
ECG SIGNAL PROCESSING
  ELECTRONICS AND COMMUNICATION ENGINEERING
                                                               T5B
                                                    MIDHUN P S 623
                                              NISSI MARY JOSEPH 637
                                       SIDDHARTH S KRISHNAN 650
                                          SONICA JOSE GOMEZ 653
                                     VAISAYAN BHATTACHARYA 660
                                                VIJAY KRISHNA S 664
                                                   VISHNUDEV R 665
                                                 VISHNU PRIYA S 666
                                INTRODUCTION
       The electrocardiogram (ECG) is a non-invasive tool widely used to monitor the
electrical activity of the heart. ECG signals contain critical information about heart health, and
analyzing these signals can aid in diagnosing various cardiovascular conditions. This project
focuses on the processing and analysis of ECG signals to extract vital information such as heart
rate and R-peak locations, which represent ventricular contractions.
In this project, a real ECG signal is first preprocessed using a bandpass filter to eliminate noise,
including baseline wander and high-frequency interference. The filtered signal is then analyzed
to detect the R-peaks, which are the most prominent features of the ECG waveform. From these
detected peaks, RR intervals are computed, allowing the calculation of the heart rate. The
performance of the signal processing steps is visualized through time-domain plots of the
original and filtered ECG, along with the identified R-peaks.
The project demonstrates fundamental techniques for ECG signal processing and lays the
foundation for advanced heart condition analysis, contributing to the broader field of biomedical
signal processing.
                                 Methodology
This project focuses on the processing and analysis of ECG signals to detect R-peaks,
   compute RR intervals, and estimate the heart rate. The methodology followed is
                          structured in the following steps:
  1.   Data Acquisition
          •   The ECG signal used in this project is loaded from a .mat file
              (ecgdemodata1.mat). The signal contains raw ECG data along with the
              corresponding sampling rate. It is essential to ensure that the signal is in the
              correct format, typically a column vector, for further processing.
  2. Preprocessing
        • Objective: Remove noise and artifacts from the ECG signal.
        • Steps:
                 i. A bandpass filter with a frequency range of 0.5 Hz to 50 Hz is designed
                    using a second-order Butterworth filter. This frequency range is
                    chosen to retain the ECG signal components of interest while removing
                    baseline wander, low-frequency noise, and high-frequency
                    interference.
                ii. The filtered ECG signal is obtained using the filtfilt function, which
                    applies the filter in a zero-phase manner to prevent any phase
                    distortion.
  3. R-Peak Detection
        • Objective: Identify the R-peaks, which correspond to the ventricular
            depolarization (the QRS complex) in the ECG signal.
        • Steps:
                 i. The findpeaks function is used to detect peaks in the filtered ECG signal.
                    Parameters such as MinPeakHeight and MinPeakDistance are set to
                    ensure accurate detection of R-peaks. The minimum peak height is
                    chosen based on the signal characteristics, and the minimum peak
                    distance is calculated as approximately 0.6 seconds, corresponding to
                    a typical human heart rate.
                ii. The locations of the R-peaks are extracted for further analysis.
  4. RR Interval Calculation
        • Objective: Calculate the intervals between consecutive R-peaks, known as RR
            intervals, which reflect the time between heartbeats.
        • Steps:
                 i. The RR intervals are computed by taking the difference between
                    consecutive R-peak locations, and these intervals are then converted
                    to seconds using the sampling rate.
  5. Heart Rate Calculation
          •   Objective: Estimate the heart rate in beats per minute (bpm).
           •   Steps:
                   i. The heart rate is calculated using the RR intervals, with the formula:
                                                                   60
                                      Heart Rate(bpm) =
                                                          𝑅𝑅 𝑖𝑛𝑡𝑒𝑟𝑣𝑎𝑙𝑠(𝑠𝑒𝑐𝑜𝑛𝑑𝑠)
                   ii. The average heart rate is computed over all RR intervals and displayed
                       as an indicator of the subject’s heart rate during the recorded ECG
                       period.
   6. Visualization
           •   Objective: Provide visual insights into the ECG signal processing steps.
           •   Steps:
                    i. Time-domain plots are created to visualize the following:
                           1. The original ECG signal to show raw data.
                           2. The filtered ECG signal to demonstrate the effect of
                              preprocessing
                           3. The filtered ECG signal with detected R-peaks to highlight the
                              detected features of the signal.
                   ii. These visualizations help in validating the effectiveness of the
                       preprocessing and peak detection steps.
This systematic approach ensures that the project addresses key objectives of ECG signal
processing while providing clear results in the form of detected R-peaks, heart rate, and
visualizations for validation.
                                    PROGRAM
clc;
clear all;
close all;
% Load ECG data from a .mat file
load('ecgdemodata1.mat');
% Ensure that ecg is a column vector
if isrow(ecg)
    ecg = ecg'; % Convert to column vector if necessary
end
% Define the time vector based on the length of the ECG signal and sampling rate
t = (0:length(ecg)-1) / samplingrate;
% 1. Preprocess the ECG Signal
% Bandpass filter to remove noise (0.5-50 Hz)
lowCutoff = 0.5; % Low cutoff frequency in Hz
highCutoff = 50; % High cutoff frequency in Hz
[b, a] = butter(2, [lowCutoff highCutoff] / (samplingrate / 2), 'bandpass');
filteredECG = filtfilt(b, a, ecg);
% 2. Detect R-Peaks
% Adjust the parameters based on your signal characteristics
[~, locs] = findpeaks(filteredECG, 'MinPeakHeight', 500, 'MinPeakDistance',
round(0.6 * samplingrate));
% Display detected peak information
disp(['Number of R-peaks detected: ', num2str(length(locs))]);
disp('Locations of R-peaks (in samples):');
disp(locs’);
% 3. Calculate the RR Intervals
if length(locs) > 1
    % Compute the RR intervals in seconds
    RR_intervals = diff(locs) / samplingrate;
% 4. Compute the Heart Rate
    if ~isempty(RR_intervals)
         % Calculate heart rate in beats per minute (bpm)
         heartRate = 60 ./ RR_intervals;
         avgHeartRate = mean(heartRate);
         disp(['Average Heart Rate: ', num2str(avgHeartRate), ' bpm']);
    else
         disp('No valid RR intervals found.');
    end
else
    disp('Not enough R-peaks detected to calculate heart rate.');
end
% 5. Plot ECG Signal with R-Peaks
subplot(3,1,1);
plot(t,ecg);
xlabel('Time (s)');
ylabel('Amplitude');
title('Original ECG Signal');
subplot(3,1,2);
plot(t, filteredECG);
xlabel('Time (s)');
ylabel('Amplitude');
title('Filtered ECG Signal');
subplot(3,1,3);
plot(t, filteredECG);
hold on;
if ~isempty(locs)
    plot(locs / samplingrate, filteredECG(locs), 'ro');
end
xlabel('Time (s)');
ylabel('Amplitude');
title('ECG Signal with R-Peaks');
legend('Filtered ECG Signal', 'R-Peaks');
                                 RESULTS
1. ECG Signal Plots:
      •   Figure 1: Original ECG Signal
              i. Include the plot of the original ECG signal (the raw data) to show what
                 the unprocessed signal looks like.
      •   Figure 2: Filtered ECG Signal
              i. Display the filtered ECG signal to demonstrate the effect of
                 preprocessing and noise removal.
      •   Figure 3: ECG Signal with R-Peaks
              i. Show the filtered ECG signal along with the detected R-peaks marked
                 on the plot. This will validate that the R-peak detection is working as
                 expected.
2. Command Window Output:
     •   A total of 46 R-peaks were detected in the ECG signal. The detected peaks are
         located at the following sample points:
     •   The calculated average heart rate is 62.8373 bpm, which falls within the normal
         range for a healthy individual. This suggests that the subject’s heart rate was
         stable during the recording.
                                 CONCLUSION
         In this project, we successfully implemented an ECG signal processing workflow that
includes noise filtering, R-peak detection, RR interval calculation, and heart rate estimation.
The methodology employed a bandpass filter to effectively remove noise from the ECG signal,
preserving key features such as the QRS complexes. Using the `findpeaks` function, we
accurately detected the R-peaks, which correspond to ventricular depolarization events in the
heart.
From the detected R-peaks, RR intervals were calculated, providing valuable information about
the time between heartbeats. These intervals were used to estimate the heart rate, which was
found to be within a normal range for the given ECG data. The visualization of the original,
filtered ECG signal, and R-peak detection further validated the accuracy of our approach.
The project demonstrated the fundamental steps required for ECG signal analysis, providing a
strong foundation for more advanced applications such as heart rhythm analysis, arrhythmia
detection, and diagnostic systems. The successful detection of R-peaks and the estimation of
heart rate underline the utility of this processing pipeline in both medical diagnostics and
research applications.
Overall, the project illustrates the effectiveness of signal processing techniques in extracting
meaningful insights from raw ECG data, which can be extended to real-time monitoring or
integrated into healthcare devices for continuous cardiac assessment.
                                  REFERENCE
   1.    https://archive.physionet.org/cgi-bin/atm/ATM