Skip to content

Octave version #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Global Spherical Harmonic package (GSH)

GSH is a MATLAB package to do **Global Spherical Harmonic Analyses** (GSHA) and **Synthesis** (GSHS) for Crust1.0.
GSH is a MATLAB package to do **Global Spherical Harmonic Analyses** (GSHA) and **Synthesis** (GSHS) for Crust1.0. This version supports running the scripts with Octave.


## Requirements
Expand Down
183 changes: 91 additions & 92 deletions Tools/GSHA.m
Original file line number Diff line number Diff line change
@@ -1,92 +1,91 @@
function cs = GSHA(f,lmax)

% GSHA global spherical harmonic analysis (wls)
%
% HOW cs = gsha(f,method,grid)
%
% IN f - global field of size N*2N
% lmax - maximum degree of development
% OUT cs - Clm & Slm in |C\S| format
%
%--------------------------------------------------------------------------
% uses Legendre_functions, SC2CS
%--------------------------------------------------------------------------

%--------------------------------------------------------------------------
% Grid definition
%--------------------------------------------------------------------------
[rows,cols] = size(f);

n = rows;
dt = 180 / n;
theta = (dt/2:dt:180)';
lam = (dt/2:dt:360); % dt = dlam

%--------------------------------------------------------------------------
% further diagnostics
%--------------------------------------------------------------------------
if lmax > n
error('Maximum degree of development is higher than number of rows of input.')
end

if lmax == n
error('max. degree 180 is not possible for block grid, problem for m = 0')
end
%--------------------------------------------------------------------------
% Init.
%--------------------------------------------------------------------------
% L = n;
L = lmax;
clm = zeros(L+1,L+1);
slm = zeros(L+1,L+1);

%--------------------------------------------------------------------------
% 1st step analysis: Am(theta) & Bm(theta)
%--------------------------------------------------------------------------
m = 0:L;
c = cos(lam'*m*pi/180);
s = sin(lam'*m*pi/180);

% preserving the orthogonality

c = c/rows;
s = s/rows;

c(:,1) = c(:,1)/2;
s(:,L+1) = s(:,L+1)/2; % not sure if this is needed? Need to take a look at the equations
%c(:,L+1) = zeros(2*n,1); % this line is not needed otherwise Clamx,lamx is not computed.
s(:,1) = zeros(2*n,1);

a = f * c;
b = f * s;

%--------------------------------------------------------------------------
% 2nd step analysis: Clm & Slm
%--------------------------------------------------------------------------

% predefine weights for the weighted least squares analysis
si = sin(theta*pi/180);
si = 2*si/sum(si);

% loop over the order of the Spherical Harmonics
for m = 0:L
% construct the legendre polynomials
p = Legendre_functions(m:L,m,theta);
% Select particular colum of the signal
ai = a(:,m+1);
bi = b(:,m+1);
d = 1:length(theta);
pts = p' * sparse(d,d,si);

% Estimate the coefficients
clm(m+1:L+1,m+1) = (pts * p) \ pts * ai;
slm(m+1:L+1,m+1) = (pts * p) \ pts * bi;
end

%--------------------------------------------------------------------------
% Write the coefficients Clm & Slm in |C\S| format
%--------------------------------------------------------------------------
slm = fliplr(slm);
%sc = [slm(:,1:L) clm];
cs = sc2cs([slm(:,1:L) clm]);
cs = cs(1:lmax+1,1:lmax+1);
function cs = GSHA(f,lmax)

% GSHA global spherical harmonic analysis (wls)
%
% HOW cs = gsha(f,method,grid)
%
% IN f - global field of size N*2N
% lmax - maximum degree of development
% OUT cs - Clm & Slm in |C\S| format
%
%--------------------------------------------------------------------------
% uses Legendre_functions, SC2CS
%--------------------------------------------------------------------------

%--------------------------------------------------------------------------
% Grid definition
%--------------------------------------------------------------------------
[rows,cols] = size(f);
n = rows;
dt = double(180 / n);
theta = double((dt/2:dt:180))';
lam = double((dt/2:dt:360)); % dt = dlam
%--------------------------------------------------------------------------
% further diagnostics
%--------------------------------------------------------------------------
if lmax > n
error('Maximum degree of development is higher than number of rows of input.')
end

if lmax == n
error('max. degree 180 is not possible for block grid, problem for m = 0')
end
%--------------------------------------------------------------------------
% Init.
%--------------------------------------------------------------------------
% L = n;
L = lmax;
clm = zeros(L+1,L+1);
slm = zeros(L+1,L+1);

%--------------------------------------------------------------------------
% 1st step analysis: Am(theta) & Bm(theta)
%--------------------------------------------------------------------------
m = double(0:L);

c = cos(lam'*m*pi/180);
s = sin(lam'*m*pi/180);

% preserving the orthogonality

c = c/rows;
s = s/rows;

c(:,1) = c(:,1)/2;
s(:,L+1) = s(:,L+1)/2; % not sure if this is needed? Need to take a look at the equations
%c(:,L+1) = zeros(2*n,1); % this line is not needed otherwise Clamx,lamx is not computed.
s(:,1) = zeros(2*n,1);

a = f * c;
b = f * s;

%--------------------------------------------------------------------------
% 2nd step analysis: Clm & Slm
%--------------------------------------------------------------------------

% predefine weights for the weighted least squares analysis
si = sin(theta*pi/180);
si = 2*si/sum(si);

%% loop over the order of the Spherical Harmonics
for m = double(0:L)
% construct the legendre polynomials
p = Legendre_functions(m:L,m,theta);
% Select particular colum of the signal
ai = a(:,m+1);
bi = b(:,m+1);
d = 1:length(theta);
pts = p' * sparse(d,d,si);

% Estimate the coefficients
clm(m+1:L+1,m+1) = (pts * p) \ (pts * ai);
slm(m+1:L+1,m+1) = (pts * p) \ (pts * bi);
end

%--------------------------------------------------------------------------
% Write the coefficients Clm & Slm in |C\S| format
%--------------------------------------------------------------------------
slm = fliplr(slm);
%sc = [slm(:,1:L) clm];
cs = sc2cs([slm(:,1:L) clm]);
cs = cs(1:lmax+1,1:lmax+1);
9 changes: 4 additions & 5 deletions Tools/GSHS.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@
end

% prepare coordinates
th = sort(th(:));
lam = lam(:).*pi/180;

th = double(sort(th(:)));
lam = double(lam(:).*pi/180);
% use the desired degree
if nargin < 5 || isempty(ldesired), ldesired = lmax; end
if ldesired < lmax
Expand All @@ -68,7 +67,7 @@
[row,~] = size(field);

% prepare cosine and sine --> cos(m*lam) and sin(m*lam)
m = 0:lmax;
m = double(0:lmax);
l = m';
mlam = (lam*m)';
cosmlam = cos(mlam);
Expand All @@ -81,7 +80,7 @@
%----------------------------------------------------------------------------
% CALCULATION
%----------------------------------------------------------------------------
for m = 0:row-1
for m = double(0:row-1)
if m==0
Cnm = field(:,row+m); % get column with order 0
Snm = zeros(row,1); % there are no Sn0 coefficients
Expand Down
Loading