How can I remove noise from my 2-D data in MATLAB 7.7 (R2008b)?
5 views (last 30 days)
Show older comments
I have a 2-D data that has noise. I want to get rid of the noise and smooth out the data.
% set figure window size
dfpos = get(0,'DefaultFigurePosition');
figure('Position',dfpos([1 2 3 4]).*[1 1 1.5 1]);
% plot peaks surface
Z = peaks(100);
subplot(1,2,1);
surf(Z)
title('Peaks Surface (underlying data)')
% add noise to the peaks data and plot it
ZN = Z + 5*rand(100);
subplot(1,2,2);
surf(ZN)
title('Peaks Surface (noise added)')
Accepted Answer
MathWorks Support Team
on 27 Jun 2009
To smooth out the 2-D data you have you can use the filtering functions that treat your 2-D data as an image you need to remove the noise from.
1. You can use the MEDFILT2 function from the Image Processing Toolbox which does 2-D median filtering.
B=medfilt2(A, [m n], padopt)
performs median filtering of the matrix A in two dimensions. Each output element of the matrix B contains the median value in the m-by-n neighborhood around the corresponding element in the input matrix A, 'padopt' option controls how the matrix boundaries are padded. Consider setting 'padopt' option to 'symmetric' to extend matrix A symmetrically at the boundaries.
For more information on the MEDFILT2 function refer to the documentation by executing the following at the MATLAB command line:
web([docroot '/toolbox/images/medfilt2.html'])
2. You can also use FILTER2 MATLAB function to filter 2-D data:
Y = filter2(h,X,shape)
filters the data in X with the two-dimensional FIR filter in the matrix h. It computes the result, Y, using two-dimensional correlation, and returns the part of the correlation specified by the 'shape' parameter. Set 'shape' parameter to 'valid' to return only those parts of the correlation that are computed without zero-padded edges. Note that when ‘shape’ is set to 'valid', Y is smaller than X
More details on the FILTER2 function can be found by executing the following in the MATLAB command prompt:
web([docroot '/techdoc/ref/filter2.html'])
To use FILTER2 function you will have to specify a filter ‘h’. You can create a predefined 2-D filter using FSPECIAL function from the Image Processing Toolbox:
h = fspecial(type, parameters)
accepts the filter specified by 'type' plus additional modifying 'parameters' particular to the type of filter chosen. If you omit these arguments, FSPECIAL uses default values for the 'parameters'. For more information on the FSPECIAL function execute the following in the MATLAB command prompt:
web([docroot '/toolbox/images/fspecial.html'])
Consider the following example of using the MEDFILT2 and FLITER2 functions on the noisy peaks data:
% set figure window size
dfpos = get(0,'DefaultFigurePosition');
figure('Position',dfpos([1 2 3 4]).*[1 1 1.5 1]);
% smooth the noisy peaks data with MEDFILT2
ZN_smooth1=medfilt2(ZN, [15 15],'symmetric');
subplot(1,2,1);
surf(ZN_smooth1);
title('Peaks Surface (2-D median filter applied)')
% smooth the noisy peaks data with FILTER2
h = fspecial('average', [10 10]);
ZN_smooth2=filter2(h,ZN,'valid');
subplot(1,2,2);
surf(ZN_smooth2);
title('Peaks Surface(2-D digital averaging filter applied)')
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!