
%------------------------------------------------
% degree variances fast
%-----------------------------------------------
% computes degree variances as a function of the
% S,C coefficients of a model
% CH 2012-03-22

function [degree_vec,sigma_squared] = degree_variances2012(n,m,C,S,maxdeg)

% Step 1: Square all coefficients
C = C.*C;
S = S.*S;

% Step 2: use degree and order as indices for a maxdeg x maxdeg matrix
JI=n+1;
MI=m+1;

matsize = max(JI);

C_mat=zeros(matsize,matsize);
S_mat=zeros(matsize,matsize);

% place the squared C and S values at the right places of a triagonal 
% matrix, where the sequence of n,m can be arbitrary
% C00
% C10 C11
% C20 C21 C22 etc
C_mat(sub2ind([matsize matsize],JI,MI))= C;
S_mat(sub2ind([matsize matsize],JI,MI))= S;

% Step 3 Sum up the squared degree variances
sigma_squared = sum(S_mat')+sum(C_mat');
degree_vec    =  0:max(JI)-1;

% Step 4 truncate spectrum after maxdef
ix = find(degree_vec>maxdeg);
sigma_squared(ix) = []; 
degree_vec(ix)    = [];

end