aboutsummaryrefslogtreecommitdiff
path: root/src/diffraction.c
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/diffraction.c
parent1380592a04cba4621341a11cfb9a5526ea9dc4bf (diff)
Normalise sampled spectrum weights to total 1
Diffstat (limited to 'src/diffraction.c')
-rw-r--r--src/diffraction.c11
1 files changed, 9 insertions, 2 deletions
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);