close all
clear all
frequency = 3e9;
lambda = 3e8/frequency;
antenna_spacing = [lambda/4,lambda/2,lambda*2/3,lambda];
x_number_total = floor(2*3*lambda./antenna_spacing); y_number_total = floor(2*3*lambda./antenna_spacing);z_number = 5;

for z_iter = 1:length(antenna_spacing)
    x_number = x_number_total(z_iter);
    y_number = y_number_total(z_iter);
axis = cell(x_number*y_number*z_number,1);
for i = 1:x_number
    for j = 1:y_number
        for k = 1:z_number
            axis{(i-1)*y_number*z_number+(j-1)*z_number+k} = [i*antenna_spacing(z_iter),j*antenna_spacing(z_iter),k*lambda/2];
        end
    end
end

[kernel_matrix] = kernel_calculate(frequency,axis,"ball");

eigen_value = eig(kernel_matrix);
eigen_value = flip(eigen_value);

    eigen_value = eigen_value/eigen_value(1);

x = 1:20:200*z_number;

if(z_iter==1)
    plot(x,eigen_value(1:20:200*z_number),'-s','color','red','Linewidth',1.5,'markersize',4)
elseif(z_iter==2)
    hold on
    plot(x(1:length(eigen_value(1:20:end))),eigen_value(1:20:end),'-o','color','m','Linewidth',1.5,'markersize',4)
elseif(z_iter==3)
    hold on
    plot(x(1:length(eigen_value(1:20:end))),eigen_value(1:20:end),'-+','color','blue','Linewidth',1.5,'markersize',4)
% elseif(z_iter==4)
%     hold on
%     plot(x(1:length(eigen_value(1:20:end))),eigen_value(1:20:end),'-^','color','blue','Linewidth',1.5,'markersize',4)
end
end

for z_iter = 1:length(antenna_spacing)
    x_number = x_number_total(z_iter);
    y_number = y_number_total(z_iter);
    [c,w] = GaussLegendre(0,2*3*lambda,x_number);
    % c = (0/x_number:1/x_number:1-1/x_number)*2*3*lambda;
    % w = ones(1,x_number);
axis = cell(x_number*y_number*z_number,1);
weight_vector = zeros(1,x_number*y_number*z_number);
for i = 1:x_number
    for j = 1:y_number
        for k = 1:z_number
            axis{(i-1)*y_number*z_number+(j-1)*z_number+k} = [c(i),c(j),k*lambda/2];
            weight_vector((i-1)*y_number*z_number+(j-1)*z_number+k) = w(i)*w(j);
        end
    end
end

%weight_vector = ones(1,x_number*y_number*z_number);

[kernel_matrix] = kernel_calculate(frequency,axis,"ball");

weight_matrix = repmat(weight_vector,x_number*y_number*z_number,1);
%kernel_matrix = kernel_matrix.*sqrt(weight_matrix).*sqrt(weight_matrix');
kernel_matrix = diag(sqrt(weight_vector))*kernel_matrix*diag(sqrt(weight_vector));
eigen_value = abs(eig(kernel_matrix));
eigen_value = sort(eigen_value,'descend');
eigen_value = eigen_value/eigen_value(1);

x = 1:20:200*z_number;

if(z_iter==1)
    plot(x,eigen_value(1:20:200*z_number),'--s','color','red','Linewidth',1.5,'markersize',4)
elseif(z_iter==2)
    hold on
    plot(x(1:length(eigen_value(1:20:end))),eigen_value(1:20:end),'--o','color','m','Linewidth',1.5,'markersize',4)
elseif(z_iter==3)
    hold on
    plot(x(1:length(eigen_value(1:20:end))),eigen_value(1:20:end),'--+','color','blue','Linewidth',1.5,'markersize',4)
% elseif(z_iter==4)
%     hold on
%     plot(x(1:length(eigen_value(1:20:end))),eigen_value(1:20:end),'--^','color','blue','Linewidth',1.5,'markersize',4)
end
end
grid on
xlabel('Eigenvalue Index','Interpreter','latex','FontSize',14);
ylabel('Eigenvalue','Interpreter','latex','FontSize',14);
legend('equal sampling $24$ points','equal sampling $12$ points','equal sampling $9$ points','Gauss-Legendre $24$ points','Gauss-Legendre $12$ points','Gauss-Legendre $9$ points','Interpreter','latex')