Main Content

Smooth Data with Convolution

You can use convolution to smooth 2-D data that contains high-frequency components.

Create 2-D data using thepeaksfunction, and plot the data at various contour levels.

Z = peaks(100); levels = -7:1:10; contour(Z,levels)

Figure contains an axes. The axes contains an object of type contour.

Inject random noise into the data and plot the noisy contours.

Znoise = Z + rand(100) - 0.5; contour(Znoise,levels)

Figure contains an axes. The axes contains an object of type contour.

Theconv2function 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 kernelKand useconv2在平滑噪声数据Znoise. Plot the smoothed contours. The'same'option inconv2makes the output the same size as the input.

K = (1/9)*ones(3); Zsmooth1 = conv2(Znoise,K,'same'); contour(Zsmooth1, levels)

Figure contains an axes. The axes contains an object of type contour.

平滑噪声数据with a 5-by-5 kernel, and plot the new contours.

K = (1/25)*ones(5); Zsmooth2 = conv2(Znoise,K,'same'); contour(Zsmooth2,levels)

Figure contains an axes. The axes contains an object of type contour.

See Also

|||

Related Topics