function [savings] = wavecompress2(x,mode,outputfile) % wavecompress2 - Lossy compression for 2D data % % [savings] = WAVECOMPRESS2(x,mode,outputfile) compresses the % 2-D data in x using the mode specified and saves to outputfile. % The return value is compression ratio achieved % % The valid values for mode are: % 1 = high compression, high error % (uses bior3.1 filter and 1xE-4 limit) % 2 = medium compression, medium error % (uses bior3.1 filter and 1xE-5 limit) % 3 = low compression, low error % (uses bior5.5 filter and 1xE-7 limit) % % To decompress the data, see: wavedecompress2 % % C. Fleizach 16-May-2006 % cfleizac@cs.ucsd.edu filter = 'bior5.5'; limit = .0000001; level = 5; switch(mode) case 1 filter = 'bior3.1'; limit = .0001; level = 5; case 2 filter = 'bior3.1'; limit = .00001; level = 5; case 3 filter = 'bior5.5'; limit = .0000001; level = 5; end [x1,s] = wavedec2(x,level,filter); s [x1,numzeros] = stripzeros(x1,limit); [savings] = compressandsave(x1,s,mode,outputfile,x); end function [savings] = compressandsave(x1,s,mode,filename,x) newfile = strcat(filename,'.wcm'); mesg = strcat('Created compressed file: ',newfile); mesg savefile(x1,s,mode,filename); zip(newfile,filename); [savings] = compare(newfile,x); delete(filename); end function [savings] = compare(newfile,x) fd1 = fopen(newfile , 'r'); oldsize = size(x); [a1] = fread(fd1,inf,'double'); newsize = size(a1); savings = (oldsize(1)*oldsize(2)) / newsize(1); fclose(fd1); end function savefile(x1,s,mode,filename) fd = fopen(filename , 'wb'); if fd < 0 disp(['could not open the frame ' filename]); end; [n,m] = size(x1); fwrite(fd,n,'double'); fwrite(fd,m,'double'); fwrite(fd,mode,'double'); fwrite(fd,s,'double'); fwrite(fd,x1,'double'); fclose(fd); end function [newA,numzeros] = stripzeros(A,limit) s = size(A); newA = A; numzeros = 0; for k=1:s(1) for j=1:s(2) if (abs(A(k,j)) < limit) numzeros = numzeros + 1; newA(k,j) = 0; end end end end