File: test_nufft.c

package info (click to toggle)
bart-cuda 0.8.00-2
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm, sid
  • size: 7,752 kB
  • sloc: ansic: 100,267; python: 717; makefile: 576; sh: 564; cpp: 104
file content (264 lines) | stat: -rw-r--r-- 5,612 bytes parent folder | download | duplicates (3)
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
/* Copyright 2017-2018. Martin Uecker.
 * All rights reserved. Use of this source code is governed by
 * a BSD-style license which can be found in the LICENSE file.
 *
 * Authors:
 * 2017-2018 Martin Uecker <martin.uecker@med.uni-goettingen.de>
 */

#include <complex.h>
#include <assert.h>

#include "num/multind.h"
#include "num/flpmath.h"
#include "num/rand.h"

#include "linops/linop.h"
#include "linops/lintest.h"

#include "noncart/nufft.h"

#include "misc/misc.h"
#include "misc/debug.h"

#include "utest.h"


enum { N = 8 };
static const long ksp_dims[N] = { 1, 5, 1, 1, 1, 1, 1, 1 };
static const long cim_dims[N] = { 8, 8, 1, 1, 1, 1, 1, 1 };
static const long trj_dims[N] = { 3, 5, 1, 1, 1, 1, 1, 1 };

static const complex float traj[5][3] = {
	{ 0., 0. , 0. },
	{ 1., 0. , 0. },
	{ 0., 1. , 0. },
	{ -1., 0., 0. },
	{ 0., -1., 0. },
};


static struct linop_s* create_nufft(bool toeplitz, bool use_weights)
{
	const complex float weights[5] = {
		0.5, 0.5, 0.5, 0.5, 0.5
	};

	struct nufft_conf_s conf = nufft_conf_defaults;
	conf.toeplitz = toeplitz;

	return nufft_create(N, ksp_dims, cim_dims, trj_dims, &traj[0][0], use_weights ? weights : NULL, conf);
}


static const long ci2_dims[N] = { 8, 8, 1, 1, 1, 1, 2, 1 };
static const long ks2_dims[N] = { 1, 5, 1, 1, 1, 1, 2, 1 };
static const long tr2_dims[N] = { 3, 5, 1, 1, 1, 1, 1, 1 };
static const long bas_dims[N] = { 1, 1, 1, 1, 1, 3, 2, 1 };
static const long wg2_dims[N] = { 1, 5, 1, 1, 1, 3, 1, 1 };



static struct linop_s* create_nufft2(bool toeplitz)
{
	const complex float weights[15] = {
		0.5, 0.5, 0.5, 0.5, 0.5,
		0.5, 0.5, 0.5, 0.5, 0.5,
		0.5, 0.5, 0.5, 0.5, 0.5,
	};

	const complex float basis[6] = {
		1., 0., 1., 0., 1., 0.,
	};

	struct nufft_conf_s conf = nufft_conf_defaults;
	conf.toeplitz = toeplitz;

	return nufft_create2(N, ks2_dims, ci2_dims, tr2_dims, &traj[0][0], wg2_dims, weights, bas_dims, basis, conf);
}



static bool test_nufft_forward(void)
{
	const complex float src[] = { 
		1., 1., 1., 1., 1., 1., 1., 1.,
		1., 1., 1., 1., 1., 1., 1., 1.,
		1., 1., 1., 1., 1., 1., 1., 1.,
		1., 1., 1., 1., 1., 1., 1., 1.,
		1., 1., 1., 1., 1., 1., 1., 1.,
		1., 1., 1., 1., 1., 1., 1., 1.,
		1., 1., 1., 1., 1., 1., 1., 1.,
		1., 1., 1., 1., 1., 1., 1., 1.,
	};

	struct linop_s* op = create_nufft(false, false);

	complex float dst[5];

	linop_forward(op, N, ksp_dims, dst, N, cim_dims, src);

	linop_free(op);

	return (cabsf(dst[0] - 8.f) < 0.02);	// !
}



static bool test_nufft_adjoint(void)
{
	struct linop_s* op = create_nufft(false, false);

	float diff = linop_test_adjoint(op);

	debug_printf(DP_DEBUG1, "adjoint diff: %f\n", diff);

	bool ret = (diff < 1.E-5f);

	linop_free(op);

	return ret;
}


static bool test_nufft_normal(void)
{
	struct linop_s* op = create_nufft(false, false);

	float nrmse = linop_test_normal(op);

	debug_printf(DP_DEBUG1, "normal nrmse: %f\n", nrmse);

	bool ret = (nrmse < 1.E-7f);

	linop_free(op);

	return ret;
}


static bool test_nufft_toeplitz(bool use_weights)
{
	complex float src[64];
	complex float dst1[64] = { 0 };
	complex float dst2[64] = { 0 };

	md_gaussian_rand(N, cim_dims, src);

	struct linop_s* op1 = create_nufft(false, use_weights);
	linop_normal(op1, N, cim_dims, dst1, src);
	linop_free(op1);

	struct linop_s* op2 = create_nufft(true, use_weights);
	linop_normal(op2, N, cim_dims, dst2, src);
	linop_free(op2);

	return md_znrmse(N, cim_dims, dst1, dst2) < 0.01;
}


static bool test_nufft_toeplitz_noweights(void)
{
	return test_nufft_toeplitz(false);
}

static bool test_nufft_toeplitz_weights(void)
{
	return test_nufft_toeplitz(true);
}


static bool test_nufft_toeplitz_forward(void)
{
	complex float src[64];
	complex float dst1[5] = { 0 };
	complex float dst2[5] = { 0 };

	md_gaussian_rand(N, cim_dims, src);

	struct linop_s* op1 = create_nufft(false, false);
	linop_forward(op1, N, ksp_dims, dst1, N, cim_dims, src);
	linop_free(op1);

	struct linop_s* op2 = create_nufft(true, false);
	linop_forward(op2, N, ksp_dims, dst2, N, cim_dims, src);
	linop_free(op2);

	return md_znrmse(N, ksp_dims, dst1, dst2) < 0.01;
}



static bool test_nufft_basis_adjoint(void)
{
	struct linop_s* op = create_nufft2(false);

	float diff = linop_test_adjoint(op);

	debug_printf(DP_DEBUG1, "adjoint diff: %f\n", diff);

	bool ret = (diff < 1.E-6f);

	linop_free(op);

	return ret;
}


static bool test_nufft_basis_normal(void)
{
	struct linop_s* op = create_nufft2(false);

	float nrmse = linop_test_normal(op);

	debug_printf(DP_DEBUG1, "normal nrmse: %f\n", nrmse);

	bool ret = (nrmse < 1.E-7f);

	linop_free(op);

	return ret;
}


static bool test_nufft_basis_toeplitz(void)
{
	complex float src[128];
	complex float dst1[128];
	complex float dst2[128];

	assert(128 == md_calc_size(N, ci2_dims));

	md_gaussian_rand(N, ci2_dims, src);

	struct linop_s* op1 = create_nufft2(false);
	linop_normal(op1, N, ci2_dims, dst1, src);
	linop_free(op1);

	struct linop_s* op2 = create_nufft2(true);
	linop_normal(op2, N, ci2_dims, dst2, src);
	linop_free(op2);

	complex float sc = md_zscalar(N, ci2_dims, dst2, dst1);
	float n = md_znorm(N, ci2_dims, dst1);

	md_zsmul(N, ci2_dims, dst1, dst1, sc / (n * n));

	return md_znrmse(N, ci2_dims, dst1, dst2) < 1.E-4;
}





UT_REGISTER_TEST(test_nufft_forward);
UT_REGISTER_TEST(test_nufft_adjoint);
UT_REGISTER_TEST(test_nufft_normal);
UT_REGISTER_TEST(test_nufft_toeplitz_weights);
UT_REGISTER_TEST(test_nufft_toeplitz_noweights);
UT_REGISTER_TEST(test_nufft_toeplitz_forward);
UT_REGISTER_TEST(test_nufft_basis_adjoint);
UT_REGISTER_TEST(test_nufft_basis_normal);
UT_REGISTER_TEST(test_nufft_basis_toeplitz);