
%%finding best subcarrier and best frame from CSI dataset

% created by Hadi Hosseini 

clear all
close all
total_frames = 2000;
total_subcarriers = 52;
prompt1 = "Enter csi filename in quotation mark? ";
prompt2 = "Enter client number? ";
csi_dataset = input(prompt1);
load_data = load(csi_dataset);
user_x = input(prompt2);
for i=1:1:total_frames
    for j=1:1:total_subcarriers
    csi_user_x(:,i,j)=load_data.uplink(i,user_x,:,j); 
    end
end
csi_subcarrier_abs(:,1)=abs(mean(csi_user_x,[1 2]));
[mx_carrier, id_carrier]=max(csi_subcarrier_abs);
csi_frame_abs(:,1)=abs(mean(csi_user_x,[1 3]));
[mx_frame, id_frame]=max(csi_frame_abs);
d = ['best subcarrier for entered client is: ',num2str(id_carrier), '  best frame for entered client is: ',num2str(id_frame)];
disp(d);
best_csi_x = csi_user_x(:,id_frame, id_carrier);
disp('best csi for entered client is: best_csi_x');
csi_subcarrier_abs_all(:,1)=abs(mean(load_data.uplink,[1 2 3]));
[mx_carrier_all, id_carrier_all]=max(csi_subcarrier_abs_all);
csi_frame_abs_all(:,1)=abs(mean(load_data.uplink,[2 3 4]));
[mx_frame_all, id_frame_all]=max(csi_frame_abs_all);
best_csi_all = load_data.uplink(id_frame_all,:,:,id_carrier_all);
disp('best csi for all clients is: best_csi_all');
d = ['best subcarrier for all client is: ',num2str(id_carrier_all), '  best frame for all client is: ',num2str(id_frame_all)];
disp(d);
%save('Filename.mat','id_carrier','id_frame','id_frame_all','id_carrier_all','best_csi_x','best_csi_all');

%% interpolation for missing values and damaged antennas

function internal_ip2 = interpolate(user_csi_internalfl);
for i = 1:100
    for j = 1:52
        h(:,:) = user_csi_internalfl(i,:,:,j);
        for k = 1:2
            H(k,:) = h(k,:);
        end
        
        real3 = (abs(real(h(2,:)))+abs(real(h(3,:))))/2;
        imag3 = (abs(imag(h(2,:)))+abs(imag(h(3,:))))/2;
        H(3,:) = complex(real3,imag3);
        mag = abs(H(3,:));
        H(4,:) = complex(real3+(mag/10),imag3+(mag/10));
  
        for k = 5:6
            H(k,:) = h(k-2,:);
        end
        real7 = (abs(real(h(6,:)))+abs(real(h(7,:))))/2;
        imag7 = (abs(imag(h(6,:)))+abs(imag(h(7,:))))/2;
        H(7,:) = complex(real7,imag7);
        mag = abs(H(7,:));
        H(8,:) = complex(real7+(mag/10),imag7+(mag/10));
        
        for k = 9:10
            H(k,:) = h(k-4,:);
        end
        real11 = (abs(real(h(10,:)))+abs(real(h(11,:))))/2;
        imag11 = (imag(h(10,:))+imag(h(11,:)))/2;
        H(11,:) = complex(real11,imag11);
        mag = abs(H(11,:));
        H(12,:) = complex(real11+(mag/10),imag11+(mag/10));
        
        for k = 13:40
            H(k,:) = h(k-6,:);
        end
        real41 = (abs(real(h(40,:)))+abs(real(h(41,:))))/2;
        imag41 = (abs(imag(h(40,:)))+abs(imag(h(41,:))))/2;
        H(41,:) = complex(real41,imag41);
        mag = abs(H(41,:));
        H(42,:) = complex(real41+(mag/10),imag41+(mag/10));
        
        for k = 43:64
            H(k,:) = h(k-8,:);
        end
        internal_ip(i,:,:,j) = H;
    end
end
for i = 1:100
    for j = 1:52
        h2(:,:) = internal_ip(i,:,:,j);
        for k = 1:2
            H2(:,k) = h2(:,k);
        end
        
        real3 = (abs(real(h2(:,2)))+abs(real(h2(:,3))))/2;
        imag3 = (abs(imag(h2(:,3)))+abs(imag(h2(:,3))))/2;
        H2(:,3) = complex(real3,imag3);
        mag = abs(H2(:,3));
        H2(:,4) = complex(real3+(mag/10),imag3+(mag/10));
  
        for k = 5:6
            H2(:,k) = h2(:,k-2);
        end
        real7 = (abs(real(h2(:,6)))+abs(real(h2(:,7))))/2;
        imag7 = (abs(imag(h2(:,6)))+abs(imag(h2(:,7))))/2;
        H2(:,7) = complex(real7,imag7);
        mag = abs(H2(:,7));
        H2(:,8) = complex(real7+(mag/10),imag7+(mag/10));
        
        for k = 9:10
            H2(:,k) = h2(:,k-4);
        end
        real11 = (abs(real(h2(:,10)))+abs(real(h2(:,11))))/2;
        imag11 = (abs(imag(h2(:,10)))+abs(imag(h2(:,11))))/2;
        H2(:,11) = complex(real11,imag11);
        mag = abs(H2(:,11));
        H2(:,12) = complex(real11+(mag/10),imag11+(mag/10));
        
        for k = 13:40
            H2(:,k) = h2(:,k-6);
        end
        real41 = (abs(real(h2(:,40)))+abs(real(h2(:,41))))/2;
        imag41 = (abs(imag(h2(:,40)))+abs(imag(h2(:,41))))/2;
        H2(:,41) = complex(real41,imag41);
        mag = abs(H2(:,41));
        H2(:,42) = complex(real41+(mag/10),imag41+(mag/10));
        
        for k = 43:64
            H2(:,k) = h2(:,k-8);
        end
        H2(3:4,3:4) = 0;
        H2(7:8,7:8) = 0;
        H2(11:12,11:12) = 0;
        H2(41:42,41:42) = 0;
        internal_ip2(i,:,:,j) = H2;
    end
end
end
