NFFT  3.3.0
reconstruct_data_2d.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 <math.h>
21 #include <stdlib.h>
22 #include <complex.h>
23 
24 #include "nfft3.h"
25 
35 static void reconstruct(char* filename,int N,int M,int iteration, int weight)
36 {
37  int j,k,l; /* some variables */
38  double t0, t1;
39  double real,imag,t; /* to read the real and imag part of a complex number */
40  nfft_plan my_plan; /* plan for the two dimensional nfft */
41  solver_plan_complex my_iplan; /* plan for the two dimensional infft */
42  FILE* fin; /* input file */
43  FILE* fout_real; /* output file */
44  FILE* fout_imag; /* output file */
45  int my_N[2],my_n[2]; /* to init the nfft */
46  double epsilon=0.0000003; /* epsilon is a the break criterium for
47  the iteration */
48  unsigned infft_flags = CGNR | PRECOMPUTE_DAMP; /* flags for the infft*/
49  int m = 6;
50  double alpha = 2.0;
51  /* initialise my_plan */
52  my_N[0]=N; my_n[0]=ceil(N*alpha);
53  my_N[1]=N; my_n[1]=ceil(N*alpha);
54  nfft_init_guru(&my_plan, 2, my_N, M, my_n, m, PRE_PHI_HUT| PRE_PSI|
55  MALLOC_X| MALLOC_F_HAT| MALLOC_F|
56  FFTW_INIT| FFT_OUT_OF_PLACE,
57  FFTW_MEASURE| FFTW_DESTROY_INPUT);
58 
59  /* precompute lin psi if set */
60  if(my_plan.flags & PRE_LIN_PSI)
61  nfft_precompute_lin_psi(&my_plan);
62 
63  /* set the flags for the infft*/
64  if (weight)
65  infft_flags = infft_flags | PRECOMPUTE_WEIGHT;
66 
67  /* initialise my_iplan, advanced */
68  solver_init_advanced_complex(&my_iplan,(nfft_mv_plan_complex*)&my_plan, infft_flags );
69 
70  /* get the weights */
71  if(my_iplan.flags & PRECOMPUTE_WEIGHT)
72  {
73  fin=fopen("weights.dat","r");
74  for(j=0;j<my_plan.M_total;j++)
75  {
76  fscanf(fin,"%le ",&my_iplan.w[j]);
77  }
78  fclose(fin);
79  }
80 
81  /* get the damping factors */
82  if(my_iplan.flags & PRECOMPUTE_DAMP)
83  {
84  for(j=0;j<N;j++){
85  for(k=0;k<N;k++) {
86  int j2= j-N/2;
87  int k2= k-N/2;
88  double r=sqrt(j2*j2+k2*k2);
89  if(r>(double) N/2)
90  my_iplan.w_hat[j*N+k]=0.0;
91  else
92  my_iplan.w_hat[j*N+k]=1.0;
93  }
94  }
95  }
96 
97  /* open the input file */
98  fin=fopen(filename,"r");
99 
100  /* read x,y,freal and fimag from the knots */
101  for(j=0;j<my_plan.M_total;j++)
102  {
103  fscanf(fin,"%le %le %le %le ",&my_plan.x[2*j+0],&my_plan.x[2*j+1],
104  &real,&imag);
105  my_iplan.y[j] = real + _Complex_I*imag;
106  }
107 
108  fclose(fin);
109 
110  /* precompute psi */
111  if(my_plan.flags & PRE_PSI)
112  nfft_precompute_psi(&my_plan);
113 
114  /* precompute full psi */
115  if(my_plan.flags & PRE_FULL_PSI)
116  nfft_precompute_full_psi(&my_plan);
117 
118  /* init some guess */
119  for(k=0;k<my_plan.N_total;k++)
120  my_iplan.f_hat_iter[k]=0.0;
121 
122  t0 = nfft_clock_gettime_seconds();
123 
124  /* inverse trafo */
125  solver_before_loop_complex(&my_iplan);
126  for(l=0;l<iteration;l++)
127  {
128  /* break if dot_r_iter is smaller than epsilon*/
129  if(my_iplan.dot_r_iter<epsilon)
130  break;
131  fprintf(stderr,"%e, %i of %i\n",sqrt(my_iplan.dot_r_iter),
132  l+1,iteration);
133  solver_loop_one_step_complex(&my_iplan);
134  }
135 
136 
137  t1 = nfft_clock_gettime_seconds();
138  t=t1-t0;
139 
140  fout_real=fopen("output_real.dat","w");
141  fout_imag=fopen("output_imag.dat","w");
142 
143  for(k=0;k<my_plan.N_total;k++) {
144  fprintf(fout_real,"%le ", creal(my_iplan.f_hat_iter[k]));
145  fprintf(fout_imag,"%le ", cimag(my_iplan.f_hat_iter[k]));
146  }
147 
148  fclose(fout_real);
149  fclose(fout_imag);
150 
151  /* finalize the infft */
152  solver_finalize_complex(&my_iplan);
153 
154  /* finalize the nfft */
155  nfft_finalize(&my_plan);
156 }
157 
158 int main(int argc, char **argv)
159 {
160  if (argc <= 5) {
161  printf("usage: ./reconstruct_data_2d FILENAME N M ITER WEIGHTS\n");
162  return 1;
163  }
164 
165  reconstruct(argv[1],atoi(argv[2]),atoi(argv[3]),atoi(argv[4]),atoi(argv[5]));
166 
167  return 1;
168 }
169 /* \} */
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
data structure for an NFFT (nonequispaced fast Fourier transform) plan with double precision ...
Definition: nfft3.h:194
NFFT_INT N_total
Total number of Fourier coefficients.
Definition: nfft3.h:194
NFFT_INT M_total
Total number of samples.
Definition: nfft3.h:194
double * alpha
Precomputed recursion coefficients /f$^n/f$ for /f$k = 0,/ldots, N_{{max}}; n=-k,/ldots,k/f$ of associated Legendre-functions /f$P_k^n/f$.
fftw_complex * y
right hand side, samples
Definition: nfft3.h:786
double * x
Nodes in time/spatial domain, size is doubles.
Definition: nfft3.h:194
unsigned flags
Flags for precomputation, (de)allocation, and FFTW usage, default setting is PRE_PHI_HUT | PRE_PSI | ...
Definition: nfft3.h:194
data structure for an inverse NFFT plan with double precision
Definition: nfft3.h:786
double * w_hat
damping factors
Definition: nfft3.h:786
static void reconstruct(char *filename, int N, int M, int iteration, int weight)
reconstruct makes an inverse 2d nfft
fftw_complex * f_hat_iter
iterative solution
Definition: nfft3.h:786