Asked by Ankit Bhandari
on 6 Aug 2017

I have attached two contour plots for pressure field outside an ellipse. The contour plots I got are jagged, may be due to the numerical integration I am using to calculate the pressure fields.The two plots are with Gaussian points 15000 and 5000 respectively. It is observable that with more Gaussian points, the contour plots are becoming smooth, but it is also taking time. Is there any other way to get smooth contour lines.

Answer by John D'Errico
on 6 Aug 2017

Accepted Answer

Sorry. If you try to resize a matrix (using interpolation) that is not itself sufficiently smooth that the original matrix yields non-smooth contours, you will get more finely interpolated noise! Interp2 is a waste of time here.

If you have a noisy matrix, then BEFORE you compute contours, you MUST smooth it FIRST if you want smooth contours. That smoothing may be done by removing the noise before you create the matrix, thus noise reduction prior to creating the matrix of data. Or you can do smoothing by applying a smoothing tool to your matrix, after creation.

You are doing some form of numerical integration that creates a noisy result. So a higher precision numerical integration is an option. Noise in the integration, as well as your comments, suggests you might be doing a Monte Carlo integration. I can't help if you don't say.

If you must do post smoothing on the matrix, then the classic solution is simply to apply a Gaussian blur to the matrix. You can do that using conv2. Or it should be possible to use methods like Savitsky-Golay, in two dimensions.

John D'Errico
on 6 Aug 2017

Ok, higher order Gaussian integration would reduce the error, which I suppose could look like noise to the contour solver.

I don't know what function you are integrating, so it becomes difficult to suggest something better than a Gaussian quadrature. Perhaps you might be better off using a different Gaussian quadrature rule though. But since I don't know the domain of your integration, nor do I know what the function looks like at all, I can offer no concrete suggestions there. The value of gaussian quadrature is it is in a sense optimal. You get the highest order result for a fixed number of points.

As for smoothing, conv2, with a Gaussian kernel would work as a smoother. This is the classic solution. Be careful around the edges of course, because conv2 will implicitly assume the function is zero outside the domain, that will bias your smooth towards zero on the perimeter.

X0 = ones(20);

u = -3:3;

ker = exp(-(u.^2 + u'.^2)/2);

ker = ker/sum(ker(:));

Xs = conv2(X0,ker,'same');

surf(Xs)

So you need to deal with the perimeter separately.

I see no reason why you could not implement a Savitsky-Golay method in 2-d. (I wonder if there is something on the FEX for that?) It just becomes a different kernel to use with conv2. Again the perimeter becomes an issue, but that should be easily enough patched.

A quick glance at the FEX shows two codes you might consider:

https://www.mathworks.com/matlabcentral/fileexchange/23287-smooth2a

https://www.mathworks.com/matlabcentral/fileexchange/37147-savitzky-golay-smoothing-filter-for-2d-data

One or both of those tools might help.

Since this sort of question does get asked frequently enough, I might consider posting some omnibus code. (Sigh.) I suppose were I to write a general 2-d smoothing tool, I could think of at least 4 quick methods to employ, thus Gaussian blur, Savitsky-Golay, finite difference methods based on a Laplacian, (which probably are essentially kissing-cousins of Savitsky-Golay methods), median filter methods, probably a few others if I started to look around.

Ankit Bhandari
on 6 Aug 2017

Hi John ! Thanks for an amazing answer.

Below are the two plots I have got. The first one is the contour plot with 10000 gaussian points calculation. This is a pressure field around an ellipse. The second plot is the smoothened curve I got using the conv2 function with kernel as you mentioned. I have a question regarding the choice of kernel? How do we decide what kernel to use?

Also, as it is observable, the values in colourbar are changed, for example, the maximum pressure in original plot was .6, but in the smoothened plot is .15. Could you please provide some suggestions how can I work this out. Thanks !

Image Analyst
on 7 Aug 2017

Sign in to comment.

Answer by Chad Greene
on 6 Aug 2017

I like KSSV's option of using imresize, because it performs antialiasing before interpolation. To scale by 1/5,

sc = 1/5; % scaling factor

contour(imresize(X,sc),imresize(Y,sc),imresize(Z,sc))

res = ?;

lambda = ?;

Zf = filt2(Z,res,lambda,'lp');

contour(X,Y,Zf)

where you'll have to enter res, which is the pixel size in x,y; and you'll have to enter lambda, the approximate lowpass wavelength.

Sign in to comment.

Answer by Teja Muppirala
on 7 Aug 2017

Edited by Teja Muppirala
on 7 Aug 2017

Maybe a median filter? However ORDFILT2 needs the Image Processing Toolbox.

Z = peaks(501); % Sample data

Z = Z +0.1*randn(size(Z));

Z(abs(Z)<0.5) = 0;

subplot(211);

contourf(Z);

title('original')

subplot(212);

rad = 5; %Filter radius

[x,y] = meshgrid(-rad:rad);

filtArea = x.^2+y.^2 <= rad.^2; % Use a round filter

Zfilt = ordfilt2(Z,ceil(nnz(filtArea)/2),filtArea);

contourf(Zfilt);

title('filtered')

Sign in to comment.

Answer by jacob faerman
on 15 Jan 2018

The median filter worked nicely for me, and filter2 does not need the image processing toolbox.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.