close all

RSR = 30; %dB

N=200;
rs = 16;
ts = 1 / rs;

bits = randi([0,1],1,N);

t = -rs/2:N*rs-rs/2-1;
t = t * ts;


%% roll-off = 0

s = zeros(1,rs*N);
s_all = zeros(rs*N,N);

for k = 1:N
  a = 2*bits(k)-1;
  s = s + a*sinc(t-k+1);
  s_all(:,k) = a*sinc(t-k+1).';
end

pot = mean(s.^2);
N0 = pot / 10^(RSR/10);
n = sqrt(N0)*randn(1,rs*N);


s = s + n;

figure(1)
plot(t,s)
title('roll-off=0');
xlabel('t');
ylabel('s(t)')

figure(2)
plot(t,s_all)
title('roll-off=0');
xlabel('t');
ylabel('s(t)')



figure(3)
title('Diagrama do Olho - roll-off = 0')
s_aux = s(rs+rs/2+1 : length(s) - rs/2);
s_eye = reshape(s_aux, rs*2, N/2-1);
plot(s_eye)


ncorr = 256;
f0 = 1 / (ncorr * 2 * ts);
f = (0:(ncorr*2-1)) * f0; % frequências amostradas na DFT


[psd_ro0, fro0] = pwelch(s, [],[],[],rs, 'twosided' );



%% roll-off = 1
s = zeros(1,rs*N);

for k = 1:N
  a = 2*bits(k)-1;
  t_aux = t-k+1;
  s_aux = zeros(1,rs*N);
  s_aux(abs(t_aux) == 1/2) = pi / 4;
  index = find(abs(t_aux) ~= 1/2);
  s_aux(index) = cos(pi*(t_aux(index)))./(1-4*(t_aux(index)).^2);
  s = s + a * s_aux .* sinc(t-k+1);
  s_all(:,k) = (a * s_aux .* sinc(t-k+1)).';  
end

figure(4)
plot(t,s)
title('roll-off=1');
xlabel('t');
ylabel('s(t)')

figure(5)
plot(t,s_all)
title('roll-off=1');
xlabel('t');
ylabel('s(t)')

figure(6)
title('Diagrama do Olho - roll-off = 1')
s_aux = s(rs+rs/2+1 : length(s) - rs/2);
s_eye = reshape(s_aux, rs*2, N/2-1);
plot(s_eye)

[psd_ro1, fro1] = pwelch(s, [],[],[],rs, 'twosided' );


figure(7);
plot(fro0, 10*log10(psd_ro0), fro1, 10*log10(psd_ro1));
legend('roll-off = 0', 'roll-off = 1')
title('DEP, roll-off = 1')
xlabel('frequência (Hz)');


