| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 727 人关注过本帖
标题:求高手指点。。。谢谢!!!
只看楼主 加入收藏
bruce_xia
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-6-16
收藏
 问题点数:0 回复次数:0 
求高手指点。。。谢谢!!!
我用的是matlab2008
以下是代码:程序将输入的向量进行哈夫曼编码,然后反编码,判断是否是无失真编码,后给出压缩前后的存储空间的比较
claear all
fprintf('Reading data...')
data=imread('cameraman.tif');
data=unit8(data);  %读入数据,并将数据限制为unit8
fprintf('Done!\n')
%编码压缩
fprintf('compressing data...');
[zipped,info]=norm2huff(data);
fprintf('Done!\n')
%解压缩
fprintf('compressing data...');
unzipped=huff2norm(zipped,info);
fprintf('Done!\n')
%测试是否无失真
isOK = isequal(data(:),unzipped(:))
%显示压缩效果
whos data zipped unzippped
%%%%%%%%%% norm2huff %%%%%%%%
function[zipped,info]=norm2huff(vector)
if ~isa(vector,'uint8'),
    error('input argument must be a unit8 vector')
end
vector=vector(:)';
%将输入向量转换为行向量
f=frequency(vector);
%计算各元素出现的概率
simbols=find(f~=0);
f=f(simbols);  %将元素按照出现的概率排列
[f,sortindex]=sort(f);
simbols=simbols(sortindex);
%产生码字 generate the codeword as the 52 bits of a double
len=length(simbols);
simbols_index=num2cell(1:len);
codeword_tmp=cell(len,1);
while length(f)>1,
    index1=simbols_index{1};
    index2=simbols_index{2};
    codeword_tmp(index1)=addnode(codeword_tmp(index1),uint8(0));
    codeword_tmp(index2)=addnode(codeword_tmp(index2),uint8(1));
    f=[sum(f(1:2)) f(3:end)];
    simbols_index=[{[index1 index2]} simbols_index(3:end)];
    %将数据重新排列,使两个节点的频率尽量与前一个节点的频率相当
    resort data in order to have the two nodes with lower frequency as
          first to
          [f,sortindex]=soer(f);
          simbols_index=simbols_index(sortindex);
end
%对应相应的元素与码字
codeword=cell(256:1);
codeword(simbols)=codeword_tmp;
%计算总的字符串长度
len=0;
for index=1:length(vector),
    len=len+length(codeword{double(vector(index))+1});
end
%长生01序列
string=repmat(unit8(0),1,len);
pointer=1;
for index=1:length(vector),
    code=codeword{double(vector(index))+1};
    len=length(code);
    string(pointer+(0:len-1))=code;
    pointer=pointer+len;
end
%如果需要,加零
len=length(string);
pad=8-mod(len,8);
if pad>0,
    string=[string unit8(zeros(1,pad))];
end
%保存实际有用的码字
codeword=cordword(simbols);
codelen=zeros(size(codeword));
weights=2.^(0:23);
maxcodelen=0;
for index=1:length(codeword),
    len=length(codeword{index});
    if len>maxcodelen,
        maxcodelen=len;
    end
    if len>0,
        code=sum(weights(codeword{index}==1));
        code=bitset(code,len+1);
        codeword{index}=code;
        codelen(index)=len;
    end
end
codeword=[codeword{:}];
%计算压缩后的向量
cols=length(string)/8;
strings=reshape(string,8,cols);
%存储一个稀疏矩阵
huffcodes=spares(1,1);  %init spares matrix
for index=1:numel(codeword),
    huffcodes(codeword(index),1)=simbols(index);
end
%产生信息结构体
info.pad=pad;
info.ratio=cols./length(vector);
info.length=length(vector);
info.maxcodelen=maxcodelen;
%%%%%%%%%%%%% addnode %%%%%%%%%%%%%
function codeword_new=addnode(codeword_old,item)
codeword_new=cell(size(codeword_old));
for index=1:length(codeword_old),
    codeword_new{index}=[item codeword_old{index}];
end
%%%%%%%%%%%% huff2norm %%%%%%%%%%%%%%%
function vector=huff2norm(zipped,info)
%HUFF2NORM Huffman 解码器
%HUFF2NORM(X,INFO)根据信息的结构体info返回向量zipped的解码结果
%矩阵参数一X(:)形式输入
if ~isa(zipped,'uint8'),
    error('input argument must be a unit8 vector')
end
%产生01序列
len=length(zipped);
bitindex=1:8;
for index=1:len,
    string(bitindex+8.*(index-1))=unit8(bitget(zipped(index),bitindex));
end
%调整字符串
string=logical(string(:)');  %make a row of it
len=length(string);
string((len-info.pad+1):end)=[]; %remove0 padding
len=length(string);
%解码
weights=2.^(0:51);
vector=remat(unit8(0),1,info.length);
vectorindex=1;
codeindex=1;
code=0;
for index=1:len,
    code=bitset(code,codeindex,string(index));
    codeindex=codeindex+1;
    byte=decode(bitset(code,codeindex),info);
    if byte>0,%
        vector(vectorindex)=byte-1;
        codeindex=1;
        code=0;
        vectorindex=vectorindex+1;
    end
end
%%%%%%%%%%%% decode %%%%%%%%%%%%
function byte=decode(code,info)
byte=info.huffcodes(code);
%%%%%%%%%%%%% frequency %%%%%%%%%%
function f=frequency(vector)
%FREQUENCY 计算元素出现概率
if ~isa(vector,'unit8'),
    error('input argument must be a unit8 vector')
end
f=repmat(0,1,256);
%扫描向量
len=length(vector);
for index=0:256,%
    f(index+1)=sum(vector==unit8(index));
end
%归一化
f=f./len;
whos


可是总是报错:
??? Error: File: huffman.m Line: 19 Column: 1
Function definitions are not permitted at the prompt or in scripts.

Error in ==> run at 57
          evalin('caller', [s ';']);
搜索更多相关主题的帖子: 空间 
2011-07-01 20:21
快速回复:求高手指点。。。谢谢!!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023893 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved