You can use convolution to smooth 2-D data that contains high-frequency components.
Create 2-D data using thepeaks
function, and plot the data at various contour levels.
Z = peaks(100); levels = -7:1:10; contour(Z,levels)
Inject random noise into the data and plot the noisy contours.
Znoise = Z + rand(100) - 0.5; contour(Znoise,levels)
Theconv2
function in MATLAB® convolves 2-D data with a specified kernel whose elements define how to remove or enhance features of the original data. Kernels do not have to be the same size as the input data. Small-sized kernels can be sufficient to smooth data containing only a few frequency components. Larger sized kernels can provide more precision for tuning frequency response, resulting in smoother output.
Define a 3-by-3 kernelK
and useconv2
在平滑噪声数据Znoise
. Plot the smoothed contours. The'same'
option inconv2
makes the output the same size as the input.
K = (1/9)*ones(3); Zsmooth1 = conv2(Znoise,K,'same'); contour(Zsmooth1, levels)
平滑噪声数据with a 5-by-5 kernel, and plot the new contours.
K = (1/25)*ones(5); Zsmooth2 = conv2(Znoise,K,'same'); contour(Zsmooth2,levels)