Smoothing 1D data by moving different filter shapes

Smoothing a 1D curve by weighted moving average. The filtering is done by convolution with different filter types. Also with automatic mode.
286 Downloads
Updated 29 May 2023

View License

Smoothing a 1D curve by weighted moving average. The averaging is done by convolution with different filter types. Also with automatic generation of suitable filters.
The main difference to Malab's smoothing filter is: For full curve filtering the curve is linear extrapolated on both sides and not zero padded. Therefore the border values are much better than Matlab's smoothing filters or convolutions.
Features:
Moving average:
  • Filter width set as odd whole number: filter is all ones
E.g. 5 -> [1 1 1 1 1]
  • Filter width set as floating number: filter is triangle shaped
E.g. 3.5- > [0.5 0.75 1 0.75 0.5]
  • Filter width not set: Automatic mode, width determined by FFT
Gaussian shaped filter:
  • Width and sigma set: Gaussian filter according those values
  • Width set and sigma=0: Gaussian filter according width and default sigma
  • Width=0: Automatic mode, width determined by FFT and default sigma
User defined filter:
You may set any filter as long as the sum is not equal zero.
E.g. [1 1]
Manual:
Invoke the function with: [ys,fout,sout]=smooth1Dconv(y,w(optional),s(optional))
Input:
The first argument y is the vector to be smoothed.
The 2nd argument, called w (width of filter), is optional
The 3rd argument, called s (sigma of filter), is optional
Four modes are possible:
1.) w and s omitted: Moving average auto filter.
Filter width is determined by FFT, an odd number.
2.) w is a single integer odd number or floating number, s is omitted:
Moving average, all ones for integer odd numbers
or triangle shaped filter for all other numbers.
The center value is always one; the sum of the filter values is w.
If w<=1, no filtering
Examples:
w=1.5 -> filter=[0.25 1 0.25]
w=2.8 -> filter=[0.9 1 0.9]
w=3 -> filter=[1 1 1], w is an integer odd number!
w=3.5 -> filter=[0.5 0.75 1 0.75 0.5]
w=4 -> filter=[0.667 0.833 1 0.833 0.667]
3.) w and s set: Gaussian shaped filter
w is the width of the filter.
If w is not an odd integer number, then the next lower integer w is taken.
s is the sigma of the filter, if sigma=0, then s=(w-1)/4;
If w=0: Auto Gaussian filter width is determined by FFT, s input is discarded.
w>y-data size: w is resized to fit
4.) w is a vector
w are filter values (e.g. [0.5 1 0.5])
w-size>y-data size: w border values are cut to fit
Output:
ys: smoothed y
fout: filter used
sout: sigma used, NaN if not used
Remark:
The border values of the smoothed output ys are approximations. In order to calculate those values, the input y is extended by extrapolation of the smoothed input. The approximated border output is one half of the filter width.
If you want to smooth sine curves, you should also look at:

Cite As

Peter Seibold (2024). Smoothing 1D data by moving different filter shapes (https://www.mathworks.com/matlabcentral/fileexchange/66099-smoothing-1d-data-by-moving-different-filter-shapes), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2016a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired: poly2traj

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

smooth1Dconv230529

Version Published Release Notes
2.0.2.0

Improved filtering of border values.

2.0.1.1

Works now with vertical or horizontal input vector. Description changed.

2.0.1.0

Works now with vertical or horizontal input vector.

2.0.0.2

Demo GUI now with corresponding m-file

2.0.0.1

Other image

2.0.0.0

New methode with convolution

1.0.0.0