NFFT  3.3.0
reconstruct_data_inh_nnfft.c
1 /*
2  * Copyright (c) 2002, 2015 Jens Keiner, Stefan Kunis, Daniel Potts
3  *
4  * This program is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 51
16  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 /* $Id$ */
20 #include <stdlib.h>
21 #include <math.h>
22 #include <limits.h>
23 #include <complex.h>
24 
25 #include "nfft3.h"
26 
27 #ifndef MAX
28 #define MAX(a,b) (((a)>(b))?(a):(b))
29 #endif
30 
40 static void reconstruct(char* filename,int N,int M,int iteration, int weight)
41 {
42  int j,k,l; /* some variables */
43  nnfft_plan my_plan; /* plan for the two dimensional nfft */
44  solver_plan_complex my_iplan; /* plan for the two dimensional infft */
45  FILE* fin; /* input file */
46  FILE* finh;
47  FILE* ftime;
48  FILE* fout_real; /* output file */
49  FILE* fout_imag; /* output file */
50  int my_N[3],my_n[3]; /* to init the nfft */
51  double t0, t1;
52  double t,epsilon=0.0000003; /* epsilon is a the break criterium for
53  the iteration */
54  unsigned infft_flags = CGNR | PRECOMPUTE_DAMP; /* flags for the infft*/
55  double time,min_time,max_time,min_inh,max_inh;
56  double real,imag;
57  double *w;
58 
59  double Ts;
60  double W;
61  int N3;
62  int m=2;
63  double sigma = 1.25;
64 
65  w = (double*)nfft_malloc(N*N*sizeof(double));
66 
67  ftime=fopen("readout_time.dat","r");
68  finh=fopen("inh.dat","r");
69 
70  min_time=INT_MAX; max_time=INT_MIN;
71  for(j=0;j<M;j++)
72  {
73  fscanf(ftime,"%le ",&time);
74  if(time<min_time)
75  min_time = time;
76  if(time>max_time)
77  max_time = time;
78  }
79 
80  fclose(ftime);
81 
82  Ts=(min_time+max_time)/2.0;
83 
84  min_inh=INT_MAX; max_inh=INT_MIN;
85  for(j=0;j<N*N;j++)
86  {
87  fscanf(finh,"%le ",&w[j]);
88  if(w[j]<min_inh)
89  min_inh = w[j];
90  if(w[j]>max_inh)
91  max_inh = w[j];
92  }
93  fclose(finh);
94 
95  N3=ceil((MAX(fabs(min_inh),fabs(max_inh))*(max_time-min_time)/2.0)*4);
96 
97 
98  W=MAX(fabs(min_inh),fabs(max_inh))*2.0;
99 
100  fprintf(stderr,"3: %i %e %e %e %e %e %e\n",N3,W,min_inh,max_inh,min_time,max_time,Ts);
101 
102  /* initialise my_plan */
103  my_N[0]=N;my_n[0]=ceil(N*sigma);
104  my_N[1]=N; my_n[1]=ceil(N*sigma);
105  my_N[2]=N3; my_n[2]=ceil(N3*sigma);
106  nnfft_init_guru(&my_plan, 3, N*N, M, my_N,my_n,m,
107  PRE_PSI| PRE_PHI_HUT| MALLOC_X| MALLOC_V| MALLOC_F_HAT| MALLOC_F );
108 
109  /* precompute lin psi if set */
110  if(my_plan.nnfft_flags & PRE_LIN_PSI)
111  nnfft_precompute_lin_psi(&my_plan);
112 
113  /* set the flags for the infft*/
114  if (weight)
115  infft_flags = infft_flags | PRECOMPUTE_WEIGHT;
116 
117  /* initialise my_iplan, advanced */
118  solver_init_advanced_complex(&my_iplan,(nfft_mv_plan_complex*)(&my_plan), infft_flags );
119 
120  /* get the weights */
121  if(my_iplan.flags & PRECOMPUTE_WEIGHT)
122  {
123  fin=fopen("weights.dat","r");
124  for(j=0;j<my_plan.M_total;j++)
125  {
126  fscanf(fin,"%le ",&my_iplan.w[j]);
127  }
128  fclose(fin);
129  }
130 
131  /* get the damping factors */
132  if(my_iplan.flags & PRECOMPUTE_DAMP)
133  {
134  for(j=0;j<N;j++){
135  for(k=0;k<N;k++) {
136  int j2= j-N/2;
137  int k2= k-N/2;
138  double r=sqrt(j2*j2+k2*k2);
139  if(r>(double) N/2)
140  my_iplan.w_hat[j*N+k]=0.0;
141  else
142  my_iplan.w_hat[j*N+k]=1.0;
143  }
144  }
145  }
146 
147  /* open the input file */
148  fin=fopen(filename,"r");
149  ftime=fopen("readout_time.dat","r");
150 
151  for(j=0;j<my_plan.M_total;j++)
152  {
153  fscanf(fin,"%le %le %le %le ",&my_plan.x[3*j+0],&my_plan.x[3*j+1],&real,&imag);
154  my_iplan.y[j]=real+ _Complex_I*imag;
155  fscanf(ftime,"%le ",&my_plan.x[3*j+2]);
156 
157  my_plan.x[3*j+2] = (my_plan.x[3*j+2]-Ts)*W/N3;
158  }
159 
160  for(j=0;j<N;j++)
161  {
162  for(l=0;l<N;l++)
163  {
164  my_plan.v[3*(N*j+l)+0]=(((double) j) -(((double) N)/2.0))/((double) N);
165  my_plan.v[3*(N*j+l)+1]=(((double) l) -(((double) N)/2.0))/((double) N);
166  my_plan.v[3*(N*j+l)+2] = w[N*j+l]/W ;
167  }
168  }
169 
170  /* precompute psi */
171  if(my_plan.nnfft_flags & PRE_PSI) {
172  nnfft_precompute_psi(&my_plan);
173  if(my_plan.nnfft_flags & PRE_FULL_PSI)
174  nnfft_precompute_full_psi(&my_plan);
175  }
176 
177  if(my_plan.nnfft_flags & PRE_PHI_HUT)
178  nnfft_precompute_phi_hut(&my_plan);
179 
180  /* init some guess */
181  for(k=0;k<my_plan.N_total;k++)
182  {
183  my_iplan.f_hat_iter[k]=0.0;
184  }
185 
186  t0 = nfft_clock_gettime_seconds();
187 
188  /* inverse trafo */
189  solver_before_loop_complex(&my_iplan);
190  for(l=0;l<iteration;l++)
191  {
192  /* break if dot_r_iter is smaller than epsilon*/
193  if(my_iplan.dot_r_iter<epsilon)
194  break;
195  fprintf(stderr,"%e, %i of %i\n",sqrt(my_iplan.dot_r_iter),
196  l+1,iteration);
197  solver_loop_one_step_complex(&my_iplan);
198  }
199 
200  t1 = nfft_clock_gettime_seconds();
201  t = t1-t0;
202 
203  fout_real=fopen("output_real.dat","w");
204  fout_imag=fopen("output_imag.dat","w");
205 
206  for(k=0;k<my_plan.N_total;k++) {
207 
208  my_iplan.f_hat_iter[k]*=cexp(2.0*_Complex_I*M_PI*Ts*w[k]);
209 
210  fprintf(fout_real,"%le ", creal(my_iplan.f_hat_iter[k]));
211  fprintf(fout_imag,"%le ", cimag(my_iplan.f_hat_iter[k]));
212  }
213 
214 
215  fclose(fout_real);
216  fclose(fout_imag);
217 
218 
219  /* finalize the infft */
220  solver_finalize_complex(&my_iplan);
221 
222  /* finalize the nfft */
223  nnfft_finalize(&my_plan);
224 
225  nfft_free(w);
226 }
227 
228 int main(int argc, char **argv)
229 {
230  if (argc <= 5) {
231  printf("usage: ./reconstruct_data_inh_nnfft FILENAME N M ITER WEIGHTS\n");
232  return 1;
233  }
234 
235  reconstruct(argv[1],atoi(argv[2]),atoi(argv[3]),atoi(argv[4]),atoi(argv[5]));
236 
237  return 1;
238 }
239 /* \} */
static void reconstruct(char *filename, int N, int M, int iteration, int weight)
reconstruct
void nnfft_precompute_full_psi(nnfft_plan *ths_plan)
computes all entries of B explicitly
Definition: nnfft.c:426
unsigned nnfft_flags
flags for precomputation, malloc
Definition: nfft3.h:426
double * w
weighting factors
Definition: nfft3.h:786
unsigned flags
iteration type
Definition: nfft3.h:786
double dot_r_iter
weighted dotproduct of r_iter
Definition: nfft3.h:786
double * v
nodes (in fourier domain)
Definition: nfft3.h:426
void nfft_free(void *p)
void nnfft_precompute_phi_hut(nnfft_plan *ths_plan)
initialisation of direct transform
Definition: nnfft.c:349
NFFT_INT M_total
Total number of samples.
Definition: nfft3.h:426
data structure for an NNFFT (nonequispaced in time and frequency fast Fourier transform) plan with do...
Definition: nfft3.h:426
double * x
nodes (in time/spatial domain)
Definition: nfft3.h:426
void * nfft_malloc(size_t n)
void nnfft_precompute_lin_psi(nnfft_plan *ths_plan)
create a lookup table
Definition: nnfft.c:369
fftw_complex * y
right hand side, samples
Definition: nfft3.h:786
NFFT_INT N_total
Total number of Fourier coefficients.
Definition: nfft3.h:426
data structure for an inverse NFFT plan with double precision
Definition: nfft3.h:786
double * w_hat
damping factors
Definition: nfft3.h:786
fftw_complex * f_hat_iter
iterative solution
Definition: nfft3.h:786