%% QPSK
% Tomas Krejci, 11.4.2015

clc 
close all
clear all
        
data = [1 1 0 1 0 0 0 1 1 1];

fs = 100E6;  
Ts = 10E-6; % interval symbolu
Nb = 2; % Q=4 -> 4=2^2
n_data = length(data);
Tr = n_data/Nb * Ts; % delka celeho signalu
nn = fs*Ts; % pocet vzorku v symbolovem intervalu
t = 0 : 1/fs :  Tr - 1/fs; 

y_c = zeros(1, nn * n_data/Nb);
y_s = zeros(1, nn * n_data/Nb);
jj = 1;
for ii = 1 : Nb : n_data-1    
    if data(ii)==0 && data(ii+1)==0
        y_c( (jj-1)*nn+1 : jj*nn ) = 0.5;
        y_s( (jj-1)*nn+1 : jj*nn ) = 0.5;
    end
    if data(ii)==0 && data(ii+1)==1
        y_c( (jj-1)*nn+1 : jj*nn ) = -0.5;
        y_s( (jj-1)*nn+1 : jj*nn ) = 0.5;
    end            
    if data(ii)==1 && data(ii+1)==0
        y_c( (jj-1)*nn+1 : jj*nn ) = -0.5;
        y_s( (jj-1)*nn+1 : jj*nn ) = -0.5;
    end  
    if data(ii)==1 && data(ii+1)==1
        y_c( (jj-1)*nn+1 : jj*nn ) = 0.5;
        y_s( (jj-1)*nn+1 : jj*nn ) = -0.5;
    end     
    jj = jj + 1;
end


f = 1E6; % f nosne
s = y_c .* cos(2*pi*f*t) + y_s .* sin(2*pi*f*t); % vysledny signal

S= 10*log10((abs(fftshift(fft(s)))).^2);% energetické spektrum signálu 
fx = fs/2 * linspace(-1,1,length(s)); 

figure;
subplot(2,1,1); plot(t,s); title('QPSK'); xlabel('t [s]'); ylabel('A [V]');
subplot(2,1,2); plot(fx,S); title('Spektrum');xlabel('f [Hz]'); ylabel('Se [dB]'); 
            ylim([-20 max(S)*1.2 ]); xlim([-3E6 3E6]);




