Skip to content

This repository provides digital signal filtering function blocks for industrial PLCs, aimed at smoothing noisy sensor data—especially for velocity measurements from encoders or other real-time control systems.

Notifications You must be signed in to change notification settings

ogunerkutay/filters

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Digital Signal Filters for PLC Applications

This repository contains a collection of digital signal filtering function blocks implemented for industrial PLC applications. These filters are designed to smooth noisy sensor data, particularly useful for velocity measurements from encoders or other real-time control systems.

Filter Types

1. IIR (Infinite Impulse Response) Filter - FB_IIR_Filter

A simple first-order low-pass filter that provides exponential smoothing.

Features:

  • Real-time filtering with minimal memory usage
  • Adjustable smoothing factor (alpha)
  • Prevents initialization jumps
  • Input range clamping for safety

Parameters:

  • fIn (LREAL): Input signal to be filtered
  • fAlpha (REAL): Smoothing factor [0.0 - 1.0]
    • Higher values = less smoothing, faster response
    • Lower values = more smoothing, slower response
  • fOut (LREAL): Filtered output signal

Use Case: Best for continuous signals where you need fast response with moderate noise reduction.

2. Moving Average Filter - FB_MovingAverage

Calculates the arithmetic mean of the last N samples.

Features:

  • Configurable window size (1-100 samples)
  • Circular buffer implementation
  • Equal weight to all samples in window
  • Good for reducing random noise

Parameters:

  • fIn (LREAL): Input signal to be filtered
  • nSize (UINT): Number of samples to average (1-100)
  • fOut (LREAL): Averaged output signal

Use Case: Ideal for reducing random noise when you can tolerate some delay and want consistent smoothing.

3. Median Filter - FB_MedianFilter

Finds the middle value from a sorted window of samples.

Features:

  • Excellent spike/outlier rejection
  • Configurable odd window size (3-21 samples)
  • Preserves edges better than averaging
  • Built-in bubble sort algorithm

Parameters:

  • fIn (LREAL): Input signal to be filtered
  • nSize (UINT): Window size (must be odd, 3-21)
  • fOut (LREAL): Median filtered output

Use Case: Perfect for removing impulse noise and spikes while preserving signal edges and features.

4. Mode Filter - FB_ModeFilter

Finds the most frequently occurring value within a tolerance range.

Features:

  • Robust against outliers
  • Configurable tolerance for value matching
  • Window size up to 21 samples
  • Good for digital-like or stepped signals

Parameters:

  • fIn (LREAL): Input signal to be filtered
  • nSize (UINT): Number of samples to analyze (1-21)
  • fTolerance (LREAL): Tolerance for considering values equal
  • fOut (LREAL): Most frequent value (mode)

Use Case: Best for signals that should have discrete levels or when you need the most "typical" recent value.

Implementation Details

Memory Considerations

  • IIR Filter: Minimal memory usage (1 previous value)
  • Moving Average: Up to 1000 LREAL buffer (max 100 used)
  • Median Filter: Up to 21 LREAL samples + sorting array
  • Mode Filter: Up to 21 LREAL samples

Performance

  • IIR Filter: Fastest execution, O(1) complexity
  • Moving Average: Fast execution, O(n) complexity
  • Median Filter: Moderate execution, O(n²) due to sorting
  • Mode Filter: Slowest execution, O(n²) due to nested loops

Safety Features

  • All filters include parameter validation and clamping
  • Buffer overflow protection
  • Division by zero prevention
  • Initialization handling to prevent startup spikes

Usage Examples

Basic Implementation

// Declare filter instance
MyVelocityFilter : FB_IIR_Filter;

// In your cyclic program
MyVelocityFilter(
    fIn := axRealEncoder.fActVelocity,
    fAlpha := 0.1
);
SmoothedVelocity := MyVelocityFilter.fOut;

Choosing the Right Filter

Signal Characteristics Recommended Filter Reason
Continuous with random noise Moving Average or IIR Good general-purpose smoothing
Contains spikes/outliers Median Filter Excellent spike rejection
Stepped or discrete levels Mode Filter Finds most common value
Need fastest response IIR with high alpha Minimal computational overhead
Critical spike removal Median Filter Preserves signal integrity

Filter Comparison

Filter Type Noise Reduction Spike Rejection Edge Preservation Computational Cost Memory Usage
IIR Good Poor Poor Very Low Very Low
Moving Average Good Poor Poor Low Medium
Median Good Excellent Good Medium Medium
Mode Fair Excellent Excellent High Medium

Technical Notes

  • All filters use LREAL (64-bit floating point) for maximum precision
  • Filters are designed for cyclic execution in PLC scan cycles
  • Window sizes are limited to prevent excessive scan time impact
  • Consider your scan time requirements when choosing window sizes

Version History

  • v1.0 - Initial implementation of all four filter types
  • Tested on CODESYS runtime

License

This project is provided as-is for educational and industrial automation purposes.

About

This repository provides digital signal filtering function blocks for industrial PLCs, aimed at smoothing noisy sensor data—especially for velocity measurements from encoders or other real-time control systems.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published