%% PAM #2
% Tomas Krejci, 11.4.2015

clc 
close all
clear all

data = [0 1 0 0 0 0 0 1 ... % A
            0 1 1 0 1 0 0 0 ... % h
            0 1 1 0 1 1 1 1 ... % o
            0 1 1 0 1 0 1 0];   %j


%% PAM - vice rozmerna

fs = 1E6; % vzorkovaci frek, pro "udelani spojite s(t)", v prednskach jako fh=1/Tv
Ts = 1E-3; % symbolovy interval, (jak dlouho potrva jeden datovy syombol (1 nebo 0 z data) po modulaci jako s(t) ?)
nn = fs*Ts;

N = 4; % kolik rozmer mod.
Nb = log2(N); % za prepokladu dat ve dvojkove soustave, pocet bitu predstavuji jednu modulacni fci h neboli stav q
kMax = length(data)/Nb; % kolik je symbolovych intervalu (pro jedno rozmernou odpovida datovych intervalu (poctu dat))
M = 2; % uvazujeme dvou svavovou modulaci 


%% REC
h_m = [ones(1, Nb*nn); 
           [ones(1, nn) -ones(1, nn)]; 
           [ones(1, nn/2) -ones(1, nn) ones(1, nn/2)];
           [ones(1, nn/2) -ones(1, nn/2) ones(1, nn/2)  -ones(1, nn/2)] ];
q = zeros(1,kMax);
jj =1;
for ii = 1:Nb: length(data)-1
    
    if data(ii)==0 && data(ii+1)==0
        q(jj) = 1;
    end
    if data(ii)==0 && data(ii+1)==1
        q(jj) = 2;
    end            
    if data(ii)==1 && data(ii+1)==0
        q(jj) = 3;
    end  
    if data(ii)==1 && data(ii+1)==1
        q(jj) = 4;
    end 
    jj = jj+1;
    
end


%% PAM expanzni cast
s_t = zeros(1, nn * length(data));

for kk = 1:kMax
    s_t( (kk-1)*nn*Nb+1 : kk*nn*Nb ) =  h_m(q(kk),:);  
end

t_osa = 0 : 1/fs : kMax*Ts*Nb-1/fs;
c_osa = 0 : Nb*nn/fs : kMax*Ts*Nb-1/fs;
q_osa = 0 : Nb*nn/fs : kMax*Ts*Nb-1/fs;
h_figure = figure; set(h_figure(:),'color',[1 1 1]);
subplot(211); hold all; 
   % [hAx,hLine1,hLine2] = plotyy(t_osa,s_t,c_osa,q,'plot','stem');
     plot(t_osa, s_t, 'Color', 'red', 'LineWidth', 2);   
      stem(c_osa, q/4, 'Color', 'blue', 'LineStyle', '--', 'Marker', '>', 'LineWidth', 2);
       % hLine2.LineStyle = '--'; hLine2.Marker = '>'; hLine2.LineWidth = 2;
        ylim([min(s_t)*1.2 max(s_t)*1.2 ]); title(['PAM  M=' num2str(M)]);
        xlabel('Time [s]'); ylabel('A [V]'); legend('PAM s(t)', 'q_m_k','Location','NorthEastOutside');
subplot(212); hold all; 
    stem([0:1:Nb*kMax-1], data, 'Color', 'Black', 'LineStyle', '-', 'Marker', 'x', 'LineWidth', 2);
        ylim([min(data)*1.2 max(data)*1.2 ]); title('data');
        xlabel('Time [n]'); ylabel('[-]'); legend('    data', 'Location','NorthEastOutside');




