TY - ET-D - 60 - DAAOA - Lab 3

You might also like

You are on page 1of 4

Name: Ruturaj Uttarwar Class: TY ET-D

GR No: 12020036 Roll No: 60

DAAOA Lab 3

Implementation of 1D and 2D Discrete Cosine Transform Equation


Code:
#include<iostream>
#include <cmath>
using namespace std;
#define PI 3.14159265
int main() {
int u, v, k, N=4;
float matrix[N][N] = {
{1, 2, 2, 1},
{2, 1, 2, 1},
{1, 2, 2, 1},
{2, 1, 2, 1}
};

cout<<"c matrix"<<endl;

// creating matrix c
double c[N][N], ct[N][N], sum=0, dctop[N][N] = {0}, x[N][N] =
{0};
for (u=0; u<N; u++) {
for (v=0; v<N; v++) {
if(u==0) {
c[0][v] = 1 / sqrt(N);
c[0][v] = round(c[u][v] * 10000.0) / 10000.0;
}
else {
c[u][v] = (sqrt(2) / sqrt(N) ) * cos( (2*v +
1) * PI * u / (2*N) ) ;
c[u][v] = round(c[u][v] * 10000.0) /
10000.0; //rounds upto 4 decimal places
Name: Ruturaj Uttarwar Class: TY ET-D
GR No: 12020036 Roll No: 60

}
ct[v][u] = c[u][v]; //creating transpose matrix in
this loop itself
cout.width(10);
cout<<c[u][v]<<" ";
}
cout<<endl;
}

cout<<"\n matrix"<<endl;
for (u=0; u<N; u++) {
for (v=0; v<N; v++) {
cout.width(10);
cout<<matrix[u][v]<<" ";
}
cout<<endl;
}

cout<<"\nc transpose"<<endl;
//printing transpose
for (u=0; u<N; u++) {
for (v=0; v<N; v++) {
cout.width(10);
cout<<ct[u][v]<<" ";
}
cout<<endl;
}

//multiplying the matrices c and matrix


for(u=0; u<N; u++) {

for(v=0; v<N; v++) {


Name: Ruturaj Uttarwar Class: TY ET-D
GR No: 12020036 Roll No: 60

for (k=0; k<N; k++) {


x[u][v] += (c[u][k] * matrix[k][v]) ;
x[u][v] = round(x[u][v] * 10000.0) / 10000.0;
}
}
}
cout<<"\n x output"<<endl;
for (u=0; u<N; u++) {
for (v=0; v<N; v++) {
cout.width(10);
cout<<x[u][v]<<" ";
}
cout<<endl;
}

//multiplying the matrices output and c transpose


for(u=0; u<N; u++) {

for(v=0; v<N; v++) {


dctop[u][v]=0;
for (k=0; k<N; k++) {
dctop[u][v] += (x[u][k] * ct[k][v]) ;
dctop[u][v] = round(dctop[u][v] * 10000.0) /
10000.0;
}
}
}

cout<<"\n DCT output"<<endl;


for (u=0; u<N; u++) {
for (v=0; v<N; v++) {
cout.width(10);
cout<<dctop[u][v]<<" ";
Name: Ruturaj Uttarwar Class: TY ET-D
GR No: 12020036 Roll No: 60

}
cout<<endl;
}
}

Output:

You might also like