LZSS压缩算法实验报告

合集下载

实验二材料力学压缩实验报告

实验二材料力学压缩实验报告

金属材料压缩实验一、实验目的1.测定低碳钢压缩时的下屈服强度R eL(或屈服极限σs);2.测定铸铁压缩时的抗压强度R m(或抗压强度极限σb);3.观察并比较低碳钢和铸铁在压缩时的缩短变形和破坏现象。

二、预习思考要点1.用短圆柱状低碳钢和铸铁试样做压缩实验时,怎样才能做到使其轴向(心)受压?放置压缩试样的支承垫板底部为什么制作成球形?2.圆柱状低碳钢试样被压缩成饼状而不破碎,而圆柱状铸铁试样被压破裂面常发生在与轴线大致成45°~55°方向上,二者的变形特征与破坏形式为什么不同?三、实验仪器和设备1.万能材料试验机;2.游标卡尺。

四、实验试样对于低碳钢和铸铁类金属材料,按照GB 7314—1987《金属压缩试验方法》的规定,金属材料的压缩试样多采用圆柱体如图1-9所示。

试样的长度L一般为直径d的2.5~3.5倍,其直径d = 10mm~20mm。

也可采用正方形柱体试样如图1-10所示。

要求试样端面应尽量光滑,以减小摩阻力对横向变形的影响。

图1-9 圆柱体试样图1-10 正方形柱体试样五、实验原理Ⅰ低碳钢:以低碳钢为代表的塑性材料,轴向压缩时会产生很大的横向变形,但由于试样两端面与试验机支承垫板间存在摩擦力,约束了这种横向变形,故试样出现显著的鼓胀效应如图1-11所示。

为了减小鼓胀效应的影响,通常的做法是除了将试样端面制作得光滑以外,还可在端面涂上润滑剂以利最大限度地减小摩擦力。

低碳钢试样的压缩曲线如图1-12所示,由于试样越压越扁,则横截面面积不断增大,试样抗压能力也随之提高,故曲线是持续上升为很陡的曲线。

从压缩曲线上可看出,塑性材料受压时在弹性阶段的比例极限、弹性模量和屈服阶段的屈服点(下屈服强度)同拉伸时是相同的。

但压缩试验过程中到达屈服阶段时不像拉伸试验时那样明显,因此要认真仔细观察才能确定屈服荷载F eL,从而得到压缩时的屈服点强度(或下屈服强度)R eL= F eL/S0。

压缩实验报告数据分析

压缩实验报告数据分析

一、实验背景压缩实验是一种常见的力学实验,通过在特定的实验条件下对材料进行压缩,研究其力学性能。

本次实验主要针对某一种材料进行压缩实验,以了解其压缩性能。

本报告将对实验数据进行详细分析,得出实验结果。

二、实验目的1. 研究材料在不同压力下的变形情况;2. 了解材料的弹性模量和屈服强度;3. 分析材料在不同压力下的力学性能。

三、实验原理压缩实验通常采用单轴压缩实验,即在轴向施加压力,使材料发生压缩变形。

根据胡克定律,材料的应力与应变之间存在线性关系,即应力=弹性模量×应变。

当材料达到屈服强度时,应力与应变之间的关系将不再线性,此时材料将发生塑性变形。

四、实验方法1. 实验材料:选取某一种材料作为实验对象;2. 实验设备:压缩试验机;3. 实验步骤:(1)将实验材料切割成规定尺寸;(2)将材料放置在压缩试验机上;(3)对材料施加轴向压力,记录材料在不同压力下的变形情况;(4)根据实验数据,绘制应力-应变曲线;(5)分析材料的力学性能。

五、实验数据及分析1. 实验数据表1:实验数据压力(MPa)应变(%)应力(MPa)0 0 010 0.5 2020 1.0 4030 1.5 6040 2.0 8050 2.5 1002. 数据分析(1)线性阶段:从表1中可以看出,在压力0-30MPa范围内,材料的应力与应变呈线性关系,弹性模量E=40MPa。

这说明材料在该压力范围内具有良好的弹性性能。

(2)非线性阶段:当压力超过30MPa时,应力与应变之间的关系不再线性,材料开始发生塑性变形。

此时,材料的屈服强度约为100MPa。

(3)应力-应变曲线:根据实验数据,绘制应力-应变曲线,如图1所示。

曲线在压力0-30MPa范围内呈线性,压力超过30MPa后,曲线出现拐点,表明材料开始发生塑性变形。

图1:应力-应变曲线(4)力学性能分析:根据实验数据,该材料在压力0-30MPa范围内具有良好的弹性性能,弹性模量为40MPa;当压力超过30MPa时,材料开始发生塑性变形,屈服强度约为100MPa。

lzss压缩标准

lzss压缩标准

lzss压缩标准LZSS压缩标准是一种常见的无损压缩算法,广泛应用于数据压缩和存储领域。

它由Lempel和Ziv于1977年首次提出,并在此后得到不断改进和优化。

本文将介绍LZSS压缩标准的基本原理、算法实现以及应用领域。

一、基本原理LZSS压缩标准基于滑动窗口的概念,将待压缩的数据分为两部分:查找缓冲区(Lookahead Buffer)和搜索缓冲区(Search Buffer)。

其中,查找缓冲区用于存储已经压缩的数据,搜索缓冲区用于匹配和压缩新的数据。

LZSS压缩标准的基本思想是尽可能利用已经压缩的数据来替代重复出现的新数据。

具体过程如下:从搜索缓冲区中找到与查找缓冲区中的数据最长匹配的字符串,将该字符串的位置和长度作为压缩码输出。

如果没有找到匹配的字符串,则直接将当前字符输出。

二、算法实现LZSS压缩标准的算法实现相对简单,主要包括两个过程:压缩和解压缩。

1. 压缩过程在压缩过程中,首先初始化滑动窗口的大小和查找缓冲区的大小。

然后,按照基本原理描述的方法,逐个字符地进行压缩编码。

最后,将压缩码和原始数据一同保存,以便后续的解压缩过程。

2. 解压缩过程在解压缩过程中,首先读入之前保存的压缩码和原始数据。

然后,根据压缩码进行解码操作,将压缩码表示的字符串写入输出缓冲区。

最后,输出解码后的数据。

三、应用领域LZSS压缩标准在各个领域都有广泛的应用。

1. 数据压缩LZSS压缩标准可以将大量的数据进行高效压缩,节省存储空间。

它被广泛应用于文件压缩、图像压缩、音频压缩等领域。

2. 数据传输LZSS压缩标准可以在数据传输过程中减少数据的传输量,提高传输效率。

它被应用于网络传输、数据备份等场景。

3. 嵌入式系统LZSS压缩标准对内存空间的利用非常高效,适用于资源有限的嵌入式系统。

它被广泛应用于嵌入式系统的固件压缩和存储。

四、总结LZSS压缩标准是一种经典的无损压缩算法,具有简单高效、广泛应用等特点。

它通过利用已经压缩的数据来替代重复出现的新数据,从而实现数据的高效压缩。

铝的压缩实验报告

铝的压缩实验报告

一、实验目的1. 研究铝在压缩过程中的应力-应变关系;2. 探究铝的压缩强度;3. 分析铝的压缩变形特征。

二、实验原理1. 压缩试验:压缩试验是一种力学性能试验,通过测量材料在压缩过程中的应力-应变关系,可以确定材料的压缩强度、弹性模量等力学性能指标。

2. 应力-应变关系:在压缩过程中,材料内部的应力与应变之间存在一定的关系,通常用应力-应变曲线表示。

三、实验设备1. 拉伸试验机:用于施加压缩载荷;2. 引伸计:用于测量材料的应变;3. 电子秤:用于测量材料的质量;4. 专用压缩试样夹具:用于固定试样;5. 精密游标卡尺:用于测量试样的尺寸。

四、实验材料实验材料为纯铝,规格为φ10mm×15mm。

五、实验步骤1. 将试样清洗干净,用精密游标卡尺测量其直径和长度,并计算截面积;2. 将试样放入专用压缩试样夹具中,确保试样与夹具接触良好;3. 将试样夹具安装在拉伸试验机上,并调整试验机的加载速度;4. 开启试验机,对试样施加压缩载荷,同时启动引伸计记录应变值;5. 当试样发生断裂时,停止加载,记录最大载荷值;6. 根据实验数据,绘制应力-应变曲线。

六、实验结果与分析1. 应力-应变曲线:根据实验数据,绘制铝的应力-应变曲线,如图1所示。

图1 铝的应力-应变曲线从图中可以看出,铝在压缩过程中的应力-应变关系呈非线性,可分为三个阶段:(1)弹性阶段:在应力较低时,铝的应力-应变关系近似线性,说明铝具有良好的弹性;(2)屈服阶段:随着应力的增加,铝的应力-应变关系出现拐点,进入屈服阶段。

此时,铝的变形主要表现为塑性变形;(3)断裂阶段:在屈服阶段之后,铝的应力-应变关系继续增加,直至试样发生断裂。

2. 压缩强度:根据实验数据,计算铝的压缩强度为:σc = Fmax / A = 123.45 MPa其中,Fmax为试样断裂时的最大载荷,A为试样截面积。

3. 压缩变形特征:从实验结果可以看出,铝在压缩过程中的变形主要表现为塑性变形,且随着应力的增加,变形量逐渐增大。

压缩实验报告 材料力学

压缩实验报告 材料力学

压缩实验报告材料力学压缩实验报告材料力学引言材料力学是研究材料在外力作用下的变形和破坏行为的学科。

在材料力学中,压缩实验是一种常用的实验方法,用于研究材料在压力作用下的性能和行为。

本文将对压缩实验进行详细分析和报告。

实验目的本次实验的目的是通过对不同材料的压缩实验,探究材料在压力下的变形特性和破坏行为,进一步了解材料的力学性质。

实验装置和方法本次实验使用了一台万能试验机,配备了压力传感器和位移传感器。

首先,选择不同材料的样品,如金属、塑料和木材等。

然后,将样品放置在试验机的压力板上,并调整试验机的参数,如压力速度和试验温度等。

最后,开始进行压缩实验,记录压力和位移的变化。

实验结果和分析通过对不同材料的压缩实验,我们得到了一系列的实验数据。

以金属样品为例,我们观察到在开始施加压力时,金属样品发生弹性变形,即在去除压力后能够恢复原状。

然而,当压力继续增加时,金属样品开始发生塑性变形,即在去除压力后无法完全恢复原状。

随着压力的增加,金属样品最终达到破坏点,发生破裂。

相比之下,塑料样品在压力作用下呈现出更大的变形。

塑料样品在开始施加压力时立即发生塑性变形,并且在去除压力后无法恢复原状。

塑料样品的破坏点较低,容易发生破裂。

而木材样品的行为与金属和塑料有所不同。

木材样品在开始施加压力时,发生较小的弹性变形。

然而,随着压力的增加,木材样品开始发生塑性变形,并且在去除压力后能够部分恢复原状。

木材样品的破坏点较高,较难发生破裂。

结论通过本次压缩实验,我们可以得出以下结论:1. 不同材料在压力作用下表现出不同的变形特性和破坏行为。

2. 金属样品在压力下发生弹性变形和塑性变形,破坏点较高。

3. 塑料样品在压力下发生塑性变形,破坏点较低。

4. 木材样品在压力下发生弹性变形和塑性变形,破坏点较高。

进一步研究在今后的研究中,我们可以进一步探究不同材料的压缩实验,并研究不同参数对材料性能的影响。

例如,可以研究不同温度下材料的压缩行为,以及不同压力速度对材料的影响。

压缩试验实验报告总结

压缩试验实验报告总结

压缩试验实验报告总结1. 引言压缩试验是材料试验中常用的一种方法,用于评估材料的强度和稳定性。

本次实验旨在通过压缩试验了解材料的力学性质,并分析压缩试验数据。

2. 实验目的- 了解压缩试验的原理和方法;- 分析材料的应力-应变关系;- 计算材料的弹性模量和极限抗压强度;- 掌握实验数据处理和报告撰写方法。

3. 实验装置和方法本次实验使用了电子万能试验机和压缩试验样品。

具体的实验过程如下:1. 制备压缩试验样品,保证样品的尺寸和形状符合要求;2. 将样品安装在电子万能试验机上,并调整仪器参数;3. 开始压缩试验,按照一定速度施加外力,记录载荷和位移数据;4. 在不同载荷下,记录对应的位移数据,得到应力-应变曲线;5. 统计实验数据,计算材料的弹性模量和极限抗压强度。

4. 实验结果通过实验数据的处理和分析,得到了应力-应变曲线,并计算了材料的弹性模量和极限抗压强度。

实验结果如下:- 应力-应变曲线呈现一定的线性关系,表明材料在一定范围内具有良好的弹性行为;- 弹性模量为XXX GPa,表示了材料在弹性阶段内的刚度;- 极限抗压强度为XXX MPa,反映了材料抵抗压缩破坏的能力。

5. 结果分析根据实验结果,我们可以得出以下结论:- 弹性模量反映了材料的刚度,数值越大表示材料越难以发生形变;- 极限抗压强度代表了材料的抗压性能,是材料在压缩荷载下的破坏极限。

同时,我们还发现了实验中的一些问题:- 样品制备的误差可能会对压缩试验结果产生一定的影响;- 实验过程中的外界因素(如温度、湿度等)也可能会对实验结果产生影响。

6. 结论与建议通过本次实验,我们成功地进行了压缩试验,并得到了相关的数据和结果。

基于实验结果的分析,我们可以得出以下结论:- 材料具有一定的弹性,能够在一定范围内恢复形状;- 材料具有一定的承载能力,能够抵抗一定的压缩荷载。

为了提高压缩试验的准确性,我们建议在今后的实验中注意以下几点:- 加强样品制备的规范性和标准化;- 提高实验过程中外界条件的控制;- 进一步扩大样品数量和变化条件的范围,以提高实验结果的统计可靠性。

LZSS压缩算法C语言实现

LZSS压缩算法C语言实现

/*----------------------------------------------------------------------------  Data Compression Program LZSS.C -- A Data Compression Program 4/6/1989 Haruhiko Okumura Use, distribute, and modify this program freely. Please send me your improved versions. PC-V AN SCIENCE PAF01022 NIFTY-Serve -Serve PAF01022 CompuServe 74050,1022 Some changes made June, 2003 by Chris Giese <*****************>, /~geezer - Changed F from 16 to 18, for compatability with Microsoft COMPRESS/EXPAND EXPAND.EXE is on the install disks for MS-DOS version 6 and Windows 3.1 COMPRESS.EXE is in the Win 3.1 SDK, and comes with Borland C++ 3.1 - Changed compress/expand chars on command line from e/d to c/u - Where possible and correct, changed ints to unsigned - Made all functions static - Changed formatting, indenting, global variable names, and function names - Tried to simplify/clarify code in some areas ----------------------------------------------------------------------------*/ #include <stdlib.h> #include <string.h> #include <ctype.h> #include <stdio.h> /* size of ring buffer */ #define N 4096 /* index for root of binary search trees */ #define NIL N /* upper limit for g_match_len. Changed from 18 to 16 for binary compatability with Microsoft COMPRESS.EXE and EXPAND.EXE #define F 18 */ #define F 16 /* encode string into position and length if match_length is greater than this: */ #define THRESHOLD 2/* these assume little-endian CPU like Intel x86 -- need byte-swap function for big endian CPU */ #define READ_LE32(X) *(uint32_t *)(X) #define WRITE_LE32(X,Y) *(uint32_t *)(X) = (Y) /* this assumes sizeof(long)==4 */ typedef unsigned long uint32_t; /* text (input) size counter, code (output) size counter, and counter for reporting progress every 1K bytes */ static unsigned long g_text_size, g_code_size, g_print_count; /* ring buffer of size N, with extra F-1 bytes to facilitate string comparison */ static unsigned char g_ring_buffer[N + F - 1]; /* position and length of longest match; set by insert_node() */ static unsigned g_match_pos, g_match_len; /* left & right children & parent -- these constitute binary search tree */ static unsigned g_left_child[N + 1], g_right_child[N + 257], g_parent[N + 1]; /* input & output files */ static FILE *g_infile, *g_outfile; /***************************************************************************** initialize trees *****************************************************************************/ static void init_tree(void) { unsigned i; /* For i = 0 to N - 1, g_right_child[i] and g_left_child[i] will be the right and left children of node i. These nodes need not be initialized.These are initialized to Also, g_parent[i] is the parent of node i. NIL (= N), which stands for 'not used.' For i = 0 to 255, g_right_child[N + i + 1] is the root of the tree for strings that begin with character i. These are initializedto NIL. Note there are 256 trees. */ for(i = N + 1; i <= N + 256; i++) g_right_child[i] = NIL; for(i = 0; i < N; i++) g_parent[i] = NIL; } /***************************************************************************** Inserts string of length F, g_ring_buffer[r..r+F-1], into one of the trees (g_ring_buffer[r]'th tree) and returns the longest-match position and length via the global variables g_match_pos and g_match_len. If g_match_len = F, then removes the old node in favor of the new one, because the old one will be deleted sooner. Note r plays double role, as tree node and position in buffer. *****************************************************************************/ static void insert_node(int r) { unsigned char *key; unsigned i, p; int cmp; cmp = 1; key = &g_ring_buffer[r]; p = N + 1 + key[0]; g_right_child[r] = g_left_child[r] = NIL; g_match_len = 0; while(1) { if(cmp >= 0) { if(g_right_child[p] != NIL) p = g_right_child[p]; else { g_right_child[p] = r; g_parent[r] = p; return; } } else { if(g_left_child[p] != NIL) p = g_left_child[p]; else { g_left_child[p] = r; g_parent[r] = p; return; } } for(i = 1; i < F; i++) { cmp = key[i] - g_ring_buffer[p + i]; if(cmp != 0) break; } if(i > g_match_len) { g_match_pos = p; g_match_len = i; if(g_match_len >= F) break; } } g_parent[r] = g_parent[p]; g_left_child[r] = g_left_child[p]; g_right_child[r] = g_right_child[p]; g_parent[g_left_child[p]] = r; g_parent[g_right_child[p]] = r; if(g_right_child[g_parent[p]] == p) g_right_child[g_parent[p]] = r; else g_left_child[g_parent[p]] = r; g_parent[p] = NIL; /* remove p */ } /***************************************************************************** deletes node p from tree *****************************************************************************/ static void delete_node(unsigned p) { unsigned q; if(g_parent[p] == NIL) return; /* not in tree */ if(g_right_child[p] == NIL) q = g_left_child[p]; else if(g_left_child[p] == NIL) q = g_right_child[p]; else { q = g_left_child[p]; if(g_right_child[q] != NIL) { do q = g_right_child[q]; while(g_right_child[q] != NIL); g_right_child[g_parent[q]] = g_left_child[q]; g_parent[g_left_child[q]] = g_parent[q]; g_left_child[q] = g_left_child[p]; g_parent[g_left_child[p]] = q; } g_right_child[q] = g_right_child[p]; g_parent[g_right_child[p]] = q; } g_parent[q] = g_parent[p]; if(g_right_child[g_parent[p]] == p) g_right_child[g_parent[p]] = q; else g_left_child[g_parent[p]] = q; g_parent[p] = NIL; } /***************************************************************************** *****************************************************************************/ static void compress(void) { unsigned i, len, r, s, last_match_length, code_buf_ptr; unsigned char code_buf[17], mask; int c; init_tree(); /* initialize trees */ /* code_buf[1..16] saves eight units of code, and code_buf[0] works as eight flags, "1" representing that the unit is an unencoded letter (1 byte), "0" a position-and-length pair (2 bytes). Thus, eight units require at most 16 bytes of code. */ code_buf[0] = 0; code_buf_ptr = mask = 1; s = 0; r = N - F; /* Clear the buffer with any character that will appear often. */ memset(g_ring_buffer + s, ' ', r - s); /* Read F bytes into the last F bytes of the buffer */ for(len = 0; len < F; len++) { c = getc(g_infile); if(c == EOF) break; g_ring_buffer[r + len] = c; } g_text_size = len; if(g_text_size == 0) /* text of size zero */ return; /* Insert the F strings, each of which begins with one or more 'space' characters. Note the order in which these strings are inserted. This way, degenerate trees will be less likely to occur. */ for(i = 1; i <= F; i++) insert_node(r - i); /* Finally, insert the whole string just read. The global variables g_match_len and g_match_pos are set. */ insert_node(r); do { /* g_match_len may be spuriously long near the end of text. */ if(g_match_len > len) g_match_len = len; /* Not long enough match. Send one byte. */ if(g_match_len <= THRESHOLD){ g_match_len = 1; code_buf[0] |= mask; /* 'send one byte' flag */ code_buf[code_buf_ptr] = g_ring_buffer[r]; /* Send uncoded. */ code_buf_ptr++; /* Send position and length pair. Note g_match_len > THRESHOLD. */ } else { code_buf[code_buf_ptr] = (unsigned char)g_match_pos; code_buf_ptr++; code_buf[code_buf_ptr] = (unsigned char) (((g_match_pos >> 4) & 0xF0) | (g_match_len - (THRESHOLD + 1))); code_buf_ptr++; } /* Shift mask left one bit. */ mask <<= 1; if(mask == 0) { /* Send at most 8 units of code together */ for(i = 0; i < code_buf_ptr; i++) putc(code_buf[i], g_outfile); g_code_size += code_buf_ptr; code_buf[0] = 0; code_buf_ptr = mask = 1; } last_match_length = g_match_len; for(i = 0; i < last_match_length; i++) { c = getc(g_infile); if(c == EOF) break; /* Delete old strings and read new bytes */ delete_node(s); g_ring_buffer[s] = c; /* If the position is near the end of buffer, extend the buffer to make string comparison easier. */ if(s < F - 1) g_ring_buffer[s + N] = c; /* Since this is a ring buffer, increment the position modulo N. */ s = (s + 1) & (N - 1); r = (r + 1) & (N - 1); /* Register the string in g_ring_buffer[r..r+F-1] */ insert_node(r); } /* Reports progress each time the g_text_size exceeds multiples of 1024. */ g_text_size += i; if(g_text_size > g_print_count) { printf("%12ld\r", g_text_size); g_print_count += 1024; } while(i++ < last_match_length)/* After the end of text, */ { delete_node(s); /* no need to read, but */ s = (s + 1) & (N - 1); r = (r + 1) & (N - 1); len--; if(len) . */ insert_node(r);/* buffer may not be empty} } while(len > 0); /* until length of string to be processed is zero */ /* Send remaining code. */ if(code_buf_ptr > 1) { for(i = 0; i < code_buf_ptr; i++) putc(code_buf[i], g_outfile); g_code_size += code_buf_ptr; } /* Encoding is done. */ printf("In : %ld bytes\n", g_text_size); printf("Out: %ld bytes\n", g_code_size); /* xxx - floating-point math: */ printf("Out/In: %.3f\n", (double)g_code_size / g_text_size); } /***************************************************************************** Just the reverse of compress() *****************************************************************************/ static void decompress(void) { unsigned r, flags; int c, i, j, k; memset(g_ring_buffer, ' ', N - F); r = N - F; for(flags = 0; ; flags >>= 1) { /* Get a byte. For each bit of this byte: 1=copy one byte literally, from input to output 0=get two more bytes describing length and position of previously-seen data, and copy that data from the ring buffer to output */ if((flags & 0x100) == 0) { c = getc(g_infile); if(c == EOF) break; /* uses higher byte cleverly to count eight */ flags = c | 0xFF00; } /* 1=copy one byte literally, from input to output */ if(flags & 1) { c = getc(g_infile); if(c == EOF) break; putc(c, g_outfile); g_ring_buffer[r] = c; r = (r + 1) & (N - 1); } /* 0=get two more bytes describing length and position of previously- seen data, and copy that data from the ring buffer to output */ else { i = getc(g_infile); if(i == EOF) break; j = getc(g_infile); if(j == EOF) break; i |= ((j & 0xF0) << 4); j = (j & 0x0F) + THRESHOLD; for(k = 0; k <= j; k++) { c = g_ring_buffer[(i + k) & (N - 1)]; putc(c, g_outfile); g_ring_buffer[r] = c; r = (r + 1) & (N - 1); } } } } /***************************************************************************** *****************************************************************************/ int main(int arg_c, char *arg_v[]) { char ms_hdr[14] = "SZDD堭'3A"; unsigned long size; /* check args */ if(arg_c != 4) USAGE: { printf("'lzss c file1 file2' compresses file1 into file2\n" "'lzss u file2 file1' uncompresses file2 into file1\n"); return 1; } if(strpbrk(arg_v[1], "CcUu") == NULL)goto USAGE; /* open input file */ g_infile = fopen(arg_v[2], "rb"); if(g_infile == NULL) { printf("Can't open input file '%s'\n", arg_v[2]); return 2; } /* get size of (uncompressed) input file */ fseek(g_infile, 0, SEEK_END); size = ftell(g_infile); fseek(g_infile, 0, SEEK_SET); /* open output file */ g_outfile = fopen(arg_v[3], "wb"); if(g_outfile == NULL) { printf("Can't open output file '%s'\n", arg_v[3]); fclose(g_infile); return 2; } /* compress or decompress */ if(toupper(*arg_v[1]) == 'C') { /* write Microsoft COMPRESS/EXPAND header */ WRITE_LE32(ms_hdr + 10, size); if(fwrite(ms_hdr, 1, 14, g_outfile) != 14) { printf("Error writing output file '%s'\n", arg_v[3]); fclose(g_infile); fclose(g_outfile); remove(arg_v[3]); } compress(); } else { /* read and validate Microsoft COMPRESS/EXPAND header */ if(fread(ms_hdr, 1, 14, g_infile) != 14) { printf("Error reading input file '%s'\n", arg_v[2]); fclose(g_infile); fclose(g_outfile); remove(arg_v[3]); } if(strncmp(ms_hdr, "SZDD堭'3A", 9)) { printf("Input file '%s' is not COMPRESSed\n", arg_v[2]); fclose(g_infile); fclose(g_outfile); remove(arg_v[3]); } size = READ_LE32(ms_hdr + 10); decompress(); } fclose(g_infile); fclose(g_outfile); return 0; } 。

LZSS压缩算法实验报告

LZSS压缩算法实验报告

LZSS压缩算法实验报告一、引言LZSS是一种常用的压缩算法,被广泛应用于数据压缩领域。

它的核心思想是通过利用数据中存在的重复信息来实现压缩。

本实验主要对LZSS压缩算法进行研究与实验,评估其压缩效果和性能。

二、算法原理LZSS算法是一种基于滑动窗口和查找缓冲区的字典算法。

其主要思想是将输入数据分为两个部分:滑动窗口和查找缓冲区。

滑动窗口维护了最长匹配的前缀字符串,而查找缓冲区则用来寻找重复的字符串。

具体实现步骤如下:1.初始化滑动窗口和查找缓冲区;2.在滑动窗口中寻找与查找缓冲区中最长匹配的字符串;3.若找到匹配字符串,则将其标记为索引和长度的形式;4.若未找到匹配字符串,则将当前字符输出;5.更新滑动窗口和查找缓冲区;6.重复2-5步骤,直到所有字符处理完毕。

三、实验设计本实验的主要目的是评估LZSS压缩算法的压缩效果和性能表现。

为了完成实验,我们设计了以下实验流程:1.实现LZSS压缩和解压缩算法;2.准备不同类型和大小的文件,用于测试压缩算法的效果;3.运行压缩算法,并记录压缩比、压缩时间和解压缩时间;4.对比不同类型和大小的文件的压缩比和压缩时间;5.分析实验结果并给出评估。

四、实验结果与分析我们分别对文本文件、图像文件和音频文件进行了压缩实验。

实验结果如下:1.文本文件压缩实验结果:压缩比为70%,压缩时间为0.5秒,解压缩时间为0.3秒。

2.图像文件压缩实验结果:压缩比为80%,压缩时间为1秒,解压缩时间为0.6秒。

3.音频文件压缩实验结果:压缩比为60%,压缩时间为0.8秒,解压缩时间为0.4秒。

从实验结果可以看出,不同类型的文件对LZSS压缩算法的效果存在差异。

文本文件由于存在大量的重复信息,所以可以获得较高的压缩比。

而图像文件和音频文件的压缩比较低,因为它们的数据特征较为复杂,存在较少的重复信息。

压缩时间和解压缩时间较短,说明LZSS压缩算法具有较好的性能表现。

五、总结本实验通过对LZSS压缩算法的实验研究,评估了其压缩效果和性能表现。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
static voidinit_tree(void)
{
unsignedi;
/* Fori= 0 to N - 1,g_right_child[i] andg_left_child[i] will be the right and
left children of nodei. These nodes need not be initialized.
and counter for reporting progress every 1K bytes */
static unsigned longg_text_size,g_code_size,g_print_count;
/* ring buffer of size N, with extra F-1 bytes
Also,g_parent[i] is the parent of nodei. These are initialized to
NIL (= N), which stands for 'not used.'
Fori= 0 to 255,g_right_child[N +i+ 1] is the root of the tree
q =g_right_child[p];
else
{
q =g_left_child[p];
if(g_right_child[q] != NIL)
{
do q =g_right_child[q];
while(g_right_child[q] != NIL);
g_right_child[g_parent[q]] =g_left_child[q];
/* encode string into position and length
ifmatch_lengthis greater than this: */
#defineTHRESHOLD2
/* these assume little-endian CPU like Intel x86
-- need byte-swap function for bigendianCPU */
/* Clear the buffer with any character that will appear often. */
memset(g_ring_buffer+ s, '', r - s);
g_parent[i] = NIL;
}
/*****************************************************************************
Inserts string of length F,g_ring_buffer[r..r+F-1], into one of the
to facilitate string comparison */
static unsigned charg_ring_buffer[N + F - 1];
/* position and length of longest match; set byinsert_node() */
static unsignedg_match_pos,g_match_len;
g_match_len= 0;
while(1)
{
if(cmp>= 0)
{
if(g_right_child[p] != NIL)
p =g_right_child[p];
else
{
g_right_child[p] = r;
g_parent[r] = p;
return;
}
}
else
{
if(g_left_child[p] ! position-and-length pair (2 bytes). Thus, eight units require at most
16 bytes of code. */
code_buf[0] = 0;
code_buf_ptr= mask = 1;
s = 0;
r = N - F;
2、编程语言:C++
五、实验代码
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
/* size of ring buffer */
#defineN4096
/* index for root of binary search trees */
static voidinsert_node(intr)
{
unsigned char *key;
unsignedi, p;
intcmp;
cmp= 1;
key = &g_ring_buffer[r];
p = N + 1 + key[0];
g_right_child[r] =g_left_child[r] = NIL;
#define NILN
/* upper limit forg_match_len. Changed from 18 to 16 for binary
compatabilitywith Microsoft COMPRESS.EXE and EXPAND.EXE
#defineF18 */
#defineF16
#defineREAD_LE32(X)*(uint32_t *)(X)
#defineWRITE_LE32(X,Y)*(uint32_t *)(X) = (Y)
/* this assumessizeof(long)==4 */
typedefunsigned longuint32_t;
/* text (input) size counter, code (output) size counter,
*****************************************************************************/
static void compress(void)
{
unsignedi,len, r, s,last_match_length,code_buf_ptr;
g_parent[g_left_child[p]] = r;
g_parent[g_right_child[p]] = r;
if(g_right_child[g_parent[p]] == p)
g_right_child[g_parent[p]] = r;
else
g_left_child[g_parent[p]] = r;
unsigned charcode_buf[17], mask;
intc;
init_tree(); /* initialize trees */
/*code_buf[1..16] saves eight units of code, andcode_buf[0] works as
eight flags, "1" representing that the unit is anunencodedletter (1 byte),
g_parent[g_left_child[q]] =g_parent[q];
g_left_child[q] =g_left_child[p];
g_parent[g_left_child[p]] = q;
}
g_right_child[q] =g_right_child[p];
g_parent[g_right_child[p]] = q;
for strings that begin with characteri. These are initialized
to NIL. Note there are 256 trees. */
for(i= N + 1;i<= N + 256;i++)
g_right_child[i] = NIL;
for(i= 0;i< N;i++)
static voiddelete_node(unsigned p)
{
unsigned q;
if(g_parent[p] == NIL)
return; /* not in tree */
if(g_right_child[p] == NIL)
q =g_left_child[p];
else if(g_left_child[p] == NIL)
p =g_left_child[p];
else
{
g_left_child[p] = r;
g_parent[r] = p;
return;
}
}
for(i= 1;i< F;i++)
{
cmp= key[i] -g_ring_buffer[p +i];
if(cmp!= 0)
break;
}
if(i>g_match_len)
/* left & right children & parent -- these constitute binary search tree */
static unsignedg_left_child[N + 1],g_right_child[N + 257],g_parent[N + 1];
/* input & output files */
}
g_parent[q] =g_parent[p];
相关文档
最新文档