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.
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 filteredfAlpha(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.
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 filterednSize(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.
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 filterednSize(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.
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 filterednSize(UINT): Number of samples to analyze (1-21)fTolerance(LREAL): Tolerance for considering values equalfOut(LREAL): Most frequent value (mode)
Use Case: Best for signals that should have discrete levels or when you need the most "typical" recent value.
- 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
- 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
- All filters include parameter validation and clamping
- Buffer overflow protection
- Division by zero prevention
- Initialization handling to prevent startup spikes
// Declare filter instance
MyVelocityFilter : FB_IIR_Filter;
// In your cyclic program
MyVelocityFilter(
fIn := axRealEncoder.fActVelocity,
fAlpha := 0.1
);
SmoothedVelocity := MyVelocityFilter.fOut;
| 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 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 |
- 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
- v1.0 - Initial implementation of all four filter types
- Tested on CODESYS runtime
This project is provided as-is for educational and industrial automation purposes.