Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Zero-Phase Digital Filter in Pure C

📌 Project Overview

This project implements a zero-phase digital IIR filter in pure C language, aiming to achieve the same numerical precision as:

  • MATLAB filtfilt()
  • Python scipy.signal.filtfilt()

Filter coefficients are designed using MATLAB/Python and exported to C for embedded-level execution.

The goal is to reproduce high-precision zero-phase filtering behavior in a lightweight, portable C implementation.

img


🚀 Motivation

In many embedded and biomedical signal processing applications (e.g., EEG, ECG, PPG), phase distortion is unacceptable.

The standard solution in high-level environments is:

filtfilt() → forward-backward filtering

However, MATLAB and Python implementations cannot be directly deployed to embedded systems.

This project provides:

  • A pure C zero-phase IIR filter
  • Edge padding compensation
  • Initial condition calculation
  • Precision benchmarking against MATLAB and Python

If you need more technical details, please refer to my blog: C语言实现IIR型零相位带通滤波器


🧠 Core Features

1️⃣ Zero-Phase Filtering

We implement forward-backward filtering:

  1. Forward filtering
  2. Reverse signal
  3. Backward filtering

This eliminates phase distortion.


2️⃣ Edge Compensation Strategy

To avoid edge distortion, we use:

  • Symmetric edge padding
  • Proper initial state computation

This ensures numerical stability and minimizes transient artifacts.


3️⃣ Precision Benchmark

We compare outputs among:

  • MATLAB filtfilt()
  • Python scipy.signal.filtfilt()
  • Pure C implementation

Result:

The numerical difference between all three implementations is accurate up to:

6 decimal places

MAE and RMSE remain extremely small, confirming numerical consistency.


📊 Visualization

The project generates:

  • Time-domain comparison
  • Difference curves
  • Error metrics (MAE / RMSE)
  • FFT spectrum comparison

All figures are automatically saved in the figures/ directory.

img

img

img


🔬 Technical Specifications

  • 4th-order Butterworth Bandpass Filter
  • Sampling rate: 250 Hz
  • Band: 4–40 Hz
  • Optional 50 Hz notch filter
  • Zero-phase forward-backward filtering
  • Double precision floating-point implementation

🎯 Application Scenarios

  • EEG signal preprocessing
  • Biomedical signal filtering
  • Embedded DSP systems
  • Real-time physiological monitoring devices

🏁 Conclusion

This project demonstrates that:

A carefully designed pure C implementation can achieve nearly identical numerical precision to MATLAB and Python filtfilt().

It provides a reliable solution for deploying high-precision zero-phase filters in embedded systems.


About

This repository provides pure C code for implementing IIR type zero phase filters with filtering accuracy similar to MATLAB's filtfilt function and Python's scipy.signal.filtfilt function

Resources

Stars

5 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages