当前位置:网站首页>MATLAB实现Huffman编码译码含GUI界面
MATLAB实现Huffman编码译码含GUI界面
2022-07-07 09:53:00 【Matlab科研工作室】
1 简介
在matlab中模拟C中链表,利用复数运算,联系具体字符和概率,每次找到最小概率的两个字符对应的编号,依次记录下来,最后根据奇偶码的不同实现Huffman编码.本算法新颖独特,易于理解,编程.
2 部分代码
function varargout = huffman_GUI(varargin)
% HUFFMAN_GUI M-file for huffman_GUI.fig
% HUFFMAN_GUI, by itself, creates a new HUFFMAN_GUI or raises the existing
% singleton*.
%
% H = HUFFMAN_GUI returns the handle to a new HUFFMAN_GUI or the handle to
% the existing singleton*.
%
% HUFFMAN_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in HUFFMAN_GUI.M with the given input arguments.
%
% HUFFMAN_GUI('Property','Value',...) creates a new HUFFMAN_GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before huffman_GUI_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to huffman_GUI_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
% Edit the above text to modify the response to help huffman_GUI
% Last Modified by GUIDE v2.5 10-Jan-2014 18:20:43
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @huffman_GUI_OpeningFcn, ...
'gui_OutputFcn', @huffman_GUI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin & isstr(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 huffman_GUI is made visible.
function huffman_GUI_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 huffman_GUI (see VARARGIN)
% Choose default command line output for huffman_GUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes huffman_GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = huffman_GUI_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)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes during object creation, after setting all properties.
function xylj_CreateFcn(hObject, eventdata, handles)
% hObject handle to xylj (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 xylj_Callback(hObject, eventdata, handles)
% hObject handle to xylj (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 xylj as text
% str2double(get(hObject,'String')) returns contents of xylj as a double
% --- Executes on button press in dqxy.
function dqxy_Callback(hObject, eventdata, handles)
% hObject handle to dqxy (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[Filename Pathname]=uigetfile({'*.txt'},'请选择文件'); %选择文件框函数
str=[Pathname Filename]; %得到路径和文件名
xinyuan=textread(str,'%s'); %以字符串的形式,读入文本文件
xinyuan=xinyuan{:}; %将cell型数据转换成char
set(handles.xylj,'string',str) %显示信源的路径
set(handles.xy,'string',xinyuan) %显示信源
% --- Executes during object creation, after setting all properties.
function xy_CreateFcn(hObject, eventdata, handles)
% hObject handle to xy (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 xy_Callback(hObject, eventdata, handles)
% hObject handle to xy (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 xy as text
% str2double(get(hObject,'String')) returns contents of xy as a double
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in bm.
function bm_Callback(hObject, eventdata, handles)
% hObject handle to bm (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clc; %清除command window
data=get(handles.xy,'String'); %读入信源
data=uint8(data); %将信源转化为uint8
[codeword_OK,simbolsout1,fout1,simbolsout2,fout2,zipped,info] = norm2huff(data); %编码
%排序前
for i=1:length(fout1)
fout1str{i}=num2str(fout1(i)); %将排序之前的概率转换成cell型数据,方便listbox使用
str1{i}=char(simbolsout1(i)-1); %将排序之前的码元转换成cell型数据,方便listbox使用,char将ASCII码转成字符
end
handles.str1=str1; %得到排序前码元
handles.fout1str=fout1str; %得到排序前概率
%排序后
for i=1:length(fout2)
str2{i}=char(simbolsout2(i)-1); %将排序之后的码元转换成cell型数据,方便listbox使用,char将ASCII码转成字符
end
handles.str2=str2; %得到排序后码元
%得到编码
for i=1:length(codeword_OK)
codestr{i}=num2str(double(codeword_OK{i}));
codestr{i}=codestr{i}(find(codestr{i}~=' ')); %去掉字符串里的空格,得到霍夫曼码
end
handles.codestr=codestr; %保存霍夫曼码
handles.zipped=zipped; %保存编码的结果
handles.info=info;
guidata(hObject, handles); %更新handles结构体
% --- Executes on button press in myzl.
function myzl_Callback(hObject, eventdata, handles)
% hObject handle to myzl (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.xsmyzl,'Value',1)
set(handles.xsmyzl,'String',handles.str1);
% --- Executes during object creation, after setting all properties.
function xsmyzl_CreateFcn(hObject, eventdata, handles)
% hObject handle to xsmyzl (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox 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
% --- Executes on selection change in xsmyzl.
function xsmyzl_Callback(hObject, eventdata, handles)
% hObject handle to xsmyzl (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns xsmyzl contents as cell array
% contents{get(hObject,'Value')} returns selected item from xsmyzl
error('input argument must be a uint8 vector')
end
%f=repmat(0,1,256);
% 扫描向量
len=length(vector);
for index=0:255; %注意向量的索引参数是从0开始的
f(index+1)=sum(vector==uint8(index));
end
%
3 仿真结果
4 参考文献
[1]吴记群, 李双科. Matlab下实现huffman编码[J]. 中国科技信息, 2006(19):2.
博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。
部分理论引用网络文献,若有侵权联系博主删除。
边栏推荐
- 分布式数据库主从配置(MySQL)
- In depth learning autumn recruitment interview questions collection (1)
- Reasons for the failure of web side automation test
- Design intelligent weighing system based on Huawei cloud IOT (STM32)
- LeetCode - 面试题17.24 最大子矩阵
- CMU15445 (Fall 2019) 之 Project#2 - Hash Table 详解
- sink 消费 到 MySQL, 数据库表里面已经设置了 自增主键, flink 里面,如何 操作?
- 【愚公系列】2022年7月 Go教学课程 005-变量
- SwiftUI Swift 内功之 Swift 中使用不透明类型的 5 个技巧
- [question] Compilation Principle
猜你喜欢
Enclosed please find. Net Maui's latest learning resources
聊聊SOC启动(十一) 内核初始化
.NET MAUI 性能提升
Use metersphere to keep your testing work efficient
SwiftUI 4 新功能之掌握 WeatherKit 和 Swift Charts
千人規模互聯網公司研發效能成功之路
Drive HC based on de2115 development board_ SR04 ultrasonic ranging module [source code attached]
[extraction des caractéristiques de texture] extraction des caractéristiques de texture de l'image LBP basée sur le mode binaire local de Matlab [y compris le code source de Matlab 1931]
18 basic introduction to divider separator component of fleet tutorial (tutorial includes source code)
【滤波跟踪】基于matlab捷联惯导仿真【含Matlab源码 1935期】
随机推荐
自律,提升自制力原来也有方法
Briefly introduce closures and some application scenarios
STM32 entry development NEC infrared protocol decoding (ultra low cost wireless transmission scheme)
There are ways to improve self-discipline and self-control
正在運行的Kubernetes集群想要調整Pod的網段地址
使用MeterSphere让你的测试工作持续高效
R语言使用quantile函数计算评分值的分位数(20%、40%、60%、80%)、使用逻辑操作符将对应的分位区间(quantile)编码为分类值生成新的字段、strsplit函数将学生的名和姓拆分
博客搬家到知乎
sink 消费 到 MySQL, 数据库表里面已经设置了 自增主键, flink 里面,如何 操作?
科普达人丨一文弄懂什么是云计算?
In my limited software testing experience, a full-time summary of automation testing experience
Distributed database master-slave configuration (MySQL)
C#中在路径前加@的作用
Flet教程之 15 GridView 基础入门(教程含源码)
MIF file format record
Complete collection of common error handling in MySQL installation
一度辍学的数学差生,获得今年菲尔兹奖
Nuclear boat (I): when "male mothers" come into reality, can the biotechnology revolution liberate women?
Have you ever met flick Oracle CDC, read a table without update operation, and read it repeatedly every ten seconds
'module 'object is not callable error