No BSD License  

Highlights from
Rudimental compression algorithms for 2D matlab plots

Be the first to rate this file! 0 Downloads (last 30 days) File Size: 6.86 KB File ID: #20514

Rudimental compression algorithms for 2D matlab plots

by Francesco Pozzi

 

30 Jun 2008 (Updated 09 Jul 2008)

Rudimental compression algorithms for 2D matlab plots (make eps images small for your LaTeX docs!)

| Watch this File

File Information
Description

Rudimental compression algorithms for 2D matlab plots (import tiny and tidy eps images in your LaTeX docs!)

Use them if you need to plot highly populated vectors when you expect high density of points per pixel.

***************************************

Function compress2D returns the vector indexes to plot, discarding the vector indexes which seem to be redundant.

N = 1000000; % A big number
x = rand(N, 1); % random x values
y = x .^ 2 + rand(N, 1) / 10; % y values
width = 950; % width of your figure
height = 630; % height of your figure

% Heavy eps File
h = figure('Position', [50 50 width height]);
plot(x, y, '.'); % heavy, uncompressed plot
saveas(h, 'heavyuncompressedimage.eps'); % heavy file
h1 = dir('heavyuncompressedimage.eps'); % file size
disp(sprintf('The file size, for the uncompressed image, is equal to %0.2f KB', h1.bytes / 1024));

% Small eps File
alpha = 0.28; % resolution coefficient

ind = compress2D(x, y, width, height, alpha); % find indexes

h = figure('Position', [50 50 width height]);
plot(x(ind), y(ind), '.'); % compressed image
saveas(h, 'compressedimage.eps'); % small file
h2 = dir('compressedimage.eps'); % file size
disp(sprintf('The file size, for the compressed image, is equal to %0.2f KB', h2.bytes / 1024));

% We have saved 95% of space!
disp(sprintf('Congratulations! You have saved %0.2f%% of space', 100 - h2.bytes / h1.bytes * 100));

***************************************

Function compress2Dh returns two new vectors to plot instead of the original x and y.

N = 1000000; % A big number
x = rand(N, 1); % random x values
y = x .^ 2 + rand(N, 1) / 10; % y values
width = 950; % width of your figure
height = 630; % height of your figure

% Heavy eps File
h = figure('Position', [50 50 width height]);
plot(x, y, '.'); % heavy, uncompressed plot
saveas(h, 'heavyuncompressedimage.eps'); % heavy file
h1 = dir('heavyuncompressedimage.eps'); % file size
disp(sprintf('The file size, for the uncompressed image, is equal to %0.2f KB', h1.bytes / 1024));

% Small eps File
alpha = 0.28; % resolution coefficient

[xind, yind] = compress2Dh(x, y, width, height, alpha); % new values

h = figure('Position', [50 50 width height]);
plot(xind, yind, '.'); % compressed image
saveas(h, 'compressedimage.eps'); % small file
h2 = dir('compressedimage.eps'); % file size
disp(sprintf('The file size, for the compressed image, is equal to %0.2f KB', h2.bytes / 1024));

% We have saved 95% of space!
disp(sprintf('Congratulations! You have saved %0.2f%% of space', 100 - h2.bytes / h1.bytes * 100));

***************************************

Both functions may be particularly useful if you need to import eps images in your LaTeX documents: by compressing the image, you can show in your plot only the more relevant information and discard all the redundancies.

The plane is initially fragmented in a number of cells which is a function of width, height and alpha:
ceil(alpha * width) * ceil(height / width * ceil(alpha * width))

By choosing values for width, height and alpha, you can choose the compression that you desire and, consequently, the quality of the final image.

Try different combinations in order to obtain the best result.

***************************************

compress2Dh is MUCH slower than compress2D and A BIT more accurate because the average of points for each cell is computed, while compress2D considers only the first point found for each cell.

MATLAB release MATLAB 7 (R14)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Updates
09 Jul 2008

Extended to trivial cases (single points or horizontal & vertical lines).
Corrispondently extended help section.
Corrected some imprecisions in the help section.

Tag Activity for this File
Tag Applied By Date/Time
compression Francesco Pozzi 22 Oct 2008 10:07:55
resolution Francesco Pozzi 22 Oct 2008 10:07:55
plot Francesco Pozzi 22 Oct 2008 10:07:55
figure Francesco Pozzi 22 Oct 2008 10:07:55
eps Francesco Pozzi 22 Oct 2008 10:07:55
latex Francesco Pozzi 22 Oct 2008 10:07:55

Contact us at files@mathworks.com