Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

(Computation with Large Matrices of Images)

Prepared by :
1)Snehal Bhayani (201211008)
2)Sagar Ahir (201211045)
3)Ankur Undhad(201211049)

Introduction
Computation of large chunks of data demands machine tools for efficient and at-leisure
implementation of the specific algorithms. And as is the case here, we are to process images of
approximately 512 * 512 resolution. Reading the images into matrices makes it an array of
numbers large enough to make it impossible to process them without a software programming
package.
Hence using various tools provided by higher level programming languages, we perform
a many operation on these images(or the matrices so obtained from them). MATLAB is used to
primarily process the given images. We use it to convert the image into a gray scale image and
then form a matrix out of the same. As an aside, we crop the image using MATLAB as well.
Apart from that, all of the other computations are done using JAVA programming
language. The IDE used for JAVA is Eclipse software. An open source IDE for a full-fledged
JAVA program development, we employ various high level API for reading, writing, and
processing substantially voluminous matrices of data. With layers of abstraction we define
objects of complex number structures and along with them operations of complex number
system. Modularity and structural technique enable us to define a systematic approach to tackling
the computational problem.
Norms of the matrices, DFT and IDFT, as well as plotting the results of the magnitude and angle
plot of DFT and IDFT as photographs is implemented using JAVA language.
Some errors abound due to the limitations of computer in representation of real numbers.
To cite a few, representation of the irrational number PI, taking Nth roots of real numbers that are
not Nth powers of some other rational number, etc. These errors being quite minute, can be
neglected when the size of the image is comparable in terms of it's resolution and pixel array.
Indeed as the comparison between the IDFT image and the DFT image show, we have been able
to recreate the original image with quite negligible errors.
Some of the concepts of representation of color models and the floating point
implementation in JAVA have been looked into along-with the computation exercise.

Project discussion
Question 1
Facial photographs of all of the three participants are taken and named accordingly.
According to first question, we use MATLAB to convert the given image from RGB format to
Gray-Scale format.
In RGB format each pixel is specified by three values of red ,green and blue wherein 8
bit and 16 bit storage methods which range from 0 to255 and 0 to 655365 respectively are
employed.
IRGB = ( IR + IG + IB )

---------------------------- (1)

For Gray-scale, the most commonly used storage method is 8 bit storage in which there
are 256 gray levels or in other words each pixel is represented by the intensity value ranging
from 0-255.
For this we use IMREAD( ) command in MATLAB to read the image from the path
given. Once the image is copied into the working directory of MATLAB, we can
Then by using command IMSHOW( ); we display both:
1) RGB Image and (2) Gray scale image.
The program for the same can be found in Appendix A.

Matrix NORMs
A matrix norm || . || is function from C m x n to R with three properties :
1) ||A|| 0 for all A C m x n and ||A|| = 0 if and only if A=0.
2) || A+B || ||A|| + ||B|| for all A C m x n .
3) || A || = || ||A|| for all C, A C m x n .

P-norm Let p 1 be a real number.

-------------------------------------------------(2)
Infinity norm : (maximum of row sum):

-------------------------------------------------(3)
One norm : ( maximum column sum ) :
--------------------------------------------------(4)

Relative Error NORM-wise for matrix multiplication


For matrices U and V, the relative error in their product due to individual errors is known by the
formula:

--------------------------(5)

As we have a product of three matrices, we will need to tweak a bit in our formula to calculate
the matrix product NORM upper bound.

-----------------(6)

Again once DFT is calculated here, we can move up to calculating the IDFT. For that the upper
bound in relative error is given as :

-------------------(7)

Here we present the general derived formulae whose detailing and application to our DFT
problem is given in appendix C.

We use these formulae for calculating the values of Error NORMs in DFT and IDFT calculation.
Here as the formulae are for infinity NORM we can get the same for One NORM as well.

DFT and IDFT


The Fourier Transform is an important image processing tool which is used to decompose an
image into its sine and cosine components. The output of the transformation represents the image
in the Fourier or frequency domain, while the input image is the spatial domain equivalent. In
the Fourier domain image, each point represents a particular frequency contained in the spatial
domain image.
The Fourier Transform is used in a wide range of applications, such as image analysis, image
filtering, image reconstruction and image compression.
The DFT is the sampled Fourier Transform and therefore does not contain all frequencies
forming an image, but only a set of samples which is large enough to fully describe the spatial
domain image. The number of frequencies corresponds to the number of pixels in the spatial
domain image, i.e. the image in the spatial and Fourier domain are of the same size.
For a square image of size NN, the two-dimensional DFT is given by:

Where 0 k,l N-1.


And In a similar way, the Fourier image can be re-transformed to the spatial domain.
The inverse Fourier transform is given by:

Where 0 m ,n N-1.

Matrix Multiplication for DFT and IDFT


A derivation of the representation of DFT and IDFT transforms in terms of Matrix
multiplication.

---------------(8)

DFT, IDFT accuracy NORM-wise


For multiplications with erroneous matrices, we need to find the NORM-wise relative
errors and their upper bounds.
For the case of DFT and IDFT as we have shown above, each is expressed as product of
three matrices. And hence the NORM-relative errors of a DFT matrix can be found by
substituting into NORM Upper bound equation (6) above, the transformation matrix W and data
matrix G. Similarly for IDFT matrix, we can use equation (7) and substitute the Conjugate
transformation matrix W* and DFT matrix GDFT .
Thus the NORM-wise error upper bounds are found by combining both the concepts and
estimated, is the error. One thing needs to be taken care of, is that here we assume- the error is
solely due to representation of numbers and there is no error in various real operations of
multiplication, power and addition. And this bound is not too tight as is shown by the results in
conclusion. The reason behind this is in not knowing the correct theoretical values of some of the
matrices, hence more inequalities employed thereby loosening up the upper bound.

Two more concepts


Two concepts relevant to JAVA are in order before we take up the exercise of Norms, DFT and
IDFT:
1. JAVA uses the RGB model as mentioned in case of MATLAB before. But here now that
we have read the Gray-scale obtained through MATLAB program, Red, Green and Blue
pixel value will be the same. Hence to represent our matrices in JAVA we simply read
anyone of the pixel values. Here we take the pixel value of Red color and consider it to
be representing that pixel. Commands of how we do that are elaborated in Appendix B.
Once we are done with processing such formed matrices, we print them back In Image
format and save the same with appropriate names on Computer hard disks. For this as we
use the RGB model.
More on the default RGB ColorModel used by Java.awt.package can be found in
references section.
2. Complex number formats are not by default defined in JAVA. Hence to work on the

same, as required in given exercises, we create custom JAVA Objects Complex. Also
are defined all of the operations that are legitimate on mathematical complex numbers as
JAVA functions. Thus at all places while processing matrices with each element as
Complex object, we employ the custom defined operations on them instead of the
orthodox plain mathematical operations.
And further employing this modularity and a top-down approach, number of JAVA
functions for generating Transform Matrices, Multiplying two Complex matrices,
printing the same on IO console, etc. are clearly defined and called from the main method
of JAVA.

JAVA program
Divided structurally and logically we have two .java files. One is the Complex.java for
modeling the mathematical complex numbers and their operations. The other is
MultiplyMatrices.java with the actual flow for processing the images.
Listing the operations performed by our program in order of execution:
1. Reading the specified image from the given path into JAVA defined Image Object.
2. Reading the pixel values from the image into a given 2D array. Again assuming its a
gray scale image (for in exercise 1 we already converted it into Gray-scale using
MATLLAB) the RGB model is split to obtain the value of brightness as an integer
between 0 and 255. The arrays so formed are represented by variable <B>.
3.

We calculate the NORMs Infinity and One using a simple looping structure for the
given array <B>.

4. The
transformation
array
<transformationMat>
and
its
conjugate
<invTransformationMat> are generated with quite a degree of accuracy. Certain
special cases are considered as specified in the program to reduce the errors due to
floating point representation.
5.

The DFT <DFT> is calculated in two steps.


First we calculate <IntDFT> = <A> * < transformationMat>.
And then <DFT> = < transformationMat> * <IntDFT>.

6. The IDFT <IDFT> is calculated in two steps.


First we calculate <IntIDFT> = <DFT> * < invTransformationMat >.
And then <IDFT> = < invTransformationMat > * <IntIDFT>.
7. Various norms of all of the above calculated arrays are calculated reusing the above
mentioned NORM logic in step-3 for both Infinity and One.
8. These values are then employed in formulae stated in theory 2.2 and 2.5. Thus
calculated are the values of upper bounds of relative error, Infinity and One NORMwise in <DFT> and <IDFT>.
9. Finally the processed arrays containing pixel information as brightness or luminance
is converted back to RGB format and printed in the form of a physical image and
saved on the hard disk. For an array <B> we print the following images :
<B>, <magnitude<DFT>>, <angle<DFT>> , <magnitude<IDFT>>.

Question 2
Here we calculate the NORMs Infinity and One for the matrices that have been represented.
Infinity Norm of Image snehalgray.jpg = 96727.0
One Norm of Image snehalgray.jpg = 129602.0
Infinity Norm of Image sagargray.gif = 87533.0
One Norm of Image sagargray.gif = 88349.0
Infinity Norm of Image ankurgray.jpg = 129418.0
One Norm of Image ankurgray.jpg = 129418.0
All of these images and their original versions are attached in appendix D.

Question 3
Below mentioned operations are performed on all of the three matrices representing the three
images snehalgray.jpg , sagargray.gif and ankurgray.jpg. These operations are done for
each image one at a time using the JAVA program.
(a) In theory 2.4 we have shown DFT and IDFT as matrix multiplications.
(b) We calculate DFT using JAVA (program given in appendix B). The operation uses a plain
nested looping structure simply multiplying matrices element by element.
(c) Similarly we calculate the IDFT reusing the matrix multiplication logic.
Time span to complete

DFT

IDFT

snehalgray.jpg

24 seconds

23 seconds

sagargray.gif

23 seconds

22 seconds

ankurgray.jpg

25 seconds

23 seconds

Image (All are 512*512


dimensioned)

Table 1 Time span to complete processing )

Question 4
As mentioned in the program steps, for each one of the three arrays representing the three
images, we print the images of their processed arrays. Special API used for the same in java, the
concepts of BufferedImage and ImageIO are employed for the same.

Creating an empty image, we fill its pixels with the values from the specified array and then
save the same to hard-disk.
The Image representing magnitude(IDFT) is the same as that of the original gray image.
Upon visual inspection its hard to point out the errors, but there is some error. The various
NORMs of many arrays as calculated are tabulated here:
Given Data

Transformation M atrix

Inverse-transformation
matrix

DFT

IDFT

snehalgray.jpg

96727.0

22.627416997969664

22.627416997969664

176877.86736597194

96726.99999999945

sagargray.gif

87533.0

22.627416997969664

22.627416997969664

133546.99128534563

87533.00000000004

ankurgray.jpg

129418.0

22.627416997969664

22.627416997969664

142651.7869675418

129417.99999999999

NORM S**
Image (All are
512*512
dimensioned)

Table 2 (Infinity NORMs)

Given Data

Transformation M atrix

Inverse-transformation
matrix

DFT

IDFT

snehalgray.jpg

129602.0

22.627416997969664

22.627416997969664

189419.03909827032

129601.99999999907

sagargray.gif

88349.0

22.627416997969664

22.627416997969664

133546.99128534563

88348.99999999999

ankurgray.jpg

129418.0

22.627416997969664

22.627416997969664

169874.22738548837

129417.99999999999

NORM S**
Image (All are
512*512
dimensioned)

Table 3 (One NORMs)

NORM -wise
upper
bounds**

Upper bound in
DFT(Infinity NORM )

Upper bound in
DFT(One NORM )

Upper bound in
IDFT(Infinity NORM )

Upper bound in
IDFT(One NORM )

snehalgray.jpg

1686.5501185722708

2505.316288474069

7775126.98042321

1.154797288695631E7

sagargray.gif

2018.8433278918765

2545.0504779533635

9306414.801078651

1.1731177348562002E7

ankurgray.jpg

2791.418087105529

2764.2717157700945

1.2866431315967677E7

1.2741259947083991E7

Image (All are


512*512
dimensioned)

Table 4
(Relative error upper bounds with respect to perturbed matrices)

Conclusion
Some points to be noticed in terms of results of Computational exercises:
(a) Owing to the vastness of the matrices being handled and the way of employing the
algorithm, a substantial amount of time was consumed in generating the DFT and IDFT
of the given images.
The approximate time in completing all of the Matrix operations turned out to be
a little over 1 minute. This comes off due to the number of real multiplications and real
additions occurring. Around (1073741824) multiplications and (1073217536) real
additions.
And the data-type being used to represent each element is a custom JAVA object.
Thus the overhead of processing each memory occupying object for an individual
element takes up substantial processor time.
(b) The matrix operations have been done using the simplest algorithms and hence errors due
to representation of special numbers as well as real operations of exponentiation, arise.
And the magnitude of the errors are seen to be proportional to the dimension of the
matrices involved. To get an idea of the relative error in calculation of DFT and IDFT we
use the matrix 1 and infinity NORMs.
Assuming that the errors are only due to representation of matrix elements. And
errors due to truncation of matrix multiplication are taken to be as errors in representation
of numbers. So that our task of specifying the NORM-wise upper bound due to matrix
multiplication becomes simpler and clearer.
So we find the upper bound of NORM-wise relative error for DFT and IDFT. The
upper bounds for both infinity and One NORM are nearly the same for DFT and IDFT.
The values are quite large for IDFT of the order of 10 ^ 5 . Refer to tables 2,3 and 4 for
comparisons of various NORMS of matrices and their NORM-wise error upper bounds.
The reason found is the large condition number due to the large values of NORMS of
individual matrices and the NORMs of intermediate matrices are much larger than the
NORMs of data and final product. And also its not helped by the fact that error in
generating the Transformation matrix is unnecessarily approximated to a less tight bound
as we cant find || - W || and thus we approximate to its upper limit as |||| + ||W||

Appendices
Appendix A
Program1 : A program for reading the image in MATLAB and converting it into a Gray-scale
image. Herewith we as well convert the 3 D array into a 2D one by converting the third
dimensions value to 1. And then save the same gray-scale image in D:// drive for being read by a
java code at a later stage.
Here as an example we read one image. The same can be used for other two images simply by
replacing the path of the image from the function imread().

Appendix B
Program1 : MultiplyMatrices.java
/*A JAVA program to perform various operations needed to compute matrices and thereby perform IMAGE processing.
* The API used in here are primarily that of JAVA level packages for working on image processing.
* The list of operations that are performed w ith this JAVA program are specified in THEORY titled "JAVA Program" in text.
* And this code is written using Eclipse Java EE IDE for Web Developers.( Indigo Release )
* It references another Complex.java code. That is also listed below.
* The version of JVM used is JRE1.6
* Images converted to gray-scale are read from path D:\<Name OF the image>
* And Printed Back to path E:\<Name Of the Image>
* It is for the Photograph of one person And iteratively we can do the same for the other two photographs.
*/
import
import
import
import
import
import
import
import
import
import

java.awt.Image;
java.awt.image.BufferedImage;
java.awt.image.Col orModel;
java.awt.image.Pi xelGrabber;
java.io.BufferedInputStream;
java.io.File;
java.io.FileInputStream;
java.io.InputStream;
java.util.Calendar;
javax.imageio.ImageIO;

public class MultiplyMatrices {


/* A main method that starts off with calling the child functions.
* The start point for any java program.
*/
public static void main(String args[])
{
readImage();
}
/*This method reads an image whose path is specified by a string as a parameter.
* 'e:\\test3.jpg'
*
*/
public static void readImage()
{
int size =512;
int[] a = new int [size*size];
Complex data[][] = new Complex [size][size];
try{
/*An input Stream is created using a given image file.
*A function <ImageIO.read()> is used to read the disk-located image into a format that JAVA has pre-defined.
*This Object of type Image is the used in a constructor of Class Type <PixelGrabber>.
*A function <grabPixels()> is called over the same <PixelGrabber> to 'Grab' the pixels into a 1D array of specifie size.
*/
InputStream is = new BufferedInputStream(new FileInputStream("D:\\sagargray3.gif"));
Image img = ImageIO.read(is);
//We need the co-ordinates of the top-left corner (here it's 0,0), the width and the height (Equal to the dimension of the image) and the scansize.
PixelGrabber pg = new PixelGrabber(img, 0, 0, size,size, a, 0, size);
//Grab the pixels
pg.grabPixels();
/* The java function <grabPixels()> reads the pixel values in RGB model. And it is a 4-byte data. represented in hexadecimal system as AARRGGBB.
* Where AA is the byte of alpha value, RR is the byte of red value, GG is the byte of Green value and BB is the byte of blue value.
* All values thus range from 0-255.
*/

/* The pixels are read into a two dimensional array.


* And hence to convert into a matrix we convert the 1D array into a 2D one using the below given loop.
* Here assumed is the fact that images supplied to the above piece of code is a grayscale image.
* Hence the Read RGB value is used to extract the Luminance. Which in a grayscale image is the value of Red or green or Blue. (All three being the same.)
*/
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
// The below function <ColorModel.getRGBdefault().getRed()> returns the value of the red pixel and thus the value of the brig htness or luminance.
int b1 =ColorModel.getRGBdefault().getRed((a[i*size+j]));
// The matrix will hold real values. But to ensure compatibility with the operations to be performed afterwards, we convert the elements into complex format.
data[i][j] = new Complex(b1,0);
}
}
/*
* calculate the Infinity and the 1 Norms of the given data matrix.
*/
double[] NORMData= findNORMs(data);
System.out.println(" >>>>>>>>NORMData "+ NORMData[0]);
/* We declare matrices to hold our end results and the intermediate data as well.
*/
// The transformation matrix of the DFT transform is generated with dimensions equal to the dimension of the data matrix or equal to the value of <Size>

Complex[][] transformationMat =wGen(size,-1);


// The transformation matrix of the IDFT transform is generated with dimensions equal to the dimension of the data matrix or equal to the value of <Size>
Complex[][] invTransformationMat =wGen(size,1);
// The DFT is calculated as matrix multiplications as mentioned in the theory.
Complex[][] IntDFT =multiplyMatrices(data,transfor mationMat);
Complex [][] DFT = multiplyMatrices(transformationMat, IntDFT);
// The IDFT is calculated as matrix multiplications as mentioned in the theory.
Complex[][] IntIDFT =multiplyMatrices(DFT,invTransformationMat);
Complex [][] IDFT = multiplyMatrices(invTransformationMat,IntIDFT);
// calculating the various norms of the matrices
double[] NORMTrans= findNORMs(transformationMat);
System.out.println(" >>>>>>>> "+ NORMTrans[0]);
double[] NORMTransInv= findNORMs(invTransformationMat);
System.out.println(" >>>>>>>> NORMTransInv "+ NORMTransInv[0]);
double[] NORMIntDFT= findNORMs(IntDFT);
System.out.println(" >>>>>>>> NORMIntDFT "+ NORMIntDFT[0]);
double[] NORMDFT= findNORMs(DFT);
System.out.println(" >>>>>>>> NORMDFT"+ NORMDFT[0]);
double[] NORMIntIDFT= findNORMs(IntIDFT);
System.out.println(" >>>>>>>>NORMIntIDFT "+ NORMIntIDFT[0]);
double[] NORMIDFT= findNORMs(IDFT);
System.out.println(" >>>>>>>>NORMIDFT "+ NORMIDFT[0]);
// Find relative NORM wise error for Infinity Norm
relErrorNorm(NORMTrans[0],NORMTransInv[0],NORMIntDFT[0],NORMDFT[0],NORMIntIDFT[0],NORMIDFT[0],NORMData[ 0],siz e);
// Find relative NORM wise error for Zero Norm
System.out.println(" >>>>>>>> "+ NORMTrans[1]);
System.out.println(" >>>>>>>> NORMTransInv "+ NORMTransInv[1]);
System.out.println(" >>>>>>>> NORMIntDFT "+ NORMIntDFT[1]);
System.out.println(" >>>>>>>> NORMDFT"+ NORMDFT[1]);
System.out.println(" >>>>>>>>NORMIntIDFT "+ NORMIntIDFT[1]);
System.out.println(" >>>>>>>>NORMIDFT "+ NORMIDFT[1]);
System.out.println(" >>>>>>>>NORMData "+ NORMData[1]);
relErrorNorm(NORMTrans[1],NORMTransInv[1],NORMIntDFT[1],NORMDFT[1],NORMIntIDFT[1],NORMIDFT[1],NORMData[ 1],siz e);
// To plot the image back from the results of DFT and IDFT matrices, we need a one dimensional array that maps uniquely into a 2D matrix.
int[] DFTData = new int[size*size];
//-----------------------------------Commented method_calls to print some of the results of the operations done above on IO console.
//printArray(transformationMat );
//printArray((multiplyMatrices(b,generatedMat)));
//printArray(DFT);
//printArray(IDFT);
//----------------------------------------------------------------------------------------------------------------------------------

/*Back transformation of the 2d resulting matrix for DFT and IDFT transformation into a 1D array.
* Thus we can then print the images resulting from the pixel values onto hard disk into the form of an image.
* ALso to note is a critical point that so far we have processed the matrices with a byte of representation for each pixel.
* To print it back in the form of RGB format, we convert back the single value into a number representing the RGB values.
* With the equation as RGBValue = (intensity)*256^2 + (intensity)*256 + (intensity) .
*/
// For Printing the magnitude of the DFT matrix
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
int temp = (int)(Complex.mag(DFT[i][j]));
DFTData[i*size+j] = temp *256 * 256 + temp *256 + temp;
}
}
// Create a custom JAVA object of type <BufferedImage> with needed dimensions (that equal those of the original image matrix).
BufferedImage image = new BufferedImage(size,size, BufferedImage.TYPE_INT_RGB);
// Set the individual pixel value to be equal to that of the one contained in the created DFTData array.
image.setRGB(0, 0, size, size, DFTData, 0, size);
// Save the image contained in JAVA object onto a hard disk and save it with the specified name.
File imageFile = new File("E://capturedImageDFTMagnitude.jpg");
ImageIO.write(image, "jpg", imageFile);
System.out.println(" DFT Magnitude Image created and saved ");
// For Printing the angle of the DFT matrix
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
int temp = (int)(Complex.arg(DFT[i][j]));
DFTData[i*size+j] = temp *256 * 256 + temp *256 + temp;
}
}
// Create a custom JAVA object of type <BufferedImage> with needed dimensions (that equal those of the original image matrix) .
image = new BufferedImage(size,size, BufferedImage.TYPE_INT_RGB);
// Set the individual pixel value to be equal to that of the one contained in the created DFTData array.
image.setRGB(0, 0, size, size, DFTData, 0, size);
// Save the image contained in JAVA object onto a hard disk and save it with the specified name.
imageFile = new File("E://capturedImageDFTAngle.jpg");
ImageIO.write(image, "jpg", imageFile);
System.out.println(" DFT Angle Image created and saved ");

// For Printing the magnitude of the IDFT matrix


for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
int temp = (int)Complex.mag(IDFT[i][j]);
DFTData[i*size+j] = temp *256 * 256 + temp *256 + temp;
}
}
// Create a custom JAVA object of type <BufferedImage> with needed dimensions (that equal those of the original image matrix).
BufferedImage image1 = new BufferedImage(size,size, BufferedImage.TYPE_INT_RGB);
// Set the individual pixel value to be equal to that of the one contained in the created DFTData array.
image1.setRGB(0, 0, size, size, DFTData, 0, size);
// Save the image contained in JAVA object onto a hard disk and save it with the specified name.
File imageFile1 = new File("E://capturedImageIDFT.jpg");
ImageIO.write(image1, "jpg", imageFile1);
System.out.println(" IDFT Image created and saved ");

} catch(Exception e)
{
e.printStackTrace();
}
}
// Norms Zero and Infinity are calculated in this function. And in an array of size-2, both the NORMS are returned.
// Here position 0 has Infinity NORM
// And position 1 has One NORM
public static double[] findNORMs(Complex[][] data)
{
int size = data[0].length;
double norms[] = new double[2];
double max=0;
for(int i=0;i<size;i++)
{
double sum =0;
for(int j=0;j<size;j++)
{
sum = sum + Complex.mag(data[i][j]);
}
if(max<sum)
max = sum;
}
norms[0]=max;
//
System.out.println(" The Infinity Norm of the given matrix is ::: "+max);
for(int i=0;i<size;i++)
{
double sum =0;
for(int j=0;j<size;j++)
{
sum = sum + Complex.mag(data[j][i]);
}
if(max<sum)
max = sum;
}
norms[1]=max;
System.out.println(" The Zero Norm of the given matrix is ::: "+max);
return norms;

//

}
/*
*
*
*
*
*
*

Here To generate the W matrix for DFT transformation, we will set some predefined rules. As per the notations of mathematic al symbolism,
w^N = 1
w^(N/2) = -1
w^(N/4) = 0 + j1
*/

public static Complex[][] wGen( int size,int inv)


{
// A complex 2D matrix to hold the generated matrix.
Complex[][] w= new Complex[size][size];
// The base of the transformation matrix.
double funArg = (inv * Math.PI *2) /size;
Complex wX = new Complex(Math.cos(funArg),Math.sin(funArg));
// Here we actually iterate through the elements of the matrix. and generate the corresponding elements.
for(int i=0;i<size;i++)
{
Complex sum = new Complex(0, 0);
for(int j=0;j<size;j++)
{
// For certain cases we can simplify the exponent.
int modulo = (i*j) % size;
// Check for the above mentioned special cases
if(modulo == 0)
w[i][j] = new Complex(1,0);

else if(size%2 ==0 && modulo == size/2)


w[i][j] = new Complex(-1,0);
else if(size % 4 ==0 && modulo == size/4)
w[i][j] = new Complex(0,inv);
else
{
w[i][j]= Complex.powC(wX,modul o);
}
// For each transformation matrix we divide the elements by 1/(sqrt(size))
w[i][j] = Complex.scale(w[i][j],1/Math.sqrt(size));
}
}
// returned the generated transformation matrix
return w;
}
/*A method to multiply two given matrices with complex elements.
* Here the formula of two matrix multiplications is used to iterate over individual elements of both of the matrices.
*/
public static Complex[][] multiplyMatrices(Complex c1[][], Complex c2[][])
{
// being a square matrix, the size is equal to the length of any row.
int size = c1[0].length;
// A complex matrix to hold the result of the multiplication.
Complex resultOfMul[][] = new Complex[size][size];
for(int i = 0;i<size ; i++)
{
for(int j = 0;j<size ; j++)
{
Complex temp = new Complex(0,0);
for(int k = 0;k<size ; k++)
{
temp=Complex.add(temp, Complex.mul(c1[i][k], c2[k][j]));
}
resultOfMul[i][j] = temp;
}
}
return resultOfMul;
}

/*A method to print the elements of a matrix on an IO console.


* Here the complex values are printed with <() + () i> format.
*/
public static void printArray(Complex c[][])
{
System.out.println("Printing the specified matrix");
for(int i =0;i<c[0].length;i++)
{
for(int j=0;j<c[0].length;j++)
{
System.out.print("< "+c[i][j].r + " " + c[i][j].i+ "i>");
}
System.out.println();
}
}
/* Function to find the NORM-wise relative error for DFT matrix and IDFT matrix with respect to the perturbed matrix.
* here the upper bounds are calculated for both One and Infinity norm.
*/
public static double relErrorNorm(double NORMTrans,double NORMTransInv,double NORMIntDFT,double NORMDFT,double NORMIntIDFT,double NORMIDFT,double NORMdata,int size)
{
double NORMW = Math.sqrt(size);
double epW = 1.0 + (NORMW/NORMTrans);
double epB = NORMdata * NORMTrans * epW /NORMIntDFT;
System.out.println(" epB :::::::: "+epB);
double epDFT = NORMTrans * NORMIntDFT * (epW + epB *(1+epW)) /NORMDFT;
System.out.println(" epDFT :::::::: "+epDFT);
double epWInv = 1.0 + (NORMW/NORMTransInv);
double epB1 = NORMDFT * NORMTransInv * (epDFT*(1+epWInv) + epWInv) /NORMIntIDFT;
System.out.println(" epB1 :::::::: "+epB1);
double epIDFT = NORMTransInv * NORMIntIDFT * (epWInv + epB1 *(1+epWInv)) /NORMDFT;
System.out.println(" epIDFT :::::::: "+epIDFT);
return epDFT;
}
}

Program2 : Complex.java
/*A Custom object "Complex" is defined along with it's attributes and functions that can operate on those attributes.
* Here mathematically a complex number will be denoted as c = a + jb
* where <a> and <b> will be the attributes of our custom object "Complex" that has it's structure mapped to mathematical complex number.
* and all of the operations of the complex number can have with itself or with another complex number are also define d on our JAVA representation
* of the complex number.
*
*/
public class Complex {
double r=0; // the real part
double i=0; // the imaginary part
/*
* A constructor to set its real and imaginary parts while declaring an instance of the complex number.
*/
public Complex(double r, double i)
{
this.r =r;
this.i=i;
}
/* An empty constructor
*/
public Complex() {
}
/*Addition of two complex numbers
*/
public static Complex add(Complex c1 , Complex c2)
{
Complex c = new Complex();
c.r = c1.r + c2.r;
c.i = c1.i + c2.i;
return c;
}
/* Multiplication of two complex numbers
*/
public static Complex mul(Complex c1 , Complex c2)
{
Complex c = new Complex();
c.r = c1.r * c2.r - c1.i *c2.i;
c.i= c1.r * c2.i + c1.i*c2.r;
return c;
}
/* Inverting of a complex number
*/
public static Complex inv(Complex c1)
{
Complex c = new Complex();
double den = (c1.i * c1.i + c1.r * c1.r);
c.r = c1.r/den;
c.i = -c1.i/(den);
return c;
}
/* Multiply ing a complex number with a real number.
*/
public static Complex scale(Complex c1,double alpha)
{
Complex c = new Complex();
c.r = c1.r * alpha;
c.i = c1.i * alpha;
return c;
}
/*Power of a complex number to a real number.
*/
public static Complex powC(Complex c1, double x)
{
Complex c = new Complex();
double mag = Math.sqrt(c1.r * c1.r + c1.i *c1.i);
double angle = Math.atan2(c1.i,c1.r);
c.r= Math.pow(mag, x) * Math.cos(angle * x);
c.i= Math.pow(mag, x) * Math.sin(angle * x);
return c;
}
/*Magnitude of a complex number
*/
public static double mag(Complex c1)
{
Complex c = new Complex();
double mag = Math.sqrt(c1.r * c1.r + c1.i *c1.i);
return mag;
}

/* Principal argument of a complex number (ranging from -PI to PI)


*/
public static double arg(Complex c1)
{
double angle = Math.atan2(c1.i,c1.r);
return angle;
}
}

Appendix C

Here we have calculated the NORM-wise error in matrix multiplication as applied to calculation
of DFT Transforms and IDFT Transforms. The DFT matrix being a product of 3 matrices and
also

Appendix D

Picture 1
Captured Image of < snehalgray.jpg>

Picture 2
Captured Image of Magnitude< DFT for< snehalgray.jpg>>

Picture 3
Captured Image of Angle< DFT for< snehalgray.jpg>>

Picture 4
Captured Image of Magnitude< IDFT for <DFT for< snehalgray.jpg>>>

Picture 5
Captured Image of < sagargray.gif >

Picture 6
Captured Image of Magnitude< DFT for< sagargray.gif >>

Picture 7
Captured Image of Angle< DFT for<sagargray.gif>>

Picture 8
Captured Image of Magnitude< IDFT for <DFT for< sagargray.gif>>>

Picture 9
Captured Image of < ankurgray.jpg>

Picture 10
Captured Image of Magnitude< DFT for< ankurgray.jpg>>

Picture 11
Captured Image of Angle< DFT for< ankurgray.jpg>>

Picture 12
Captured Image of Magnitude<IDFT for < DFT for< ankurgray.jpg>>>

Further Reading
[1] Color Model in Java :
http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/image/ColorModel.html
[2] Luminance in Color Model:
http://en.wikipedia.org/wiki/Luminance_(relative)
[3] Java.awt.Image :
http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Image.html
[4] IEEE floating point representation
http://en.wikipedia.org/wiki/IEEE_floating_point

You might also like