How to smooth out or fit a surface?

47 次查看(过去 30 天)
I have my data stored in a 44 by 44 matrix. When I plot this data, it has a lot of irregular peaks. I want a smooth surface instead. (I cannot use the cftool since I've got a long loop and cannot do it manually everytime.) Following is the code I'm using but I get an error saying that x and y should be a column matrix. If I remove the fit command line, the whole code runs perfectly fine. Can someone please point out my mistake or suggest changes to this code?
x = 1:44;
y = 1:44;
for i = 1:35
a = xlsread('\\uoa.auckland.ac.nz\engdfs\Homeair.xlsx',strcat('CO',int2str(46*i-18),':','EF',int2str(46*i+25)));
b = fit([x,y,],a,'lowess');
figure;
surf(x,y,b);
axis([0 44 0 44 -50 120]);
fname = sprintf('A%d.png',i);
saveas(gcf,fname);
end

采纳的回答

Chad Greene
Chad Greene 2016-5-20
If you have the image processing toolbox you could do a moving average or a moving median filter. Median filters tend to be good at removing lone bad grid points:
b_smooth = medfilt2(b,[5 5]));
performs a 5x5 moving median filter. Similarly you could do a 5x5 moving average:
b_smooth = imfilter(b,fspecial('average',[5 5]));
  3 个评论
Ke Chao
Ke Chao 2019-8-22
Hi, I have a question about this method. Since the window is [5 5], how about first/last 2 cols or rows? Do we need to extrapolate first and then doing moving average here?
Image Analyst
Image Analyst 2019-8-26
From the help for imfilter: "Input array values outside the bounds of the array are assigned the value X. When no padding option is specified, the default is 0." So you can see that when the window goes outside the image, and the center of the window is on the edge of the image or close to it, it assumes that the image is bigger and the value is zero out there. in other words, it does not shrink/crop the window as the window gets closer to the edge of the image.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2016-5-20
You can do a regression to fit a 2D polynomial surface to it. See John D'Errico's polyfitn: http://www.mathworks.com/matlabcentral/fileexchange/34765-polyfitn My demo of it is attached where I get a smooth background.

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by