Skip to content

Commit 225f401

Browse files
committed
rtl_power: linear output
1 parent 6d9bb99 commit 225f401

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

src/rtl_power.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ struct tuning_state
105105
int downsample_passes; /* for the recursive filter */
106106
int comp_fir_size;
107107
int peak_hold;
108+
int linear;
108109
double crop;
109110
//pthread_rwlock_t avg_lock;
110111
//pthread_mutex_t avg_mutex;
@@ -132,6 +133,7 @@ struct misc_settings
132133
int boxcar;
133134
int comp_fir_size;
134135
int peak_hold;
136+
int linear;
135137
int target_rate;
136138
double crop;
137139
int gain;
@@ -176,7 +178,8 @@ void usage(void)
176178
"\t fir_size can be 0 or 9. 0 has bad roll off,\n"
177179
"\t try with '-c 50%%'\n"
178180
"\t[-r max_sample_rate (default: 2.4M)]\n"
179-
"\t[-P enables peak hold (default: off)]\n"
181+
"\t[-P enables peak hold (default: off/averaging)]\n"
182+
"\t[-L enable linear output (default: off/dB)]\n"
180183
"\t[-D direct_sampling_mode, 0 (default/off), 1 (I), 2 (Q), 3 (no-mod)]\n"
181184
"\t[-O enable offset tuning (default: off)]\n"
182185
"\n"
@@ -622,6 +625,7 @@ void frequency_range(char *arg, struct misc_settings *ms)
622625
ts->downsample_passes = c.downsample_passes;
623626
ts->comp_fir_size = ms->comp_fir_size;
624627
ts->peak_hold = ms->peak_hold;
628+
ts->linear = ms->linear;
625629
ts->avg = (long*)malloc((1<<c.bin_e) * sizeof(long));
626630
if (!ts->avg) {
627631
fprintf(stderr, "Error: malloc.\n");
@@ -873,6 +877,7 @@ void csv_dbm(struct tuning_state *ts)
873877
int i, len, ds, i1, i2, bw2, bin_count;
874878
long tmp;
875879
double dbm;
880+
char *sep = ", ";
876881
len = 1 << ts->bin_e;
877882
ds = ts->downsample;
878883
/* fix FFT stuff quirks */
@@ -895,18 +900,19 @@ void csv_dbm(struct tuning_state *ts)
895900
i1 = 0 + (int)((double)len * ts->crop * 0.5);
896901
i2 = (len-1) - (int)((double)len * ts->crop * 0.5);
897902
for (i=i1; i<=i2; i++) {
903+
if (i == i2) {
904+
sep = "\n";
905+
}
898906
dbm = (double)ts->avg[i];
899907
dbm /= (double)ts->rate;
900908
dbm /= (double)ts->samples;
901-
dbm = 10 * log10(dbm);
902-
fprintf(file, "%.2f, ", dbm);
909+
if (ts->linear) {
910+
fprintf(file, "%.5g%s", dbm, sep);
911+
} else {
912+
dbm = 10 * log10(dbm);
913+
fprintf(file, "%.2f%s", dbm, sep);
914+
}
903915
}
904-
dbm = (double)ts->avg[i2] / ((double)ts->rate * (double)ts->samples);
905-
if (ts->bin_e == 0) {
906-
dbm = ((double)ts->avg[0] / \
907-
((double)ts->rate * (double)ts->samples));}
908-
dbm = 10 * log10(dbm);
909-
fprintf(file, "%.2f\n", dbm);
910916
for (i=0; i<len; i++) {
911917
ts->avg[i] = 0L;
912918
}
@@ -922,6 +928,8 @@ void init_misc(struct misc_settings *ms)
922928
ms->gain = AUTO_GAIN;
923929
ms->window_fn = rectangle;
924930
ms->smoothing = 0;
931+
ms->peak_hold = 0;
932+
ms->linear = 0;
925933
}
926934

927935
int main(int argc, char **argv)
@@ -951,7 +959,7 @@ int main(int argc, char **argv)
951959
freq_optarg = "";
952960
init_misc(&ms);
953961

954-
while ((opt = getopt(argc, argv, "f:i:s:r:t:d:g:p:e:w:c:F:1PD:Oh")) != -1) {
962+
while ((opt = getopt(argc, argv, "f:i:s:r:t:d:g:p:e:w:c:F:1PLD:Oh")) != -1) {
955963
switch (opt) {
956964
case 'f': // lower:upper:bin_size
957965
if (f_set) {
@@ -1015,6 +1023,9 @@ int main(int argc, char **argv)
10151023
case 'P':
10161024
ms.peak_hold = 1;
10171025
break;
1026+
case 'L':
1027+
ms.linear = 1;
1028+
break;
10181029
case 'D':
10191030
direct_sampling = atoi(optarg);
10201031
break;

0 commit comments

Comments
 (0)