How to get a smooth scalar field on regular grids from irregularly distributed sample data?

11 views (last 30 days)
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
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 ?
Zhichen
Zhichen on 2 Jun 2025
Edited: Zhichen on 2 Jun 2025
These cuts are considered to be continuous surfaces. There is no problem due to these cuts from scatteredInterpolant.

Sign in to comment.

Accepted Answer

Zhichen
Zhichen on 2 Jun 2025
Edited: Zhichen on 3 Jun 2025
After interpolation to get additional (fake) sample data according to the original data points in their coordinates, the ScatteredInterpolation or the griddata function can get similar contourf plot as the it is ploted using the original data.
It seems contourf plot may interpolate data in thier original format (coordinates) to get a smooth figure in 2D space. It is more advanced than ScatteredInterpolation in MATLAB. I think MATLAB should have a Interpolation function behaves as contour plot, who can automatically smooth the data depending on how it is arranged.
If any one have any other simpler method in MATLAB to generate smooth data on structured grid, please post it. I will appreciate and accept your answer.
Original data and contourf plot:
The more (fake) sample data:
Smooth data on structed gird by "griddata", similar as the plot from original data:
  2 Comments
Mathieu NOE
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')
Zhichen
Zhichen on 3 Jun 2025
A very smooth figure can be plotted using contourf plot in MATLAB with original data (as shown in the 1st figure in my question).
In MATLAB, it seem that the only way to get a smooth scalar field on structured grid using the original data I have here is to increase the sample data points in some regions according to the original sample data distribution, as how I did in my answer.

Sign in to comment.

More Answers (2)

Image Analyst
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
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.
Zhichen
Zhichen on 3 Jun 2025
Yes, the the dots are original data. They are listed in 2D array. In one direction, the field changes slowly, so the dots are further away from each other, while in the other direction, they need to be close to each other to record the rapidly varied field. Contour plot knows this, it might interpolate data on the directions along the 2D array, and can give a smooth 2D figure based on the 2D data, but the interpolation functions in MATLAB will not.
I think an interpolation function based on multidimensional array will be usefaull in MATLAB.

Sign in to comment.


TED MOSBY
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
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');

Sign in to comment.

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!