function [W,record2] = GSwb_design_withCIN(f,NumAntenna,range,grid_width,codebook_o,WBT_times,sample,num_rf,num_qua)
% widebeam design with codebook as input
codebook = codebook_o.';%原码本
% range x坐标范围
% grid_width 原码本的格点大小
% WBT_times 码本拓宽倍数
c= 3e8;
lambda = c/f;
% NumAntenna=128; %基站天线数
% BSAntennas_Index=0:1:NumAntenna-1;
Rayleigh_dis = 2*(NumAntenna*lambda/2)^2/lambda;
% range = 200; %ceil(Rayleigh_dis/100)*100;  
% X_BS=15; %x划分的格点数
% Y_BS=30; 
i = 0;

AbG = codebook;
% for i=1:1:G_BS
%     AbG(:,i)=exp(1j*(pi)*BSAntennas_Index*(-1+(i*2-1)/G_BS)); %参考式（3），Ω在[-1,1]间
% end
A = AbG; %导引矩阵A，和AbG是一回事
B = A'*pinv(A*A')*A;
R = [real(B),-imag(B);imag(B),real(B)]; %式（24）

P1 = [0,range,-range,range];
Y_BS = -range:grid_width:range;
X_BS = 0:grid_width:range;
L_Y_BS = length(Y_BS);
L_X_BS = length(X_BS);
%宽波束码本的块划分
Y_BS_W = -range:WBT_times*grid_width:range;
X_BS_W = 0:WBT_times*grid_width:range;

%%宽波束码本的大小
if mod(length(X_BS),WBT_times)==0 && mod(length(Y_BS),WBT_times)==0
    size_W_codebook = length(X_BS_W)*length(Y_BS_W)-1; 
elseif mod(length(X_BS),WBT_times)==0 && mod(length(Y_BS),WBT_times)~=0
    size_W_codebook = length(X_BS_W)*(length(Y_BS_W)-1)-1;
elseif mod(length(X_BS),WBT_times)~=0 && mod(length(Y_BS),WBT_times)==0
    size_W_codebook = (length(X_BS_W)-1)*length(Y_BS_W)-1;    
elseif mod(length(X_BS),WBT_times)~=0 && mod(length(Y_BS),WBT_times)~=0
    size_W_codebook = (length(X_BS_W)-1)*(length(Y_BS_W)-1)-1;
end

W = zeros(size_W_codebook,NumAntenna);
record2 = zeros(size_W_codebook,4);
for i_sam = 1:size_W_codebook
    if i_sam == sample
    i_sam 
    
    %%%%给出理想beam pattern
    g02 = zeros(L_X_BS*L_Y_BS,1);
%     i_beam = Beam_Ind(i_sam); %%%取对应波束
    
    i_col = floor(i_sam/length(Y_BS_W))+1;
    i_row = mod(i_sam,length(Y_BS_W));
    if i_row == 0
        i_row = length(Y_BS_W);
    end

        Y_i = [i_row*WBT_times-1:1:i_row*WBT_times];
        X_i = [i_col*WBT_times-1:1:i_col*WBT_times];
        for q = 1:WBT_times
            for p = 1:WBT_times
%                  aaa=(X_i(q)-1)*L_Y_BS+Y_i(p)
                g02((X_i(q)-1)*L_Y_BS+Y_i(p)) = 1;
            end
        end

    %%%% 记录宽波束范围
%     bbb = [((i_col)-1)*grid_width, i_col*grid_width,...
%         ((i_row)-1)*grid_width-range, i_row*grid_width-range]
    record2(i_sam,:)=[((i_col)-1)*grid_width, i_col*grid_width,...
        ((i_row)-1)*grid_width-range, i_row*grid_width-range];

    g = g02;
    
    g_temp = g;
    gModulus = abs(g); %幅度
    G_BS = L_X_BS*L_Y_BS;
    K=G_BS; %导引矢量的个数
    
    for iter = 1:20      
        v_temp=pinv(AbG*AbG')*AbG*g_temp;
        g_temp = gModulus.*exp(1j*phase(AbG'*v_temp));
    end
    

    %% 获得理想码字
    w=pinv(AbG*AbG')*AbG*g_temp/norm(pinv(AbG*AbG')*AbG*g_temp,'fro'); 
    W(i_sam,:) = w;

    figure()
    zz_0 = reshape(g02,[length(Y_BS),length(X_BS)]);
    zz_0 = zz_0/max(max(zz_0));
    imagesc(X_BS,Y_BS,zz_0)

    colorbar
    set(gca,'YDir','normal')

    xlabel('X (meters)')
    ylabel('Y (meters)')
    % box on;

    
%     %%%%理想码字绘图
%     figure()
%     Z_1 = abs(AbG'*w);
%     zz_1 = reshape(Z_1,[length(Y_BS),length(X_BS)]);
%     zz_1= zz_1/max(max(zz_1));
%     imagesc(X_BS,Y_BS,zz_1)
%     colorbar
%     set(gca,'YDir','normal')
%     xlabel('X (meters)')
%     ylabel('Y (meters)')
    


% 获得实际码字

    [wp,~]=AltMinFS(w,num_rf,num_qua); %RF数量大于等于3
    figure();

    Z_2 = abs(AbG'*wp);
    zz_2 = reshape(Z_2,[length(Y_BS),length(X_BS)]);
    zz_2 = zz_2/max(max(zz_2));
    imagesc(X_BS,Y_BS,zz_2)
    
    colorbar
    % axis off
     set(gca,'YDir','normal')

    xlabel('X (meters)')
    ylabel('Y (meters)')
   end
end
end



