diff mbox series

[FFmpeg-devel,7/9] avradio/sdr: Compute and use detection histogram

Message ID 20230707172224.2368067-7-michael@niedermayer.cc
State New
Headers show
Series [FFmpeg-devel,1/9] avradio/sdr: use AVTree for candidate stations | expand

Checks

Context Check Description
yinshiyou/configure_loongarch64 warning Failed to apply patch
andriy/configure_x86 warning Failed to apply patch

Commit Message

Michael Niedermayer July 7, 2023, 5:22 p.m. UTC
By analyzing the behavior of the detectability of stations with
different SDR settings we can separate some SDR artifacts from
weak stations.

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavradio/sdr.h      |  5 +++++
 libavradio/sdrdemux.c | 36 +++++++++++++++++++++++++++++++++++-
 2 files changed, 40 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/libavradio/sdr.h b/libavradio/sdr.h
index 838fb1cef7..1053e45efe 100644
--- a/libavradio/sdr.h
+++ b/libavradio/sdr.h
@@ -66,6 +66,8 @@  typedef enum Modulation {
     //QAM, PSK, ...
 } Modulation;
 
+#define HISTOGRAMM_SIZE 9
+
 typedef struct Station {
     char *name;
     enum Modulation modulation;
@@ -78,6 +80,9 @@  typedef struct Station {
     int timeout;            //since how many blocks was this detectable but not detected
     int multiplex_index;    //DAB can have multiple stations on one frequency
 
+    int detection_per_mix_frequency[HISTOGRAMM_SIZE];
+    int non_detection_per_mix_frequency[HISTOGRAMM_SIZE];
+
     struct SDRStream *stream;
 } Station;
 
diff --git a/libavradio/sdrdemux.c b/libavradio/sdrdemux.c
index 6cb9d3b70a..1fc528317c 100644
--- a/libavradio/sdrdemux.c
+++ b/libavradio/sdrdemux.c
@@ -111,6 +111,23 @@  static void free_station(Station *station)
     av_free(station);
 }
 
+static inline int histogram_index(SDRContext *sdr, double f)
+{
+    f = HISTOGRAMM_SIZE*((f - sdr->block_center_freq) / sdr->sdr_sample_rate + 0.5);
+    return av_clip((int)f, 0, HISTOGRAMM_SIZE-1);
+}
+
+static int histogram_score(Station *s)
+{
+    int score = 0;
+    for(int i = 0; i<HISTOGRAMM_SIZE; i++) {
+        score +=
+             (5*s->detection_per_mix_frequency[i] > s->non_detection_per_mix_frequency[i])
+            -(5*s->detection_per_mix_frequency[i] < s->non_detection_per_mix_frequency[i]);
+    }
+    return score;
+}
+
 typedef struct FindStationContext {
     double freq;
     double range;
@@ -184,6 +201,10 @@  static int create_station(SDRContext *sdr, Station *candidate_station) {
     if (candidate_station->in_station_list)
         return 0;
 
+    // suspect looking histogram
+    if (histogram_score(candidate_station) <= 0)
+        return 0;
+
     Station *station_list[1000];
     int nb_stations = find_stations(sdr, sdr->block_center_freq, sdr->sdr_sample_rate*0.5, station_list, FF_ARRAY_ELEMS(station_list));
     for (i=0; i<nb_stations; i++) {
@@ -328,8 +349,20 @@  static void decay_stations(SDRContext *sdr)
             station->frequency + station->bandwidth/2 > sdr->block_center_freq + sdr->bandwidth/2)
             continue;
 
+        if (station->timeout)
+            station->non_detection_per_mix_frequency[histogram_index(sdr, station->frequency)] ++;
+
         if (station->in_station_list) {
-            if (station->timeout++ > STATION_TIMEOUT) {
+            int station_timeout = STATION_TIMEOUT;
+            int hs = histogram_score(station);
+
+            if (hs == 0) {
+                station_timeout = 5; //give the station a moment to be properly detected and then discard it
+            } else if(hs < 0) {
+                station_timeout = 0; //probably not a station
+            }
+
+            if (station->timeout++ > station_timeout) {
                 if (!station->stream)
                     station->in_station_list = 0;
             }
@@ -376,6 +409,7 @@  static int create_candidate_station(SDRContext *sdr, enum Modulation modulation,
     }
     station->frequency /= ++station->nb_frequency;
 
+    station->detection_per_mix_frequency[histogram_index(sdr, freq)] ++;
     station->modulation   = modulation;
     station->bandwidth    = bandwidth;
     station->bandwidth_p2 = bandwidth_p2;