多媒体数据库实验2.docx
《多媒体数据库实验2.docx》由会员分享,可在线阅读,更多相关《多媒体数据库实验2.docx(14页珍藏版)》请在冰豆网上搜索。
多媒体数据库实验2
指标等级
A
B
C
D
功能完整
程序质量
按时检查
提问回答
检查时间
总评成绩
云南大学软件学院
实验报告
序号:
实验老师:
秦江龙
课程名称:
多媒体数据库系统设计实验实验名称:
多媒体数据压缩
学号:
姓名:
一、实验名称
多媒体数据压缩
二、实验目的
通过使用不同的数据压缩软件,熟悉不同的数据压缩标准的特性
三、实验内容(算法、程序、步骤和方法)
任务1:
3‐5类不同的多媒体数据类型(如:
文本、图像、音频、视频等),根据压缩率、压缩时长、CPU占用率、内存占用情况,评价常用压缩工作3-5款(如WinRAR,WinZip,Zip7等)请给出你的结论,哪一款压缩软件更适合压缩哪一类多媒体数据类型。
任务2:
请使用你熟悉的语言,实现如下程序:
对用户输入的任意英文字符串,先统计各英文字符出现的次数和概率,采用Huffman编码对字符串编码压缩,然后再解码还原输入字符串。
对比3‐5组测试数据,比较Huffman编码的码长和等长编码的码长。
说明Huffman编码的优点。
四、数据记录和计算
7-zip
WinRAR
WinZip
原始数据
视频(mp4)
视频(mp4)
视频(mp4)
压缩格式
7z
rar
zip
原文件大小
87.5M
87.5M
87.5M
压缩包大小
68.1M
68.5M
85.6M
压缩率
77.8%
78.3%
97.8%
CPU占用率
80%
84%
78%
内存占用
191012K
96744K
52668K
评价
7-zip对MP4视频的压缩率较低,CPU占用率居中,内存占用率较高,总体压缩效果较为理想。
WinRAR对MP4视频的压缩率居中,CPU占用率较高,内存占用率居中,总体压缩效果一般。
WinZip对MP4视频的压缩率较高,CPU占用率较低,内存占用率较低,总体压缩效果不太理想。
7-zip
WinRAR
WinZip
原始数据
文档(docx)
文档(docx)
文档(docx)
压缩格式
7z
rar
zip
原文件大小
12.02M
12.02M
12.02M
压缩包大小
11.2M
11.3M
12M
压缩率
93.2%
94.0%
99.8%
CPU占用率
71%
66%
84%
内存占用
139764K
96788K
42736K
评价
7-zip对docx文档的压缩率较低,CPU占用率居中,内存占用率较高,总体压缩效果较为理想。
WinRAR对docx文档的压缩率居中,CPU占用率较低,内存占用率居中,总体压缩效果一般。
WinZip对docx文档的压缩率较高,CPU占用率较高,内存占用率较低,总体压缩效果不太理想。
7-zip
WinRAR
WinZip
原始数据
图片(bmp)
图片(bmp)
图片(bmp)
压缩格式
7z
rar
zip
原文件大小
4.11M
4.11M
4.11M
压缩包大小
562K
728K
622K
压缩率
13.4%
17.3%
14.8%
CPU占用率
93%
87%
89%
内存占用
35572K
3940K
42288K
评价
7-zip对bmp图片的压缩率较低,CPU占用率较高,内存占用率较低,总体压缩效果较为理想。
WinRAR对bmp图片的压缩率较高,CPU占用率较低,内存占用率居中,总体压缩效果一般。
WinZip对bmp图片的压缩率居中,CPU占用率居中,内存占用率较高,总体压缩效果不太理想。
源代码
#include
#include
#include
#include
usingnamespacestd;
constintMAXSIZE=100;
typedefstructHuffmantree
{
charcname;
intweight,mark;
structHuffmantree*parent,*lchild,*rchild,*next;
}Hftree,*linktree;
linktreetidy_string(charch[])
{
inti=0;
linktreetree,ptr,beforeptr,node;
tree=(linktree)malloc(sizeof(Hftree));
if(!
tree)
returnNULL;
tree->next=NULL;
for(i=0;ch[i]!
='\0'&&ch[i]!
='\n';i++)
{
ptr=tree;
beforeptr=tree;
node=(linktree)malloc(sizeof(Hftree));
if(!
node)
returnNULL;
node->parent=NULL;
node->lchild=NULL;
node->rchild=NULL;
node->next=NULL;
node->mark=0;
node->cname=ch[i];
node->weight=1;
if(tree->next==NULL)
tree->next=node;
else
{
ptr=tree->next;
while(ptr&&ptr->cname!
=node->cname)
{
ptr=ptr->next;
beforeptr=beforeptr->next;
}
if(ptr&&ptr->cname==node->cname)
{
ptr->weight+=1;
free(node);
}
else
{
node->next=beforeptr->next;
beforeptr->next=node;
}
}
}
returntree;
}
linktreesquent_node(linktreetree)
{
linktreehead,ph,pt,beforeph;
head=(linktree)malloc(sizeof(Hftree));
if(!
head)
returnNULL;
head->next=NULL;
ph=head;
beforeph=head;
while(tree->next)
{
pt=tree->next;
tree->next=pt->next;
pt->next=NULL;
ph=head->next;
beforeph=head;
if(head->next==NULL)
head->next=pt;
else
{
while(ph&&ph->weightweight)
{
ph=ph->next;
beforeph=beforeph->next;
}
pt->next=beforeph->next;
beforeph->next=pt;
}
}
free(tree);
returnhead;
}
linktreecreateHtree(linktreetree)
{
linktreep,q,newnode,beforep;
for(p=tree->next,q=p->next;p!
=NULL&&q!
=NULL;p=tree->next,q=p->next)
{
tree->next=q->next;
q->next=NULL;
p->next=NULL;
newnode=(linktree)malloc(sizeof(Hftree));
if(!
newnode)
returnNULL;
newnode->next=NULL;
newnode->mark=0;
newnode->lchild=p;
newnode->rchild=q;
p->parent=newnode;
q->parent=newnode;
newnode->weight=p->weight+q->weight;
p=tree->next;
beforep=tree;
if(p!
=NULL&&p->weight>=newnode->weight)
{
newnode->next=beforep->next;
beforep->next=newnode;
}
else
{
while(p!
=NULL&&p->weightweight)
{
p=p->next;
beforep=beforep->next;
}
newnode->next=beforep->next;
beforep->next=newnode;
}
}
return(tree->next);
}
voidHuffman_Coding(linktreetree)
{
intindex=0;
char*code;
linktreeptr=tree;
code=(char*)malloc(10*sizeof(char));
printf("字符以及它的相应权数:
哈夫曼编码:
\n\n");
if(ptr==NULL)
{
printf("哈夫曼树是空的!
\n");
exit(0);
}
else
{
while(ptr->lchild&&ptr->rchild&&ptr->mark==0)
{
while(ptr->lchild&&ptr->lchild->mark==0)
{
code[index++]='0';
ptr=ptr->lchild;
if(!
ptr->lchild&&!
ptr->rchild)
{
ptr->mark=1;
code[index]='\0';
printf("\tw[%c]=%d\t\t\t",ptr->cname,ptr->weight);
for(index=0;code[index]!
='\0';index++)
printf("%c",code[index]);
printf("\n");
ptr=tree;
index=0;
}
}
if(ptr->rchild&&ptr->rchild->mark==0)
{
ptr=ptr->rchild;
code[index++]='1';
}
if(!
ptr->lchild&&!
ptr->rchild)
{
ptr->mark=1;
code[index++]='\0';
printf("\tw[%c]=%d\t\t\t",ptr->cname,ptr->weight);
for(index=0;code[index]!
='\0';index++)
printf("%c",code[index]);
printf("\n");
ptr=tree;
index=0;
}
if(ptr->lchild->mark==1&&ptr->rchild->mark==1)
{
ptr->mark=1;
ptr=tree;
index=0;
}
}
}
printf("\n");
free(code);
}
voidHuffamn_Decoding(linktreetree,charcode[])
{
inti=0,j=0;
char*char0_1;
linktreeptr=tree;
char0_1=(char*)malloc(10*sizeof(char));
cout<<"哈夫曼编码:
相应的字符:
\n\n";
for(j=0,ptr=tree;code[i]!
='\0'&&ptr->lchild&&ptr->rchild;j=0,ptr=tree)
{
for(j=0;code[i]!
='\0'&&ptr->lchild&&ptr->rchild;j++,i++)
{
if(code[i]=='0')
{
ptr=ptr->lchild;
char0_1[j]='0';
}
if(code[i]=='1')
{
ptr=ptr->rchild;
char0_1[j]='1';
}
}
if(!
ptr->lchild&&!
ptr->rchild)
{
char0_1[j]='\0';
for(j=0;char0_1[j]!
='\0';j++)
cout<printf("\t\t%c\n",ptr->cname);
}
if(code[i]=='\0'&&ptr->lchild&&ptr->rchild)
{
char0_1[j]='\0';
cout<<"没有与最后几个字符的0,1序列:
"<"<return;
}
}
free(char0_1);
}
voiddeletenode(linktreetree)
{
linktreeptr=tree;
if(ptr)
{
deletenode(ptr->lchild);
deletenode(ptr->rchild);
free(ptr);
}
}
intmain()
{
charstring[MAXSIZE],code[MAXSIZE];
linktreetemp,ht,htree,ptr=NULL;
cout<<"编码:
请输入当前要进行编码的字符串:
"<cin>>string;
cout<temp=tidy_string(string);
ht=squent_node(temp);
htree=createHtree(ht);
Huffman_Coding(htree);
cout<<"解码:
请输入要解码的0,1序列:
"<cin>>code;
cout<Huffamn_Decoding(htree,code);
deletenode(htree);
return0;
}
截图
五、结论(结果)
1.在对三种压缩软件的测试中发现,7-zip的压缩率较低,内存占用一般,但CPU占用较高,整体压缩效果最好;WinRAR压缩率居中,内存占用一般,CPU占用居中,整体压缩效果一般;WinZip压缩率最低,内存占用较高,CPU占用较高,整体压缩效果最差。
其中7-zip适合对视频等大文件进行压缩,WinRAR适合对文档等文件进行压缩,WinZip适合对图片等小文件进行压缩。
2.Huffman编码的码长是变化的,对于出现频率较高的信息,编码的长度较短;而对于出现频率较低的信息,编码的长度较长,这样,处理全部信息的总码长一定小于实际信息的符号长度。
Huffman编码的码长虽然是可变的,但却自带同步代码。
Huffman编码方法的编码效率比Shannon-Fano编码方法更高。
指导教师签名:
秦江龙