Normand Briere
2019-11-21 ddb10cb84dddfeef1ef9946f2e13cef3c93e6cc4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
 
import java.lang.Math.*;
 
/**
 * TwoDArray is a data structure to represent a two-dimensional array
 * of complex numbers.  ie The result of applying the 2D FFT to an image.
 *
 * @author Simon Horne.
 */
public class TwoDArray
{
 
    /**
     * The actual width of the image represented by the TwoDArray.
     */
    static public int width;
    /**
     * The actual height of the image represented by the TwoDArray.
     */
    static public int height;
    /**
     * Smallest value of 2^n such that the 2^n > width and 2^n > height.
     * The dimensions of the square 2D array storing the image.
     */
    public int size;
    /**
     * The 2D array of complex numbers padded out with (0,0) 
     * to 2^n width and height.
     */
    public ComplexNumber[][] values;
 
    /**
     * Default no-arg constructor.
     */
    public TwoDArray()
    {
    }
 
    /**
     * Constructor that takes a TwoDArray and duplicates it exactly.
     *
     * @param a TwoDArray to be duplicated.
     */
    public TwoDArray(TwoDArray a)
    {
 
        width = a.width;
        height = a.height;
//System.out.println("NEW 2D 1 w: "+width+" height: "+height);    
        size = a.size;
        values = new ComplexNumber[size][size];
        for (int j = 0; j < size; ++j)
        {
            for (int i = 0; i < size; ++i)
            {
                ComplexNumber c = new ComplexNumber(a.values[i][j]);
                values[i][j] = c;
            }
        }
    }
 
    /**
     * Constructor that takes a width and a height, generates the appropriate
     * size values and then sets up an array of (0,0) complex numbers.
     *
     * @param w Width of the new TwoDArray.
     * @param h Height of the new TwoDArray.
     */
    public TwoDArray(int w, int h)
    {
        width = w;
        height = h;
//System.out.println("NEW 2D 2 w: "+width+" height: "+height);    
        int n = 0;
        while (Math.pow(2, n) < Math.max(w, h))
        {
            ++n;
        }
        size = (int) Math.pow(2, n);
        values = new ComplexNumber[size][size];
        for (int j = 0; j < size; ++j)
        {
            for (int i = 0; i < size; ++i)
            {
                values[i][j] = new ComplexNumber(0, 0);
            }
        }
    }
 
    /** 
     * Constructor that takes a single dimension, generates an appropriate
     * size and sets up a size x size array of (0,0) complex numbers.
     *
     * @param s Width or height of new TwoDArray.
     */
    public TwoDArray(int s)
    {
        width = s;
        height = s;
//System.out.println("NEW 2D 3 w: "+width+" height: "+height);    
        int n = 0;
        while (Math.pow(2, n) < s)
        {
            ++n;
        }
        size = (int) Math.pow(2, n);
        values = new ComplexNumber[size][size];
 
        for (int j = 0; j < size; ++j)
        {
            for (int i = 0; i < size; ++i)
            {
                values[i][j] = new ComplexNumber(0, 0);
            }
        }
    }
 
    /** 
     * Constructor taking int array of pixel values and width and height
     * of the image represented by the array of pixels, sets values to
     * (x,0) for each pixel x.
     *
     * @param p int array of pixel values.
     * @param w Width of image.
     * @param h Height of image.
     */
    public TwoDArray(double[] p, int w, int h)
    {
        width = w;
        height = h;
        //System.out.println("NEW 2D 4 w: "+width+" height: "+height);    
        int n = 0;
 
        while (Math.pow(2, n) < Math.max(w, h))
        {
            ++n;
        }
        //System.out.println("n is "+n+" w is "+w+" h is "+h);
 
        size = (int) Math.pow(2, n);
        values = new ComplexNumber[size][size];
        /*
        for (int j = h; j < size; ++j)
        {
        for (int i = 0; i < size; ++i)
        {
        values[i][j] = new ComplexNumber(0, 0);
        }
        }
        for (int j = 0; j < size-h; ++j)
        {
        for (int i = w; i < size; ++i)
        {
        values[i][j] = new ComplexNumber(0, 0);
        }
        }
         */
 
        /*System.err.println("Just about to add image to array");*/
        /* DONALD NAIRN 2003 */
        for (int j = 0; j < size; ++j)
        {
            for (int i = 0; i < size; ++i)
            {
                double real = 0;
 
                if (i < w && j < h)
                {
                    real = p[i + (j * w)];
                }
 
                values[i][j] = new ComplexNumber(real, 0.0);
            }
        }
    }
 
    /** 
     * Constructor taking 2D int array of pixels values, width and height,
     * sets values to (x,0) for each pixel x.
     *
     * @param v 2D array of pixel values.
     * @param w Width of image.
     * @param h Height of image.
     */
    public TwoDArray(int[][] v, int w, int h)
    {
        width = w;
        height = h;
//System.out.println("NEW 2D 5 w: "+width+" height: "+height);    
        int n = 0;
        while (Math.pow(2, n) < Math.max(w, h))
        {
            ++n;
        }
        size = (int) Math.pow(2, n);
        values = new ComplexNumber[size][size];
 
        for (int j = 0; j < size; ++j)
        {
            for (int i = 0; i < size; ++i)
            {
                values[i][j] = new ComplexNumber(0, 0);
            }
        }
        for (int j = 0; j < h; ++j)
        {
            for (int i = 0; i < w; ++i)
            {
                values[i][j] = new ComplexNumber(v[i][j], 0.0);
            }
        }
    }
 
    /**
     * Constructor taking 2D array of complex numbers, width and height.
     *
     * @param v 2D array of complex numbers.
     * @param w Width of image.
     * @param h Height of image.
     */
    public TwoDArray(ComplexNumber[][] v, int w, int h)
    {
        width = w;
        height = h;
        //System.out.println("NEW 2D 6 w: "+width+" height: "+height);    
        int n = 0;
        while (Math.pow(2, n) < Math.max(w, h))
        {
            ++n;
        }
        size = (int) Math.pow(2, n);
        values = new ComplexNumber[size][size];
 
        for (int j = 0; j < size; ++j)
        {
            for (int i = 0; i < size; ++i)
            {
                values[i][j] = new ComplexNumber(0, 0);
            }
        }
        for (int j = 0; j < h; ++j)
        {
            for (int i = 0; i < w; ++i)
            {
                values[i][j] = new ComplexNumber(v[i][j]);
            }
        }
    }
 
    /** 
     * Takes a column number and returns an array containing the 
     * complex numbers in that column.
     *
     * @param n int column number (0 is first column).
     * @return ComplexNumber array containing column.
     */
    public ComplexNumber[] getColumn(int n)
    {
        ComplexNumber[] c = new ComplexNumber[size];
        for (int i = 0; i < size; ++i)
        {
            c[i] = new ComplexNumber(values[n][i]);
        }
        return c;
    }
 
    /**
     * Takes a column number and an array of complex numbers and replaces
     * that column with the new data.
     *
     * @param n int column number (0 is first column).
     * @param Array of complex numbers representing the new data.
     */
    public void putColumn(int n, ComplexNumber[] c)
    {
        for (int i = 0; i < size; ++i)
        {
            values[n][i] = new ComplexNumber(c[i]);
        }
    }
 
    /**
     * Takes a row number and an array of complex numbers and replaces
     * that row with the new data.
     *
     * @param n int row number (0 is first row).
     * @param c Array of complex numbers representing the new data.
     */
    public void putRow(int n, ComplexNumber[] c)
    {
        for (int i = 0; i < size; ++i)
        {
            values[i][n] = new ComplexNumber(c[i]);
        }
    }
 
    /** 
     * Takes a row number and returns an array containing the 
     * complex numbers in that row.
     *
     * @param n int row number (0 is first row).
     * @return ComplexNumber array containing row.
     */
    public ComplexNumber[] getRow(int n)
    {
        ComplexNumber[] r = new ComplexNumber[size];
        for (int i = 0; i < size; ++i)
        {
            r[i] = new ComplexNumber(values[i][n]);
        }
        return r;
    }
 
    /** 
     * Takes a 2D array of doubles representing an image and translates
     * and wraps the image to put (0,0) the DC value in the centre of the image.
     * at (width/2,height/2)  [because image runs -128..+127]
     *
     * @param input 2D array of doubles.
     * @return 2D array of doubles representing the new image.
     */
    public double[][] DCToCentre(double[][] input)
    {
        double[][] output = new double[width][height];
        int x = width / 2;
        int y = height / 2;
        int i2, j2;
        for (int j = 0; j < height; ++j)
        {
            for (int i = 0; i < width; ++i)
            {
                i2 = i + x;
                j2 = j + y;
                if (i2 >= width)
                {
                    i2 = i2 % width;
                }
                if (j2 >= height)
                {
                    j2 = j2 % height;
                }
                output[i][j] = input[i2][j2];
            }
        }
        return output;
    }
 
    /** 
     * Takes a 2D array of doubles representing an image and translates
     * and wraps the image to put the centre pixel at (0,0).
     *
     * @param input 2D array of doubles.
     * @return 2D array of doubles representing the new image.
     */
    public double[][] DCToTopLeft(double[][] input)
    {
        double[][] output = new double[width][height];
        int i2, j2;
        int x = width / 2;
        int y = height / 2;
        for (int j = 0; j < height; ++j)
        {
            for (int i = 0; i < width; ++i)
            {
                i2 = i + x;
                j2 = j + x;
                if (i2 >= width)
                {
                    i2 = i2 % width;
                }
                if (j2 >= height)
                {
                    j2 = j2 % height;
                }
                output[i][j] = input[i2][j2];
            }
        }
        return output;
    }
 
    static public ComplexNumber[][] DCToTopLeft(ComplexNumber[][] input)
    {
        ComplexNumber[][] output = new ComplexNumber[width][height];
        int i2, j2;
        int x = width / 2;
        int y = height / 2;
        for (int j = 0; j < height; ++j)
        {
            for (int i = 0; i < width; ++i)
            {
                i2 = i + x;
                j2 = j + x;
                if (i2 >= width)
                {
                    i2 = i2 % width;
                }
                if (j2 >= height)
                {
                    j2 = j2 % height;
                }
                output[i][j] = input[i2][j2];
            }
        }
        return output;
    }
 
    /** 
     * Takes an array of doubles representing an image and translates
     * and wraps the image to put (0,0) the DC value in the centre of the image.
     * at (width/2,height/2)  [because image runs -128..+127]
     *
     * @param input array of doubles.
     * @return array of doubles representing the new image.
     */
    static public double[] DCToCentre(double[] input)
    {
        double[][] input2 = new double[width][height];
        double[][] output2 = new double[width][height];
        double[] output = new double[width * height];
        for (int j = 0; j < height; ++j)
        {
            for (int i = 0; i < width; ++i)
            {
                input2[i][j] = input[j * width + i];
            }
        }
        int x = width / 2;
        int y = height / 2;
        int i2, j2;
        for (int j = 0; j < height; ++j)
        {
            for (int i = 0; i < width; ++i)
            {
                i2 = i + x;
                j2 = j + y;
//if (input2[i][j] == 0){System.out.println("ZEROa at ("+i+","+j+") moved to ("+i2+","+j2+")");}
                if (i2 >= width)
                {
                    i2 = i2 % width;
                }
                if (j2 >= height)
                {
                    j2 = j2 % height;
                }
                output2[i][j] = input2[i2][j2];
            }
        }
        for (int j = 0; j < height; ++j)
        {
            for (int i = 0; i < width; ++i)
            {
                output[j * width + i] = output2[i][j];
            }
        }
        return output;
    }
 
    /**
     * Method to extract the real parts from a TwoDArray.
     *
     * @return An array of doubles representing the real parts of
     * each element of the TwoDArray.
     */
    public double[] getReal()
    {
        double[] output = new double[width * height];
        for (int j = 0; j < height; ++j)
        {
            for (int i = 0; i < width; ++i)
            {
                output[(j * width) + i] = values[i][j].real;
//if (values[i][j].real == 0){System.out.println("found ZERO at ("+i+","+j+")");}
            }
        }
        return output;
    }
 
    /**
     * Method to extract the imaginary parts from a TwoDArray.
     *
     * @return An array of doubles representing the imaginary parts of
     * each element of the TwoDArray.
     */
    public double[] getImaginary()
    {
        double[] output = new double[width * height];
        for (int j = 0; j < height; ++j)
        {
            for (int i = 0; i < width; ++i)
            {
                output[(j * width) + i] = values[i][j].imaginary;
            }
        }
        return output;
    }
 
    /**
     * Method to extract the magnitude of each element from a TwoDArray.
     *
     * @return An array of doubles representing the magnitude of
     * each element of the TwoDArray.
     */
    public double[] getMagnitude()
    {
        double[] output = new double[width * height];
        for (int j = 0; j < height; ++j)
        {
            for (int i = 0; i < width; ++i)
            {
                output[(j * width) + i] = values[i][j].magnitude();
            }
        }
        return output;
    }
 
    /**
     * Method to extract the phase angle of each element from a TwoDArray.
     *
     * @return An array of doubles representing the phase angle of
     * each element of the TwoDArray.
     */
    public double[] getPhase()
    {
        double[] output = new double[width * height];
        for (int j = 0; j < height; ++j)
        {
            for (int i = 0; i < width; ++i)
            {
                output[(j * width) + i] = values[i][j].phaseAngle();
            }
        }
        return output;
    }
}