Adaptive Filtering Routine: Adaptive Window Sliding Filter Implementation in MATLAB – Comparison of Original Signal, Fixed Window Filtering, and Adaptive Window Filtering

Adaptive Filtering Routine: Adaptive Window Sliding Filter Implementation in MATLAB - Comparison of Original Signal, Fixed Window Filtering, and Adaptive Window Filtering

The program implements three types of processing and comparison for one-dimensional time series signals: the original signal, fixed window sliding filtering, and adaptive window sliding filtering based on local variance judgment. The purpose is to simulate a real noise environment and examine the impact of different window strategies on signal smoothing effects, fidelity, and error characteristics, evaluated through multi-angle error metrics and visual graphics.

<span>Download link for the code</span>

Table of Contents

  • Program Design, Adaptive Concepts
  • Results
  • MATLAB Source Code

Program Design, Adaptive Concepts

The adaptive concept used in this program: First, a reference signal composed of two sets of sine waves is constructed, with controllable intensity of random noise added as the original data to be filtered. In the fixed window filtering section, a fixed sliding window length is set for median smoothing of the signal; in contrast, the adaptive filtering module traverses multiple odd window sizes within a specified range for each sampling point, calculates the local variance corresponding to each window, and selects the window with the minimum variance as the optimal window, thus achieving a balance between local stability of the signal and detail preservation. The program also records the trajectory of the adaptive window, visually demonstrating how the algorithm dynamically adjusts the smoothing degree based on noise intensity.

The plotting module includes various subplots such as signal comparison, adaptive window trajectory, error curves, absolute error, error histograms, and cumulative error, allowing for a systematic observation of the differences among the three signals. Subsequently, the performance evaluation module calculates and outputs metrics such as Mean Squared Error (MSE), Root Mean Square Error (RMSE), Signal-to-Noise Ratio (SNR), and smoothness indicators to quantify the differences in denoising effects, signal fidelity, and smoothing capabilities of different methods.

Results

Signal curves before and after filtering:Adaptive Filtering Routine: Adaptive Window Sliding Filter Implementation in MATLAB - Comparison of Original Signal, Fixed Window Filtering, and Adaptive Window FilteringAdaptive window sizes:Adaptive Filtering Routine: Adaptive Window Sliding Filter Implementation in MATLAB - Comparison of Original Signal, Fixed Window Filtering, and Adaptive Window FilteringError output:Adaptive Filtering Routine: Adaptive Window Sliding Filter Implementation in MATLAB - Comparison of Original Signal, Fixed Window Filtering, and Adaptive Window Filtering

Performance metrics:Adaptive Filtering Routine: Adaptive Window Sliding Filter Implementation in MATLAB - Comparison of Original Signal, Fixed Window Filtering, and Adaptive Window Filtering

MATLAB Source Code

The complete code is as follows:

% Adaptive Window Sliding Filter
% Function: Compare original signal, fixed window filtering, and adaptive window filtering
% Author: matlabfilter (WeChat same number), custom MATLAB code related to positioning and navigation, filtering
% 2025-11-18/Ver1 

clear;
close all;clc;
rng(0);


%% Parameter Settings
signal_length =2000;% Signal length
noise_level =0.3;% Noise intensity
min_window =5;% Minimum window size
max_window =13;% Maximum window size
fixed_window =3;% Fixed window size

%% Generate Test Signal: Sine Wave + Noise
fprintf('Generating test signal\n');
% Generate sine wave composite signal and add noise
    t =(0:signal_length-1)/10;
    base_signal =sin(t)+0.5*sin(3* t);
    noise =(randn(1, signal_length)-0.5)*2* noise_level;
    raw_signal = base_signal + noise;
%% Fixed Window Filtering
fprintf('Performing fixed window filtering\n');
fixed_filtered =fixed_window_filter(raw_signal, fixed_window);

%% Adaptive Window Filtering
fprintf('Performing adaptive window filtering\n');
[adaptive_filtered, window_sizes]=adaptive_window_filter(raw_signal, min_window, max_window);

%% Plot Result Comparison
fprintf('Plotting results\n');
plot_results(raw_signal, fixed_filtered, adaptive_filtered, window_sizes, fixed_window);

%% Calculate Performance Metrics
[metrics, errors]=calculate_performance(raw_signal, fixed_filtered, adaptive_filtered,base_signal);

%% Plot Error Comparison
plot_error_analysis(errors, raw_signal, fixed_filtered, adaptive_filtered);

%% ==================== Function Definitions ====================

Complete code:

https://mall.bilibili.com/neul-next/detailuniversal/detail.html?isMerchant=1&page=detailuniversal_detail&saleType=10&itemsId=13553916&loadingShow=1&noTitleBar=1&msource=merchant_share

Or click on “Read the original text” at the end to jump.

<span>If you need assistance or have custom code requirements related to navigation and positioning filtering, you can contact the author via WeChat below</span>

Adaptive Filtering Routine: Adaptive Window Sliding Filter Implementation in MATLAB - Comparison of Original Signal, Fixed Window Filtering, and Adaptive Window Filtering

Leave a Comment