Skip to content

Commit

Permalink
More robust short signal us guess.
Browse files Browse the repository at this point in the history
  • Loading branch information
antirez committed Jan 2, 2023
1 parent 630290f commit 4ce568a
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ uint32_t duration_delta(uint32_t a, uint32_t b) {
}

/* This function starts scanning samples at offset idx looking for the
* longest run of pulses, either high or low, that are among 10%
* of each other, for a maximum of three classes. The classes are
* counted separtely for high and low signals (RF on / off) because
* many devices tend to have different pulse lenghts depending on
* longest run of pulses, either high or low, that are not much different
* from each other, for a maximum of three duration classes.
* So for instance 50 successive pulses that are roughly long 340us or 670us
* will be sensed as a coherent signal (example: 312, 361, 700, 334, 667, ...)
*
* The classes are counted separtely for high and low signals (RF on / off)
* because many devices tend to have different pulse lenghts depending on
* the level of the pulse.
*
* For instance Oregon2 sensors, in the case of protocol 2.1 will send
Expand Down Expand Up @@ -54,7 +57,10 @@ uint32_t search_coherent_signal(RawSamplesBuffer *s, uint32_t idx) {
uint32_t classavg = classes[k].dur[level];
uint32_t count = classes[k].count[level];
uint32_t delta = duration_delta(dur,classavg);
if (delta < classavg/10) {
/* Is the difference in duration between this signal and
* the class we are inspecting less than a given percentage?
* If so, accept this signal. */
if (delta < classavg/8) { /* 100%/8 = 12%. */
/* It is useful to compute the average of the class
* we are observing. We know how many samples we got so
* far, so we can recompute the average easily.
Expand All @@ -79,17 +85,26 @@ uint32_t search_coherent_signal(RawSamplesBuffer *s, uint32_t idx) {
/* Update the buffer setting the shortest pulse we found
* among the three classes. This will be used when scaling
* for visualization. */
uint32_t short_dur[2] = {0,0};
for (int j = 0; j < SEARCH_CLASSES; j++) {
for (int level = 0; level < 2; level++) {
if (classes[j].dur[level] == 0) continue;
if (classes[j].count[level] < 3) continue;
if (s->short_pulse_dur == 0 ||
s->short_pulse_dur > classes[j].dur[level])
if (short_dur[level] == 0 ||
short_dur[level] > classes[j].dur[level])
{
s->short_pulse_dur = classes[j].dur[level];
short_dur[level] = classes[j].dur[level];
}
}
}

/* Use the average between high and low short pulses duration.
* Often they are a bit different, and using the average is more robust
* when we do decoding sampling at short_pulse_dur intervals. */
if (short_dur[0] == 0) short_dur[0] = short_dur[1];
if (short_dur[1] == 0) short_dur[1] = short_dur[0];
s->short_pulse_dur = (short_dur[0]+short_dur[1])/2;

return len;
}

Expand Down Expand Up @@ -117,8 +132,8 @@ void scan_for_signal(ProtoViewApp *app) {
raw_samples_copy(DetectedSamples,copy);
DetectedSamples->idx = (DetectedSamples->idx+i)%
DetectedSamples->total;
FURI_LOG_E(TAG, "Displayed sample updated (%d samples)",
(int)thislen);
FURI_LOG_E(TAG, "Displayed sample updated (%d samples %lu us)",
(int)thislen, DetectedSamples->short_pulse_dur);
decode_signal(DetectedSamples,thislen);
}
i += thislen ? thislen : 1;
Expand Down

0 comments on commit 4ce568a

Please sign in to comment.