How to get a smooth scalar field on regular grids from irregularly distributed sample data?
11 views (last 30 days)
Show older comments
I have irregularly distributed sample data (Rs, Zs, phi00), its contourf plot looks smooth like:

I use:
phi=griddata(Rs,Zs,phi00,Rgrid,Zgrid,'cubic');
to get the data on regular grids. There are some zigzags on the contourf plot from regular grids like:

Since we can get a smooth contourf plot from the irregular sample data, I believe that there must a simple way to get the smooth data on regular grids. How can I get it?
The sample data points and the contourf plot and the scatteredinterpolant figures are:


8 Comments
Mathieu NOE
on 2 Jun 2025
well, maybe scatteredInterpolant is not very happy with your complex shape
there are "cuts" that probably causes some trouble here

we may have to find another (custom) solution
must those cuts remain or can we consider it could be a continuous surface ?
Accepted Answer
Zhichen
on 2 Jun 2025
Edited: Zhichen
on 3 Jun 2025
2 Comments
Mathieu NOE
on 3 Jun 2025
FYI, I could get a fairly smooth plot with the original data (coarse) , thanks to : surfir - Surface plot from irregular data points - File Exchange - MATLAB Central

code is traightforward :
x = readmatrix('Rs.dat');
y = readmatrix('Zs.dat');
z = readmatrix('000.dat');
figure
subplot(1,2,1)
scatter3(x(:),y(:),z(:))
title('data points')
subplot(1,2,2)
surfir(x(:),y(:),z(:));
title('surface plot')
More Answers (2)
Image Analyst
on 2 Jun 2025
See my attached scattered interpolant demo. It will make a smooth function everywhere. If you don't want it for certain pixels, then you can mask the final interpolated image with a mask of where you want it.
finalOutputImage(mask) = 0;
3 Comments
Image Analyst
on 3 Jun 2025
It looks like the non-smooth dotted lines in your figures are probably actually in the data and are not an artifact of the interpolation. Correct me if I'm wrong. If so then why do you want it to be smoother than the actual data, which looks like a smooth analytical function because I don't see any random noise in it. Anyway, you can do any number of smoothing filters on the interpolation image to smooth out sharp edges and glitches.
TED MOSBY
on 3 Jun 2025
You can try using the function "gridfit" which can be downloaded from here:
You can unzip the zipped file and add it to path to start using the function.
"gridfit" works as it does not insist on hitting every data point; instead it balances data fidelity against smoothness. By letting a few points move a little, the fitted surface can stay globally smooth rather than sacrificing smoothness to hit every sample exactly.
Below is a practical workflow of this:
% data already in Rs, Zs, phi0
rvec = linspace(min(Rs), max(Rs), 300);
zvec = linspace(min(Zs), max(Zs), 300);
[Rg,Zg] = meshgrid(rvec, zvec);
phiGF = gridfit(Rs, Zs, phi0, rvec, zvec, ...
'smooth', 0.02, ...
'interp', 'bilinear');
figure('Color','w');
contourf(Rg, Zg, phiGF, 20, 'LineColor','none');
axis equal tight, colorbar
title('\phi gridfit, smooth = 0.02');
Hope this helps!
1 Comment
Mathieu NOE
on 3 Jun 2025
Edited: Mathieu NOE
on 3 Jun 2025
hmm, I tried this one too but I think it does not provide a 100% satisfying result, as this leads to unrealistic values where gridfit extrapolates in (yellow) areas where there is no original data

this is your code :
Rs = readmatrix('Rs.dat');
Zs = readmatrix('Zs.dat');
phi0 = readmatrix('000.dat');
figure('Color','w');
contourf(Rs, Zs, phi0, 20, 'LineColor','none');
caxis([min(phi0(:)) max(phi0(:))]);
axis equal tight, colorbar
% data already in Rs, Zs, phi0
rvec = linspace(min(Rs(:)), max(Rs(:)), 300);
zvec = linspace(min(Zs(:)), max(Zs(:)), 300);
[Rg,Zg] = meshgrid(rvec, zvec);
phiGF = gridfit(Rs, Zs, phi0, rvec, zvec, ...
'smooth', 0.02, ...
'interp', 'bilinear');
figure('Color','w');
contourf(Rg, Zg, phiGF, 20, 'LineColor','none');
caxis([min(phi0(:)) max(phi0(:))]);
axis equal tight, colorbar
title('\phi gridfit, smooth = 0.02');
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!