32 Downloads
Updated 09 Oct 2006
No License
Radial base functions (RBF) can be used for interpolation and and approximation of scattered data i.e. data is not required to be on any regular grid. The same function can handle data interpolation in any dimension. See file rbftest.m for more examples.
1. Create RBF interpolation using
rbf=rbfcreate(x, f); ?x? ? coordinates of the nodes and ?f? - values of the function at the nodes
2. Calculate interpolated values ?fi? at nodes ?xi? using
fi = rbfinterp(xi, rbf); rbf ? is structure returned by rbf=rbfcreate(x, f)
%1D example
x = 0:1.25:10; f = sin(x);
xi = 0:.1:10;
%Matlab interpolation
fi = interp1(x,f,xi);
% RBF interpolation
rbf=rbfcreate(x, f);
fi = rbfinterp(xi, rbf);
%2D example
x = rand(50,1)*4-2; y = rand(50,1)*4-2; z = x.*exp(-x.^2-y.^2);
ti = -2:.05:2;
[XI,YI] = meshgrid(ti,ti);
%Matlab interpolation
ZI = griddata(x,y,z,XI,YI,'cubic');
%RBF interpolation
rbf=rbfcreate([x'; y'], z');
ZI = rbfinterp([XI(:)'; YI(:)'], op);
ZI = reshape(ZI, size(XI));
Optional parameters:
1. Radial Base Function:
rbfcreate(x, f ,'RBFFunction', 'multiquadric');
available RBF functions are: multiquadric, gaussian, linear, cubic, thinplate
2. Smoothing level: (must be a positive scalar)
rbfcreate(x, f ,'RBFSmooth', 0.1);
3. Multiquadric and gaussian functions have definable constants
rbfcreate(x, f ,?RBFConstant', 0.1);
RBF interpolation usually produces much better results that standard Matlab functions but computation complexity of RBF interpolation is n^3 thus it is not recommended to use it for more then 2000 nodes.
1.0.0.0 | Several users complained about lack of explanations. So I added presentation that explains how to use provided set of functions. |
Inspired: Sound Power Directivity Analysis
Create scripts with code, output, and formatted text in a single executable document.
Gianni Schena (view profile)
Krzysztof Pilczynski (view profile)
Gianni Schena (view profile)
% same acceleration ( 6 X ) possible in function [f] = rbfinterp(x, options).
f = zeros(1, nPoints);
r = zeros(1, n);
rbfcoeff_np1=rbfcoeff(n+1); % scalar
rbfcoeff_1ton=rbfcoeff(1:n);
idx=(1:dim)+n+1;
rbfcoeff1_dim= rbfcoeff((1:dim)+n+1);
parfor i=1:1:nPoints
s=0;
% r=repmat(x(:,i),1,n)-nodes; %safer ( old mlb ver <2016b)
% new mlb feature : implicit expansion
r=x(:,i)-nodes; % recent mlb versions 2016b>
r_sq = r(1,:).*r(1,:)+r(2,:).*r(2,:);
% r_sq = sum(r.*r,1); % ok
r_v = sqrt(r_sq);
%
s = rbfcoeff_np1 + rbfcoeff_1ton*(r_sq.*log(r_v+1))';
f(i) = s+rbfcoeff1_dim*x(1:dim,i);
end
Gianni Schena (view profile)
% sensible speed-up (5 X on my HW) by avoiding the use of 'fval' to invoke functions and transforming the original nested loop into a simple parfor loop. see following example
function [A]=rbfAssemble(x, phi, const, smooth)
[dim n]=size(x),
A=zeros(n,n);
Ai=zeros(1,n);
if isa(phi,'function_handle')
s = functions(phi) ,
end
%%
if strcmp(s.function, 'rbfphi_thinplate')
tic
parfor i=1:n
Ai=zeros(n,1);
xvi=x(:,i)-x(:,1:i); %
% new section
% more readable but less performing :
% for j=1:i
% r=xvi(:,j); %
%
% Ai(j)=sqrt(r'*r);
% end
% less readable but more performing :
Ai(1:i)=sqrt(sum(xvi(:,1:i).*xvi(:,1:i),1));
A(i,:)=(Ai.*Ai.*log(Ai+1));
% end new section
end
net_time_assemblage=toc,
else
Philip Gröger (view profile)
Works nicely. Help should be implemented properly when using "help rbfinterp/rpfcreate".
One mis-spelling in the help comments: to use multiquadric the string has to be "multiquadric" not "multiquadrics"
ava rahimi (view profile)
weapon325 (view profile)
Adil Sbai (view profile)
good work ...
Kim jieon (view profile)
Kim jieon (view profile)
Michal Kvasnicka (view profile)
Perfect code, thanks!!!
liang (view profile)
The program has been translated into c++
see: https://github.com/seuliang/RBF_interpolation/
walid silabdi (view profile)
please give me the programme of identification's systems no-linear with RBF network
Michael Dicker (view profile)
Alejandro Arrizabalaga (view profile)
Nice function. Only, for large data sets it easily runs into memory problems. This could be improved by using the fact that only half of the matrix A in rbfAssemble is actually needed, since it is symmetric.
Seth (view profile)
While this is not a perfectly implemented code for the file exchange (see J. D'Errico's comments) - it is exactly what I was looking for and has proven extremely helpful for my engineering problem. The big ticket item for me was the ability to extrapolate scattered data (as well as interpolate). Thank you for providing this tool.
Ying zhang (view profile)
good stuff, thanks
very quickly, thanks!
Windows-only documentation, a complete waste of time
It seems the time is due to review this code again, at least Scott Andrews seems to think it is. Note that my first review was made before any improvements were made. It was an entirely accurate review at that time.
The Powerpoint doc is indeed quite helpful. But remember that when you choose to use this code in the future, it might be nice to have Matlab help available for these codes. I'll paste in the complete help provided for the rbfinterp function here:
[]
Yes. That is right. There is no help. No indication of what the arguments mean. So next month when you want to use these tools, you should drop out of matlab and fire up this file into powerpoint just to remember what order the arguments follow and what they mean? Sorry, but this is ridiculous.
Back to the Powerpoint doc. The author chooses to make an apples to oranges comparison to show how good these tools are. Surely the author understands that use of Matlab's Pchip interpolant is a poor choice for interpolating an ocsillatory function? Had the author actually compared a true spline fit to the RBF model, the difference would have been far smaller. Conversely, It is trivial to show an example of a monotone function where pchip (the interp1 'cubic' interpolant) will shine, yet RBF interpolation will be miserable.
My earlier comments about the slowness of RBF interpolation are still valid. These techniques create dense matrices, so interpolation over thousands of points may be slow.
Overall, the powerpoint help improves these tools considerably. The lack of any help for a main function would have normally pulled my rating down farther, but the author deserves commendation for the effort expended on the ppt doc. The error checking is good, plus its a good implementation of the property/value pair interface in rbfcreate. Plus, these tools do provide an alternative to linear interpolation in multiple dimensions.
This is amazingly simple and well documented code. With regards to the prior reviewers comments, I'm not sure what aspect he believes difficult to use. I read the ppt included and was up and running in 5 minutes. Also, the package does interpolate and approximate. If the smoothing is off, then it interpolates exactly as advertised. The only slight downside is that the algo is a bit slow, but there is no way to fit high-dimensional data in any Mathworks package other than linear interpolations. As such, it is a wonderful addition.
A very good introduction to RBF method.
The code is concise and clear, no extra comments required to figure out how it works. The package is good to conduct a quick test of how RBF methods work in particular application. Good work!
This has potential. It also has virtually no help. No hints about how to use it, except by trying to read the uncommented test examples and guess what the options do. You will need to edit the code just to learn what the options are. Its a major flaw in a package that some might find as useful. I will also note that this package is touted as doing interpolation, but the examples show that smoothing was done. Interpolation REPRODUCES the original data. It should not smooth the data, so the description is inaccurate.
Finally, I'll repeat the athor's statement that this code will be slow for large sets of data. It will easily exceed your system memory since it generates large dense matrices.
I would have given this code a lower rating, except I also recognize that this code, when cleaned up, might actually be useful. That is, if anybody but the author could figure out how to use it.