📄 hufflen.m
字号:
function HL = HuffLen(S)
% HuffLen Find the lengths of the Huffman code words
% Based on probability (or number of occurences) of each symbol
% the length for the Huffman codewords are calculated.
%
% HL = hufflen(S);
% ------------------------------------------------------------------% Arguments:
% S a vector with number of occurences or probability of each symbol
% Only positive elements of S are used, zero (or negative)
% elements get length 0.
% HL length (bits) for the codeword for each symbol
% ------------------------------------------------------------------
% Example:
% hufflen([1,0,4,2,0,1]) => ans = [3,0,1,2,0,3]% hufflen([10,40,20,10]) => ans = [3,1,2,3]
%----------------------------------------------------------------------
% Copyright (c) 1999. Karl Skretting. All rights reserved.
% Hogskolen in Stavanger (Stavanger University), Signal Processing Group
% Mail: karl.skretting@tn.his.no Homepage: http://www.ux.his.no/~karlsk/
%
% HISTORY:
% Ver. 1.0 28.08.98 KS: Function made as part of Signal Compression Project 98
% Ver. 1.1 25.12.98 English version of program
% Ver. 1.2 28.07.99 Problem when length(S)==1 was corrected
% Ver. 1.3 22.06.00 KS: Some more exceptions handled
%----------------------------------------------------------------------
if nargin<1
error('HuffLen: see help.')
end
% some checks and exceptions
if (length(S)==0) % ver 1.2
warning('HuffLen: Symbol sequence is empty.'); % a warning is appropriate
HL=0;
return;
end
I=find(S<0);
S(I)=0;
if (sum(S)==max(S))
disp('HuffLen: Only one symbol.'); % a message is appropriate
HL=zeros(size(S)); % no Huffman code is needed
return;
end
% Algorithm "explained" in Norwegian:
% En bygger opp "treet" ved
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -