## my own DCT2 and IDCT2 Solved

### Pawel Gagatek (view profile)

on 9 Dec 2018 at 0:05
Latest activity Edited by Pawel Gagatek

### Pawel Gagatek (view profile)

on 15 Dec 2018 at 15:26

### Pawel Gagatek (view profile)

Hey guys i am trying to make my own DCT2 and IDCT2 for school but
i can't find any error -> this is how i checked that it is not working ( as i know when i will do B=DCT2(vector) and then C=IDCT2(B) C will equal vector)
( this is exacly for 8x8 blocks and is use in JPEG coder )
Here is photo what exactly i have to do
DCT
and here is code
% dct2
function [ F ] = doDCT2( v2 )
N=8;
F=zeros(N,N);
for u=0:1:N-1
for v=0:1:N-1
vC=getC(v);
uC=getC(u);
temp=0;
for x=0:1:N-1
for y=0:1:N-1
temp =temp+v2(u+1,v+1)*cos(((2*x+1)*pi*u)/16)*cos(((2*y+1)*pi*v)/16);
end
end
F(u+1,v+1)=vC*uC*(1/4)*temp;
end
end
end
%c(u)
function [ C ] = getC( N )
if N==0
C=1/sqrt(2);
else
C=1;
end
end
%IDCT2
function [ F ] = doIDCT2( v2 )
N=8;
F=zeros(N,N);
for u=0:1:N-1
for v=0:1:N-1
vC=getC(v);
uC=getC(u);
temp=0;
for x=0:1:N-1
for y=0:1:N-1
temp =temp+vC*uC*v2(u+1,v+1)*cos(((2*x+1)*pi*u)/16)*cos(((2*y+1)*pi*v)/16);
end
end
F(u+1,v+1)=(1/4)*temp;
end
end
end

### Pawel Gagatek (view profile)

on 14 Dec 2018 at 18:18
Edited by Pawel Gagatek

### Pawel Gagatek (view profile)

on 14 Dec 2018 at 18:21

@Edit here is working code maybe someone will need it :)
%% fdct2
function [ F ] = YUV2FDCT( f )
N=8;
F=zeros(N,N);
for u=1:1:N
for v=1:1:N
uO = (( u - 1 ) * pi ) ;
vO = (( v - 1 ) * pi ) ;
temp=0;
for x=1:1:N
for y=1:1:N
n1 = ( 2 *( x - 1 ) + 1 ) ;
n2 = ( 2 *( y - 1 ) + 1 ) ;
s1 = ( n1 * uO );
s2 = ( n2 * vO );
temp =temp + f ( x , y ) ...
* cos( s1/16) ...
* cos( s2/16 ) ;
end
end
F(u,v)=(1/4)*getC(u-1)*getC(v-1)*temp;
end
end
end
%% IFDCT2
function [ f ] = YUV2IFDCT( F )
N=8;
f=zeros(N,N);
for x=1:1:N
for y=1:1:N
n1 = ( 2 *( x - 1 ) + 1 ) ;
n2 = ( 2 *( y - 1 ) + 1 ) ;
temp=0;
for u=1:1:N
for v=1:1:N
uO = (( u - 1 ) * pi ) ;
vO = (( v - 1 ) * pi ) ;
s1 = ( n1 * uO );
s2 = ( n2 * vO );
temp =temp + F ( u , v ) ...
* getC(u-1)*getC(v-1) ...
* cos( s1/16) ...
* cos( s2/16 ) ;
end
end
f(x,y)=(1/4)*temp;
end
end
end
%---------------------------------------%
function [ C ] = getC( N )
if N==0
C=1/sqrt(2);
else
C=1;
end
end
%---------------------------------------%