%% save channel T

%% 固定信道 测量， 只改变snr
% clear all;
% M_h = 8; 
% M_v = 8;
% M = M_h*M_v;
% Nt = 16;% 32; 
% grid_rh = 32;
% grid_rv = 32;
% grid_r = grid_rh*grid_rv;
% grid_t = 64;% 32;
% Nr = M; 

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_Chan_grid_3; %% 一样的
% 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) = exp(1j*2*pi*rand(M,1));
%     W(:,t) = ( 1/sqrt(Nt))*exp(1j*2*pi*rand(Nt,1));
%     W(:,t) = sqrt(1/2)*(randn(Nt,1)+1j*randn(Nt,1));
%     W(:,t) = W(:,t)/norm(W(:,t));
%     y0(t) = V(:,t)'*H_cas*W(:,t);
% end
% VW = tKR(W.',V');

% VW_T = dftmtx(Nt*M);
% VW = VW_T(1:T,:);
% y0 = VW*vecH_0;

ARR = tKR(conj(AR),AR);
Arr = ARR(:,[1:grid_r]);
Atrr = kron(conj(AT),Arr);
% Phi = VW*Atrr;  %% measurement matrix *******************

%% least perfect

ARR_ls = tKR(conj(AT_R_irs),AR_G_irs);
ATRR_ls = kron(conj(AT_G_bs),ARR_ls);
alpa_Sig = kron(conj(gain_R_path),diag(gain_G_path));
al_Sig_vec = alpa_Sig(:);
% VAtrr = VW*ATRR_ls;

% new
% ind=find(abs(al_Sig_vec)>0);
% al_new = al_Sig_vec(ind);
% % VAtrr_new = VAtrr(:,ind);
% ATRR_ls_new = ATRR_ls(:,ind);
% VAtrr_new = VW*ATRR_ls_new;
% vecH_psur = ATRR_ls*al_Sig_vec;



% norm(VW*ATRR_ls_new*al_new-VAtrr*al_Sig_vec)
%  norm(VAtrr_new*al_new-VAtrr*al_Sig_vec)
 % cun VAtrr_
% VW_T = dftmtx(Nt*M);
% [at,bt] = size(ATRR_ls);
% VW = VW_T(:,1:T)';



%% LS
% TtM = t;
% V_bi = zeros(M,TtM);
% W_bi = zeros(Nt,TtM);
% 
% for t = 1:TtM
%     V_bi(:,t) = exp(1j*2*pi*rand(M,1));%( 1/sqrt(M))*exp(1j*2*pi*rand(M,1));
%     W_bi(:,t) = sqrt(1/2)*(randn(Nt,1)+1j*randn(Nt,1));%( 1/sqrt(Nt))*exp(1j*2*pi*rand(Nt,1));
%      W_bi(:,t) =  W_bi(:,t)/norm( W_bi(:,t));
% end
% VW_bi = tKR(W_bi.',V_bi');
%% normalize
% mag_VW_bi = vecnorm(VW_bi,2);
% VW_bi_n = VW_bi*diag(1./mag_VW_bi);
% DT = dftmtx(TtM);
% VW_bi = DT(:,1:Nt*M);


