Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
Merge branch 'devel'
Browse files Browse the repository at this point in the history
  • Loading branch information
lerwys committed Mar 7, 2019
2 parents d05e561 + 7c8a779 commit 9981ae7
Showing 1 changed file with 127 additions and 25 deletions.
152 changes: 127 additions & 25 deletions libs/bpmclient/src/bpmclient/c/bpm_client_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,19 @@ static void _setup_transaction (bpm_single_pass_t *self);
static void _process_single_pass_sample (bpm_single_pass_t *self,
bpm_sample_t *sample);

static double _squared_value (uint8_t *adc_sample, uint32_t atom_size);
static double _cast_to_double (uint8_t *adc_sample, uint32_t atom_size);

static void _calculate_bpm_sample (bpm_parameters_t *parameters, double a,
static double _squared_value (double value);

static void _calculate_bpm_sample_std (bpm_parameters_t *parameters, double a,
double b, double c, double d, bpm_sample_t *sample);

static void _calculate_bpm_sample_partial (bpm_parameters_t *parameters, double a,
double b, double c, double d, bpm_sample_t *sample);

static void _calculate_bpm_sample (bpm_parameters_t *parameters, double a,
double b, double c, double d, bpm_sample_t *sample, bool partial_delta);

static void _release_transaction (bpm_single_pass_t *self);

bpm_single_pass_t *bpm_single_pass_new (char *broker_endp, int verbose,
Expand Down Expand Up @@ -310,10 +318,10 @@ static void _setup_transaction (bpm_single_pass_t *self)
uint32_t chan = self->request.chan;
uint32_t sample_size = 0;

uint32_t num_samples = (request->num_samples_pre + request->num_samples_post) *
uint32_t num_samples = (request->num_samples_pre + request->num_samples_post) *
request->num_shots;
halcs_client_err_e err = halcs_get_acq_ch_sample_size (acq_client, service, chan, &sample_size);
ASSERT_TEST(err == HALCS_CLIENT_SUCCESS, "Could not get sample size",
ASSERT_TEST(err == HALCS_CLIENT_SUCCESS, "Could not get sample size",
err_get_acq_prop);

memcpy (&transaction->req, request, sizeof (transaction->req));
Expand All @@ -333,18 +341,26 @@ static void _process_single_pass_sample (bpm_single_pass_t *self,
"data acquisition\n");

acq_req_t *request = &self->request;
uint32_t num_samples = (request->num_samples_pre + request->num_samples_post) *
uint32_t num_samples = (request->num_samples_pre + request->num_samples_post) *
request->num_shots;
double a = 0.0;
double b = 0.0;
double c = 0.0;
double d = 0.0;
double a_energy = 0.0;
double b_energy = 0.0;
double c_energy = 0.0;
double d_energy = 0.0;
double a_sum = 0.0;
double b_sum = 0.0;
double c_sum = 0.0;
double d_sum = 0.0;
double a_energy_ac = 0.0;
double b_energy_ac = 0.0;
double c_energy_ac = 0.0;
double d_energy_ac = 0.0;
uint32_t atom_width = 0;
uint32_t num_atoms = 0;

int err = halcs_get_acq_ch_atom_width (self->acq_client, self->service, self->request.chan, &atom_width);
err |= halcs_get_acq_ch_num_atoms (self->acq_client, self->service, self->request.chan, &num_atoms);
ASSERT_TEST(err == HALCS_CLIENT_SUCCESS, "Could not get channel properties",
ASSERT_TEST(err == HALCS_CLIENT_SUCCESS, "Could not get channel properties",
err_get_acq_prop);
ASSERT_TEST(num_atoms == 4, "Single-Pass processing is only valid for num_atoms = 4",
err_num_atoms);
Expand All @@ -355,33 +371,59 @@ static void _process_single_pass_sample (bpm_single_pass_t *self,
uint8_t *raw_data = (uint8_t*) self->transaction.block.data;

for (uint32_t i = 0; i < num_samples; ++i) {
a += _squared_value (raw_data + atom_width*((i*num_atoms)+0), atom_width);
b += _squared_value (raw_data + atom_width*((i*num_atoms)+1), atom_width);
c += _squared_value (raw_data + atom_width*((i*num_atoms)+2), atom_width);
d += _squared_value (raw_data + atom_width*((i*num_atoms)+3), atom_width);
/* Use the correct BPM convenction for channel 0, 1, 2 and 3
* A = ch0
* B = ch2
* C = ch1
* D = ch3
*/
double a_sample = _cast_to_double (raw_data + atom_width*((i*num_atoms)+0), atom_width);
double c_sample = _cast_to_double (raw_data + atom_width*((i*num_atoms)+1), atom_width);
double b_sample = _cast_to_double (raw_data + atom_width*((i*num_atoms)+2), atom_width);
double d_sample = _cast_to_double (raw_data + atom_width*((i*num_atoms)+3), atom_width);

a_sum += a_sample;
b_sum += b_sample;
c_sum += c_sample;
d_sum += d_sample;

a_energy += _squared_value (a_sample);
b_energy += _squared_value (b_sample);
c_energy += _squared_value (c_sample);
d_energy += _squared_value (d_sample);
}

double a_energy_dc = _squared_value (a_sum) / num_samples;
double b_energy_dc = _squared_value (b_sum) / num_samples;
double c_energy_dc = _squared_value (c_sum) / num_samples;
double d_energy_dc = _squared_value (d_sum) / num_samples;

DBE_DEBUG (DBG_LIB_CLIENT | DBG_LVL_TRACE, "[libbpmclient] "
"_process_single_pass_sample: (A^2, B^2, C^2, D^2) = "
"(%f, %f, %f, %f)\n", a, b, c, d);
"(%f, %f, %f, %f)\n", a_energy, b_energy, c_energy, d_energy);

a_energy_ac = a_energy - a_energy_dc;
b_energy_ac = b_energy - b_energy_dc;
c_energy_ac = c_energy - c_energy_dc;
d_energy_ac = d_energy - d_energy_dc;

a = sqrt (a);
b = sqrt (b);
c = sqrt (c);
d = sqrt (d);
double a = sqrt (a_energy_ac);
double b = sqrt (b_energy_ac);
double c = sqrt (c_energy_ac);
double d = sqrt (d_energy_ac);

DBE_DEBUG (DBG_LIB_CLIENT | DBG_LVL_TRACE, "[libbpmclient] "
"_process_single_pass_sample: (A, B, C, D) = (%f, %f, %f, %f)\n",
a, b, c, d);

_calculate_bpm_sample (self->bpm_parameters, a, b, c, d, sample);
_calculate_bpm_sample (self->bpm_parameters, a, b, c, d, sample, true);

err_num_atoms:
err_get_acq_prop:
return;
}

static double _squared_value (uint8_t *adc_sample, uint32_t atom_size)
static double _cast_to_double (uint8_t *adc_sample, uint32_t atom_size)
{
double value = 0.0;
switch (atom_size) {
Expand All @@ -401,16 +443,20 @@ static double _squared_value (uint8_t *adc_sample, uint32_t atom_size)
default:
value = -1;
DBE_DEBUG (DBG_LIB_CLIENT | DBG_LVL_FATAL, "[libbpmclient] "
"_squared_value: invalid atom_size = %u. "
"Single Pass processing will be invalid\n",
"_cast_to_double: invalid atom_size = %u. ",
atom_size);
break;
}

return value;
}

static double _squared_value (double value)
{
return value * value;
}

static void _calculate_bpm_sample (bpm_parameters_t *parameters, double a,
static void _calculate_bpm_sample_std (bpm_parameters_t *parameters, double a,
double b, double c, double d, bpm_sample_t *sample)
{
double sum = a + b + c + d;
Expand All @@ -428,18 +474,74 @@ static void _calculate_bpm_sample (bpm_parameters_t *parameters, double a,
sample->b = b;
sample->c = c;
sample->d = d;

sample->x = kx * (a - b - c + d) / sum - offset_x;
sample->y = ky * (a + b - c - d) / sum - offset_y;
sample->q = kq * (a - b + c - d) / sum - offset_q;
sample->sum = ksum * sum;

DBE_DEBUG (DBG_LIB_CLIENT | DBG_LVL_TRACE, "[libbpmclient] "
"_calculate_bpm_sample: (A, B, C, D, X, Y, Q, SUM) = "
"_calculate_bpm_sample_std: (A, B, C, D, X, Y, Q, SUM) = "
"(%f, %f, %f, %f, %f, %f, %f, %f)\n",
sample->a, sample->b, sample->c, sample->d,
sample->x, sample->y, sample->q, sample->sum);
}

static void _calculate_bpm_sample_partial (bpm_parameters_t *parameters, double a,
double b, double c, double d, bpm_sample_t *sample)
{
double sum_ac = a + c;
double sum_bd = b + d;
double sum_ab = a + b;
double sum_cd = c + d;
double sum = sum_ac + sum_bd;

double kx = parameters->kx;
double ky = parameters->ky;
double kq = parameters->kq;
double ksum = parameters->ksum;

double offset_x = parameters->offset_x;
double offset_y = parameters->offset_y;
double offset_q = parameters->offset_q;

double partial_ac_pos_x = kx * (a - c) / (2 * sum_ac);
double partial_bd_pos_x = kx * (b - d) / (2 * sum_bd);

double partial_ac_pos_y = ky * (a - c) / (2 * sum_ac);
double partial_bd_pos_y = ky * (b - d) / (2 * sum_bd);

double partial_ab_pos_q = kq * (a - b) / (2 * sum_ab);
double partial_cd_pos_q = kq * (c - d) / (2 * sum_cd);

sample->a = a;
sample->b = b;
sample->c = c;
sample->d = d;

sample->x = partial_ac_pos_x - partial_bd_pos_x - offset_x;
sample->y = partial_ac_pos_y + partial_bd_pos_y - offset_y;
sample->q = partial_ab_pos_q + partial_cd_pos_q - offset_q;
sample->sum = ksum * sum;

DBE_DEBUG (DBG_LIB_CLIENT | DBG_LVL_TRACE, "[libbpmclient] "
"_calculate_bpm_sample_partial: (A, B, C, D, X, Y, Q, SUM) = "
"(%f, %f, %f, %f, %f, %f, %f, %f)\n",
sample->a, sample->b, sample->c, sample->d,
sample->x, sample->y, sample->q, sample->sum);
}

static void _calculate_bpm_sample (bpm_parameters_t *parameters, double a,
double b, double c, double d, bpm_sample_t *sample, bool partial_delta)
{
if (partial_delta) {
_calculate_bpm_sample_partial (parameters, a, b, c, d, sample);
}
else {
_calculate_bpm_sample_std (parameters, a, b, c, d, sample);
}
}

static void _release_transaction (bpm_single_pass_t *self)
{
acq_trans_t *transaction = &self->transaction;
Expand Down

0 comments on commit 9981ae7

Please sign in to comment.