From 72487f7869ecbccb76030916ec64ff66da9982fc Mon Sep 17 00:00:00 2001 From: "Christian W. Zuckschwerdt" Date: Sun, 25 Nov 2018 09:18:19 +0000 Subject: [PATCH] Optimization use ints for limits in demod --- include/rtl_433.h | 16 +++++++++------- src/pulse_demod.c | 26 +++++++++++++------------- src/rtl_433.c | 15 +++++++++------ 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/include/rtl_433.h b/include/rtl_433.h index c95af24a..3986daa6 100755 --- a/include/rtl_433.h +++ b/include/rtl_433.h @@ -52,13 +52,15 @@ struct protocol_state { char **fields; unsigned int disabled; - /* pwm limits (provided by driver in µs and converted to samples) */ - float short_limit; - float long_limit; - float reset_limit; - float gap_limit; - float sync_width; - float tolerance; + /* pulse limits (provided by decoder in us and converted to samples) */ + float f_short_limit; // precision reciprocal for PCM + float f_long_limit; // precision reciprocal for PCM + int short_limit; + int long_limit; + int reset_limit; + int gap_limit; + int sync_width; + int tolerance; }; #endif /* INCLUDE_RTL_433_H_ */ diff --git a/src/pulse_demod.c b/src/pulse_demod.c index 929019d7..1713a886 100644 --- a/src/pulse_demod.c +++ b/src/pulse_demod.c @@ -23,14 +23,14 @@ int pulse_demod_pcm(const pulse_data_t *pulses, struct protocol_state *device) { int events = 0; bitbuffer_t bits = {0}; - const int MAX_ZEROS = device->reset_limit / device->long_limit; - const int TOLERANCE = device->long_limit / 4; // Tolerance is ±25% of a bit period + const int max_zeros = device->reset_limit / device->long_limit; + const int tolerance = device->long_limit / 4; // Tolerance is ±25% of a bit period for(unsigned n = 0; n < pulses->num_pulses; ++n) { // Determine number of high bit periods for NRZ coding, where bits may not be separated - int highs = (pulses->pulse[n] + device->short_limit/2) / device->short_limit; + int highs = (pulses->pulse[n]) * device->f_short_limit + 0.5; // Determine number of bit periods in current pulse/gap length (rounded) - int periods = (pulses->pulse[n] + pulses->gap[n] + device->long_limit/2) / device->long_limit; + int periods = (pulses->pulse[n] + pulses->gap[n]) * device->f_long_limit + 0.5; // Add run of ones (1 for RZ, many for NRZ) for (int i=0; i < highs; ++i) { @@ -38,14 +38,14 @@ int pulse_demod_pcm(const pulse_data_t *pulses, struct protocol_state *device) } // Add run of zeros periods -= highs; // Remove 1s from whole period - periods = min(periods, MAX_ZEROS); // Don't overflow at end of message + periods = min(periods, max_zeros); // Don't overflow at end of message for (int i=0; i < periods; ++i) { bitbuffer_add_bit(&bits, 0); } // Validate data if ((device->short_limit != device->long_limit) // Only for RZ coding - && (fabsf(pulses->pulse[n] - device->short_limit) > TOLERANCE) // Pulse must be within tolerance + && (abs(pulses->pulse[n] - device->short_limit) > tolerance) // Pulse must be within tolerance ) { // Data is corrupt if (debug_output > 3) { @@ -303,10 +303,10 @@ int pulse_demod_dmc(const pulse_data_t *pulses, struct protocol_state *device) { } for(n = 0; n < pulses->num_pulses * 2; ++n) { - if ( fabsf(symbol[n] - device->short_limit) < device->tolerance) { + if ( abs(symbol[n] - device->short_limit) < device->tolerance) { // Short - 1 bitbuffer_add_bit(&bits, 1); - if ( fabsf(symbol[++n] - device->short_limit) > device->tolerance) { + if ( abs(symbol[++n] - device->short_limit) > device->tolerance) { if (symbol[n] >= device->reset_limit - device->tolerance ) { // Don't expect another short gap at end of message n--; @@ -318,7 +318,7 @@ int pulse_demod_dmc(const pulse_data_t *pulses, struct protocol_state *device) { */ } } - } else if ( fabsf(symbol[n] - device->long_limit) < device->tolerance) { + } else if ( abs(symbol[n] - device->long_limit) < device->tolerance) { // Long - 0 bitbuffer_add_bit(&bits, 0); } else if (symbol[n] >= device->reset_limit - device->tolerance @@ -352,10 +352,10 @@ int pulse_demod_piwm_raw(const pulse_data_t *pulses, struct protocol_state *devi } for (n = 0; n < pulses->num_pulses * 2; ++n) { - w = symbol[n] / device->short_limit + 0.5; + w = symbol[n] * device->f_short_limit + 0.5; if (symbol[n] > device->long_limit) { bitbuffer_add_row(&bits); - } else if (fabsf(symbol[n] - w * device->short_limit) < device->tolerance) { + } else if (abs(symbol[n] - w * device->short_limit) < device->tolerance) { // Add w symbols for (; w > 0; --w) bitbuffer_add_bit(&bits, 1-n%2); @@ -399,10 +399,10 @@ int pulse_demod_piwm_dc(const pulse_data_t *pulses, struct protocol_state *devic } for (n = 0; n < pulses->num_pulses * 2; ++n) { - if (fabsf(symbol[n] - device->short_limit) < device->tolerance) { + if (abs(symbol[n] - device->short_limit) < device->tolerance) { // Short - 1 bitbuffer_add_bit(&bits, 1); - } else if (fabsf(symbol[n] - device->long_limit) < device->tolerance) { + } else if (abs(symbol[n] - device->long_limit) < device->tolerance) { // Long - 0 bitbuffer_add_bit(&bits, 0); } else if (symbol[n] < device->reset_limit diff --git a/src/rtl_433.c b/src/rtl_433.c index a001e34f..d31a76ae 100644 --- a/src/rtl_433.c +++ b/src/rtl_433.c @@ -309,12 +309,15 @@ static void sighandler(int signum) { static void register_protocol(struct dm_state *demod, r_device *t_dev) { struct protocol_state *p = calloc(1, sizeof (struct protocol_state)); - p->short_limit = (float)t_dev->short_limit / (1000000.0 / (float)cfg.samp_rate); - p->long_limit = (float)t_dev->long_limit / (1000000.0 / (float)cfg.samp_rate); - p->reset_limit = (float)t_dev->reset_limit / (1000000.0 / (float)cfg.samp_rate); - p->gap_limit = (float)t_dev->gap_limit / (1000000.0 / (float)cfg.samp_rate); - p->sync_width = (float)t_dev->sync_width / (1000000.0 / (float)cfg.samp_rate); - p->tolerance = (float)t_dev->tolerance / (1000000.0 / (float)cfg.samp_rate); + float samples_per_us = cfg.samp_rate / 1.0e6; + p->f_short_limit = 1.0 / (t_dev->short_limit * samples_per_us); + p->f_long_limit = 1.0 / (t_dev->long_limit * samples_per_us); + p->short_limit = t_dev->short_limit * samples_per_us; + p->long_limit = t_dev->long_limit * samples_per_us; + p->reset_limit = t_dev->reset_limit * samples_per_us; + p->gap_limit = t_dev->gap_limit * samples_per_us; + p->sync_width = t_dev->sync_width * samples_per_us; + p->tolerance = t_dev->tolerance * samples_per_us; p->modulation = t_dev->modulation; p->callback = t_dev->json_callback;