⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tree_code2.m

📁 ) Compression using huffman code -with a number of bits k per code word -provide huffma
💻 M
字号:
%to generate the code

function y = tree_code2(x)
k=length(x);   %sums the probabilities such that sum of probabilities =1
j=sort(x);
l=fliplr(j);
y(1,:)=l;
index=k;
for i=2:k %Puts the variables/probabilities through the Huffman process
      l(index-1)=l(index)+l(index-1);
      l(index)=0;
      j=sort(l);
      y(i,:)=fliplr(j);
      index=index-1;
end
i=k-1;
index=2;
code(i,index)=1;
code(i,index-1)=0;
while i>1 %This loop goes through the different paths of the tree and generates the Huffman codes in decimal (base-10)
    i=i-1;
    index=index+1;
    [r,c]=find(y(i+1,:)==y(i,index)+y(i,index-1));
    code(i,index)=2*code(i+1,c)+1;
    code(i,index-1)=2*code(i+1,c);
    for subindex=1:(index-2)
        [r,c]=find(y(i+1,:)==y(i,subindex));
        code(i,subindex)=code(i+1,c);
    end
end
words=dec2bin(code(1,:)); % Converts the codes from decimal (base-10) to binary
for i=1:k % This loop matches the code word with the variables and prints the output
    [r,c]=find(y(1,:)==x(i));
    fprintf('x%g is %s\n',i,words(c,:));
end
y=y';

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -