Using Matlab dctdemo.m to learn 2D-DCT and JPEG Encoding
Short demo on ZigZag sequence and JPEG compression ratio
The dctdemo.m matlab code
in http://cs.uccs.edu/~cs525/dct/dctlib
implements a demonstation of 2D-DCT transformation. It utilizes the functions defined in the dctlib directory. You can sftp the whole dctlib directory to your designated machine in A210 or lats.eas.uccs.edu, which are installed with MATLAB R2009b package, and then follow
the following instruction for computing the transformed coefficients. You can enter each statement on the command window to execute them and observe the results. Here I assume you remote access se-a210-01 (128.198.62.11) for this exercise.
-
Use SSH Secure File Transfer to copy the whole dctlib directory from viva:
- Enter C:\work\cs525\S2010\chow\ on the left panel for for the destination directory replacing chow with your login
- Enter /home/cs525/public_html/dct on the righ panel for the source directory.
- Drag the whole dctlib directory to the left panel.
-
Goto C:\work\cs525\S2010\chow\dctlib folder (replace chow in the file path with your login), click on dctdemo.m
- It will start the matlab and take about 1-2 minutes to initialize the program. You should see the matlab editor showing the content of dctdemo.m.
- When the main matlab status window show "ready" at the bottom of the main window. Click on the play (green triangle) button on the matlab editor window. It will ask if we want to add the currently directory to the search path. Click the "Add to Path".
- You will the execution results of dctdemo.m on the main command window with content similar to http://cs.uccs.edu/~cs525/dct/matlab/matlabDCTResults.txt
- Figure window will show Figure 1 with the plot of last surf(X,Y,getdctf(zf1)) similar to http://cs.uccs.edu/~cs525/dct/matlab/ex1dct.png
- dctlib folder contains ,
- the dctf.m contains the function dctf(u, v, f) that given indices u and v, and a 8x8 matrix f, it will return the corresponding dct coefficient.
- The getdctf.m contains the getdctf(f) function that given an 8x8 matric f, it will return the corresponding 8x8 dct coefficients. Matlab require each function defined in a file with the same function name followed by .m file extension. idctf and getidctf are equivalent of functions for inverse DCT.
- dctdemo.m contains the statements that generate an cell array with 64 dct coefficient 8x8 matrices where only one element has value one and others are all zero.
z=zeros(8,8);
for x=1:8
for y=1:8
t=z; t(x,y)=1; fm{x,y}=t;
end
end
- Try tye surf(X,Y,getidctf(fm{3,3})) as command and see the plot on figure window.
- Note that matrix in matlab is
a special array with 1-based index.
-
To set the matrix f125 with all
elements = 0.125, we can implement the following procedure:
f125=0.125*ones(8,8)
- Note that comparing with those in maple http://cs.uccs.edu/~cs525/dct/dctmws.htm, it is much shorter.
- The following procedure, bwstrip
in maple produces a matrix f with the black and whitel stripe pattern:
bwstrip:=proc(f)
local
i,j;
for
i from 1 to 8 do
for
j from 1 to 8 do
if (i mod 4 > 1) then f[i,j]:=0;
else f[i,j]:=100;
fi;
od;
od;
end;
bwstrip(f);
-
While in matlab, it is only
zf1([1 4 5 8],:)=100*ones(4,8)
- print(f) shows the matrix content
-
On maple, matrixplot(f, axes=BOXED) display
the pattern in 3D.
On matlab surf(X,Y,zf1) shows
-
We then execute getdctf(F);
to obtain the 2D-DCT coefficients in F matrix.
-
On maple, Use print(F); to print the matrix; On matalbe just type zf1
On matalbe just type getdctf(zf1)
ans =
400.0000 0.0000 -0.0000 0.0000 0.0000 0.0000 -0.0000 -0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
-0.0000 0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.0000 0.0000
-0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
400.0000 0.0000 -0.0000 0.0000 0.0000 0.0000 -0.0000 -0.0000
0.0000 0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 0.0000
-0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
-0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 -0.0000 0.0000
Note that there are quite a few -0.0000 They are small negative numbers
which can not show with default
format short format.
If we type format short e then getdictf(zf1) again it will show
ans =
Columns 1 through 6
4.0000e+002 3.5209e-014 -3.2619e-014 2.2649e-014 2.0136e-014 1.1560e-013
5.0635e-015 5.5511e-017 7.1609e-015 3.6082e-015 3.6082e-015 5.3846e-015
-5.5228e-014 3.6082e-015 -1.0603e-014 -3.4972e-015 -7.0499e-015 7.1609e-015
-9.5422e-014 1.7819e-014 3.6082e-015 5.5511e-017 5.3846e-015 5.5511e-017
4.0000e+002 1.7819e-014 -2.4813e-014 3.0254e-014 2.1372e-014 1.1730e-013
1.4323e-013 1.8319e-015 5.3846e-015 -8.8263e-015 5.5511e-017 -3.4972e-015
-1.6074e-013 7.1609e-015 1.8319e-015 9.4369e-016 1.8319e-015 1.8319e-015
-7.6581e-014 5.3846e-015 1.0714e-014 2.7200e-015 1.0714e-014 2.7200e-015
Columns 7 through 8
-6.2764e-014 -3.1991e-014
5.5511e-017 3.6082e-015
3.6082e-015 3.6082e-015
6.2728e-015 2.7200e-015
-6.5670e-014 -3.2363e-014
9.4369e-016 2.7200e-015
1.3878e-015 1.1657e-015
-1.4988e-015 1.6098e-015
Related question here is how to get rid of these small numbers? At what steps of JPEG we did that?
On maple, matrixplot(F, axes=BOXED)
to plot result in 3D.
On matlab, type surf(X,Y,getdctf(zf1)) to get the following figure
Here is the zig-zag sequence
for the DCT coefficients in the matrix F:
400.0, 9 zeros, 400.0, EOB
Intermediate sequence for the
above zig-zag sequence (values encoded using the VLI encoding):
(9,400) (9, (9, 400)) (0, 0)
Here is the VLI encodign rule :
Note that value 400 in the group of values (-511..-256, 256..511) it will be encoded as (9, 400).
Assume that the size and run
lenght fields are encoded with 4 bits, and the amplitude fields are encoded
using VLI encoding. We encode the intermediate sequence with 4+9+4+4+9+4+4=38
bits.
Assume each of original
source 8x8 block data is 8 bit, the compression ratio is 8*64:38=13.47:1