fft2_filt.c 3.63 KB
Newer Older
niklas.thiel's avatar
niklas.thiel committed
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
/*-----------------------------------------------------------------------------------------
 * Copyright (C) 2016  For the list of authors, see file AUTHORS.
 *
 * This file is part of IFOS.
 *
 * IFOS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 2.0 of the License only.
 *
 * IFOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with IFOS. See file COPYING and/or <http://www.gnu.org/licenses/gpl-2.0.html>.
 -----------------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------
 *   2D-FFT preparation
 *  ----------------------------------------------------------------------*/

#include "fd.h"

void	fft2(float **array, float **arrayim, int NYG, int NXG, int dir) {

		extern FILE *FP;
		
		COMPLEX	*C;
		int	j, i, k, nx, ny, nxy;
		float **RE, **IM;
	
		fprintf(FP,"\n Testposition fft2_filt 1\n ");
		usleep(900000);
	
		/****************************************************/
		/* recompute the dimensions to become values of 2^m */
		nx = (int)(pow(2,(ceil(log(NXG)/log(2)))));
		ny = (int)(pow(2,(ceil(log(NYG)/log(2)))));
		nxy = max(nx,ny);
		nxy *= 2;
		
		fprintf(FP,"\n Testposition fft2_filt 1, nxy: %i\n ",nxy);
		usleep(900000);
		
		RE = matrix(1, nxy, 1, nxy);
		IM = matrix(1, nxy, 1, nxy);
		C = cplxvector(1, nxy*nxy);
		

				
		/* copy array data to the temp-array */
		for (i=1;i<=NXG;i++)
			for (j=1;j<=NYG;j++){
			  RE[j][i] = array[j][i];
			  IM[j][i] = arrayim[j][i];
			}
			
		fprintf(FP,"\n Testposition fft2_filt 3\n ");
		usleep(900000);
	
	if (dir==1) {
		k = 1;
		for (i=1;i<=nxy;i++)
			for (j=1;j<=nxy;j++) {
				C[k].re = RE[j][i];
				C[k].im = 0.0;
				k++;
			}
		forward_fft2f(C, nxy, nxy);
		k = 1;
		for (i=1;i<=nxy;i++)
			for (j=1;j<=nxy;j++) {
				RE[j][i] = C[k].re;
				IM[j][i] = C[k].im;
				k++;
			}
	} else {
		k = 1;
		for (i=1;i<=nxy;i++)
			for (j=1;j<=nxy;j++) {
				C[k].re = RE[j][i];
				C[k].im = IM[j][i];
				k++;
			}
		inverse_fft2f(C, nxy, nxy);
		k = 1;
		for (i=1;i<=nxy;i++)
			for (j=1;j<=nxy;j++) {
				RE[j][i] = C[k].re;
				IM[j][i] = 0.0;
				k++;
			}
	}
	/* write result back into array*/
	for (i=1;i<=NXG;i++)
		for (j=1;j<=NYG;j++){
			  array[j][i]=RE[j][i];
			  arrayim[j][i]=IM[j][i];
		}
				
	free_cplxvector(C, 1, nxy*nxy);
	free_matrix(RE, 1,nxy,1,nxy);
	free_matrix(IM, 1,nxy,1,nxy);
}



/********************************************************/
/* 2D-FFTSHIFT						*/
/********************************************************/
// void	fft2shift(float **RE, float **IM, int ny, int nx) {
// 
// 	int	j, i;
// 	float	**A;
// 		
// 	
// 	A = matrix(1, ny, 1, nx);
// 	
// 	/* shift the real part */
// 	for (i=1;i<=nx;i++)
// 		for (j=1;j<=ny;j++)	A[j][i] = RE[j][i];
// 	for (i=1;i<=nx/2;i++)
// 		for (j=1;j<=ny/2;j++) {
// 			RE[j][i]		= A[j+ny/2][i+nx/2];
// 			RE[j+ny/2][i+nx/2]	= A[j][i];
// 			RE[j+ny/2][i]		= A[j][i+nx/2];
// 			RE[j][i+nx/2]		= A[j+ny/2][i];
// 		}
// 
// 	/* shift the imaginary part */
// 	for (i=1;i<=nx;i++)
// 		for (j=1;j<=ny;j++)	A[j][i] = IM[j][i];
// 	for (i=1;i<=nx/2;i++)
// 		for (j=1;j<=ny/2;j++) {
// 			IM[j][i]		= A[j+ny/2][i+nx/2];
// 			IM[j+ny/2][i+nx/2]	= A[j][i];
// 			IM[j+ny/2][i]		= A[j][i+nx/2];
// 			IM[j][i+nx/2]		= A[j+ny/2][i];
// 		}
// 	
// 	free_matrix(A, 1, ny, 1, nx);
// }