Coder Social home page Coder Social logo

ahbarnett / spharm-interp Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 466 KB

MATLAB+Fortran routines for interpolation from scattered points on the sphere via spherical harmonics

License: Other

MATLAB 20.95% C 23.33% Makefile 0.82% Fortran 54.91%
global harmonics interpolation least-squares sphere spherical

spharm-interp's Introduction

spharm-interp

Interpolation from scattered points on the sphere via spherical harmonic expansion, in MATLAB/Octave with some Fortran.

Authors: Alex Barnett, using Fortran codes by Leslie Greengard, Zydrunas Gimbutas, Marina Spivak. 2015-2016. Repackaged 8/25/22.

The main task is to use CG to solve the normal equations for the least-squares problem of matching a global spherical harmonic expansion of given maximum degree P to given data at M scattered points on the sphere. This fitting stage costs O(M + P^3) per iteration. This expansion may then be evaluated on a regular (theta,phi) tensor-product grid on the sphere, or at arbitrary new points, currently at similar cost. There are also some helper and visualization routines. The tests are not formal unit tests, so the user has to parse the output. The drivers are in MATLAB/Octave, with a MEX library calling Fortran90 routines.

Installation

Edit makefile for your system, then make. If you have trouble you may want to see advice at mwrapdemo. Then from MATLAB run spharmproj_test to test; errors should be around 1e-15 and a figure should pop up. You may also run the below demo, or most of the function files since they perform a self-test when called without arguments.

Example of use

spharm-interp demo image

Here is a demo of fitting a scalar function (a complex plane wave) to noisy values at iid uniform random points on the sphere, in MATLAB/Octave:

kvec = [9;2;6]; f = @(x) exp(1i*x*kvec);    % test function on R^3
M = 1e5;                                    % how many data pts on S^2
z = rand(M,1)*2-1; phi = 2*pi*rand(M,1);    % iid rand pts on S^2
rho = sqrt(1-z.^2);
x = [rho.*cos(phi), rho.*sin(phi), z];      % M*3 coords of points in R^3
ynoiseless = f(x);
sigma = 0.1;                                % additive noise level
y = ynoiseless + sigma*randn(M,1);          % make noisy data
P = 20;                                     % S.H. expansion max degree
coeffs = lsqsolvespharm(y, z, phi, P);      % fit the expansion
fSH = spharmeval(coeffs, z, phi);           % evaluate expansion at the points
rmsr = norm(fSH-y)/sqrt(M)                  % RMS residual vs data
rmse = norm(fSH-ynoiseless)/sqrt(M)         % RMS error vs underlying function

On a laptop this takes about 0.15 sec to fit, and 1.3 sec to evaluate the expansion. In our run the RMS residual was 0.0998 (which should be close to sigma = 0.1), and the RMS error in recovering the underlying function was 0.00663 (which is 15x smaller, and should be close to sigma/sqrt(M/P^2) = 0.00632, since P^2 is the approximate number of degrees of freedom in the function).

For the full code with error reporting and plots see https://github.com/ahbarnett/spharm-interp/blob/main/demo.m

โš ๏ธ The above took a very small 6 CG iterations to solve the linear system to 6 digits. However, if your data points do not well cover the sphere (at least down to the 1/P length scale), the system will probably be much poorer conditioned and may take a large number of iterations (this may manifest itself as a a "hang").

Main routines available from MATLAB:

lsqsolvespharm : iterative LSQ solve of sph harm coeffs to match data at arbitrary scattered points on sphere
spharmeval : evaluate spherical harmonic expansion at arbitrary sphere points (not yet performance code)
spharmgrideval : evaluate sph harm expansion on grid (pure MATLAB version)
spharmgridevalf : evaluate sph harm expansion on grid (MEX interface version)
spharmproj : project grid data on the sphere onto spherical harmonic coeffs (MEX interface)
spharmprojfunc : same as spharmproj but acts on function handle
showsphgrid : color plot of a real function on a spherical grid
showspharmexp : color plot of real part of a spherical harmonic expansion
demo : example as above
testall : run all tests

Info about some other files:

spharm.mw : MWrap file used to generate gateway.c MEX interface
gateway.c : MEX interface
*.f : Fortran sources

To do

  • provide non-MEX option for spharmproj
  • wrap the sheval3d Fortran evaluator, test its speed
  • convert to double-Fourier expansion and use FINUFFT for faster evaluation at arbitrary targets
  • use the above idea to replace the sparse matvec and compare speed
  • exploit Toeplitz structure in phi variable for the normal equations

spharm-interp's People

Contributors

ahbarnett avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

spharm-interp's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.