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.
- 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]
Invoke the function with: [ys,fout,sout]=smooth1Dconv(y,w(optional),s(optional))
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
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
ys: smoothed y
fout: filter used
sout: sigma used, NaN if not used
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: