Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Practica 5 Codificación Shannon - F.
Practica 5 Codificación Shannon - F.
Profesor:
José Eduardo Guzmán Rodríguez
Grupo: 7CM21
30/03/2020
Objetivo:
El alumno identifica el procedimiento que se lleva a cabo para la
codificación de Shannon - Fano, realizando un programa en
MATLAB, Este deberá mostrar una interfaz gráfica de usuario
(GUI).
CONSIDERACIONES GENERALES
Las practicas se entregan en una carpeta con el nombre de alguno de
los integrantes empezando por el apellido paterno, la carpeta debe
incluir en formato PDF o DOCX el reporte de la práctica, anexando
si es necesario el escaneo de los cálculos realizados en el cuaderno,
junto con el programa en MATLAB en la hora que corresponda al
laboratorio y en la fecha indicada, cualquier práctica recibida
después de la fecha no se tomará en cuenta para la calificación del
departamental. El día que se realiza la práctica se debe entregar un
programa funcional de manera individual (valor de 70%) y en la
próxima sesión se elige entre los integrantes el mejor programa y se
entrega ya terminado (valor 30%). Se tomará en cuenta para la
calificación del reporte: portada (O. I %), programa con GUI (20%),
desarrollo (.9%).
ANTECEDENTES
En 1960 Claude E. Shannon (MIT) y Robert M. Fano (Laboratorios
Bell) desarrollan el procedimiento para generar un código binario de
árbol. El procedimiento evalúa la probabilidad del símbolo y asigna
una palabra código con su correspondiente longitud de código.
Comparado con Otros métodos la codificación de Shannon - Fano es
fácil de implementar. En la práctica la codificación de Shannon -
Fano no es de tanta importancia. Esto especialmente causado por la
baja eficiencia del código en comparación con la codificación de
Huffman que se realizará en la siguiente práctica.
Algoritmo de codificación:
l.- Crear la tabla que proporcione las frecuencias.
2.- Ordenar los símbolos de acuerdo a la frecuencia en orden
decreciente.
3.- Comenzar a trabajar con toda la tabla, división:
a) Toma en cuenta todos los símbolos de la tabla.
b) Divide el segmento en dos partes, ambas casi iguales en la suma
de las frecuencias.
c) Agregar un 0 a los símbolos de la parte superior y un 1 a los de la
parte inferior.
d) Buscar en el siguiente segmento que contenga más de dos
símbolos y repite la división de segmentos hasta que no queden más
símbolos.
e) Codifica los datos de según las palabras código que se generaron
en la tabla
El proceso de decodificación sigue el algoritmo general para la
interpretación de árboles de código binario.
El proceso de decodificación sigue el algoritmo general para la
interpretación de árboles de código binario.
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 untitled is made visible.
function texto_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to untitled (see VARARGIN)
% --- Outputs from this function are returned to the command line.
function varargout = texto_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%%%%%%%% TEXTO
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
mensaje = get(hObject,'String');
handles.mensaje = mensaje;
guidata(hObject,handles);
%A = get(handles.edit1, 'string');
[archivo,ruta]=uigetfile({'*.txt;*UTF-8'}, 'Escoger')
A = fread(fid,10000000,'uint8=>char')'; %
%disp('El archivo de texto se carga')
%disp('Su contenido del archivo .txt es')
end
j=j+1;
k=k+1;
l=l+1;
m=m+1;
H=H+p*in;
end
end
simbolob = char(simbolo)
handles.simbolo =simbolo;
guidata(hObject,handles);
%datos = [cellstr(simbolob)' veces' ps' is];
% datos = [cellstr(simbolob)' num2cell(veces(:))' num2cell(ps(:))'
num2cell(is(:))']
datos = [cellstr(simbolob') num2cell(veces)' num2cell(ps)'
num2cell(is)'];
set(handles.uitable1,'data',datos)
set(handles.edit6,'String', H);
set(handles.edit7,'String',length(handles.mensaje));
%voy a hacer las variables globales
handles.simbolob= simbolob;
handles.veces=veces;
handles.ps=ps;
handles.is=is;
guidata(hObject,handles);
handles.H=H;
guidata(hObject,handles);
letra = char(handles.simbolo(1));
letra2 = char(handles.simbolo(2));
if(handles.j>2)
letra3 = char(handles.simbolo(3));
x = categorical({letra, letra2,letra3});
end
if(handles.j>3)
letra4 = char(handles.simbolo(4));
x = categorical({letra, letra2,letra3,letra4});
end
if(handles.j>4)
letra5 = char(handles.simbolo(5));
x = categorical({letra, letra2,letra3,letra4,letra5});
end
if(handles.j>5)
letra6 = char(handles.simbolo(6));
x = categorical({letra, letra2,letra3,letra4,letra5,letra6});
end
if(handles.j>6)
letra7 = char(handles.simbolo(7));
x = categorical({letra, letra2,letra3,letra4,letra5,letra6,letra7});
end
if (handles.j>7)
letra8 = char(handles.simbolo(8));
x = categorical({letra,
letra2,letra3,letra4,letra5,letra6,letra7,letra8});
end
if (handles.j>8)
letra9 = char(handles.simbolo(9));
x = categorical({letra,
letra2,letra3,letra4,letra5,letra6,letra7,letra8,letra9});
end
if (handles.j>9)
letra10 = char(handles.simbolo(10));
x = categorical({letra,
letra2,letra3,letra4,letra5,letra6,letra7,letra8,letra9,letra10});
end
if (handles.j>10)
letra11 = char(handles.simbolo(11));
x = categorical({letra,
letra2,letra3,letra4,letra5,letra6,letra7,letra8,letra9,letra10,letra11})
;
end
if(handles.j>1)
y = [handles.veces(1), handles.veces(2)];
end
if(handles.j>2)
y = [handles.veces(1), handles.veces(2), handles.veces(3)];
end
if(handles.j>3)
y = [handles.veces(1), handles.veces(2), handles.veces(3),
handles.veces(4)];
end
if(handles.j>4)
y = [handles.veces(1), handles.veces(2), handles.veces(3),
handles.veces(4),handles.veces(5)];
end
if(handles.j>5)
y = [handles.veces(1), handles.veces(2), handles.veces(3),
handles.veces(4),handles.veces(5), handles.veces(6)];
end
if(handles.j>6)
y = [handles.veces(1), handles.veces(2), handles.veces(3),
handles.veces(4),handles.veces(5),handles.veces(6),handles.veces(7)];
end
if(handles.j>7)
y = [handles.veces(1), handles.veces(2), handles.veces(3),
handles.veces(4),handles.veces(5),handles.veces(6),handles.veces(7),handl
es.veces(8)];
end
if(handles.j>8)
y = [handles.veces(1), handles.veces(2), handles.veces(3),
handles.veces(4),handles.veces(5),handles.veces(6),handles.veces(7),handl
es.veces(8),handles.veces(8)];
end
if(handles.j>9)
y = [handles.veces(1), handles.veces(2), handles.veces(3),
handles.veces(4),handles.veces(5),handles.veces(6),handles.veces(7),handl
es.veces(8),handles.veces(8),handles.veces(9)];
end
if(handles.j>10)
y = [handles.veces(1), handles.veces(2), handles.veces(3),
handles.veces(4),handles.veces(5),handles.veces(6),handles.veces(7),handl
es.veces(8),handles.veces(8),handles.veces(9),handles.veces(10)];
end
bar(x,y);
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Outputs from this function are returned to the command line.
function varargout = archivo1_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%B imagen = imread('imagen.jpg'); %B
end
%handles es identificador de datos de la aplicación, almacena %id_global
handles.imagen=imagen; %B
guidata(hObject, handles)% A Y B salva los datos de la aplicación
binA = im2bw(handles.imagen);
handles.binA=binA;
guidata(hObject, handles)% salva los datos de la aplicación
imshow(binA)
title 'Imagen blanco y negro'; xlabel 'eje x'; ylabel 'eje y';
disp('A continuación mostraremos la matriz de valores binarios de B/N');
dimensiones = size(binA)
set(handles.size, 'String', strcat(num2str(dimensiones(1)),' x ',
num2str(dimensiones(2))) );
%calculo la probabilidad de 1 y 0
for x=1:9
for y = 1:11
if (handles.binA(x,y) == 1 )
P_x=P_x+1;
end
if (handles.binA(x,y) == 0 )
P_y=P_y+1;
end
end
end
prob1 = P_x/(P_x+P_y); %ps
prob2 = P_y/(P_x+P_y);
handles.prob1 = prob1;
handles.prob2 = prob2;
guidata(hObject,handles);
in1 = log2(1/prob1);
in2 = log2(1/prob2);
handles.in1 = in1;
handles.in2 = in2;
guidata(hObject,handles);
uno =1;
cero =0;
H=(in1+prob1)+(in2+prob2);
d= {1, P_x, prob1, in1 ; 0, P_y, prob2, in2 }
A= [1, P_x, prob1, in1 ];
B= [0, P_y, prob2, in2 ];
table = [A' B']
set(handles.uitable1,'data', d);
set(handles.edit5,'string',H);
handles.H=H;
guidata(hObject,handles);
handles.P_x=P_x;
guidata(hObject,handles);
handles.P_y=P_y;
guidata(hObject,handles);
handles.longitud = longitud;
guidata(hObject,handles);
set(handles.res_long, 'String',longitud);
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Outputs from this function are returned to the command line.
function varargout = archivo1_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%B imagen = imread('imagen.jpg'); %B
end
%handles es identificador de datos de la aplicación, almacena %id_global
handles.imagen=imagen; %B
guidata(hObject, handles)% A Y B salva los datos de la aplicación
%var_y = handles.pixel_y
blanco =0; gris = 0; negro =0;
for i = 1:SZ(1)
for y = 1:SZ(2)
if(imagen_gris(i,y) == 0)
negro=negro+1;
end
if(imagen_gris(i,y) == 127)
gris=gris+1;
end
if(imagen_gris(i,y) == 255)
blanco=blanco+1;
end
end
end
set(handles.edit7,'String',blanco);
set(handles.edit8,'String',gris);
set(handles.edit9,'String',negro);
%grafica de barras
c = categorical({'Negro','Gris','Blanco'});
%x = [255,127,0];
y = [negro, gris, blanco];
axes(handles.axes3);
bar(c,y);
handles.blanco = blanco;
handles.negro = negro;
handles.gris = gris;
guidata(hObject,handles);
Programa RGB
function varargout = archivo1(varargin)
% ARCHIVO1 MATLAB code for archivo1.fig
% ARCHIVO1, by itself, creates a new ARCHIVO1 or raises the existing
% singleton*.
%
% H = ARCHIVO1 returns the handle to a new ARCHIVO1 or the handle to
% the existing singleton*.
%
% ARCHIVO1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in ARCHIVO1.M with the given input
arguments.
%
% ARCHIVO1('Property','Value',...) creates a new ARCHIVO1 or raises
the
% existing singleton*. Starting from the left, property value pairs
are
% applied to the GUI before archivo1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property
application
% stop. All inputs are passed to archivo1_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only
one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Outputs from this function are returned to the command line.
function varargout = archivo1_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%B imagen = imread('imagen.jpg'); %B
end
%handles es identificador de datos de la aplicación, almacena %id_global
handles.imagen=imagen; %B
guidata(hObject, handles)% A Y B salva los datos de la aplicación
dimensiones = size(imagen)
set(handles.size, 'String', strcat(num2str(dimensiones(1)),' x ',
num2str(dimensiones(2)), ' x',num2str(dimensiones(3))) );
Red = handles.imagen;
handles.Red = Red;
guidata(hObject,handles);
Red(:,:,2) = 0; %RGB componente Rojo de la imagen original
%la entropia en cualquier capa es la misma
entropiared = entropy(handles.Red(:,:,2))
handles.entropiared = entropiared;
guidata(hObject,handles);
set(handles.edit7,'String',entropiared*3);
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Outputs from this function are returned to the command line.
function varargout = texto_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% BOTON
% --- Executes on button press in pushbutton1.
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)
%mensaje = fread(fid,10000000,'uint8=>char')
%A = get(handles.edit1, 'string');
A = fread(fid,10000000,'uint8=>char')'; %
%disp('El archivo de texto se carga')
%disp('Su contenido del archivo .txt es')
end
j=j+1;
k=k+1;
l=l+1;
m=m+1;
H=H+p*in;
end
end
simbolob = char(simbolo)
%datos = [cellstr(simbolob)' veces' ps' is];
% datos = [cellstr(simbolob)' num2cell(veces(:))' num2cell(ps(:))'
num2cell(is(:))']
datos = [cellstr(simbolob') num2cell(veces)' num2cell(ps)'
num2cell(is)'];
set(handles.uitable1,'data',datos)
set(handles.edit6,'String', H);
set(handles.edit7,'String',length(handles.mensaje));