aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2019-05-23 16:42:27 +0200
committerThomas White <taw@physics.org>2019-05-29 10:42:14 +0200
commit581f8e8ab1b1d2833fd689540a2758bea1fc93ee (patch)
treef2d0f329ebced450a7451287f3be138ac0aabab5 /src
parent1380592a04cba4621341a11cfb9a5526ea9dc4bf (diff)
Normalise sampled spectrum weights to total 1
Diffstat (limited to 'src')
-rw-r--r--src/diffraction-gpu.c19
-rw-r--r--src/diffraction.c11
2 files changed, 20 insertions, 10 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"
diff --git a/src/diffraction.c b/src/diffraction.c
index bcbc62e8..6f52df11 100644
--- a/src/diffraction.c
+++ b/src/diffraction.c
@@ -457,6 +457,7 @@ void get_diffraction(struct image *image, int na, int nb, int nc,
double *lut_c;
int i;
double kmin, kmax, step;
+ double norm = 0.0;
cell_get_cartesian(cell, &ax, &ay, &az, &bx, &by, &bz, &cx, &cy, &cz);
@@ -466,14 +467,20 @@ void get_diffraction(struct image *image, int na, int nb, int nc,
spectrum_get_range(image->spectrum, &kmin, &kmax);
step = (kmax-kmin)/(n_samples+1);
- STATUS("%i samples\n", n_samples);
+
+ /* 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);