function [codebook,record] = generate_near_field_codebook(N,d,P1,Delta,lambda)

Xmax1=P1(2); Xmin1=P1(1); Ymax1=P1(4); Ymin1=P1(3);% Zmax1=P1(5); Zmin1=P1(6);

Xdelta1=Delta(1);Ydelta1=Delta(2); %Zdelta1=Delta(3);

% Xgrid1 = linspace(Xmin1,Xmax1,Xnum1); Ygrid1 = linspace(Ymin1,Ymax1,Ynum1); Zgrid1 = linspace(Zmin1,Zmax1,Znum1);
% Xgrid2 = linspace(Xmin2,Xmax2,Xnum2); Ygrid2 = linspace(Ymin2,Ymax2,Ynum2); Zgrid2 = linspace(Zmin2,Zmax2,Znum2);

Xgrid1=[Xmin1:Xdelta1:Xmax1]; Ygrid1=[Ymin1:Ydelta1:Ymax1];% Zgrid1=[Zmin1:Zdelta1:Zmax1]; 


Xnum1=length(Xgrid1); Ynum1=length(Ygrid1); %Znum1=length(Zgrid1);

record=zeros(Xnum1*Ynum1,2);
codebook = zeros(Xnum1*Ynum1,N);
i=1;
for x1=Xgrid1
    for y1=Ygrid1
        a = zeros(N,1);
        for n = 1:N
            D = sqrt(x1^2+(y1-d*(n-(N+1)/2))^2);
            a(n) = exp(1j*2*pi*D/lambda);
 %           a(n) = exp(1j*2*pi*D/(0.5*d));
%             a(n) = exp(1j*2*pi*(sqrt((x1-(n1-1-(N1-1)/2)*d)^2+(z1-(n2-1-(N2-1)/2)*d)^2+y1^2)+sqrt((x2-(n1-1-(N1-1)/2)*d)^2+(z2-(n2-1-(N2-1)/2)*d)^2+y2^2)));
        end
         a=a/sqrt(N);
%         a = norm(a,'fro');
        codebook(i,:)=a;
        record(i,:)=[x1,y1];
        i=i+1;
    end
end

% [codebook,index]=unique(codebook,'row');
% record=record(index,:);
end

