global M_h
global M_v
global Nt
global grid_rh
global grid_rv
global grid_t
global grid_r
global M
Nr = M



%% generate grid
alpha = zeros(grid_r,1);
Sigma = zeros(grid_r,grid_t);
% test AR/At 
Ar_y = @(x) exp(1j*2*pi*(0:M_h-1)'*x);
Ar_z = @(x) exp(1j*2*pi*(0:M_v-1)'*x);
At = @(x) exp(1j*2*pi*(0:Nt-1)'*x);

x_t = -1/2:1/grid_t:1/2-1/grid_t;

AT = At(x_t); % BS dictionary

x_rz = -1/2:1/grid_rh:1/2-1/grid_rh;
x_ry = -1/2:1/grid_rv:1/2-1/grid_rv;

AR_y = Ar_y(x_ry); % y -axis
AR_z = Ar_z(x_rz); %z-axis

AR = kron(AR_y,AR_z); % IRS dictionary


%% generate cascade channel
gen_Chan_grid;
% gen_Rician_cas; % info: H_cas AT_R_irs AoA_R AoA_G, AoD_G 
vecH_0 = H_cas(:); %%%%%%%%%%%%%%%%% true channel

%% to grid
pAR = pinv(AR);
Sigma = pAR*H_cas*pinv(AT');
alpha = pAR*hr;
vecH  = kron(conj(alpha),Sigma);
t0 = vecH(:); %%%%%%%%%%%%%%%%%%% sparse signal




%% generate y ,T training overhead
V = zeros(M,T);
W = zeros(Nt,T);
y0 = zeros(T,1);
for t = 1:T
    V(:,t) = ( 1/sqrt(M))*exp(1j*2*pi*rand(M,1));
    W(:,t) = ( 1/sqrt(Nt))*exp(1j*2*pi*rand(Nt,1));
    y0(t) = V(:,t)'*H_cas*W(:,t);
end
VW = tKR(W.',V');
ARR = tKR(conj(AR),AR);
Arr = ARR(:,[1:grid_r]);
Atrr = kron(conj(AT),Arr);
Phi = VW*Atrr;  %% measurement matrix *******************
%% generate noise
if noise
    var_no = norm(y0,'fro')^2/(T*10^(snr/10));
    noi = sqrt(var_no/2)*(randn(T,1)+1j*randn(T,1));
    y0 = y0+noi;   
else
    noi = zeros(T,1);
end

% Les_solver;
% nmse_ls_psur
% nmse_ls