clear all, close all, clc
fs = 2^16;
fi = 2^11;
Rb = 2^10;
frec = [Rb fi fs];
L = floor(fs/Rb);
cDIV = 5;
trama(1:2:100) = ones(1,50);
BOD = [1 1 0 0 1 1 0 0];
trama = [trama 0 BOD];
trama = [trama 1 0 0 1 1 1 0 1 0 0 0 0 0 0 1 1 0 1 0 1 0];
pepe = trama;
trama2 = 2*trama - ones(1,length(trama));
PAM1 = 5.*generaPAM(trama, L);
Linea = cosenoAlzado(PAM1, fs, Rb, 0.5);
t = 0:(1/fs):(length(Linea)-1)*(1/fs);
in = Linea.*(sin(2*pi*fi*t)+cos(2*pi*fi*t));
out = [];
RELOJ = 8; % Fila a leer en la memoria
contMUESTRA = 1; % Contador de Muestras
contFLANCOS = 2; % Contador de Flancos
MUESTRAS_BIT = 3; % N muestras/bit
muestraANT = 4; % Muestra Anterior
ESTADO = 5; % Banera: 0 = recuperacin sincronismo; 1 = recepcin trama
valorUNO = 6; % Voltaje que se le asigna al "1" lgico
valorMUESTRA = 7; % Suma de los voltajes de las muestras en 1 smbolo
contCAB = 8; % N que especificael nmero de bits de la cabecera BOF rx.
posCAB = 9; % Donde comenzamos a guardar la 1 cabecera BOF
trama = [];
CAB_SYNCRO = 100; % Longitud de la cabecera de sincronismo
ruta = 'D:\Estudios\UPM Superior\4Teleco\2Cuatrimestre\Lab. Trat. Dig. Seal\BPSK\Cdigo\Ficheros\';
%PAM1 = cargar(ruta,'decisor.rx',1, inf, 2);
%plot(PAM!
%PAM2 = generaPAM(trama, L);
%PAM2 = [zeros(1,100) PAM2];
%PAM1 = [PAM1 zeros(1,100)];
M = zeros(8,60);
%media = mean(in.^2);
%H = fir1(33,2.*[0.12 0.13]);
%gp = grpdelay(H,1);
%retardo = ceil(max(abs(gp)));
P = zeros(5,3);
%out=5;
pPLL = 1; % Posicin de los parmetros del PLL.
pLEAD = 2; % Posicin de los parmetros del Filtro del lazo del PLL.
pVCO = 3; % Posicin de los parmetros del VCO.
pKv = 1;
pWn = 2;
pPsi = 3;
mem= M;
N=10;
s= 1;
% Llamamos a recupera Portadora
%################################
% long = length(in);
M = zeros(9,60);
% P = zeros(5,3);
P(pPLL, pKv) = 9e3; % Hz/V
P(pPLL, pWn) = 4e3; % rad/sg
P(pPLL, pPsi) = 1/(sqrt(2));
% media = mean(in.^2);
cBB = 7;
% 2.- Diseo del filtro de FI
%-----------------------------
H_fi = fir1(33,2.*[0.12 0.13]);
% gp = grpdelay(H_fi,1);
% tau_fi = ceil(max(abs(gp)));
% 3.- Diseo del filtro de Banda Base
%-----------------------------------
H_bb = fir1(15,0.2);
% gp = grpdelay(H_bb,1);
% retardo = ceil(max(abs(gp)));
cFILTRO_DC = 8;
M(RELOJ,contMUESTRA)=0;
M(RELOJ,MUESTRAS_BIT)=L;
M(RELOJ,valorUNO)=5;
retardo = 0;
BwDC = 2*frec(2)/frec(3); % fi/fs
H_dc = fir1(50,2*BwDC,'high');
gp = grpdelay(H_dc,1);
retardo = ceil(max(abs(gp)))+retardo;
out = [];
aux = [];
ref = [];
nBits = 0;
for k = 1:length(in),
%[ref(k), aux] = filter(H_dc, 1 , PAM1(k) , M(cFILTRO_DC,(1:length(H_dc)-1)));
%M(cFILTRO_DC,1:length(aux) ) = aux';
%end;
[out(k), M, P] = recuperaPortadora(in(k), M, ...
fs, k==1, H_dc, H_fi, P);
bandaBase(k) = out(k)*in(k);
[Linea(k), aux] = filter(H_bb, 1, bandaBase(k),...
M(cBB,(1:length(H_bb)-1)));
M(cBB,1:length(aux)) = aux';
% Linea(k) = ((Linea(k)>0)| (k < retardo))*5;
%[out] = demoduladorBPSK (in, fs);
% [aux, M] = recuperaReloj(PAM1(k), M, k==1, 10);
% M(RELOJ, contMUESTRA) = M(RELOJ, contMUESTRA) + 1; % Contador Muestras ++
%[trama, M(RELOJ,:)] = rxTrama(PAM1(k), M(RELOJ,:));
%M(RELOJ,:) = sincroMsj(PAM1(k), M(RELOJ,:));
%M(RELOJ,:) = periodoReloj(PAM1(k), 10, M(RELOJ,:));
% if M(RELOJ,contMUESTRA)==0,
% M(RELOJ,MUESTRAS_BIT)
% cont = round(k/32)
% if cont > 108, break; end;
end;
%end;
%nBits = nBits+length(aux);
%if length(out)==0,
% posicion = k/32;
%end;
%freqz(H_dc,1,2^12);
%[out(k), M, P] = pll (in(k), M, fs, k==1, P);
%end;
%plot(5.*out,'r'); hold on;
%plot(in,'b'); hold off
%error2 = filter(b,a,desfase);
%t = 0:(1/fs):(1/fs)*(length(PAM1)-1);
%subplot(2,1,1),
%plot(t,abs(fftshift(fft(out.*in))),t,abs(fftshift(fft(out))));
%ylabel('Seales PAM');
%OUT = fftshift(fft(out));
%IN = fftshift(fft(in));
%BASE = fftshift(fft(bandaBase));
%BASE = fftshift(fft(bandaBase));
%f = -0.5:1/(length(out)-1):0.5;
%subplot(2,1,2),%
%plot(t,desfase,t, error,t,vco);
%plot(t(r+1:length(t)),out(r+1:length(out)));
%MITAD = fftshift(fft(mitad));
%semilogy(f,abs(IN), f, abs(OUT), f, abs(BASE));
%ylabel('Seal diferencia fase');
%legend('IN','OUT');
%out;