-
Notifications
You must be signed in to change notification settings - Fork 1
/
FFT.cs
781 lines (687 loc) · 26.6 KB
/
FFT.cs
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Threading;
using System.IO;
namespace Fast_Fourier_Transform
{
/// <summary>
/// Defining Structure for Complex Data type N=R+Ii
/// </summary>
struct COMPLEX
{
public double real, imag;
public COMPLEX(double x, double y)
{
real = x;
imag = y;
}
public float Magnitude()
{
return ((float)Math.Sqrt(real * real + imag * imag));
}
public float Phase()
{
return ((float)Math.Atan(imag / real));
}
}
class FFT
{
public Bitmap Obj; // Input Object Image
public Bitmap FourierPlot; // Generated Fouruer Magnitude Plot
public Bitmap PhasePlot; // Generated Fourier Phase Plot
public int[,] mtwithKey;
public int [,]PhasePlot1= new int[256, 256];
public int[,] MagniturePlot1 = new int[256, 256];
public double[,] RealOUT = new double[256, 256];
public double[,] ImagOUT = new double[256, 256];
public int[,] GreyImage; //GreyScale Image Array Generated from input Image
public float[,] FourierMagnitude;
public float[,] FourierPhase;
float[,] FFTLog; // Log of Fourier Magnitude
float[,] FFTPhaseLog; // Log of Fourier Phase
public int[,] FFTNormalized; // Normalized FFT Magnitude : Scale 0-1
public int[,] FFTPhaseNormalized;// Normalized FFT Phase : Scale 0-1
int nx, ny; //Number of Points in Width & height
int Width, Height;
COMPLEX[,] Fourier; //Fourier Magnitude Array Used for Inverse FFT
public COMPLEX[,] FFTShifted; // Shifted FFT
public COMPLEX[,] Output; // FFT Normal
public COMPLEX[,] FFTNormal; // FFT Shift Removed - required for Inverse FFT
/// <summary>
/// Parameterized Constructor for FFT Reads Input Bitmap to a Greyscale Array
/// </summary>
/// <param name="Input">Input Image</param>
///
public static Color[][] GetBitMapColorMatrix(string bitmapFilePath)
{
Bitmap b1 = new Bitmap(bitmapFilePath);
int hight = b1.Height;
int width = b1.Width;
Color[][] colorMatrix = new Color[width][];
for (int i = 0; i < width; i++)
{
colorMatrix[i] = new Color[hight];
for (int j = 0; j < hight; j++)
{
colorMatrix[i][j] = b1.GetPixel(i, j);
}
}
return colorMatrix;
}
public static Bitmap CombineBitmap(IEnumerable<string> files)
{
//read all images into memory
List<Bitmap> images = new List<Bitmap>();
Bitmap finalImage = null;
try
{
int width = 0;
int height = 0;
foreach (string image in files)
{
// create a Bitmap from the file and add it to the list
Bitmap bitmap = new Bitmap(image);
// update the size of the final bitmap
width += bitmap.Width;
height = bitmap.Height > height ? bitmap.Height : height;
images.Add(bitmap);
}
// create a bitmap to hold the combined image
finalImage = new Bitmap(width, height);
// get a graphics object from the image so we can draw on it
using (Graphics g = Graphics.FromImage(finalImage))
{
// set background color
g.Clear(Color.Transparent);
// go through each image and draw it on the final image
foreach (Bitmap image in images)
{
g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height));
}
}
return finalImage;
}
catch (Exception)
{
if (finalImage != null) finalImage.Dispose();
throw;
}
finally
{
// clean up memory
foreach (Bitmap image in images)
{
image.Dispose();
}
}
}
//===================================================================================================
public FFT(Bitmap Input)
{
Obj = Input;
Width = nx = Input.Width;
Height= ny = Input.Height;
ReadImage();
}
/// <summary>
/// Parameterized Constructor for FFT
/// </summary>
/// <param name="Input">Greyscale Array</param>
public FFT(int[,] Input)
{
Width = nx = Input.GetLength(0);
Height = ny = Input.GetLength(1);
GreyImage = Input;
}
/// <summary>
/// Constructor for Inverse FFT
/// </summary>
/// <param name="Input"></param>
public FFT(COMPLEX[,] Input)
{
nx = Width = Input.GetLength(0);
ny = Height = Input.GetLength(1);
Fourier = Input;
}
/// <summary>
/// Function to Read Bitmap to greyscale Array
/// </summary>
private void ReadImage()
{
int i, j;
GreyImage = new int[Width, Height]; //[Row,Column]
Bitmap image = Obj;
BitmapData bitmapData1 = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
unsafe
{
byte* imagePointer1 = (byte*)bitmapData1.Scan0;
for (i = 0; i < bitmapData1.Height; i++)
{
for (j = 0; j < bitmapData1.Width; j++)
{
GreyImage[j, i] = (int)((imagePointer1[0] + imagePointer1[1] + imagePointer1[2]) / 3.0);
//4 bytes per pixel
imagePointer1 += 4;
}//end for j
//4 bytes per pixel
imagePointer1 += bitmapData1.Stride - (bitmapData1.Width * 4);
}//end for i
}//end unsafe
image.UnlockBits(bitmapData1);
return;
}
public Bitmap Displayimage()
{
int i, j;
Bitmap image = new Bitmap(Width, Height);
BitmapData bitmapData1 = image.LockBits(new Rectangle(0, 0, Width, Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
unsafe
{
byte* imagePointer1 = (byte*)bitmapData1.Scan0;
for (i = 0; i < bitmapData1.Height; i++)
{
for (j = 0; j < bitmapData1.Width; j++)
{
// write the logic implementation here
imagePointer1[0] = (byte)GreyImage[j, i];
imagePointer1[1] = (byte)GreyImage[j, i];
imagePointer1[2] = (byte)GreyImage[j, i];
imagePointer1[3] = (byte)255;
//4 bytes per pixel
imagePointer1 += 4;
}//end for j
//4 bytes per pixel
imagePointer1 += (bitmapData1.Stride - (bitmapData1.Width * 4));
}//end for i
}//end unsafe
image.UnlockBits(bitmapData1);
return image;// col;
}
public Bitmap Displayimage(int[,] image)
{
int i, j;
Bitmap output = new Bitmap(image.GetLength(0), image.GetLength(1));
BitmapData bitmapData1 = output.LockBits(new Rectangle(0, 0, image.GetLength(0), image.GetLength(1)),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
unsafe
{
byte* imagePointer1 = (byte*)bitmapData1.Scan0;
for (i = 0; i < bitmapData1.Height; i++)
{
for (j = 0; j < bitmapData1.Width; j++)
{
imagePointer1[0] = (byte)image[j, i];
imagePointer1[1] = (byte)image[j, i];
imagePointer1[2] = (byte)image[j, i];
imagePointer1[3] = 255;
//4 bytes per pixel
imagePointer1 += 4;
}//end for j
//4 bytes per pixel
imagePointer1 += (bitmapData1.Stride - (bitmapData1.Width * 4));
}//end for i
}//end unsafe
output.UnlockBits(bitmapData1);
return output;// col;
}
public Bitmap Displayimage(double[,] image)
{
int i, j;
Bitmap output = new Bitmap(image.GetLength(0), image.GetLength(1));
BitmapData bitmapData1 = output.LockBits(new Rectangle(0, 0, image.GetLength(0), image.GetLength(1)),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
unsafe
{
byte* imagePointer1 = (byte*)bitmapData1.Scan0;
for (i = 0; i < bitmapData1.Height; i++)
{
for (j = 0; j < bitmapData1.Width; j++)
{
imagePointer1[0] = (byte)image[j, i];
imagePointer1[1] = (byte)image[j, i];
imagePointer1[2] = (byte)image[j, i];
imagePointer1[3] = 255;
//4 bytes per pixel
imagePointer1 += 4;
}//end for j
//4 bytes per pixel
imagePointer1 += (bitmapData1.Stride - (bitmapData1.Width * 4));
}//end for i
}//end unsafe
output.UnlockBits(bitmapData1);
return output;// col;
}
/// <summary>
/// Calculate Fast Fourier Transform of Input Image
/// </summary>
public void ForwardFFT()
{
//Initializing Fourier Transform Array
int i,j;
Fourier =new COMPLEX [Width,Height];
Output = new COMPLEX[Width, Height];
//Copy Image Data to the Complex Array
for (i=0;i<=Width -1;i++)
for (j = 0; j <= Height - 1; j++)
{
Fourier[i, j].real =(double) GreyImage[i, j];
Fourier[i, j].imag = 0;
}
//Calling Forward Fourier Transform
Output= FFT2D( Fourier, nx, ny, 1);
return;
}
public void MultMass(COMPLEX[,] Op1, COMPLEX[,] Op2)
{
int i, j;
for (i = 0; i <= Width - 1; i++)
{
for (j = 0; j <= Height - 1; j++)
{
RealOUT[i, j] = (Op1[i, j].real * Op2[i, j].real) - (Op1[i, j].imag * Op2[i, j].imag);
ImagOUT[i, j] = (Op1[i, j].imag * Op2[i, j].real) + (Op1[i, j].real * Op2[i, j].imag);
}
}
}
/// <summary>
/// Shift The FFT of the Image
/// </summary>
public void FFTShift()
{
int i, j;
FFTShifted = new COMPLEX[nx, ny];
for(i=0;i<=(nx/2)-1;i++)
for (j = 0; j <= (ny / 2) - 1; j++)
{
FFTShifted[i + (nx / 2), j + (ny / 2)] = Output[i, j];
FFTShifted[i, j] = Output[i + (nx / 2), j + (ny / 2)];
FFTShifted[i + (nx / 2), j] = Output[i , j + (ny / 2)];
FFTShifted[i, j + (nx / 2)] = Output[i + (nx / 2), j ];
}
return;
}
/// <summary>
/// Removes FFT Shift for FFTshift Array
/// </summary>
public void RemoveFFTShift()
{
int i, j;
FFTNormal = new COMPLEX[nx, ny];
for (i = 0; i <= (nx / 2) - 1; i++)
for (j = 0; j <= (ny / 2) - 1; j++)
{
FFTNormal[i + (nx / 2), j + (ny / 2)] = FFTShifted[i, j];
FFTNormal[i, j] = FFTShifted[i + (nx / 2), j + (ny / 2)];
FFTNormal[i + (nx / 2), j] = FFTShifted[i, j + (ny / 2)];
FFTNormal[i, j + (nx / 2)] = FFTShifted[i + (nx / 2), j];
}
return;
}
/// <summary>
/// FFT Plot Method for Shifted FFT
/// </summary>
/// <param name="Output"></param>
///
public void FFTPlot(COMPLEX[,]Output)
{
int i, j;
float max;
FFTLog = new float[nx, ny];
FFTPhaseLog = new float[nx, ny];
FourierMagnitude = new float[nx, ny];
FourierPhase = new float[nx, ny];
FFTNormalized = new int[nx, ny];
FFTPhaseNormalized = new int[nx, ny];
for (i = 0; i <= Width - 1; i++)
for (j = 0; j <= Height - 1; j++)
{
FourierMagnitude[i, j] = Output[i, j].Magnitude();
FourierPhase[i, j] = Output[i, j].Phase();
FFTLog[i, j] = (float)Math.Log(1 + FourierMagnitude[i, j]);
FFTPhaseLog[i, j] = (float) Math.Log(1 + Math.Abs(FourierPhase[i, j]));
}
//Generating Magnitude Bitmap
max = FFTLog[0, 0];
for (i = 0; i <= Width - 1; i++)
for (j = 0; j <= Height - 1; j++)
{
if (FFTLog[i, j] > max)
max = FFTLog[i, j];
}
for (i = 0; i <= Width - 1; i++)
for (j = 0; j <= Height - 1; j++)
{
FFTLog[i, j] = FFTLog[i, j] / max;
}
for (i = 0; i <= Width - 1; i++)
for (j = 0; j <= Height - 1; j++)
{
FFTNormalized[i, j] = (int)(255 * FFTLog[i, j]);
}
//Transferring Image to Fourier Plot
FourierPlot = Displayimage(FFTNormalized);
for (int n = 0; n < 255; n++)
{
for (int k = 0; k < 255; k++)
{
MagniturePlot1[n, k] = FFTNormalized[n, k];
}
}
//generating phase Bitmap
FFTPhaseLog[0, 0] = 0;
max = FFTPhaseLog[1, 1];
for (i = 0; i <= Width - 1; i++)
for (j = 0; j <= Height - 1; j++)
{
if (FFTPhaseLog[i, j] > max)
max = FFTPhaseLog[i, j];
}
for (i = 0; i <= Width - 1; i++)
for (j = 0; j <= Height - 1; j++)
{
FFTPhaseLog[i, j] = FFTPhaseLog[i, j] / max;
}
for (i = 0; i <= Width - 1; i++)
for (j = 0; j <= Height - 1; j++)
{
FFTPhaseNormalized[i, j] = (int)(255 * FFTPhaseLog[i, j]);
}
//Transferring Image to Fourier Plot
PhasePlot = Displayimage(FFTPhaseNormalized);
for (int n = 0; n < 255; n++)
{
for (int k = 0; k < 255; k++)
{
PhasePlot1[n, k] = FFTPhaseNormalized[n, k];
}
}
}
/// <summary>
/// generate FFT Image for Display Purpose
/// </summary>
/// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public void FFTmult(COMPLEX[,] Output)
{
int i, j;
float max;
FFTLog = new float [nx,ny];
FFTPhaseLog = new float[nx, ny];
FourierMagnitude = new float[nx, ny];
FourierPhase = new float[nx, ny];
FFTNormalized = new int[nx, ny];
FFTPhaseNormalized = new int[nx, ny];
for(i=0;i<=Width-1;i++)
for (j = 0; j <= Height-1; j++)
{
FourierMagnitude[i, j] = Output[i, j].Magnitude();
FourierPhase[i, j] = Output[i, j].Phase();
FFTLog[i, j] = (float)Math.Log(1 + FourierMagnitude[i, j]);
FFTPhaseLog[i, j] = (float)Math.Log(1 + Math.Abs(FourierPhase[i, j]));
}
//Generating Magnitude Bitmap
max = FFTLog[0, 0];
for(i=0;i<=Width-1;i++)
for (j = 0; j <= Height-1; j++)
{
if (FFTLog[i, j] > max)
max = FFTLog[i, j];
}
for(i=0;i<=Width-1;i++)
for (j = 0; j <= Height-1; j++)
{
FFTLog[i, j] = FFTLog[i, j] / max;
}
for(i=0;i<=Width-1;i++)
for (j = 0; j <= Height-1; j++)
{
FFTNormalized [i,j]=(int)(1000*FFTLog[i,j]);
}
//Transferring Image to Fourier Plot
FourierPlot = Displayimage(FFTNormalized);
//generating phase Bitmap
max = FFTPhaseLog[0, 0];
for (i = 0; i <= Width-1; i++)
for (j = 0; j <= Height-1; j++)
{
if (FFTPhaseLog[i, j] > max)
max = FFTPhaseLog[i, j];
}
for (i = 0; i <= Width-1; i++)
for (j = 0; j <= Height-1; j++)
{
FFTPhaseLog[i, j] = FFTPhaseLog[i, j] / max;
}
for (i = 0; i <= Width-1; i++)
for (j = 0; j <= Height-1; j++)
{
FFTPhaseNormalized[i, j] = (int)(2000 * FFTLog[i, j]);
}
//Transferring Image to Fourir Plot
FourierPlot = Displayimage(FFTNormalized);
PhasePlot1 = FFTPhaseNormalized;
}
/// <summary>////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Calculate Inverse from Complex [,] Fourier Array
/// </summary>
///
public void InverseFFT()
{
//Initializing Fourier Transform Array
int i, j;
//Calling Forward Fourier Transform
Output =new COMPLEX [nx,ny];
Output = FFT2D(Fourier, nx, ny, -1);
Obj = null; // Setting Object Image to Null
//Copying Real Image Back to Greyscale
//Copy Image Data to the Complex Array
for (i = 0; i <= Width - 1; i++)
for (j = 0; j <= Height - 1; j++)
{
GreyImage[i, j] = (int)Output[i, j].Magnitude();
}
Obj = Displayimage(GreyImage);
return;
}
/// <summary>
/// Generates Inverse FFT of Given Input Fourier
/// </summary>
/// <param name="Fourier"></param>
public void InverseFFT(COMPLEX [,] Fourier)
{
//Initializing Fourier Transform Array
int i, j;
//Calling Forward Fourier Transform
Output = new COMPLEX[nx, ny];
Output = FFT2D(Fourier, nx, ny, -1);
//Copying Real Image Back to Greyscale
//Copy Image Data to the Complex Array
for (i = 0; i <= Width - 1; i++)
for (j = 0; j <= Height - 1; j++)
{
GreyImage[i, j] = (int)Output [i, j].Magnitude();
}
Obj = Displayimage(GreyImage);
return;
}
public void InverseOBJ(COMPLEX[,] Fourier)
{
//Initializing Fourier Transform Array
int i, j;
//Calling Forward Fourier Transform
Output = new COMPLEX[nx, ny];
Output = FFT2D(Fourier, nx, ny, -1);
//Copying Real Image Back to Greyscale
//Copy Image Data to the Complex Array
for (i = 0; i <= Width - 1; i++)
for (j = 0; j <= Height - 1; j++)
{
GreyImage[i, j] = (int)Fourier[i, j].Magnitude();
}
Obj = Displayimage(GreyImage);
return;
}
/*-------------------------------------------------------------------------
Perform a 2D FFT inplace given a complex 2D array
The direction dir, 1 for forward, -1 for reverse
The size of the array (nx,ny)
Return false if there are memory problems or
the dimensions are not powers of 2
*/
public COMPLEX [,] FFT2D(COMPLEX[,] c, int nx, int ny, int dir)
{
int i,j;
int m;//Power of 2 for current number of points
double []real;
double []imag;
COMPLEX [,] output;//=new COMPLEX [nx,ny];
output = c; // Copying Array
// Transform the Rows
real = new double[nx] ;
imag = new double[nx];
for (j=0;j<ny;j++)
{
for (i=0;i<nx;i++)
{
real[i] = c[i,j].real;
imag[i] = c[i,j].imag;
}
// Calling 1D FFT Function for Rows
m = (int)Math.Log((double)nx, 2);//Finding power of 2 for current number of points e.g. for nx=512 m=9
FFT1D(dir,m,ref real,ref imag);
for (i=0;i<nx;i++)
{
// c[i,j].real = real[i];
// c[i,j].imag = imag[i];
output[i, j].real = real[i];
output[i, j].imag = imag[i];
}
}
// Transform the columns
real = new double[ny];
imag = new double[ny];
for (i=0;i<nx;i++)
{
for (j=0;j<ny;j++)
{
//real[j] = c[i,j].real;
//imag[j] = c[i,j].imag;
real[j] = output[i, j].real;
imag[j] = output[i, j].imag;
}
// Calling 1D FFT Function for Columns
m = (int)Math.Log((double)ny, 2);//Finding power of 2 for current number of points e.g. for nx=512 m=9
FFT1D(dir,m,ref real,ref imag);
for (j=0;j<ny;j++)
{
//c[i,j].real = real[j];
//c[i,j].imag = imag[j];
output[i, j].real = real[j];
output[i, j].imag = imag[j];
}
}
// return(true);
return(output);
}
/*-------------------------------------------------------------------------
This computes an in-place complex-to-complex FFT
x and y are the real and imaginary arrays of 2^m points.
dir = 1 gives forward transform
dir = -1 gives reverse transform
Formula: forward
N-1
---
1 \ - j k 2 pi n / N
X(K) = --- > x(n) e = Forward transform
N / n=0..N-1
---
n=0
Formula: reverse
N-1
---
\ j k 2 pi n / N
X(n) = > x(k) e = Inverse transform
/ k=0..N-1
---
k=0
*/
private void FFT1D(int dir, int m, ref double[] x, ref double[] y )
{
long nn, i, i1, j, k, i2, l, l1, l2;
double c1, c2, tx, ty, t1, t2, u1, u2, z;
/* Calculate the number of points */
nn = 1;
for (i = 0; i < m; i++)
nn *= 2;
/* Do the bit reversal */
i2 = nn >> 1;
j = 0;
for (i = 0; i < nn - 1; i++)
{
if (i < j)
{
tx = x[i];
ty = y[i];
x[i] = x[j];
y[i] = y[j];
x[j] = tx;
y[j] = ty;
}
k = i2;
while (k <= j)
{
j -= k;
k >>= 1;
}
j += k;
}
/* Compute the FFT */
c1 = -1.0;
c2 = 0.0;
l2 = 1;
for (l = 0; l < m; l++)
{
l1 = l2;
l2 <<= 1;
u1 = 1.0;
u2 = 0.0;
for (j = 0; j < l1; j++)
{
for (i = j; i < nn; i += l2)
{
i1 = i + l1;
t1 = u1 * x[i1] - u2 * y[i1];
t2 = u1 * y[i1] + u2 * x[i1];
x[i1] = x[i] - t1;
y[i1] = y[i] - t2;
x[i] += t1;
y[i] += t2;
}
z = u1 * c1 - u2 * c2;
u2 = u1 * c2 + u2 * c1;
u1 = z;
}
c2 = Math.Sqrt((1.0 - c1) / 2.0);
if (dir == 1)
c2 = -c2;
c1 = Math.Sqrt((1.0 + c1) / 2.0);
}
/* Scaling for forward transform */
if (dir == 1)
{
for (i = 0; i < nn; i++)
{
x[i] /= (double)nn;
y[i] /= (double)nn;
}
}
// return(true) ;
return;
}
}
}