-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFFTUtil.java
More file actions
253 lines (185 loc) · 4.77 KB
/
Copy pathFFTUtil.java
File metadata and controls
253 lines (185 loc) · 4.77 KB
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
class FFTUtil {
public static
double [][][] doFFT( int[] in, int width)
{
double out[][][] = new double[2] [ width ][ width ];
double re[] = new double[ width ];
double im[] = new double[ width ];
// First row by row
for( int i=0; i < width; i++ ) {
for( int j=0; j < width; j++ ) {
re[j] = in[i * width + j];
im[j] = 0;
}
// FT.FFT( 8, re, im );
FT.DFT( 1, width, re, im );
for( int j=0; j < width; j++ ) {
out[0][i][j] = re[j];
out[1][i][j] = im[j];
}
}
// and now column by columns
for( int i=0; i < width; i++ ) {
for( int j=0; j < width; j++ ) {
re[j] = out[0][j][i];
im[j] = out[1][j][i];
}
// FT.FFT( 8, re, im );
FT.DFT( 1, width, re, im );
for( int j=0; j < width; j++ ) {
out[0][j][i] = re[j];
out[1][j][i] = im[j];
}
}
return out;
}
public static
double [][] [] doIFFT( double[][][] in, int width )
{
double out[][][] = new double [2] [ width ][ width ];
double re[] = new double[ width ];
double im[] = new double[ width ];
// First row by row
for( int i=0; i < width; i++ ) {
for( int j=0; j < width; j++ ) {
re[j] = in[0][i][j];
im[j] = in[1][i][j];
}
// FT.iFFT( 8, re, im );
FT.DFT( -1, width, re, im );
for( int j=0; j < width; j++ ) {
out[0][i][j] = re[j];
out[1][i][j] = im[j];
}
}
// and now column by columns
for( int i=0; i < width; i++ ) {
for( int j=0; j < width; j++ ) {
re[j] = out[0][j][i];
im[j] = out[1][j][i];
}
// FT.iFFT( 8, re, im );
FT.DFT( -1, width, re, im );
for( int j=0; j < width; j++ ) {
out[0][j][i] = re[j];
out[1][j][i] = re[j];
}
}
return out;
}
public static
double magnitude( double re, double im )
{
return Math.sqrt( re*re + im*im );
}
public static
double magnitudeSquare( double re, double im )
{
return ( re*re + im*im );
}
public static
int imagnitude( double re, double im )
{
return (int)Math.round(Math.sqrt( re*re + im*im ));
}
public static
int [] normalizeValues( double [][][] in, int width )
{
int length = width * width;
int img[] = new int[ length ];
int white = 0;
int black = 0;
int min = 1000;
int max = -1000;
for( int i=0; i < width; i++ ) {
for( int j=0; j < width; j++ ) {
int value = (int)(in[0][i][j]);
if ( value < min ) min = value;
else
if ( value > max ) max = value;
img[i* width + j] = value;
}
}
// for( int j=0; j < width; j++ )
// System.out.println("iFFT[255,"+j+"]: "+img[255][j]);
// System.out.print( "Total: [" + width * width + "] White: [" + white + "] Black: [" + black + "]" );
// System.out.println(" Min: ["+min+"] Max:["+max+"]");
return img;
}
public static
double [][] doFFT2( int[] in, int xLength, int yLength )
{
return doFFT2( in, xLength, yLength, xLength, yLength );
/*
int length = xLength * yLength;
double data[][] = new double[length][ 2 ];
for( int i=0; i < length; i++ )
{
data[ i ][0] = in[i];
data[ i ][1] = 0;
}
FT.FFT2( data, xLength, yLength, 0 );
return data;
*/
}
public static
double [][] doFFT2( int[] in, int xLength, int yLength, int xBlock, int yBlock )
{
int length = xLength * yLength;
double data[][] = new double[length][ 2 ];
for( int i=0; i < length; i++ )
{
data[ i ][0] = in[i];
data[ i ][1] = 0;
}
FT.FFT2( data, xLength, yLength, xBlock, yBlock, 0 );
// FT.FFT2( data, xLength, yLength, 0 );
return data;
}
public static
void doFFT2( double[][] data, int xLength, int yLength, int dir)
{
FT.FFT2( data, xLength, yLength, dir );
}
public static
void doFFT2( double[][] data, int xLength, int yLength, int xBlock, int yBlock, int dir)
{
FT.FFT2( data, xLength, yLength, xBlock, yBlock, dir );
}
public static
double [][] doIFFT2( double[][] in, int xLength, int yLength)
{
int length = xLength * yLength;
double data[][] = new double[ length ][ 2 ];
// Copy the image matrix into a flat array
for( int i=0; i < length; i++ ) {
data[ i ][0] = in[i][0];
data[ i ][1] = in[i][1];
}
FT.FFT2( data, xLength, yLength, 8, 8, 1 );
// FT.FFT2( data, xLength, yLength, 1 );
return data;
}
public static
int [] normalizeValues( double[][] in, int xLength, int yLength )
{
int length = xLength * yLength;
int img[] = new int[ length ];
int min = 1000;
int max = -1000;
int cnLt = 0;
int cnGt = 0;
for( int i=0; i < length; i++ ) {
int value = (int)(in[ i ][0]);
if ( value < min ) min = value;
else
if ( value > max ) max = value;
if ( value < 0) { value = 0; cnLt++; }
else
if ( value > 255) { value = 250; cnGt++; }
img[i] = value;
}
System.out.println( "Min: " + min + " Max:" + max + " (Cn < 0 = " + cnLt + ") (Cn > 255 = "+cnGt);
return img;
}
}