当前位置:网站首页>MATLAB實現Huffman編碼譯碼含GUI界面
MATLAB實現Huffman編碼譯碼含GUI界面
2022-07-07 11:49: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代碼問題可私信交流。
部分理論引用網絡文獻,若有侵權聯系博主删除。
边栏推荐
- [filter tracking] comparison between EKF and UKF based on MATLAB extended Kalman filter [including Matlab source code 1933]
- MySQL安装常见报错处理大全
- Verilog design responder [with source code]
- 【神经网络】卷积神经网络CNN【含Matlab源码 1932期】
- 聊聊SOC启动(十) 内核启动先导知识
- Enclosed please find. Net Maui's latest learning resources
- Poor math students who once dropped out of school won the fields award this year
- Software design - "high cohesion and low coupling"
- SwiftUI Swift 内功之 Swift 中使用不透明类型的 5 个技巧
- 核舟记(一):当“男妈妈”走进现实,生物科技革命能解放女性吗?
猜你喜欢
Drive HC based on de2115 development board_ SR04 ultrasonic ranging module [source code attached]
About how to install mysql8.0 on the cloud server (Tencent cloud here) and enable local remote connection
Zhou Yajin, a top safety scholar of Zhejiang University, is a curiosity driven activist
超标量处理器设计 姚永斌 第9章 指令执行 摘录
一起探索云服务之云数据库
The Oracle message permission under the local Navicat connection liunx is insufficient
[filter tracking] comparison between EKF and UKF based on MATLAB extended Kalman filter [including Matlab source code 1933]
In SQL, I want to set foreign keys. Why is this problem
Automated testing framework
Camera calibration (2): summary of monocular camera calibration
随机推荐
【愚公系列】2022年7月 Go教学课程 005-变量
软件内部的定时炸弹:0-Day Log4Shell只是冰山一角
There are ways to improve self-discipline and self-control
Half of the people don't know the difference between for and foreach???
【滤波跟踪】基于matlab扩展卡尔曼滤波EKF和无迹卡尔曼滤波UKF比较【含Matlab源码 1933期】
MIF file format record
《论文阅读》Neural Approaches to Conversational AI(1)
QT implements the delete method of the container
[texture feature extraction] LBP image texture feature extraction based on MATLAB local binary mode [including Matlab source code 1931]
R語言使用magick包的image_mosaic函數和image_flatten函數把多張圖片堆疊在一起形成堆疊組合圖像(Stack layers on top of each other)
STM32 entry development NEC infrared protocol decoding (ultra low cost wireless transmission scheme)
There are so many factors that imprison you
正在運行的Kubernetes集群想要調整Pod的網段地址
【数据聚类】基于多元宇宙优化DBSCAN实现数据聚类分析附matlab代码
LeetCode - 面试题17.24 最大子矩阵
【最短路】ACwing 1127. 香甜的黄油(堆优化的dijsktra或spfa)
【最短路】Acwing1128信使:floyd最短路
Talk about SOC startup (VI) uboot startup process II
Reasons for the failure of web side automation test
[shortest circuit] acwing 1127 Sweet butter (heap optimized dijsktra or SPFA)