diff options
author | Thomas White <taw@physics.org> | 2019-05-23 16:42:27 +0200 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2019-05-29 10:42:14 +0200 |
commit | 581f8e8ab1b1d2833fd689540a2758bea1fc93ee (patch) | |
tree | f2d0f329ebced450a7451287f3be138ac0aabab5 /src/diffraction-gpu.c | |
parent | 1380592a04cba4621341a11cfb9a5526ea9dc4bf (diff) |
Normalise sampled spectrum weights to total 1
Diffstat (limited to 'src/diffraction-gpu.c')
-rw-r--r-- | src/diffraction-gpu.c | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/src/diffraction-gpu.c b/src/diffraction-gpu.c index e2bce0cc..4ecf0558 100644 --- a/src/diffraction-gpu.c +++ b/src/diffraction-gpu.c @@ -294,8 +294,7 @@ int get_diffraction_gpu(struct gpu_context *gctx, struct image *image, int n_neg = 0; int n_nan = 0; int i; - double kmin, kmax, step; - double tot = 0.0; + double kmin, kmax, step, norm; if ( gctx == NULL ) { ERROR("GPU setup failed.\n"); @@ -341,24 +340,28 @@ int get_diffraction_gpu(struct gpu_context *gctx, struct image *image, } spectrum_get_range(image->spectrum, &kmin, &kmax); - step = (kmax-kmin)/n_samples; - for ( i=0; i<=n_samples; i++ ) { + step = (kmax-kmin)/(n_samples+1); + + /* Determine normalisation factor such that weights add up to 1 after + * sampling (bins must have constant width) */ + for ( i=1; i<=n_samples; i++ ) { + double k = kmin + i*step; + norm += spectrum_get_density_at_k(image->spectrum, k); + } + for ( i=1; i<=n_samples; i++ ) { double k = kmin + i*step; double prob; /* Probability = p.d.f. times step width */ - prob = step * spectrum_get_density_at_k(image->spectrum, k); + prob = spectrum_get_density_at_k(image->spectrum, k)/norm; STATUS("Wavelength: %e m, weight = %.5f\n", 1.0/k, prob); err = do_panels(gctx, image, k, prob, &n_inf, &n_neg, &n_nan); if ( err ) return 1; - tot += prob; - } - STATUS("Total weight = %f\n", tot); if ( n_neg + n_inf + n_nan ) { ERROR("WARNING: The GPU calculation produced %i negative" |