霍夫曼编码代码matlab,matlab 实现霍夫曼编码

编码内容包括:用matlab实现霍夫曼编码,并且求出相应的信源熵,平均码长,和编码效率。

以下是代码:

%霍夫曼编码

%huffman_code

%编码思路:根据位置矩阵的变化过程反推生成霍夫曼编码

n=input('请输入编码个数:\n');

c=[];

% for i=1:n

%     fprintf('请输入第%d个编码:\n',i);

%     c(i)=input('');

% end

c=rand(1,n);%********随机生成数据********************

c=c/sum(c);

p=c;

disp(p);

w=[;];%***************位置矩阵,记录数据的变化过程*****

for i=1:n-1

[p,l]=sort(p);

w(i,:)=[l(1:n-i+1),zeros(1,i-1)];

p=[p(1)+p(2),p(3:n),1];

end

a={;};%**********字符数组,生成霍夫曼码****************

for i=1;2*(n-1)

for j=1:n

a{i,j}=' ';

end

end%*****************初始化*********************

a{2*n-2,1}='0';

a{2*n-2,2}='1';

for i=1:n-1

for j=1:i+1

a{2*(n-i)-1,j}=a{2*(n-i),find(w(n-i,:)==j)};

end

if((n-i-1)<=0)

break;

end

a{2*(n-i-1),1}=strcat(a{2*(n-i)-1,1},'0');

a{2*(n-i-1),2}=strcat(a{2*(n-i)-1,1},'1');

for j=3:i+2

a{2*(n-i-1),j}=a{2*(n-i)-1,j-1};

end

end

h=-c.*log2(c);

hx=sum(h);

for i=1:n

len(i)=length(a{1,i});

end

plen=len.*c;

averagelen=sum(plen);

fprintf('霍夫曼编码为:\n');

% disp(a);

for i=1:n

disp(a(1,i));

end

fprintf('平均码长为:\n');

disp(averagelen);

fprintf('信源熵为:\n');

disp(hx);

fprintf('编码效率为:\n');

disp(hx/averagelen);

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Matlab实现霍夫曼编码代码: ```matlab function [dict, avglen] = huffmandict(symbols, prob) % HUFFMANDICT Create Huffman code dictionary. % [DICT,AVGLEN] = HUFFMANDICT(SYMBOLS,PROB) returns a Huffman code % dictionary, DICT, for the given input symbols and their probabilities, % PROB. DICT is a cell array of size 2-by-N, where N is the number of % input symbols. The first row of DICT contains the input symbols, and % the second row contains the corresponding Huffman codewords. AVGLEN % is the average length of the Huffman codewords. % % Class support for inputs SYMBOLS and PROB: % float: double, single % % Example: % symbols = {'A', 'B', 'C', 'D', 'E'}; % prob = [0.35, 0.15, 0.2, 0.05, 0.25]; % [dict, avglen] = huffmandict(symbols, prob); % % See also HUFFMANENCODE, HUFFMANDECODE. % Copyright 2011-2017 The MathWorks, Inc. % Validate input arguments narginchk(2, 2); validateattributes(symbols, {'cell'}, {'vector'}, mfilename, 'SYMBOLS'); validateattributes(prob, {'numeric'}, {'vector', 'real', 'nonnegative', '<=', 1}, mfilename, 'PROB'); % Sort symbols and probabilities in decreasing order of probability [prob, sortIndex] = sort(prob, 'descend'); symbols = symbols(sortIndex); % Create initial cell array of nodes n = numel(prob); nodes = cell(n, 1); for i = 1:n nodes{i} = i; end % Build Huffman tree while numel(nodes) > 1 % Combine two nodes with smallest probability newProb = prob(end-1) + prob(end); nodes{end+1} = [nodes{end-1}, nodes{end}]; prob(end-1:end) = []; prob(end+1) = newProb; % Sort nodes and probabilities in decreasing order of probability [prob, sortIndex] = sort(prob, 'descend'); nodes = nodes(sortIndex); end % Traverse Huffman tree to generate codewords dict = cell(2, n); traverseHuffmanTree(nodes{1}, '', dict); % Sort codewords by input symbols [~, sortIndex] = sort(sortIndex); dict = dict(:, sortIndex); % Calculate average codeword length avglen = sum(prob .* cellfun('length', dict(2,:))); end function traverseHuffmanTree(node, prefix, dict) % Traverse Huffman tree to generate codewords if isscalar(node) dict{1, node} = node; dict{2, node} = prefix; else traverseHuffmanTree(node(1), [prefix, '0'], dict); traverseHuffmanTree(node(2), [prefix, '1'], dict); end end ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值