%% PAM #1
% Tomas Krejci, 11.4.2015

clc 
close all
clear all
      
%data = [1 1 0 1 0 0 0 1];
data = [0 1 2 3 0 3 2 1];

%% PAM - jedno rozmerna
% modulaèní funkce h jsou nasobky modulaèniho pulzu, napø obdelnikove pulzy, h0 = 1V, h1=2V,... 
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;
kMax = length(data); % kolik je symbolovych intervalu (pro jedno rozmernou odpovida datovych intervalu)

c_m = [-2 -1 1 2]; % nasobici koeficienty pro modulacni fci
M = length(c_m); 
c_m_k = zeros(1, kMax);
for kk = 1:kMax
   
    switch (data(kk))
        case 0
            c_m_k(kk) = c_m(2);
        case 1
            c_m_k(kk) = c_m(3);
        case 2
            c_m_k(kk) = c_m(1);
        case 3
            c_m_k(kk) = c_m(4);
        otherwise
           c_m_k(kk) = 0;
      end    
end

%% REC
h_xy = ones(1, nn);
%% BiP
%h_xy = [ones(1, nn/2) -ones(1, nn/2)];


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

for kk = 1:kMax
    s_t( (kk-1)*nn+1 : kk*nn ) = c_m_k(kk) * h_xy(1,:);  
end

t_osa = 0 : 1/fs : kMax*Ts-1/fs;
c_osa = 0 : nn/fs : kMax*Ts-1/fs;

h_figure = figure; set(h_figure(:),'color',[1 1 1]);
subplot(211); hold all; 
    plot(t_osa, s_t, 'Color', 'red', 'LineWidth', 2); 
    stem(c_osa, c_m_k, 'Color', 'blue', 'LineStyle', '--', 'Marker', '>', '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)', 'c_m_(_k_)','Location','NorthEastOutside');
subplot(212); hold all; 
    stem([0:1: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');




