Functions > Data Analysis > Smoothing > Example: Smoothing of X-Y Data
Example: Smoothing of X-Y Data
Use the ksmooth, medsmooth, and supsmooth functions to smooth x-y data. Use the movavg function to smooth data by taking a moving average with a specific window of width.
1. Define a matrix with x values in its first column and y values in its second column.
2. Sort column 0 in ascending order.
ksmooth
ksmooth returns a vector of local weighted averages of vy using a Gaussian kernel with bandwidth b; which controls the smoothing window.
Bandwidth b is usually set to a few times the spacing between data points on the x-axis, depending on the desired degree of smoothing. The larger the bandwidth, the smoother the resulting curve.
1. Set b to a value between the minimum and maximum value of X.
2. Evaluate the ksmooth function.
3. Plot the ksmooth function.
It is important to select bandwidth carefully. Too large a bandwidth will wash out details as it averages over the whole data set. Too small a bandwidth may create artificial details in the smoothed data. Try changing b above to numbers between 0.01 and 2 to see these effects.
medsmooth
medsmooth returns a smoothed vector by replacing each value in vy with the median of the n points centered on that value.
The smoothing window argument, n, must be an odd integer.
1. Define n as an odd integer.
2. Evaluate the medsmooth function.
3. Plot the medsmooth function.
supsmooth
supsmooth requires no additional arguments.
1. Evaluate the supsmooth function.
Compare the above three smoothed sets of data to the original data.
movavg
1. Set the window width.
The wider the window, the smoother the resulting curve. Number of data points was calculated to be 100.
2. Evaluate the movavg function.
3. Plot the movavg function.
Depending on the variations in your original data, one of the above smoothing functions might be more suitable than the others for generating the desired smoothed data.