File: simple_test.c

package info (click to toggle)
nfft 3.5.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,972 kB
  • sloc: ansic: 44,442; lisp: 1,697; makefile: 1,322; sh: 744; sed: 5
file content (341 lines) | stat: -rw-r--r-- 10,206 bytes parent folder | download
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * Copyright (c) 2002, 2017 Jens Keiner, Stefan Kunis, Daniel Potts
 *
 * This program 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; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program 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
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <stdlib.h>
#include <math.h>
#include <complex.h>

#include "nfft3.h"

/** Swap two vectors. */
#define CSWAP(x,y) {double _Complex * NFFT_SWAP_temp__; \
  NFFT_SWAP_temp__=(x); (x)=(y); (y)=NFFT_SWAP_temp__;}

void simple_test_nnfft_1d(void)
{
  int j,k;                              /**< index for nodes and freqencies   */
  nnfft_plan my_plan;                    /**< plan for the nfft                */

  int N[1];
  N[0]=10;

  /** init an one dimensional plan */
  nnfft_init(&my_plan, 1, 3, 19, N);

  /** init pseudo random nodes */
  for(j=0;j<my_plan.M_total;j++)
  {
    my_plan.x[j]=((double)rand())/((double)RAND_MAX)-0.5;
  }
  /** init pseudo random nodes */
  for(j=0;j<my_plan.N_total;j++)
  {
    my_plan.v[j]=((double)rand())/((double)RAND_MAX)-0.5;
  }

  /** precompute psi, the entries of the matrix B */
/*  if(my_plan.nnfft_flags & PRE_PSI)
        nnfft_precompute_psi(&my_plan);

  if(my_plan.nnfft_flags & PRE_FULL_PSI)
 	nnfft_precompute_full_psi(&my_plan);
  if(my_plan.nnfft_flags & PRE_LIN_PSI)
	 nnfft_precompute_lin_psi(&my_plan);
  /** precompute phi_hut, the entries of the matrix D */
/*  if(my_plan.nnfft_flags & PRE_PHI_HUT)
	  nnfft_precompute_phi_hut(&my_plan);
*/

nnfft_precompute_one_psi(&my_plan);


  /** init pseudo random Fourier coefficients and show them */
  for(k=0;k<my_plan.N_total;k++)
    my_plan.f_hat[k] = ((double)rand())/((double)RAND_MAX) + _Complex_I*((double)rand())/((double)RAND_MAX);

  nfft_vpr_complex(my_plan.f_hat,my_plan.N_total,"given Fourier coefficients, vector f_hat");

  /** direct trafo and show the result */
  nnfft_trafo_direct(&my_plan);
  nfft_vpr_complex(my_plan.f,my_plan.M_total,"nndft, vector f");

  /** approx. trafo and show the result */
  nnfft_trafo(&my_plan);
  nfft_vpr_complex(my_plan.f,my_plan.M_total,"nnfft, vector f");

  /** finalise the one dimensional plan */
  nnfft_finalize(&my_plan);
}

static void simple_test_adjoint_nnfft_1d(void)
{
  int j;                                 /**< index for nodes and freqencies   */
  nnfft_plan my_plan;                    /**< plan for the nfft                */

  int N[1];
  N[0]=12;

  /** init an one dimensional plan */
  nnfft_init(&my_plan, 1, 20, 33, N);

  /** init pseudo random nodes */
  for(j=0;j<my_plan.M_total;j++)
  {
    my_plan.x[j]=((double)rand())/((double)RAND_MAX)-0.5;
  }
  /** init pseudo random nodes */
  for(j=0;j<my_plan.N_total;j++)
  {
    my_plan.v[j]=((double)rand())/((double)RAND_MAX)-0.5;
  }

  /** precompute psi, the entries of the matrix B */
  if(my_plan.nnfft_flags & PRE_PSI)
    nnfft_precompute_psi(&my_plan);

  if(my_plan.nnfft_flags & PRE_FULL_PSI)
    nnfft_precompute_full_psi(&my_plan);

  if(my_plan.nnfft_flags & PRE_LIN_PSI)
    nnfft_precompute_lin_psi(&my_plan);

  /** precompute phi_hut, the entries of the matrix D */
  if(my_plan.nnfft_flags & PRE_PHI_HUT)
    nnfft_precompute_phi_hut(&my_plan);

  /** init pseudo random Fourier coefficients and show them */
  for(j=0;j<my_plan.M_total;j++)
    my_plan.f[j] = ((double)rand())/((double)RAND_MAX) + _Complex_I*((double)rand())/((double)RAND_MAX);

  nfft_vpr_complex(my_plan.f,my_plan.M_total,"given Samples, vector f");

  /** direct trafo and show the result */
  nnfft_adjoint_direct(&my_plan);
  nfft_vpr_complex(my_plan.f_hat,my_plan.N_total,"adjoint nndft, vector f_hat");

  /** approx. trafo and show the result */
  nnfft_adjoint(&my_plan);
  nfft_vpr_complex(my_plan.f_hat,my_plan.N_total,"adjoint nnfft, vector f_hat");

  /** finalise the one dimensional plan */
  nnfft_finalize(&my_plan);
}

static void simple_test_nnfft_2d(void)
{
  int j,k;                              /**< index for nodes and freqencies   */
  nnfft_plan my_plan;                    /**< plan for the nfft                */

  int N[2];
  N[0]=12;
  N[1]=14;

  /** init an one dimensional plan */
  nnfft_init(&my_plan, 2,12*14,19, N);

  /** init pseudo random nodes */
  for(j=0;j<my_plan.M_total;j++)
  {
    my_plan.x[2*j]=((double)rand())/((double)RAND_MAX)-0.5;
    my_plan.x[2*j+1]=((double)rand())/((double)RAND_MAX)-0.5;
  }

  /** init pseudo random nodes */
  for(j=0;j<my_plan.N_total;j++)
  {
    my_plan.v[2*j]=((double)rand())/((double)RAND_MAX)-0.5;
    my_plan.v[2*j+1]=((double)rand())/((double)RAND_MAX)-0.5;
  }

  /** precompute psi, the entries of the matrix B */
  if(my_plan.nnfft_flags & PRE_PSI)
    nnfft_precompute_psi(&my_plan);

  if(my_plan.nnfft_flags & PRE_FULL_PSI)
    nnfft_precompute_full_psi(&my_plan);

  if(my_plan.nnfft_flags & PRE_LIN_PSI)
    nnfft_precompute_lin_psi(&my_plan);

  /** precompute phi_hut, the entries of the matrix D */
  if(my_plan.nnfft_flags & PRE_PHI_HUT)
    nnfft_precompute_phi_hut(&my_plan);

  /** init pseudo random Fourier coefficients and show them */
  for(k=0;k<my_plan.N_total;k++)
    my_plan.f_hat[k] = ((double)rand())/((double)RAND_MAX) + _Complex_I*((double)rand())/((double)RAND_MAX);

  nfft_vpr_complex(my_plan.f_hat,12,
        "given Fourier coefficients, vector f_hat (first 12 entries)");

  /** direct trafo and show the result */
  nnfft_trafo_direct(&my_plan);
  nfft_vpr_complex(my_plan.f,my_plan.M_total,"ndft, vector f");

  /** approx. trafo and show the result */
  nnfft_trafo(&my_plan);
  nfft_vpr_complex(my_plan.f,my_plan.M_total,"nfft, vector f");

  /** finalise the one dimensional plan */
  nnfft_finalize(&my_plan);
}

static void simple_test_innfft_1d(void)
{
  int j,k,l,N=8;                        /**< index for nodes, freqencies, iter*/
  nnfft_plan my_plan;                   /**< plan for the nnfft               */
  solver_plan_complex my_iplan;         /**< plan for the inverse nnfft       */

  /** initialise an one dimensional plan */
  nnfft_init(&my_plan,1 ,8 ,8 ,&N);

  /** initialise my_iplan */
  solver_init_advanced_complex(&my_iplan,(nfft_mv_plan_complex*)(&my_plan),CGNR);

  /** init pseudo random nodes */
  for(j=0;j<my_plan.M_total;j++)
    my_plan.x[j]=((double)rand())/((double)RAND_MAX)-0.5;

  /** init pseudo random nodes */
  for(k=0;k<my_plan.N_total;k++)
    my_plan.v[k]=((double)rand())/((double)RAND_MAX)-0.5;

  /** precompute psi, the entries of the matrix B */
  if(my_plan.nnfft_flags & PRE_PSI)
    nnfft_precompute_psi(&my_plan);

  if(my_plan.nnfft_flags & PRE_FULL_PSI)
      nnfft_precompute_full_psi(&my_plan);

  /** precompute phi_hut, the entries of the matrix D */
  if(my_plan.nnfft_flags & PRE_PHI_HUT)
    nnfft_precompute_phi_hut(&my_plan);

  /** init pseudo random samples (real) and show them */
  for(j=0;j<my_plan.M_total;j++)
    my_iplan.y[j] = ((double)rand())/((double)RAND_MAX);

  nfft_vpr_complex(my_iplan.y,my_plan.M_total,"given data, vector given_f");

  /** initialise some guess f_hat_0 */
  for(k=0;k<my_plan.N_total;k++)
    my_iplan.f_hat_iter[k] = 0.0;

  nfft_vpr_complex(my_iplan.f_hat_iter,my_plan.N_total,
        "approximate solution, vector f_hat_iter");

  /** solve the system */
  solver_before_loop_complex(&my_iplan);

  for(l=0;l<8;l++)
  {
    printf("iteration l=%d\n",l);
    solver_loop_one_step_complex(&my_iplan);
    nfft_vpr_complex(my_iplan.f_hat_iter,my_plan.N_total,
          "approximate solution, vector f_hat_iter");

    CSWAP(my_iplan.f_hat_iter,my_plan.f_hat);
    nnfft_trafo(&my_plan);
    nfft_vpr_complex(my_plan.f,my_plan.M_total,"fitting the data, vector f");
    CSWAP(my_iplan.f_hat_iter,my_plan.f_hat);
  }

  solver_finalize_complex(&my_iplan);
  nnfft_finalize(&my_plan);
}

static void measure_time_nnfft_1d(void)
{
  int j,k;                              /**< index for nodes and freqencies   */
  nnfft_plan my_plan;                    /**< plan for the nfft                */
  int my_N,N=4;
  double t;
  double t0, t1;

  for(my_N=16; my_N<=16384; my_N*=2)
  {
    nnfft_init(&my_plan,1,my_N,my_N,&N);

    for(j=0;j<my_plan.M_total;j++)
      my_plan.x[j]=((double)rand())/((double)RAND_MAX)-0.5;

    for(j=0;j<my_plan.N_total;j++)
      my_plan.v[j]=((double)rand())/((double)RAND_MAX)-0.5;

    if(my_plan.nnfft_flags & PRE_PSI)
      nnfft_precompute_psi(&my_plan);

    if(my_plan.nnfft_flags & PRE_FULL_PSI)
        nnfft_precompute_full_psi(&my_plan);

    if(my_plan.nnfft_flags & PRE_PHI_HUT)
      nnfft_precompute_phi_hut(&my_plan);

    for(k=0;k<my_plan.N_total;k++)
      my_plan.f_hat[k] = ((double)rand())/((double)RAND_MAX) + _Complex_I*((double)rand())/((double)RAND_MAX);

    t0 = nfft_clock_gettime_seconds();
    nnfft_trafo_direct(&my_plan);
    t1 = nfft_clock_gettime_seconds();
    t = t1 - t0;
    printf("t_nndft=%e,\t",t);

    t0 = nfft_clock_gettime_seconds();
    nnfft_trafo(&my_plan);
    t1 = nfft_clock_gettime_seconds();
    t = t1 - t0;
    printf("t_nnfft=%e\t",t);

    printf("(N=M=%d)\n",my_N);

    nnfft_finalize(&my_plan);
  }
}

int main(void)
{
  system("clear");
  printf("1) computing a one dimensional nndft, nnfft\n\n");
  simple_test_nnfft_1d();

  /*getc(stdin);

  system("clear");
  printf("1a) computing a one dimensional adjoint nndft, nnfft\n\n");
  simple_test_adjoint_nnfft_1d();

  getc(stdin);

  system("clear");
  printf("2) computing a two dimensional nndft, nnfft\n\n");
  simple_test_nnfft_2d();

  getc(stdin);

  system("clear");
  printf("3) computing a one dimensional innfft\n\n");
  simple_test_innfft_1d();

  getc(stdin);

  system("clear");
  printf("4) computing times for one dimensional nnfft\n\n");
  measure_time_nnfft_1d();
*/
  return 1;
}