Discrete Cosine Transform ( Algorithm and Program ) | C # program to perform discrete cosine transform ; Function to find discrete cosine transform and print it ; dct will store the discrete cosine transform ; ci and cj depends on frequency as well as number of row and columns of specified matrix ; sum will temporarily store the sum of cosine signals ; Driver code
using System ; class GFG { public static void dctTransform ( int [ , ] matrix , int m , int n ) { double pi = 3.142857 ; int i , j , k , l ; double [ , ] dct = new double [ m , n ] ; double ci , cj , dct1 , sum ; for ( i = 0 ; i < m ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { if ( i == 0 ) ci = 1 / Math . Sqrt ( m ) ; else ci = Math . Sqrt ( 2 ) / Math . Sqrt ( m ) ; if ( j == 0 ) cj = 1 / Math . Sqrt ( n ) ; else cj = Math . Sqrt ( 2 ) / Math . Sqrt ( n ) ; sum = 0 ; for ( k = 0 ; k < m ; k ++ ) { for ( l = 0 ; l < n ; l ++ ) { dct1 = matrix [ k , l ] * Math . Cos ( ( 2 * k + 1 ) * i * pi / ( 2 * m ) ) * Math . Cos ( ( 2 * l + 1 ) * j * pi / ( 2 * n ) ) ; sum = sum + dct1 ; } } dct [ i , j ] = ci * cj * sum ; } } for ( i = 0 ; i < m ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { Console . Write ( dct [ i , j ] ) ; } Console . WriteLine ( ) ; } } static void Main ( ) { const int m = 8 , n = 8 ; int [ , ] matrix = new int [ m , n ] { { 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 } , { 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 } , { 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 } , { 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 } , { 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 } , { 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 } , { 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 } , { 255 , 255 , 255 , 255 , 255 , 255 , 255 , 255 } } ; dctTransform ( matrix , m , n ) ; } }