【媒体信号处理】No.1 哈夫曼编码

来源:互联网 发布:数据库的导入和导出 编辑:程序博客网 时间:2024/05/21 21:43

第一次用matlab啥也不会= =
差不多全是抄助教的代码(躺

实验内容:
– 随机生成一个离散信号序列
– 计算该信源的熵
– 利用matlab内嵌函数进行huffman编码
– 利用matlab内嵌函数进行huffman解码

calculate_source_entropy.m】function Hs=calculate_source_entropy(probs)    %create a functionmps=1e-7;probs(probs==0)=mps;    %avoid zero valueif min(probs(:))<=0       %avoid value that is less than zero    error('some probability value is less than zero!');endif sum(probs)~=1    probs=probs/sum(probs);   %calculate the frequencyendHs=sum(probs.*log(1./probs));     %calculate the frequencyentropyenddemo_call_matlab_huffman.m】function demo_call_matlab_huffmanclc;       %clear the screendigits=0:9;     %distinct digit symbols that the data source can produceprobs=rand(1,10);     %set the probability for each symbols randomlyprobs=probs./sum(probs);   %noromalize the sum to 1digitSeq=randsrc(500,1,[digits;probs]);   %create digit sequenceHs=calculate_source_entropy(probs);[dict,avglen]=huffmandict(digits,probs);  %create dictionarycomp=huffmanenco(digitSeq,dict);     %Encode the sequencedeDigitSeq=huffmandeco(comp,dict);     %Decode the sequencefigure;     %create a windowsubplot(3,1,1),plot(digitSeq);title('The original data');    %show the result in the new windowsubplot(3,1,2),plot(deDigitSeq);title('The decoded resule');subplot(3,1,3),plot(deDigitSeq-digitSeq);title('The difference value');end
0 0
原创粉丝点击