Está en la página 1de 13

ESCUELA POLITCNICA NACIONAL

LABORATORIO DE COMUNICACIN DIGITAL


Prctica N 9
Cdigos de lnea utilizando matlab.

Objetivos:

Utilizar los conocimientos adquiridos en la teora para la realizacin de un programa


que permita implementar codificacin de lnea utilizando la interfaz grfica de usuario
de MATLAB.

Preparatorio:

Disear un programa en la GUI de MATLAB que permita realizar la codificacin de lnea


de una secuencia de caracteres ingresada por teclado dentro de la interfaz grfica.
DATOS DE ENTRADA:
Secuencia de bits (mnimo 15)
Tiempo de bit.
DATOS DE SALIDA
Calcular y mostrar en el programa los valores de Ancho de Banda de la seal codificada
crtica, velocidad de transmisin y velocidad de la seal.
Graficar la seal de reloj, seal binaria original y seal codificada.
Graficar el espectro de la seal binaria original y el espectro de la seal codificada
correspondiente a la secuencia ingresada. Se debe solicitar el nmero de armnicos que se
desea incluir para obtener las grficas solicitadas.
Las grficas debern incluir las escalas de tiempo y frecuencia correspondientes.
CDIGO A IMPLEMENTAR:
Viernes: 4B3T, Diferencial tipo M y HDB3

VENTANA PRINCIPAL

CDIGO 4BRT
function btnTransmitir_Callback(hObject, eventdata, handles)
% hObject
handle to btnTransmitir (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
% A=handles.txtTiempoBit;
% Vtx=1/A;
% set(handles.txtVt,'String',Vtx);
% Vs=(3*Vtx)/4;
% set(handles.txtVs,'String',Vs);
% ABc1=(3/4*A);
% set(handles.txtAB,'String',ABc1);
%-----------------------------------set(handles.btnNuevo,'Visible','on');
transmitir

%Se habilita el boton

syms h1 A;
%h=[];
A=str2double(get(handles.txtTiempoBit,'String')) / 1000;
k=str2double(get(handles.txtArmonicos,'String'));
Vtx=1/A;

set(handles.txtVt,'String',Vtx);
Vs=(3*Vtx)/4;
set(handles.txtVs,'String',Vs);
ABc1=(3/4*A);
set(handles.txtAB,'String',ABc1);
%Para el llenado de datos a transmitir
%-----------------------% for z=1:20
%
w(z)=str2double(get(handles.bit(z)))
% po=w
armonicos=get(handles.txtArmonicos,'string');
d1=get(handles.edit47,'String');
for i=1:1:length(d1)
if d1(i)==49
%49 ASCII (decimal) del 1
h(i)=d1(i)/49;
else
h(i)=d1(i)-48;
%48 ASCII (decimal) del 0
end
end
h;
if length(h)==15
h=[h 0 0 0 0 0];
end
if length(h)==16
h=[h 0 0 0 0];
end
if length(h)==17
h=[h 0 0 0];
end
if length(h)==18
h=[h 0 0];
end
if length(h)==19
h=[h 0];
end
if length(h)==20
h=[h];
end

%seal de reloj
fs=1000*(1/A);
axes(handles.axes5);
t = 0:1/fs:(length(h)-1)*A;
y = (1/2)*square(2*pi*(1/A)*t)+(1/2);
plot(t,y);
axis([0 (length(h)-1)*A -2 2]);
title('Seal de reloj')
%FUNCION PARA HACER LA CODIFICACION
%h=[1 0 1 0 1 0 0 0 1 1 0 1 0 1 0 0 0 1 1 0];
%h1=[1 0 1 0 1 0 0 0 1 1 0 1 0 1 0 0 0 1 1 0];
%h=[h h1];
w=[];
T=[];

Y=[];
TT=[];
YY=[];
w(1)=h(1);
w(2)=h(2);
w(3)=h(3);
w(4)=h(4);
w(5)=str2double(get(handles.txtEstadoInicial,'String'));
gg=codigo(w);
hh=[gg(1) gg(2) gg(3)];
% n=1;
% l=length(h);
% h(l+1)=1;
for z=1:4
w(1)=h((z*4)+1);
w(2)=h((z*4)+2);
w(3)=h((z*4)+3);
w(4)=h((z*4)+4);
w(5)=gg(4);
z=z+3;
gg=codigo(w);
hh=[hh gg(1) gg(2) gg(3)];
end

axes(handles.axes4)
hold off
ppp=[];
ttt=[];
for ll=1:length(h)
tt=(ll-1)*A:0.001:ll*A;
for e=1:length(tt)
pp(e)= h(ll);
end
ppp=[ppp pp];
ttt=[ttt tt];
end
grid on
ejes=plot(ttt,ppp,'r')
set(ejes,'LineWidth',2);
esp1=fft(ppp);
ty=max(esp1);
esp1=esp1/ty;

axes(handles.axes2)

%stem(ttt,abs(esp1),'g')
%stem(ttt,abs(esp1),'g')
axis([0 0.05 -1.1 1.1 ])
axes(handles.axes3)

hold off
axis([0 length(hh) -1.5 1.5])
ppp=[];
ttt=[];
for ll=1:length(hh)
tt=(ll-1)*A*(4/3):0.001:ll*A*(4/3);
for e=1:length(tt)
pp(e)= hh(ll);
end
ppp=[ppp pp];
ttt=[ttt tt];
end
ejes=plot(ttt,ppp,'b')
set(ejes,'LineWidth',2);
esp=fft(ppp);
ty=max(esp);
esp=esp/ty;
axes(handles.axes1)
grid on

n=5000;

m=0.1:1/(length(h)*A):(1/(length(h)*A))*k;
z=abs(A*n*(sin(pi*m*A)./(pi*m*A)));
%figure
stem(m,z);%title('Espectro Codigo Codificado');
xlabel('Frecuencia [Hz]');ylabel('Amplitud [V]');grid on
axes(handles.axes2)

m=0.1:1/(length(ttt)*A):(1/(length(ttt)*A))*k;
z=abs(A*n*(sin(pi*m*A)./(pi*m*A)));
%figure
stem(m,z);%title('Espectro Datos');
xlabel('Frecuencia [Hz]');ylabel('Amplitud [V]');grid on
axes(handles.axes1)

function txtEstadoInicial_CreateFcn(hObject, eventdata, handles)


% hObject
handle to txtEstadoInicial (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
empty - handles not created until after all CreateFcns
called
% Hint: edit controls usually have a white background on Windows.
%
See ISPC and COMPUTER.
if ispc
set(hObject,'BackgroundColor','white');
else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')
);
end

function q=codigo(w)
q=[] ;
H=[0 0 0 0 0 -1 1 0 -1 1 0 0;
0 0 0 1 -1 1 0 -1 1 0 0 0;
0 0 1 0 -1 0 1 -1 0 1 0 0;
0 0 1 1 1 -1 1 -1 1 -1 1 -1;
0 1 0 0 0 1 1 0 -1 -1 2 -2;
0 1 0 1 0 1 0 0 -1 0 1 -1;
0 1 1 0 0 0 1 0 0 -1 1 -1;
0 1 1 1 -1 1 1 1 -1 -1 1 -1;
1 0 0 0 0 1 -1 0 1 -1 0 0;
1 0 0 1 1 -1 0 1 -1 0 0 0;
1 0 1 0 1 0 -1 1 0 -1 0 0;
1 0 1 1 1 0 0 -1 0 0 1 -1;
1 1 0 0 1 0 1 -1 0 -1 2 -2;
1 1 0 1 1 1 0 -1 -1 0 2 -2;
1 1 1 0 1 1 -1 -1 -1 1 1 -1;
1 1 1 1 1 1 1 -1 -1 -1 3 -3];

ES=[-3 -2 -1 1;
-2 -1 1 2;
-1 1 2 3;
1 -1 -2 -3;
2 1 -1 -2;
3 2 1 -1];

for j=1:16
if H(j,1)==w(1)
if H(j,2)==w(2)
if H(j,3)==w(3)
if H(j,4)==w(4)
if w(5)>=0
q=[H(j,8) H(j,9) H(j,10)];
for o=1:6
if ES(o,1) == w(5)
if abs(H(j,12)) ==0
q =[q ES(o,1)];
else
if abs(H(j,12)) ==1
q =[q ES(o,2)];
else
if abs(H(j,12)) ==2
q =[q ES(o,3)];

else
if abs(H(j,12)) ==3
q =[q ES(o,4)];
end
end
end
end
end
end
else

q=[H(j,5) H(j,6) H(j,7)];


for o=1:6
if ES(o,1) == w(5)
if abs(H(j,11)) ==0
q =[q ES(o,1)];
else
if abs(H(j,11)) ==1
q =[q ES(o,2)];
else
if abs(H(j,11)) ==2
q =[q ES(o,3)];
else
if abs(H(j,11)) ==3
q =[q ES(o,4)];
end
end
end
end
end
end
end
end
end
end
end

end
%end

Simulacin:

CDIGO DIFERENCIAL TIPO M


function pushbutton1_Callback(hObject, eventdata, handles)

tbit=handles.tb;
vtxs=1/tbit;
set(handles.text2,'String',vtxs);

d1=get(handles.edit3,'String');
for i=1:1:length(d1)
if d1(i)==49
%49 ASCII (decimal) del 1
h(i)=d1(i)/49;
else
h(i)=d1(i)-48;
%48 ASCII (decimal) del 0
end
end

h;
xn=h;
axes(handles.axes4);
stairs([0:length(xn)-1]*tbit,xn);axis([0 (length(xn)-1)*tbit -2
2]);%title('Codigo Binario a Transmitirse')
%seal de reloj
fs=1000*(1/tbit);
axes(handles.axes5);
t = 0:1/fs:(length(xn)-1)*tbit;
y = (1/2)*square(2*pi*(1/tbit)*t)+(1/2);
plot(t,y);
axis([0 (length(xn)-1)*tbit -2 2]);%title('Seal de reloj')

tbc=handles.tb;
vt=1/tbc;
vs=vt;
set(handles.text3,'String',vs);
ab=vs;
set(handles.text4,'String',ab);
hold off
xn;
%xn=[a b c d e f g h i0 j0 k l m n o p];
secuencia=xn;
tbit2=handles.tb;
cod=0;
cambio=-1;
ref=+1;
for k=1:length(secuencia)
k;
if k==1
if secuencia(1)==0
cod(1)=(ref);
else
cod(1)=(ref*cambio);
end
else
if k>1
if secuencia(k)==0
cod(k)=(cod(k-1));
else
cod(k)=(cod(k-1)*cambio);
end
end
end
end
yn=[cod 0];
a=1;
%amplitud
T=tbit2;
%periodo
fs=1000*(1/T);
%frec. de muestreo
t = 0:1/fs:length(cod)*tbit2;
%# de periodos de la onda
y = (a/2)*square(2*pi*(1/T)*t)+(a/2);
axes(handles.axes3);
stairs([0:length(yn)-1]*tbit2,yn);

axis([0 (length(xn)-1)*tbit2,-2 2]);%title('Diferencial tipo M');


grid on;
cod;
codigo=cod;
Tb=handles.tb;
a=handles.edit2;
n=4;
axes(handles.axes1)
m=0.1:1/(length(codigo)*Tb):(1/(length(codigo)*Tb))*a;
z=abs(Tb*n*(sin(pi*m*Tb)./(pi*m*Tb)));
%figure
stem(m,z,'g');%title('Espectro Datos');
xlabel('Frecuencia [Hz]');ylabel('Amplitud [V]');grid on
axes(handles.axes2)
m=0.1:1/(length(xn)*Tb):(1/(length(xn)*Tb))*a;
z=abs(Tb*n*(sin(pi*m*Tb)./(pi*m*Tb)));
%figure
stem(m,z,'y');%title('Espectro Datos');
xlabel('Frecuencia [Hz]');ylabel('Amplitud [V]');grid on

SIMULACIN:

CDIGO HDB3
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject
handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
Tb=handles.tb;
AB_codigo=strcat(int2str(1/Tb));
set(handles.text4,'String',AB_codigo)
vt=strcat(int2str(1/Tb));
set(handles.text2,'String',vt);
vs=strcat(int2str(1/Tb));
set(handles.text3,'String',vt);

d1=get(handles.edit4,'String');
for i=1:1:length(d1)
if d1(i)==49
%49 ASCII (decimal) del 1
h(i)=d1(i)/49;
else
h(i)=d1(i)-48;
%48 ASCII (decimal) del 0
end
end
h;
secuencia=h

global yh;
xn=[secuencia 0];
yn=xn;
num=0;
los 1s y determinar si son pares o impares
%Generacion de Codigo AMI
for k=1:length(xn)
if xn(k)==1
num=num+1;
if num/2== fix(num/2)
numeros de 1s pares
yn(k)=-1;
else
yn(k)=1;
numeros de 1s impares
end
end
end

%Codigo a Modificar
%variable para contar

%Condicion para

%Condicion para

%conteo de 0s continuos para cambiar el codigo por 000v o B00V


num=0;
%Reinicio de la
variable de conteo de 1s
yh=yn;
%Variable nueva del
codigo almacenado en yn
sign=0;
%Variable de signo
V=zeros(1,length(yn));
%Variable para
guardar las violaciones (V)
B=zeros(1,length(yn));

for k=1:length(yn)
if yn(k)==0
num=num+1;
if num== 4
num=0;
yh(k)=1*yh(k-4);
V(k)=yh(k);
if yh(k)==sign
yh(k)=-1*yh(k);
yh(k-3)=yh(k);
B(k-3)=yh(k);
V(k)=yh(k);
yh(k+1:length(yn))=-1*yh(k+1:length(yn));
end
sign=yh(k);
%Guarda el signo de la
violacion
end
else
num=0;
%Reestablece el contador
de 0s
end
end
re=[xn',yn',yh',V',B'];
a=1;
%amplitud
T=Tb;
%periodo
fs=1000*(1/T);
%frec. de muestreo
t = 0:1/fs:(length(xn)-1)*T;
y = (a/2)*square(2*pi*(1/T)*t)+(a/2);
axes(handles.axes4)
% %subplot(3,1,1);
stairs([0:length(xn)-1]*Tb,xn);
axis([0 (length(xn)-1)*Tb -2 2]);%title('Codigo Binario Transmitido');
xlabel('Tiempo [s]');ylabel('Amplitud [V]');grid on;%grafica la el
codigo binario de Entrada
axes(handles.axes3)
stairs([0:length(xn)-1]*Tb,yh);axis([0 (length(xn)-1)*Tb -2
2]);%title('Codigo HDB3');
xlabel('Tiempo [s]');ylabel('Amplitud [V]');grid on;
axes(handles.axes5)
plot(t,y,'r');axis([0 (length(xn)-1)*Tb -2 2]);title('Seal de
reloj');xlabel('Tiempo [s]');ylabel('Amplitud [V]');grid on;
re;
codigo=re;
Tb=handles.tb;
a=handles.edit2;
n=4;
axes(handles.axes1)
m=0.1:1/(length(codigo)*Tb):(1/(length(codigo)*Tb))*a;
z=abs(Tb*n*(sin(pi*m*Tb)./(pi*m*Tb)));
%figure
stem(m,z,'r');%title('Espectro Datos');
xlabel('Frecuencia [Hz]');ylabel('Amplitud [V]');grid on
axes(handles.axes2)
m=0.1:1/(length(xn)*Tb):(1/(length(xn)*Tb))*a;
z=abs(Tb*n*(sin(pi*m*Tb)./(pi*m*Tb)));
%figure
stem(m,z,'y');%title('Espectro Datos');

xlabel('Frecuencia [Hz]');ylabel('Amplitud [V]');grid on

SIMULACIN:

Bibliografa:

Help de Matlab R2008b


Libro Ing. Mara Soledad

También podría gustarte