%% Read Data from file
%scope_file = 'CAP2D_mod4_Transmitter4.txt';
%y_Tx_mod = textread(['C:\Users\acer\Desktop\Access Network\' scope_file],'%f'); 

%% system parameters
Fs = 40e9; %Sampling frequency (this one can be reduce to 20e9 or 10e9)
Ts = 1/Fs;
Fawg = 1.25e9;  % Sampling frquency of AWG
Nss = Fs/Fawg;
Nos =4;
F_RF = 5e9; % RF frequency

%% upsample
y_Tx_mod_v1 = upsample(y_Tx_mod,Nss);

%% channel
%y_Tx_mod_v1= filter(LPF,y_Tx_mod_v1);
y_Rx_mod = awgn(y_Tx_mod_v1,50,'measured');%add awgn noise

%% Receiver
y_Rx_mod = resample(y_Rx_mod,Fawg,Fs);% if you are using Fs=Fawg you don't need resample function

%% Demodulation of CAP 
y_Rx1 = conv(y_Rx_mod,fliplr(s1_2D_up5)); 
y_Rx2 = conv(y_Rx_mod,fliplr(s2_2D_up5));

% decimate
y_Rx1 = decimate3(y_Rx1,Nos);
y_Rx2 = decimate3(y_Rx2,Nos);

y_Rx1 = y_Rx1(30:end-30);%cut at the beginning and at the end of frame if not you will see doted at the middle
y_Rx2 = y_Rx2(30:end-30);

%figure;
%plot(y_Rx1,y_Rx2,'.k');

%% DFE channel estimation
mu_i = 1e-5;
mu_dfe_i = 1e-3;
Nfil_i = 9;
L_dfe_i = 11;
C = 1;

[decision_i,y_i] = FFE_DFE_equalizer_v2(y_Rx1,mu_i,mu_dfe_i,Nfil_i,L_dfe_i,C);
[decision_q,y_q] = FFE_DFE_equalizer_v2(y_Rx2,mu_i,mu_dfe_i,Nfil_i,L_dfe_i,C);

y_Rx_1 = y_i;
y_Rx_2 = y_q;

% figure;
% plot(y_Rx_1,y_Rx_2,'.r');

% figure;
% plot3(y_Rx_1,y_Rx_2,y_Rx_3,'.r');grid on;

figure;
plot(y_Rx1,y_Rx2,'.r');%before channel estimation
%figure;plot(y_Rx1,'.r');
%figure;plot(y_Rx2,'.r');
figure;
plot(y_Rx_1,y_Rx_2,'.b');%after channel estimation
%figure;plot(y_Rx_1,'.b');

%% BER counter
%decision
y_Rx = y_Rx1; %before channel estimation

%For 4QAM
 for i = 1:length(y_Rx)
     if y_Rx(i)<=0 
         y_Rx(i) = 0;
     elseif y_Rx(i)>0
         y_Rx(i) = 1;
     end
 end

% For 16QAM
%for i = 1:length(y_Rx)
%    if y_Rx(i)<-0.032&& y_Rx(i)>=-0.07
%        y_Rx(i) = 0;
%    elseif y_Rx(i)<0 && y_Rx(i)>=-0.032
%        y_Rx(i) = 1;
%    elseif y_Rx(i)<0.031 && y_Rx(i)>=0
%        y_Rx(i) = 2;  
%    elseif y_Rx(i)<0.07 && y_Rx(i)>=0.031
%        y_Rx(i) = 3;
%    else
%        y_Rx(i) = 0;
%    end
%end

% %For 64QAM
% for i = 1:length(y_Rx)
%     if y_Rx(i)<-3 && y_Rx(i)>=-4
%         y_Rx(i) = 0;
%     elseif y_Rx(i)<-2 && y_Rx(i)>=-3
%         y_Rx(i) = 1;
%     elseif y_Rx(i)<-1 && y_Rx(i)>=-2
%         y_Rx(i) = 2;  
%     elseif y_Rx(i)<0 && y_Rx(i)>=-1
%         y_Rx(i) = 3;
%     elseif y_Rx(i)<1 && y_Rx(i)>=0
%         y_Rx(i) = 4;
%     elseif y_Rx(i)<2 && y_Rx(i)>=1
%         y_Rx(i) = 5;
%     elseif y_Rx(i)<3 && y_Rx(i)>=2
%         y_Rx(i) = 6;
%     elseif y_Rx(i)<4 && y_Rx(i)>=3
%         y_Rx(i) = 7;
%     else
%         y_Rx(i) = 0;
%     end
% end
y_Rx = de2bi(y_Rx,'left-msb').';
rx_data =reshape(y_Rx,1,size(y_Rx,1).*size(y_Rx,2));
[Nerr,BER] = BER_counter_optimal(rx_data,15);
disp(sprintf('Error count: %d/%d, uncoded BER=%.2e, -log10(BER)=%0.3f',Nerr,length(rx_data),BER,-log10(BER)));



%% save file to .txt
file_prefix = 'CAP2D';
awg_file = [file_prefix '_demod' num2str(Nos) '_Receiver' num2str(M.^2)]; % Baseline name for Agilent AWG files

%if debug 
    switch 'tektronix'
        case 'agilent'
            agt_awg_savebin(['C:\Users\acer\Desktop\Access Network\' awg_file ],y_Tx_mod);
            
        case 'tektronix'
            save (['C:\Users\acer\Desktop\Access Network\' awg_file '.txt'], 'y_Rx_mod','-ASCII');
    end
%end