Gaussian fit on a sphere - matlab

I have an array of spatial data [lat,lon,intensity] on the Earth surface. Plotting the data with surf(lon,lat,intensity) shows the surface is a Gaussian shaped. I want to fit a 2D Gaussian function to the data to get the center and spread (mean and variance) of the data.
It's easy to fit a bivariate Gaussian function for data as [x,y,intensity]. But my data is sampled on the sphere. Latitude and longitude cannot be treated as for x and y in cartesian coordinates.

Related

Convert cross section data to volumetric data

I have an ultrasound function, where for a distance y on a material, it gives cross sectional b-scan which is ultrasound amplitudes (f) for each X coordinate and depth of the material (Z)
[X,Z,f] = function(y)
I need to turn this data into volumetric form for use with contour slice, but I'm not sure how to. I want to plot contourslice at each Y using X,Y,Z,f

I need to fit a best circle to the 3D data in matlab

Basically, I have a many irregular circle on the ground in the form of x,y,z coordinates (of 200*3 matrix). but I want to fix a best circle in to the data of x,y,z coordinates (of 200*3 matrix).
Any help will be greatly appreciated.
I would try using the RANSAC algorithm which finds the parameters of your model (in your case a circle) given noisy data. The algorithm is quite easy to understand and robust against outliers.
The wikipedia article has a Matlab example for fitting a line but it shouldn't be too hard to adapt it to fit a circle.
These slides give a good introduction to the RANSAC algorithm (starting from page 42). They even show examples for fitting a circle.
Though this answer is late, I hope this helps others
To fit a circle to 3d points
Find the centroid of the 3d points (nx3 matrix)
Subtract the centroid from the 3D points.
Using RANSAC, fit a plane to the 3D points. You can refer here for the function to fit plane using RANSAC
Apply SVD to the 3d points (nx3 matrix) and get the v matrix
Generate the axes along the RANSAC plane using the axes from SVD. For example, if the plane norm is along the z-direction, then cross product between the 1st column of v matrix and the plane norm will generate the vector along the y-direction, then the cross product between the generated y-vector and plane norm will generate a vector along the x-direction. Using the generated vectors, form a Rotation matrix [x_vector y_vector z_vector]
Multiply the Rotation matrix with the centroid subtracted 3d points so that the points will be parallel to the XY plane.
Project the points to XY plane by simply removing the Z-axes from the 3d points
fit a circle using Least squares circle fit
Rotate the center of the circle using the inverse of the rotation matrix obtained from step 5
Translate back the center to the original location using the centroid
The circle in 3D will have the center, the radius will be the same as the 2D circle we obtained from step 8, the circle plane will be the RANSAC plane we obtained from step 3

MATLAB - Only plot a spherical section of data set which is cubic

I have a physics model that simulates a few things in a radius of 5000 km around an object in spherical coordinates. I found no way to interpolate spherical coordinates in MATLAB so I changed them to Cartesian with sph2cart function. Then I used scatteredinterpolant function with a 10000 by 10000 by 10000 km meshgrid to interpolate the data. I want to plot this data but only the sphere of a radius 5000 km. Is there a nice way to plot a certain specified volume of the data?
If you just want to delete part of your data "spherically" you can do
being x,y,z your geometric data and v the values:
level=5000; %km (or whatever value it is in your data)
V(sqrt(X.^2+Y.^+Z.^2)>level)=0;
%or depending in the plotting functions.
V(sqrt(X.^2+Y.^+Z.^2)>level)=NaN;
% plot things

How to plot polar with data in matlab like this picture

I have data representing frequency and decibels, and I want to make a polar like this picture:
What is the command for this?
On MathWorks:
The polar function accepts polar coordinates, plots them in a Cartesian plane, and draws the polar grid on the plane.
polar(theta,rho) creates a polar coordinate plot of the angle theta versus the radius rho. theta is the angle from the x-axis to the radius vector specified in radians; rho is the length of the radius vector specified in dataspace units.
You should be able to get your result with polar(decibels, frequency).

Plotting a surface from a set of interior 3D scatter points in MATLAB

I have a large (~60,000) set of triplet data points representing x,y, and z coordinates, which are scattered throughout a Cartesian volume.
I'm looking for a way to use Matlab to visualize the non-convex shape/volume described by the maximum extent of the points.
I can of course visualize the individual points using scatter3, but given the large number of points the details of the shape are obscured by the noise of the dots.
As an analogy, imagine that you filled a hour glass with spheres of random sizes such as BBs, ping pong balls, and kix and then were given the coordinates of the center of each of each object. How would you take those coordinates and visualize the shape of the hour glass containing them?
My example uses different sized objects because the spacing between data points is non-uniform and effectively random; it uses an hourglass because the shape is non-convex.
If your surface enclosing the points can be described as a convex polyhedron (i.e. like the surface of a cube or a dodecahedron, without concave pits or jagged pointy parts), then I would start by creating a 3-D Delaunay triangulation of the points. This will fill the volume around the points with a series of tetrahedral elements with the points as their vertices, and you can then find the set of triangular faces that form the outer shell of the volume using the convexHull method of the DelaunayTri class.
Here's an example that generates 200 random points uniformly distributed within the unit cube, creates a tetrahedral mesh for these points, then finds the 3-D convex hull for the volume:
interiorPoints = rand(200,3); %# Generate 200 3-D points
DT = DelaunayTri(interiorPoints); %# Create the tetrahedral mesh
hullFacets = convexHull(DT); %# Find the facets of the convex hull
%# Plot the scattered points:
subplot(2,2,1);
scatter3(interiorPoints(:,1),interiorPoints(:,2),interiorPoints(:,3),'.');
axis equal;
title('Interior points');
%# Plot the tetrahedral mesh:
subplot(2,2,2);
tetramesh(DT);
axis equal;
title('Tetrahedral mesh');
%# Plot the 3-D convex hull:
subplot(2,2,3);
trisurf(hullFacets,DT.X(:,1),DT.X(:,2),DT.X(:,3),'FaceColor','c')
axis equal;
title('Convex hull');
You could treat your data as a sample from a three-dimensional probability density, and estimate that density on a grid, e.g. via a 3d histogram, or better a 3d kernel density estimator. Then apply a threshold and extract the surface using isosurface.
Unfortunately, hist3 included in the Statistics Toolbox is (despite its name) just a 2d histogram, and ksdensity works only with 1d data, so you would have to implement 3d versions yourself.