clc 
close all
clear all

Fs = 100E6;
Ts = 10E-6; % delka symbol itervalu
Nbit = 15;
d = round(rand(1,Nbit)); % skoro jako q, data do modulace

%tavova cast
q = 2.*d - 1;   % bipolar -1, 1

%expanzni cast
Sout = zeros(1,Nbit*length(d));
for ii=1:Nbit
    Sout( ((ii-1)*Nbit+1) : (ii*Nbit) ) = q(ii);
    
end
t = 0:Ts:length(Sout)*Ts-Ts;
plot(t,Sout); ylim([-1.2 1.2]);


%% vice stavova

Nbit = 16;
h0 = zeros(1,20); h0(1:end) = 1;
a = 1; b = 5;
%d = floor( a + (b-a).*rand(1,Nbit)); % skoro jako q, data do modulace
d = round(rand(1,Nbit));
q = zeros(1,Nbit/2);
jj = 1;
for ii = 1:2:length(d)
    if d(ii)==0 && d(ii+1)==0
        q(jj) = 1;
    end
    if d(ii)==0 && d(ii+1)==1
        q(jj) = 2;
    end            
    if d(ii)==1 && d(ii+1)==0
        q(jj) = 3;
    end  
    if d(ii)==1 && d(ii+1)==1
        q(jj) = 4;
    end 
    jj = jj+1;
end


%
Sout = zeros(1,Nbit*length(d));
for ii = 1:length(q)
    
    if q(ii) == 1
        Sout( ((ii-1)*2*Ts*Fs+1) : (ii*2*Ts*Fs) ) = ones(1, 2*Ts*Fs);
    end
    
     if q(ii) == 2
        Sout( ((ii-1)*2*Ts*Fs+1) : (ii*2*Ts*Fs) ) = [ones(1, Ts*Fs) zeros(1, Ts*Fs)];
     end   

     if q(ii) == 3
        Sout( ((ii-1)*2*Ts*Fs+1) : (ii*2*Ts*Fs) ) = [ones(1, Ts*Fs/2) zeros(1, Ts*Fs) ones(1 ,Ts*Fs/2)];
    end   
    
     if q(ii) == 4
        Sout( ((ii-1)*2*Ts*Fs+1) : (ii*2*Ts*Fs) ) = [ones(1, Ts*Fs/2) zeros(1, Ts*Fs/2) ones(1, Ts*Fs/2) zeros(1, Ts*Fs/2)];
    end       
    
end

figure; 
subplot(211); plot(Sout); ylim([-0.2 1.2]);
subplot(212); stem(q);   
       

Fs = 1E6;
tnos = 1:1/Fs:length(q)*2/Ts;
Snos = cos(2*pi*Fs*tnos);
figure; plot(tnos, Snos);

S_Sout = 10*log10((abs(fftshift(fft(Sout)))).^2);% energetické spektrum signálu 
fx = Fs/2 * linspace(-1,1,length(Sout)); 
figure; plot(fx,S_Sout);













