You are on page 1of 15

INSTITUTO POLITÉCNICO NACIONAL

ESCUELA SUPERIOR DE INGENIERÍA MECÁNICA


Y ELÉCTRICA

UNIDAD CULHUACAN

INGENIERÍA EN COMPUTACIÓN

TEORÍA DE LA INFORMACIÓN Y CODIFICACIÓN.

PRACTICA N.º 4

ALUMNOS:

GRUPO: 7CM21

PROFESOR:

DR. JOSE EDUARDO GUZMAN RODRIGUEZ

CIUDAD DE MÉXICO, 05 DE SEPTIEMBRE DE


2018

 OBJETIVO
Identificar la diferencia entre la longitud de la palabra código y la longitud promedio de la
palabra código y calcular la eficiencia del código, realizando un programa en MATLAB, el
cual deberá mostrar una interfaz gráfica de usuario (GUI).

 DESARROLLO
- Versión de MATLAB utilizada 2017b.

1. Al programa en MATLAB modificado en prácticas anteriores agrega la capacidad


para que calcule la longitud promedio de la palabra código y la eficiencia del código,
así como la redundancia del código de un archivo de texto, de una imagen a blanco
y negro, escala de grises y a color.

Programa para longitud promedio, eficiencia y redundancia de texto


function varargout = practicaTexto(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @practicaTexto_OpeningFcn, ...
'gui_OutputFcn', @practicaTexto_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before practicaTexto is made visible.


function practicaTexto_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;

% Update handles structure


guidata(hObject, handles);

% --- Outputs from this function are returned to the command line.
function varargout = practicaTexto_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function texto_Callback(hObject, eventdata, handles)
val=get(hObject, 'String');
handles.texto=val;
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.


function texto_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in calcular.


function calcular_Callback(hObject, eventdata, handles)
texto=upper(handles.texto);
tam=length(texto);
numEspacios=0;
for i=1:1:tam
if texto(i)==' '
if texto(i+1)==' ' || (texto(i+1)<65 || texto(i+1)>90)
i=i+1;
else
numEspacios=numEspacios+1;
end
end
end
set(handles.numPalabras,'String',numEspacios+1);

numLetras=0;
for i=1:1:tam;
if texto(i)>=65 && texto(i)<=90
numLetras=numLetras+1;
elseif texto(i)=='Ñ'
numLetras=numLetras+1;
end
end
set(handles.numLetras,'String',numLetras);

simbolos=zeros(1,27);
for i=1:1:tam
if texto(i)>=65 && texto(i)<=90
indice=texto(i)-64;
simbolos(indice)=simbolos(indice)+1;
end
if texto(i)=='Ñ'
simbolos(27)=simbolos(27)+1;
end
end
probabilidades=zeros(1,27);
for i=1:1:27
probabilidades(i)=simbolos(i)/numLetras;
end

bits=0;
informacion=0;
for i=1:1:27
if probabilidades(i)>0

informacion=informacion+probabilidades(i)*log2(1/probabilidades(i));
bits=bits+log2(1/probabilidades(i));
ACS1=bits/1.44; %nats
ACS2=bits/3.32; %hartleys

end
end
informacion=round(informacion+0.4);
bits=round(bits+0.4);
set(handles.informacion,'String',bits);
set(handles.nats,'String',ACS1);
set(handles.hartleys,'String',ACS2);

set(handles.codificacion, 'String', ' ');


codigos=zeros(1,27);
n=0;
for i=1:1:tam
if texto(i)>=65 && texto(i)<=90
indice=double(texto(i))-64;
end
if texto(i)=='Ñ'
indice=27;
end
if codigos(indice)==0
codigos(indice)=1;
binario=[dec2bin(n,informacion)];
n=n+1;

t = get(handles.codificacion, 'String');
if ischar(t); t = cellstr(t); end
t = [t; texto(i) ' ' binario];
set(handles.codificacion, 'String', t);
end
end

entropia=0;
for i=1:1:27
if probabilidades(i)>0
entropia=entropia-probabilidades(i)*log2(probabilidades(i));
end
end
set(handles.entropia,'String',entropia);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%
%%%%%%%%%%%%% Practica 3
%%%%%%%%%%%%% Histograma

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%

simbolos=zeros(1,27);
for i=1:1:tam
if texto(i)>=65 && texto(i)<=90
indice=texto(i)-64;
simbolos(indice)=simbolos(indice)+1;
end
if texto(i)=='Ñ'
simbolos(27)=simbolos(27)+1;
end
end
axes(handles.histograma);
bar(simbolos);
%PRACTICA 4 LONGITUD
longitud = ceil(entropia);
eficiencia=entropia/longitud;
redundancia=1-eficiencia;
set(handles.longitud,'String',longitud);
set(handles.eficiencia,'String',eficiencia);
set(handles.redundancia,'String',redundancia);

% --- Executes on selection change in codificacion.


function codificacion_Callback(hObject, eventdata, handles)
% hObject handle to codificacion (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% --- Executes during object creation, after setting all properties.


function codificacion_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'),


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

function nats_Callback(hObject, eventdata, handles)


% hObject handle to nats (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% --- Executes during object creation, after setting all properties.


function nats_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'),


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

function hartleys_Callback(hObject, eventdata, handles)


% hObject handle to hartleys (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function hartleys_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

Programa para longitud promedio, eficiencia y redundancia de imagen


function varargout = Practica1B(varargin)

gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Practica1B_OpeningFcn, ...
'gui_OutputFcn', @Practica1B_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end

function Practica1B_OpeningFcn(hObject, eventdata, handles, varargin)


handles.output = hObject;

% Update handles structure


guidata(hObject, handles);

function varargout = Practica1B_OutputFcn(hObject, eventdata, handles)

varargout{1} = handles.output;

function direccion_Callback(hObject, eventdata, handles)


% hObject handle to direccion (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% --- Executes during object creation, after setting all properties.


function direccion_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'),


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

% --- Executes on button press in Cargar.


function Cargar_Callback(hObject, eventdata, handles)

[filename pathname ] = uigetfile( {'*.jpg';'*.bmp';'*.png'},'Seleccionar


imagen');
if(filename ~= 0 )
I = strcat(pathname,filename);
axes(handles.axes1)
imshow(I)
im = imread(I);

Ibw=im2bw(imread(I));
axes(handles.axes2);
imshow(Ibw);

Ig = rgb2gray(imread(I));
axes(handles.axes3);
imshow(Ig);

[y,x] = size(Ibw);
negro = 0;
blanco = 0;

for i=1:1:y
for j=1:1:x
if Ibw(i,j)==1
negro = negro+1;
else
blanco = blanco+1 ;
end
end
end

%%histograma para imagen blanco y negro


Nn=0;
Bn=0;
[y,x]=size(Ibw);
for i=1:1:y
for j=1:1:x
if Ibw(i,j)==false
Nn=Nn+1;
else
Bn=Bn+1;
end
end
end
vector=[Nn Bn];
axes(handles.axes8)
bar(vector);
set(gca, 'XTickLabel', {'negro','blanco'})
%%histograma imagen grises
simbolos=zeros(1,256);
[y,x]=size(Ig);
for i=1:1:y
for j=1:1:x
indice=Ig(i,j)+1;
simbolos(indice)=simbolos(indice)+1;
end
end

axes(handles.axes7)
bar(simbolos);

%%histograma imagen a color


r = im(:,:,1);
red_hist = imhist(r);
axes(handles.axes12);
plot(red_hist,'red');

g = im(:,:,2);
green_hist = imhist(g);
axes(handles.axes13);
plot(green_hist,'green');

b = im(:,:,3);
blue_hist = imhist(b);
axes(handles.axes14);
plot(blue_hist,'blue');

R=(x*y)*24;
set (handles.inf,'string',num2str(R));
n=negro/(x*y);
b=blanco/(x*y);
set (handles.ACSX,'string',num2str(x));
set (handles.ACSY,'string',num2str(y));
H=(-n*log2(n))+(-b*log2(b));
set(handles.H,'String',num2str(H));
L = ceil(H);
set (handles.longitud,'string',num2str(L));
eficiencia=H/L;
set (handles.eficiencia,'string',num2str(eficiencia));
redundancia=1-eficiencia;
set (handles.redundancia,'string',num2str(redundancia));

else
return;
end

function inf_Callback(hObject, eventdata, handles)


% hObject handle to inf (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function inf_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'),


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

function H_Callback(hObject, eventdata, handles)


% hObject handle to H (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% --- Executes during object creation, after setting all properties.


function H_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'),


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

function edit4_Callback(hObject, eventdata, handles)


% hObject handle to edit4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% --- Executes during object creation, after setting all properties.


function edit4_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'),


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

function Direccion_Callback(hObject, eventdata, handles)


% hObject handle to Direccion (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% --- Executes during object creation, after setting all properties.


function Direccion_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'),


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

function ACSY_Callback(hObject, eventdata, handles)


% hObject handle to ACSY (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% --- Executes during object creation, after setting all properties.


function ACSY_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'),


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

function ACSX_Callback(hObject, eventdata, handles)


% hObject handle to ACSY (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% --- Executes during object creation, after setting all properties.


function ACSX_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function longitud_Callback(hObject, eventdata, handles)


% hObject handle to longitud (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of longitud as text


% str2double(get(hObject,'String')) returns contents of longitud
as a double

% --- Executes during object creation, after setting all properties.


function longitud_CreateFcn(hObject, eventdata, handles)
% hObject handle to longitud (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 && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function eficiencia_Callback(hObject, eventdata, handles)


% hObject handle to eficiencia (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of eficiencia as text


% str2double(get(hObject,'String')) returns contents of eficiencia
as a double

% --- Executes during object creation, after setting all properties.


function eficiencia_CreateFcn(hObject, eventdata, handles)
% hObject handle to eficiencia (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 && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function redundancia_Callback(hObject, eventdata, handles)


% hObject handle to redundancia (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of redundancia as text


% str2double(get(hObject,'String')) returns contents of
redundancia as a double

% --- Executes during object creation, after setting all properties.


function redundancia_CreateFcn(hObject, eventdata, handles)
% hObject handle to redundancia (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 && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

 PRUEBAS Y RESULTADOS

En la parte de arriba se muestra el programa de texto corriendo con la interfaz de manera


correcta.
En la parte de arriba se muestra el programa de imagen corriendo con la interfaz de manera
correcta.

En la figura 3a y 3b , mostradas en la parte superior se aprecia el programa de texto


funcionando de manera correcta, mostrando su longitud promedio, eficiencia y
redundancia para los nombres de los integrantes del equipo.

Las figuras de abajo muestran las longitudes promedio, eficiencias y redundancias para 3
diferentes imágenes:
 CONCLUSIONES
Con la realización de esta práctica, pudimos ver que el programa funciono de manera
correcta, y se pudo reforzar el conocimiento visto en las clases, se cumplió el objetivo de
como obtener el histograma con la ayuda del programa desarrollado en MATLAB.