[Scilab-users] [Users] Discrete cosine transform

Serge Steer Serge.Steer at inria.fr
Tue Aug 21 15:27:38 CEST 2012


Le 17/08/2012 20:29, tvitklg at rambler.ru a écrit :
>
> Hello. Whether there is a function Discrete cosine transform (DCT)?
>
> tvitklg at rambler.ru
>
>
>
> _______________________________________________
> Users mailing list
> Users at lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users
Please find below the dct and idct scilab code:

function y = dct (x,flag)
//Ref:  A. K. Jain, "Fundamentals of Digital Image Processing", pp. 150-153.
   [mx,nx]=size(x);
   if argn(2)==1 then flag=-1,end
   if size(x,1)==1 then
     //transform x as a column vector
     x=matrix(x,-1,1);
     n=mx*nx;
     nc=1;
   else
     n=mx;
     nc=nx
   end
   if n==1 then y=matrix(y,mx,nx),return,end
   if flag==-1 then
w=[sqrt(0.25/n);sqrt(0.5/n)*exp((-%i*%pi/2/n)*(1:n-1)')]*ones(1,nc);
     if isreal(x)&modulo(n,2)==0 then
       y=2*real(w.*fft([x(1:2:n,:); x(n:-2:2,:)],flag,1));
     else
       y=fft([x;x($:-1:1,:)],flag,1);
       y=w.*y(1:n,:);
       if isreal(x) then y=real(y); end
     end
   else //inverse transform
     if isreal(x)&modulo(n,2)==0 then
       w = [sqrt(n/4);sqrt(n/2)*exp((%i*%pi/2/n)*(1:n-1)')]*ones(1,nc);
       y=fft(w.*x,flag,1);
       y([1:2:n,n:-2:1],:)=2*real(y);
     else
       w = [sqrt(4*n);sqrt(2*n)*exp((%i*%pi/2/n)*(1:n-1)')]*ones(1,nc);
       y=[x.*w;zeros(1,nc);-%i*w(2:$,:).*x($:-1:2,:)];
       y=fft([x.*w;zeros(1,nc);-%i*w(2:$,:).*x($:-1:2,:)],flag,1);
       y=y(1:n,:);
       if isreal(x) then y=real(y); end
     end
   end
   y=matrix(y,mx,nx);
endfunction
function y=idct(x)
   y=dct(x,1)
endfunction


Serge Steer
INRIA
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20120821/f9490847/attachment.htm>


More information about the users mailing list