@@ -116,6 +116,12 @@ static void configSetDefaults(void) {
116
116
117
117
// Now initialise things that should not be 0/NULL to their defaults
118
118
Modes .gain = MODES_MAX_GAIN ;
119
+
120
+ // 8 bit autogain defaults, will be squared and compared against magnitude data
121
+ Modes .loudThreshold = 245 ;
122
+ Modes .noiseLowThreshold = 25 ;
123
+ Modes .noiseHighThreshold = 35 ;
124
+
119
125
Modes .freq = MODES_DEFAULT_FREQ ;
120
126
Modes .check_crc = 1 ;
121
127
Modes .net_heartbeat_interval = MODES_NET_HEARTBEAT_INTERVAL ;
@@ -752,61 +758,76 @@ static void *globeBinEntryPoint(void *arg) {
752
758
}
753
759
754
760
static void gainStatistics (struct mag_buf * buf ) {
755
- static uint64_t loudSamples ;
756
- static uint64_t quietSamples ;
757
- static uint64_t quiet2Samples ;
761
+ static uint64_t loudEvents ;
762
+ static uint64_t noiseLowSamples ;
763
+ static uint64_t noiseHighSamples ;
758
764
static uint64_t totalSamples ;
759
765
static int slowRise ;
760
766
static int lastGain ;
761
767
762
- loudSamples += buf -> loudSamples ;
763
- quietSamples += buf -> quietSamples ;
764
- quiet2Samples += buf -> quiet2Samples ;
768
+ loudEvents += buf -> loudEvents ;
769
+ noiseLowSamples += buf -> noiseLowSamples ;
770
+ noiseHighSamples += buf -> noiseHighSamples ;
765
771
totalSamples += buf -> length ;
766
772
767
- if (totalSamples < 2 * Modes .sample_rate ) {
773
+ double interval = 0.5 ;
774
+
775
+ if (totalSamples < interval * Modes .sample_rate ) {
768
776
return ;
769
777
}
770
778
771
- double loudPercent = loudSamples / (double ) totalSamples * 100.0 ;
772
- double quietPercent = quietSamples / (double ) totalSamples * 100.0 ;
773
- double quiet2Percent = quiet2Samples / (double ) totalSamples * 100.0 ;
774
-
775
- // reset
776
- loudSamples = 0 ;
777
- quietSamples = 0 ;
778
- quiet2Samples = 0 ;
779
- totalSamples = 0 ;
779
+ double noiseLowPercent = noiseLowSamples / (double ) totalSamples * 100.0 ;
780
+ double noiseHighPercent = noiseHighSamples / (double ) totalSamples * 100.0 ;
780
781
781
782
if (!Modes .autoGain ) {
782
- // don't adjust anything
783
- return ;
783
+ goto reset ;
784
784
}
785
785
786
- if (loudPercent > 0.05 || quiet2Percent < 0.05 ) {
786
+ // 29 gain values for typical rtl-sdr
787
+ // allow startup to sweep entire range quickly, half it for double steps
788
+ int starting = getUptime () < (29 / 2.0 * interval ) * SECONDS ;
789
+
790
+ char * action = "" ;
791
+ int noiseLow = noiseLowPercent > 5 ; // too many samples < noiseLowThreshold
792
+ int noiseHigh = noiseHighPercent < 0.2 ; // too few samples < noiseHighThreshold
793
+ int loud = loudEvents > 1 ;
794
+ if (loud || noiseHigh ) {
795
+ action = "decreased" ;
787
796
Modes .lowerGain = 1 ;
788
- } else if (
789
- quietPercent > 10.0
790
- ) {
791
- if (getUptime () < 1 * MINUTES || slowRise > 10 ) {
797
+ } else if (noiseLow ) {
798
+ if (starting || slowRise > 15 / interval ) {
792
799
slowRise = 0 ;
793
800
Modes .increaseGain = 1 ;
801
+ action = "increased" ;
794
802
} else {
795
803
slowRise ++ ;
796
804
}
797
805
}
806
+
798
807
if (Modes .increaseGain || Modes .lowerGain ) {
799
- if (getUptime () < 1 * MINUTES ) {
808
+ if (starting ) {
800
809
Modes .lowerGain *= 2 ;
801
810
Modes .increaseGain *= 2 ;
802
811
}
812
+ sdrSetGain ();
803
813
if (Modes .gain != lastGain ) {
814
+ if (loud ) {
815
+ fprintf (stderr , "%9s gain. loudEvents: %4lld\n" , action , (long long ) loudEvents );
816
+ } else if (noiseHigh ) {
817
+ fprintf (stderr , "%9s gain. noise high.\n" , action );
818
+ } else if (noiseLow ) {
819
+ fprintf (stderr , "%9s gain. noise low.\n" , action );
820
+ }
821
+ //fprintf(stderr, "%9s gain. noiseLow: %5.2f %% noiseHigh: %5.2f %% loudEvents: %4lld\n", action, noiseLowPercent, noiseHighPercent, (long long) loudEvents);
804
822
lastGain = Modes .gain ;
805
- fprintf (stderr , "loud: %8.4f %% quiet: %8.4f %% quiet2 %8.4f %%\n" , loudPercent , quietPercent , quiet2Percent );
806
823
}
807
- sdrSetGain ();
808
824
}
809
825
826
+ reset :
827
+ loudEvents = 0 ;
828
+ noiseLowSamples = 0 ;
829
+ noiseHighSamples = 0 ;
830
+ totalSamples = 0 ;
810
831
}
811
832
812
833
@@ -1505,9 +1526,24 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) {
1505
1526
Modes .dev_name = strdup (arg );
1506
1527
break ;
1507
1528
case OptGain :
1508
- if (strcmp (arg , "auto" ) == 0 ) {
1529
+ if (strcasestr (arg , "auto" ) == arg ) {
1509
1530
Modes .autoGain = 1 ;
1510
- Modes .gain = 439 ;
1531
+ char * argdup = strdup (arg );
1532
+ tokenize (& argdup , "," , token , maxTokens );
1533
+ if (token [1 ]) {
1534
+ Modes .gain = (int ) (atof (token [1 ])* 10 ); // Gain is in tens of DBs
1535
+ } else {
1536
+ Modes .gain = 439 ;
1537
+ }
1538
+ if (token [2 ]) {
1539
+ Modes .noiseLowThreshold = atoi (token [2 ]);
1540
+ }
1541
+ if (token [3 ]) {
1542
+ Modes .noiseHighThreshold = atoi (token [3 ]);
1543
+ }
1544
+ if (token [4 ]) {
1545
+ Modes .loudThreshold = atoi (token [4 ]);
1546
+ }
1511
1547
} else {
1512
1548
Modes .gain = (int ) (atof (arg )* 10 ); // Gain is in tens of DBs
1513
1549
}
0 commit comments