NFFT  3.4.1
reconstruct_data_inh_nnfft.c
1 /*
2  * Copyright (c) 2002, 2017 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 #include <stdlib.h>
19 #include <math.h>
20 #include <limits.h>
21 #include <complex.h>
22 
23 #include "nfft3.h"
24 
25 #ifndef MAX
26 #define MAX(a,b) (((a)>(b))?(a):(b))
27 #endif
28 
38 static void reconstruct(char* filename,int N,int M,int iteration, int weight)
39 {
40  int j,k,l; /* some variables */
41  nnfft_plan my_plan; /* plan for the two dimensional nfft */
42  solver_plan_complex my_iplan; /* plan for the two dimensional infft */
43  FILE* fin; /* input file */
44  FILE* finh;
45  FILE* ftime;
46  FILE* fout_real; /* output file */
47  FILE* fout_imag; /* output file */
48  int my_N[3],my_n[3]; /* to init the nfft */
49  double t0, t1;
50  double t,epsilon=0.0000003; /* epsilon is a the break criterium for
51  the iteration */
52  unsigned infft_flags = CGNR | PRECOMPUTE_DAMP; /* flags for the infft*/
53  double time,min_time,max_time,min_inh,max_inh;
54  double real,imag;
55  double *w;
56 
57  double Ts;
58  double W;
59  int N3;
60  int m=2;
61  double sigma = 1.25;
62 
63  w = (double*)nfft_malloc(N*N*sizeof(double));
64 
65  ftime=fopen("readout_time.dat","r");
66  finh=fopen("inh.dat","r");
67 
68  min_time=INT_MAX; max_time=INT_MIN;
69  for(j=0;j<M;j++)
70  {
71  fscanf(ftime,"%le ",&time);
72  if(time<min_time)
73  min_time = time;
74  if(time>max_time)
75  max_time = time;
76  }
77 
78  fclose(ftime);
79 
80  Ts=(min_time+max_time)/2.0;
81 
82  min_inh=INT_MAX; max_inh=INT_MIN;
83  for(j=0;j<N*N;j++)
84  {
85  fscanf(finh,"%le ",&w[j]);
86  if(w[j]<min_inh)
87  min_inh = w[j];
88  if(w[j]>max_inh)
89  max_inh = w[j];
90  }
91  fclose(finh);
92 
93  N3=ceil((MAX(fabs(min_inh),fabs(max_inh))*(max_time-min_time)/2.0)*4);
94 
95 
96  W=MAX(fabs(min_inh),fabs(max_inh))*2.0;
97 
98  fprintf(stderr,"3: %i %e %e %e %e %e %e\n",N3,W,min_inh,max_inh,min_time,max_time,Ts);
99 
100  /* initialise my_plan */
101  my_N[0]=N;my_n[0]=ceil(N*sigma);
102  my_N[1]=N; my_n[1]=ceil(N*sigma);
103  my_N[2]=N3; my_n[2]=ceil(N3*sigma);
104  nnfft_init_guru(&my_plan, 3, N*N, M, my_N,my_n,m,
106 
107  /* precompute lin psi if set */
108  if(my_plan.nnfft_flags & PRE_LIN_PSI)
109  nnfft_precompute_lin_psi(&my_plan);
110 
111  /* set the flags for the infft*/
112  if (weight)
113  infft_flags = infft_flags | PRECOMPUTE_WEIGHT;
114 
115  /* initialise my_iplan, advanced */
116  solver_init_advanced_complex(&my_iplan,(nfft_mv_plan_complex*)(&my_plan), infft_flags );
117 
118  /* get the weights */
119  if(my_iplan.flags & PRECOMPUTE_WEIGHT)
120  {
121  fin=fopen("weights.dat","r");
122  for(j=0;j<my_plan.M_total;j++)
123  {
124  fscanf(fin,"%le ",&my_iplan.w[j]);
125  }
126  fclose(fin);
127  }
128 
129  /* get the damping factors */
130  if(my_iplan.flags & PRECOMPUTE_DAMP)
131  {
132  for(j=0;j<N;j++){
133  for(k=0;k<N;k++) {
134  int j2= j-N/2;
135  int k2= k-N/2;
136  double r=sqrt(j2*j2+k2*k2);
137  if(r>(double) N/2)
138  my_iplan.w_hat[j*N+k]=0.0;
139  else
140  my_iplan.w_hat[j*N+k]=1.0;
141  }
142  }
143  }
144 
145  /* open the input file */
146  fin=fopen(filename,"r");
147  ftime=fopen("readout_time.dat","r");
148 
149  for(j=0;j<my_plan.M_total;j++)
150  {
151  fscanf(fin,"%le %le %le %le ",&my_plan.x[3*j+0],&my_plan.x[3*j+1],&real,&imag);
152  my_iplan.y[j]=real+ _Complex_I*imag;
153  fscanf(ftime,"%le ",&my_plan.x[3*j+2]);
154 
155  my_plan.x[3*j+2] = (my_plan.x[3*j+2]-Ts)*W/N3;
156  }
157 
158  for(j=0;j<N;j++)
159  {
160  for(l=0;l<N;l++)
161  {
162  my_plan.v[3*(N*j+l)+0]=(((double) j) -(((double) N)/2.0))/((double) N);
163  my_plan.v[3*(N*j+l)+1]=(((double) l) -(((double) N)/2.0))/((double) N);
164  my_plan.v[3*(N*j+l)+2] = w[N*j+l]/W ;
165  }
166  }
167 
168  /* precompute psi */
169  if(my_plan.nnfft_flags & PRE_PSI) {
170  nnfft_precompute_psi(&my_plan);
171  if(my_plan.nnfft_flags & PRE_FULL_PSI)
172  nnfft_precompute_full_psi(&my_plan);
173  }
174 
175  if(my_plan.nnfft_flags & PRE_PHI_HUT)
176  nnfft_precompute_phi_hut(&my_plan);
177 
178  /* init some guess */
179  for(k=0;k<my_plan.N_total;k++)
180  {
181  my_iplan.f_hat_iter[k]=0.0;
182  }
183 
184  t0 = nfft_clock_gettime_seconds();
185 
186  /* inverse trafo */
187  solver_before_loop_complex(&my_iplan);
188  for(l=0;l<iteration;l++)
189  {
190  /* break if dot_r_iter is smaller than epsilon*/
191  if(my_iplan.dot_r_iter<epsilon)
192  break;
193  fprintf(stderr,"%e, %i of %i\n",sqrt(my_iplan.dot_r_iter),
194  l+1,iteration);
195  solver_loop_one_step_complex(&my_iplan);
196  }
197 
198  t1 = nfft_clock_gettime_seconds();
199  t = t1-t0;
200 
201  fout_real=fopen("output_real.dat","w");
202  fout_imag=fopen("output_imag.dat","w");
203 
204  for(k=0;k<my_plan.N_total;k++) {
205 
206  my_iplan.f_hat_iter[k]*=cexp(2.0*_Complex_I*M_PI*Ts*w[k]);
207 
208  fprintf(fout_real,"%le ", creal(my_iplan.f_hat_iter[k]));
209  fprintf(fout_imag,"%le ", cimag(my_iplan.f_hat_iter[k]));
210  }
211 
212 
213  fclose(fout_real);
214  fclose(fout_imag);
215 
216 
217  /* finalize the infft */
218  solver_finalize_complex(&my_iplan);
219 
220  /* finalize the nfft */
221  nnfft_finalize(&my_plan);
222 
223  nfft_free(w);
224 }
225 
226 int main(int argc, char **argv)
227 {
228  if (argc <= 5) {
229  printf("usage: ./reconstruct_data_inh_nnfft FILENAME N M ITER WEIGHTS\n");
230  return 1;
231  }
232 
233  reconstruct(argv[1],atoi(argv[2]),atoi(argv[3]),atoi(argv[4]),atoi(argv[5]));
234 
235  return 1;
236 }
237 /* \} */
void nnfft_precompute_psi(nnfft_plan *ths_plan)
Definition: nnfft.c:385
static void reconstruct(char *filename, int N, int M, int iteration, int weight)
reconstruct
#define PRECOMPUTE_DAMP
Definition: nfft3.h:792
unsigned nnfft_flags
flags for precomputation, malloc
Definition: nfft3.h:424
#define MALLOC_X
Definition: nfft3.h:199
#define MALLOC_F_HAT
Definition: nfft3.h:200
#define PRECOMPUTE_WEIGHT
Definition: nfft3.h:791
double * w
weighting factors
Definition: nfft3.h:785
unsigned flags
iteration type
Definition: nfft3.h:785
double dot_r_iter
weighted dotproduct of r_iter
Definition: nfft3.h:785
double * v
nodes (in fourier domain)
Definition: nfft3.h:424
void nnfft_precompute_phi_hut(nnfft_plan *ths_plan)
initialisation of direct transform
Definition: nnfft.c:347
void nnfft_precompute_full_psi(nnfft_plan *ths_plan)
computes all entries of B explicitly
Definition: nnfft.c:424
#define MALLOC_V
Definition: nfft3.h:425
void nnfft_precompute_lin_psi(nnfft_plan *ths_plan)
create a lookup table
Definition: nnfft.c:367
void nfft_free(void *p)
NFFT_INT M_total
Total number of samples.
Definition: nfft3.h:424
#define MALLOC_F
Definition: nfft3.h:201
data structure for an NNFFT (nonequispaced in time and frequency fast Fourier transform) plan with do...
Definition: nfft3.h:424
double * x
nodes (in time/spatial domain)
Definition: nfft3.h:424
#define PRE_LIN_PSI
Definition: nfft3.h:195
#define PRE_PSI
Definition: nfft3.h:197
#define CGNR
Definition: nfft3.h:788
void * nfft_malloc(size_t n)
fftw_complex * y
right hand side, samples
Definition: nfft3.h:785
NFFT_INT N_total
Total number of Fourier coefficients.
Definition: nfft3.h:424
#define PRE_FULL_PSI
Definition: nfft3.h:198
Header file for the nfft3 library.
#define PRE_PHI_HUT
Definition: nfft3.h:193
void nnfft_init_guru(nnfft_plan *ths_plan, int d, int N_total, int M_total, int *N, int *N1, int m, unsigned nnfft_flags)
Definition: nnfft.c:577
data structure for an inverse NFFT plan with double precision
Definition: nfft3.h:785
void nnfft_finalize(nnfft_plan *ths_plan)
Definition: nnfft.c:655
double * w_hat
damping factors
Definition: nfft3.h:785
fftw_complex * f_hat_iter
iterative solution
Definition: nfft3.h:785