NFFT  3.4.1
reconstruct_data_inh_2d1d.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 
35 static void reconstruct(char* filename,int N,int M,int iteration , int weight)
36 {
37  int j,k,l;
38  double time,min_time,max_time,min_inh,max_inh;
39  double t0, t1;
40  double t,real,imag;
41  double w,epsilon=0.0000003; /* epsilon is a the break criterium for
42  the iteration */;
43  mri_inh_2d1d_plan my_plan;
44  solver_plan_complex my_iplan;
45  FILE* fp,*fw,*fout_real,*fout_imag,*finh,*ftime;
46  int my_N[3],my_n[3];
49  unsigned infft_flags = CGNR | PRECOMPUTE_DAMP;
50 
51  double Ts;
52  double W,T;
53  int N3;
54  int m=2;
55  double sigma = 1.25;
56 
57  ftime=fopen("readout_time.dat","r");
58  finh=fopen("inh.dat","r");
59 
60  min_time=INT_MAX; max_time=INT_MIN;
61  for(j=0;j<M;j++)
62  {
63  fscanf(ftime,"%le ",&time);
64  if(time<min_time)
65  min_time = time;
66  if(time>max_time)
67  max_time = time;
68  }
69 
70  fclose(ftime);
71 
72  Ts=(min_time+max_time)/2.0;
73 
74 
75  min_inh=INT_MAX; max_inh=INT_MIN;
76  for(j=0;j<N*N;j++)
77  {
78  fscanf(finh,"%le ",&w);
79  if(w<min_inh)
80  min_inh = w;
81  if(w>max_inh)
82  max_inh = w;
83  }
84  fclose(finh);
85 
86  N3=ceil((MAX(fabs(min_inh),fabs(max_inh))*(max_time-min_time)/2.0+(m)/(2*sigma))*4*sigma);
87  /* N3 has to be even */
88  if(N3%2!=0)
89  N3++;
90 
91  T=((max_time-min_time)/2.0)/(0.5-((double) (m))/N3);
92  W=N3/T;
93 
94  my_N[0]=N; my_n[0]=ceil(N*sigma);
95  my_N[1]=N; my_n[1]=ceil(N*sigma);
96  my_N[2]=N3; my_n[2]=N3;
97 
98  /* initialise nfft */
99  mri_inh_2d1d_init_guru(&my_plan, my_N, M, my_n, m, sigma, flags,
100  FFTW_MEASURE| FFTW_DESTROY_INPUT);
101 
102 
103  /* precompute lin psi if set */
104  if(my_plan.plan.flags & PRE_LIN_PSI)
105  nfft_precompute_lin_psi(&my_plan.plan);
106 
107  if (weight)
108  infft_flags = infft_flags | PRECOMPUTE_WEIGHT;
109 
110  /* initialise my_iplan, advanced */
111  solver_init_advanced_complex(&my_iplan,(nfft_mv_plan_complex*)(&my_plan), infft_flags );
112 
113  /* get the weights */
114  if(my_iplan.flags & PRECOMPUTE_WEIGHT)
115  {
116  fw=fopen("weights.dat","r");
117  for(j=0;j<my_plan.M_total;j++)
118  {
119  fscanf(fw,"%le ",&my_iplan.w[j]);
120  }
121  fclose(fw);
122  }
123 
124  /* get the damping factors */
125  if(my_iplan.flags & PRECOMPUTE_DAMP)
126  {
127  for(j=0;j<N;j++){
128  for(k=0;k<N;k++) {
129  int j2= j-N/2;
130  int k2= k-N/2;
131  double r=sqrt(j2*j2+k2*k2);
132  if(r>(double) N/2)
133  my_iplan.w_hat[j*N+k]=0.0;
134  else
135  my_iplan.w_hat[j*N+k]=1.0;
136  }
137  }
138  }
139 
140  fp=fopen(filename,"r");
141  ftime=fopen("readout_time.dat","r");
142 
143  for(j=0;j<my_plan.M_total;j++)
144  {
145  fscanf(fp,"%le %le %le %le",&my_plan.plan.x[2*j+0],&my_plan.plan.x[2*j+1],&real,&imag);
146  my_iplan.y[j]=real+ _Complex_I*imag;
147  fscanf(ftime,"%le ",&my_plan.t[j]);
148 
149  my_plan.t[j] = (my_plan.t[j]-Ts)/T;
150  }
151  fclose(fp);
152  fclose(ftime);
153 
154 
155  finh=fopen("inh.dat","r");
156  for(j=0;j<N*N;j++)
157  {
158  fscanf(finh,"%le ",&my_plan.w[j]);
159  my_plan.w[j]/=W;
160  }
161  fclose(finh);
162 
163 
164  if(my_plan.plan.flags & PRE_PSI) {
165  nfft_precompute_psi(&my_plan.plan);
166  }
167  if(my_plan.plan.flags & PRE_FULL_PSI) {
168  nfft_precompute_full_psi(&my_plan.plan);
169  }
170 
171  /* init some guess */
172  for(j=0;j<my_plan.N_total;j++)
173  {
174  my_iplan.f_hat_iter[j]=0.0;
175  }
176 
177  t0 = nfft_clock_gettime_seconds();
178 
179  /* inverse trafo */
180  solver_before_loop_complex(&my_iplan);
181  for(l=0;l<iteration;l++)
182  {
183  /* break if dot_r_iter is smaller than epsilon*/
184  if(my_iplan.dot_r_iter<epsilon)
185  break;
186  fprintf(stderr,"%e, %i of %i\n",sqrt(my_iplan.dot_r_iter),
187  l+1,iteration);
188  solver_loop_one_step_complex(&my_iplan);
189  }
190 
191  t1 = nfft_clock_gettime_seconds();
192  t = t1-t0;
193 
194  fout_real=fopen("output_real.dat","w");
195  fout_imag=fopen("output_imag.dat","w");
196 
197  for (j=0;j<N*N;j++) {
198  /* Verschiebung wieder herausrechnen */
199  my_iplan.f_hat_iter[j]*=cexp(-2.0*_Complex_I*M_PI*Ts*my_plan.w[j]*W);
200 
201  fprintf(fout_real,"%le ",creal(my_iplan.f_hat_iter[j]));
202  fprintf(fout_imag,"%le ",cimag(my_iplan.f_hat_iter[j]));
203  }
204 
205  fclose(fout_real);
206  fclose(fout_imag);
207  solver_finalize_complex(&my_iplan);
208  mri_inh_2d1d_finalize(&my_plan);
209 }
210 
211 
212 int main(int argc, char **argv)
213 {
214  if (argc <= 5) {
215 
216  printf("usage: ./reconstruct_data_inh_2d1d FILENAME N M ITER WEIGHTS\n");
217  return 1;
218  }
219 
220  reconstruct(argv[1],atoi(argv[2]),atoi(argv[3]),atoi(argv[4]),atoi(argv[5]));
221 
222  return 1;
223 }
224 /* \} */
#define PRECOMPUTE_DAMP
Definition: nfft3.h:792
#define MALLOC_X
Definition: nfft3.h:199
#define MALLOC_F_HAT
Definition: nfft3.h:200
#define PRECOMPUTE_WEIGHT
Definition: nfft3.h:791
void mri_inh_2d1d_finalize(mri_inh_2d1d_plan *ths)
Definition: mri.c:174
double * w
weighting factors
Definition: nfft3.h:785
unsigned flags
iteration type
Definition: nfft3.h:785
void nfft_precompute_lin_psi(nfft_plan *ths)
double dot_r_iter
weighted dotproduct of r_iter
Definition: nfft3.h:785
void nfft_precompute_full_psi(nfft_plan *ths)
void nfft_precompute_psi(nfft_plan *ths)
#define FFTW_INIT
Definition: nfft3.h:203
NFFT_INT M_total
Total number of samples.
Definition: nfft3.h:525
#define MALLOC_F
Definition: nfft3.h:201
void mri_inh_2d1d_init_guru(mri_inh_2d1d_plan *ths, int *N, int M, int *n, int m, double sigma, unsigned nfft_flags, unsigned fftw_flags)
Definition: mri.c:156
#define FFT_OUT_OF_PLACE
Definition: nfft3.h:202
#define PRE_LIN_PSI
Definition: nfft3.h:195
#define PRE_PSI
Definition: nfft3.h:197
#define CGNR
Definition: nfft3.h:788
fftw_complex * y
right hand side, samples
Definition: nfft3.h:785
#define PRE_FULL_PSI
Definition: nfft3.h:198
Header file for the nfft3 library.
double * x
Nodes in time/spatial domain, size is doubles.
Definition: nfft3.h:192
#define PRE_PHI_HUT
Definition: nfft3.h:193
NFFT_INT N_total
Total number of Fourier coefficients.
Definition: nfft3.h:525
unsigned flags
Flags for precomputation, (de)allocation, and FFTW usage, default setting is PRE_PHI_HUT | PRE_PSI | ...
Definition: nfft3.h:192
data structure for an inverse NFFT plan with double precision
Definition: nfft3.h:785
double * w_hat
damping factors
Definition: nfft3.h:785
fftw_complex * f_hat_iter
iterative solution
Definition: nfft3.h:785