Sunteți pe pagina 1din 6

Introduction:

A discrete cosine transform (DCT) expresses a sequence of finitely many data points in terms of a sum of
cosine functions oscillating at different frequencies. The JPEG process is a widely used form of lossy
image compression that centers on the Discrete cosine transform. DCT and Fourier transform convert
images from time-domain to frequency-domain to de-correlate pixels. The DCT transformation is
reversible. In other words, the DCT works by separating images into parts of differing frequencies. After
de-correlation each transform coefficient can be encoded independently without losing compression
efficiency.

PART 1:
In the first part of the lab, we were to create two functions. The first function was to return an N-by-N
matrix that could be used to transform a !D signal of size N into its DCT coefficients. The second
function was developed to perform the DCT operation on a 1D signal X of arbitrary signal. The required
two functions can be seen as the followings:

1. Generating a matrix of N-by-N: The following function gets an arbitrary integer and generates a
square matrix and outputs the size of matrix.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function[]=GenerateT(N)
T=zeros(N);
display(size(T));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

2. Performing DCT: the following function, gets an input and does the required operations to find the 1D
DCT of the input signal.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function []=PerformDCT(x)
f=x;
M=length(f); % finding the length of the input
for u=0:(M-1)
if (u==0) % setting up the condition for Cv
Cu=(0.5)*(2^0.5);
else
Cu=1;
end
for k=0:(M-1) % DCT operation based on the equation
su(k+1)=(cos((2*k+1)*(u*pi)/(2*M)))*f(k+1);
S=sum(su);
F(u+1)=(((2/M)^0.5)*Cu*S);
end
end
F_round=round(F); display(F); display(F_round);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
The following test was performed to check the accuracy of the written functions:
Our sample: f = [85 -65 15 30 -56 35 90 60];
Result : F = 68.5894 -49.3459 74.2082 11.4312 15.5563 116.7578 44.2679 -4.9931
F_round = 69 -49 74 11 16 117 44 -5
Comparing with the results obtained from DCT function of Matlab:
ans = 68.5894 -49.3459 74.2082 11.4312 15.5563 116.7578 44.2679 -4.9931
As can be verified by the Matlab function, part 1 was done successfuly.

PART 2
In this part, the same concept as part 1 was implemented to perform 2D DCT on a M-by-N signal. The
developed functions were tested by performing the following test:
The sample test -> f=[10 4 1 20;11 5 0 18];
Calling the function : GenerateT2(M,N) for M=2 and N=4 results:
ans =

0.3536 0.3536 0.3536 0.3536 0.3536 0.3536 0.3536 0.3536


0.4619 0.4619 0.1913 0.1913 -0.1913 -0.1913 -0.4619 -0.4619
0.3536 0.3536 -0.3536 -0.3536 -0.3536 -0.3536 0.3536 0.3536
0.1913 0.1913 -0.4619 -0.4619 0.4619 0.4619 -0.1913 -0.1913
0.3536 -0.3536 0.3536 -0.3536 0.3536 -0.3536 0.3536 -0.3536
0.4619 -0.4619 0.1913 -0.1913 -0.1913 0.1913 -0.4619 0.4619
0.3536 -0.3536 -0.3536 0.3536 -0.3536 0.3536 0.3536 -0.3536
0.1913 -0.1913 -0.4619 0.4619 0.4619 -0.4619 -0.1913 0.1913

And calling PerformDCT2(f) function results to:


ans =

24.3952 -6.3222 17.3241 -6.9483


0.3536 -1.7685 0.3536 0.3499

NOTE:
 It should be noted that, part3 of this lab was done in order to meet the requirements of part 2 steps
3 to 7.
PART 3:

The required code for this part was written and then tested for three different pictures, and then the IDCT
function was performed to get back to the original images: the following shows the steps taken:

The following image has been DCT ed for 3 factors as can be seen in the figure.
Then performing IDCT, resulted to the following images:
By looking at the obtained results, it can be observed that the JPEG image will be realizable from the
original. It is due to the fact that DCT takes advantage of redundancies in the data by grouping pixels
with similar frequencies together. Moreover, looking at the images can verify that since the resolution of
the tested images were considerably high, the DCT operation led to a good picture in terms of quality,
hence it can be expected that the MSR should not be high.
CODE FOR PART2: The following code does the DCT2 operations.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function []=PerformDCT_2D(x,y)
%f=[10 4 1 20;11 5 0 18];
M=size(f,1);
N=size(f,2);

for u=0:(M-1)
for v=0:(N-1)

if (u==0)
Cu=(0.5)*(2^0.5);
end
if(v==0)
Cv=(0.5)*(2^0.5);
end
if(u ~=0 || v~=0)
Cu=1;
Cv=1;
end

for k=0:(M-1)
for l=0:(N-1)
su_1=cos((2*k+1)*(u*pi)/(2*M));
S_1=sum(su_1);

su_2=cos((2*l+1)*(v*pi)/(2*N));
S_2=sum(su_2);

S=(S_1)*(S_2);
lena(k+1,l+1)=f(k+1,l+1)*(S);
lena1=sum(sum(lena));
F(u+1,v+1)=((2/(N*M)^0.5)*Cu*Cv*lena1);

end
end
end
end
display(F);

end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

S-ar putea să vă placă și