EKG with a Buzz: Removing 60 Hz Noise
Summary
In this lab, students explore biomedical signal processing using a provided MATLAB script that simulates an electrocardiogram (ECG/EKG). They analyze how 60 Hz noise affects the signal and how a filter can be applied to clean it. Students then convert the script into a Live Script and add interactive parameters—such as heart rate, noise frequency, and filter bandwidth—to observe how these changes alter the waveform and spectrum. Throughout the activity, they use AI tools such as MATLAB Copilot and ChatGPT to assist with code explanations, parameter tuning, and documentation.
Learning Outcomes:
- Understand the effect of 60 Hz interference on an ECG signal.
- Learn how to use MATLAB tools to analyze and clean signals..
- Modify a provided script to include interactive parameters and visualize results by converting an .m file into a Live Script with AI-assisted guidance.
Keywords:
ECG, MATLAB, signal processing, noise filtering, FFT, spectrogram, notch filter, AI-assisted learning, Live Script, parameter exploration.
Learning Goals
Concepts and Content:
- Understand the structure and key components of an ECG signal (P, QRS, T, U).
- Recognize the effect of 60 Hz power-line interference on biomedical signals.
- Explore time-domain and frequency-domain representations (plots, FFT, spectrograms).
- Apply and evaluate a digital notch filter to reduce noise while preserving the ECG shape.
- Experiment with parameters such as heart rate, noise frequency, and filter bandwidth to observe their impact on the signal.
Use of MATLAB to Improve Learning:
- MATLAB is used to visualize ECG signals, add noise, analyze spectra, and apply filters.
- Students modify a provided script and then convert it into an interactive Live Script, adding controls for key parameters.
- Using AI tools such as MATLAB Copilot and ChatGPT, students learn to document, explain, and refine their code—seeing how AI can support engineering problem-solving and learning.
Higher-Order Thinking Skills:
- Critical thinking: interpreting how parameter changes and filtering affect signal quality.
- Computation: modifying existing code to run analyses and automate visualizations.
- Data analysis: using FFT and spectrograms to identify and quantify noise components.
- Model refinement: tuning parameters to balance signal preservation and noise reduction.
- Metacognition: reflecting on how AI assistance influences understanding and workflow.
Other Skills:
- Gain experience in scientific programming and code annotation within MATLAB.
- Develop AI-assisted coding literacy, including prompt design and result interpretation.
- Optionally prepare a short lab report or presentation describing results and design choices, reinforcing technical communication.
Context for Use
This activity is designed for upper-level undergraduate engineering or computer science students in courses such as Signal Processing, Biomedical Engineering Applications, or Digital Filtering. It works well in class sizes of up to about 40 students in a university setting, but can also be scaled for larger lab sections with group work.
The activity functions as a guided laboratory exercise (40–70 minutes) in which students work with a provided MATLAB script to explore the effects of 60 Hz interference on an ECG signal and learn how to clean it using filtering techniques. It can also be extended into a multi-day project if students are asked to design alternative filters, compare parameter effects, or prepare a short analysis report.
Prerequisites and Skills:
- Basic familiarity with MATLAB (running scripts, plotting, editing code).
- Introductory understanding of signal-processing concepts such as sampling, frequency, and sinusoidal signals.
- No prior knowledge of ECG physiology is required, although familiarity with biomedical signals enhances engagement.
- Basic awareness of AI-assisted tools (e.g., MATLAB Copilot, ChatGPT) is helpful but not required.
Adaptability:
- Works in any MATLAB-enabled lab or remote MATLAB Online environment.
- Faculty can shorten the activity to emphasize time- and frequency-domain visualization or expand it into a more advanced filtering and parameter-exploration project.
- Easily integrated into Engineering, Computer Science, or Biomedical Engineering courses where students are learning to connect theoretical concepts with computational tools.
Description and Teaching Materials
Activity Description
In this activity, students use MATLAB to explore biomedical signal processing with a provided script that simulates an electrocardiogram (ECG/EKG), adds 60 Hz noise, and applies a digital filter to recover a cleaner version. The activity progresses in three stages:
1. Signal Generation
A synthetic ECG waveform is generated based on its main wave components (P, QRS, T, and U).
The original generation code is adapted from R. Karthik (College of Engineering, Guindy, Anna University, Chennai).
2. Noise Contamination and Visualization
A 60 Hz sinusoidal signal—simulating power-line interference—is added to the ECG.
Students observe the effects of this noise in both the time domain and frequency domain (using FFT and spectrogram analysis).
3. Filtering and Recovery
A digital notch filter is implemented to attenuate the 60 Hz noise.
Students then compare the original, noisy, and filtered signals to evaluate the filter's effectiveness.
They also learn to enhance the provided script by adding adjustable parameters and converting it into a Live Script for interactive exploration and visualization.
Teaching Materials
MATLAB Script (annotated code)
A fully commented script that generates the ECG, adds noise, and performs filtering. Comments identify the original code (Karthik) and the extensions (Luis M. Vicente).
Role: Central teaching material for students to run and modify.
Sample Output Plots (PDF or images)
Plots of the clean ECG, noisy ECG, frequency spectrum, spectrogram, and filtered ECG.
Role: Reference for expected results so students can verify their outputs.
Instructor Handout (optional)
A concise, one-page guide summarizing objectives, key MATLAB commands, and expected results.
Role: Helps instructors integrate the activity efficiently.
Software
MATLAB is the primary platform because of its powerful signal-processing and visualization capabilities, which allow students to connect code directly with biomedical applications.
Instructions for Students (single .m script)
What to Do
- Download and unzip GeneratePQREKGandFilter.zip.
- Open MATLAB and run the script EKG_Filtering_Lab.m.
- Follow the in-script comments to:
- Generate a synthetic ECG (PQRST).
- Add 60 Hz noise.
- Visualize the signal in time and frequency (FFT and spectrogram).
- Apply a 60 Hz notch filter.
- Compare clean, noisy, and filtered signals and answer the prompts.
What to Submit
- Your completed .m script (include your name at the top).
- Convert your .m script into a Live Script (.mlx) and enhance it by adding interactive functionality:
- Click Convert ▸ Convert to Live Script (or Save As... and choose .mlx).
- In the Live Editor, click Insert ▸ Control to add sliders/dropdowns for parameters (bind them to variables named below).
- Split the script into sections (%%) in this order: Parameters → Generate ECG → Add Noise → Analyze (time/FFT) → Filter → Compare → Questions.
- Turn on Section Breakpoints only if you want students to step through.
- (Optional) Use "Signal Analyzer" app links or Live Editor Tasks ▸ Filter for demos, but keep the code solution with iirnotch.
- A Report in pdf format containing:
- Required plots (clean, noisy, and filtered signals; FFT of noisy and filtered signals).
- Brief answers to the reflection prompts at the end of the script.
- Please follow the instructions on how to create the report.
Files Provided to Students
- EKG_Filtering_Lab.m – main student script to edit and run.
- p_wav.m, q_wav.m, qrs_wav.m, s_wav.m, t_wav.m, u_wav.m – functions that generate ECG waveform components.
- All files are included in the GeneratePQREKGandFilter.zip package.
Live Script Interactivity:
Students will use Live Script controls to adjust the following parameters and observe how they affect the ECG waveform and filtered results.
Recommended Parameters:
Make each of these a Live Editor control:
- HeartRate_bpm (slider)
- Default: 72; Range: 40 to 140; Step: 1
- Purpose: changes ECG cycle length; students see morphology repeat faster/slower.
- Duration_s (slider)
- Default: 6; Range: 2 to 20
- Purpose: more cycles → clearer spectral lines.
- fs_Hz (dropdown or slider)
- Default: 240; Options: 120, 240, 500, 1000
- Purpose: sampling effects; demonstrates how resolution and Nyquist limit change.
- NoiseFreq_Hz (dropdown)
- Default: 60; Options: 60, 50
- Purpose: US vs. EU mains; lets them miss the notch on purpose, then fix it.
- NoiseAmp (slider)
- Default: 0.9; Range: 0 to 1.5; Step: 0.05
- Purpose: SNR stress test; demonstrates the distortion/over-notching tradeoffs.
- Notch_Q (slider)
- Default: 35; Range: 10 to 80; Step: 1
- Purpose: bandwidth control; higher Q = narrower, deeper notch.
- UseZeroPhase (switch)
- Default: false
- Purpose: toggles filter vs. filtfilt to discuss phase distortion.
- SpecWinLen (slider)
- Default: 256; Range: 64 to 1024 (power of two)
- Purpose: time–frequency resolution tradeoff in spectrogram.
Needed Files
- GeneratePQREKGandFilter.zip (Zip Archive 3kB Oct28 25)
- Handout EKG Filtering Labv02.docx (Microsoft Word 2007 (.docx) 442kB Oct28 25)
Teaching Notes and Tips
Supporting Students:
- Provide the annotated script in advance so students can focus on interpretation, not syntax.
- Review basic MATLAB commands (plot, fft, indexing) before starting.
- Encourage use of Run Section to visualize each stage of the code.
Common Challenges:
- Clarify the difference between time-domain and frequency-domain plots (the 60 Hz noise appears as a spectral peak).
- Emphasize that filters reduce noise but may slightly affect the ECG shape.
- Remind students that the ECG is synthetic, not a medical signal.
Extension Ideas:
- Relate results to sampling, aliasing, and filter design in real biomedical systems.
- Have students vary noise frequency (e.g., 50 Hz) or Q-factor and observe effects.
- Advanced option: design a custom notch filter using the Signal Processing Toolbox.
Classroom Adaptation:
- Suitable for small groups: one runs code, one documents, one interprets plots.
- Can be shortened to focus on visualization or expanded into a multi-day project.
Safety Note:
All work is simulation-based; remind students this is a teaching model, not for diagnosis.
Assessment
Student performance will be evaluated through a single lab report that demonstrates their understanding of the MATLAB simulation, signal analysis, and filtering concepts.
The report must include:
- MATLAB Results: Plots of the clean, noisy, and filtered ECG signals, plus FFT and spectrogram visualizations showing 60 Hz attenuation and preservation of the 1 Hz component.
- Analysis Responses: Short written explanations interpreting the plots—for example, how the spectrum confirms that the noise was removed and how filtering affects the signal shape.
- Filter Justification: A brief discussion of why a notch filter is appropriate for this problem and how changing parameters (e.g., Q-factor or noise frequency) influences results.
- Conclusion and References: A short summary of findings and proper acknowledgment of sources.
Performance will be judged by the clarity and accuracy of results, depth of interpretation, and completeness of documentation.
References and Resources
The following materials support both faculty and students in completing this lab activity:
[1] Luis M. Vicente – Class Notes on Signal Processing, Polytechnic University of Puerto Rico.
Instructor-provided material covering fundamentals of signal processing, filtering, and biomedical signal applications. Used as the primary teaching reference for this lab.
[2] MathWorks Documentation – Signal Processing Toolbox
https://www.mathworks.com/help/signal/
Official MATLAB documentation on filter design, Fourier transforms, and time–frequency analysis. Provides examples of how to implement IIR notch filters and visualize results.
[3] MathWorks Documentation – iirnotch Function
https://www.mathworks.com/help/signal/ref/iirnotch.html
Reference page for designing digital notch filters in MATLAB, relevant to the 60 Hz rejection filter implemented in this activity.
[4] Najarian, K., & Splinter, R. (2012). Biomedical Signal and Image Processing (2nd ed.). CRC Press.
Textbook covering practical applications of signal processing in medicine. Useful for students seeking additional context on ECG filtering and biomedical engineering applications.