From ecae3d5109e07cc240a4aee45c83e801ec8d857d Mon Sep 17 00:00:00 2001 From: Kyle Keen Date: Mon, 25 Aug 2014 21:28:29 -0400 Subject: [PATCH] rtl_power: fix bugs with bin counting --- src/rtl_power.c | 48 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/src/rtl_power.c b/src/rtl_power.c index 5d43986..feef236 100644 --- a/src/rtl_power.c +++ b/src/rtl_power.c @@ -106,8 +106,10 @@ struct tuning_state int comp_fir_size; int peak_hold; int linear; + int bin_spec; double crop; int crop_i1, crop_i2; + int freq_low, freq_high; //pthread_rwlock_t avg_lock; //pthread_mutex_t avg_mutex; /* having the iq buffer here is wasteful, but will avoid contention */ @@ -573,7 +575,8 @@ void frequency_range(char *arg, struct misc_settings *ms) { struct channel_solve c; struct tuning_state *ts; - int i, j, buf_len, length, final_bins; + int i, j, buf_len, length, hop_bins, logged_bins, planned_bins; + int lower_edge, actual_bw, upper_perfect, remainder; fprintf(stderr, "Range: %s\n", arg); parse_frequency(arg, &c); @@ -614,13 +617,17 @@ void frequency_range(char *arg, struct misc_settings *ms) buf_len = DEFAULT_BUF_LENGTH; } /* build the array */ + logged_bins = 0; + lower_edge = c.lower; + planned_bins = (c.upper - c.lower) / c.bin_spec; for (i=0; i < c.hops; i++) { ts = &tunes[tune_count + i]; - ts->freq = c.lower + i*c.bw_wanted + c.bw_wanted/2; + /* copy common values */ ts->rate = c.bw_needed; ts->gain = ms->gain; ts->bin_e = c.bin_e; ts->samples = 0; + ts->bin_spec = c.bin_spec; ts->crop = c.crop; ts->downsample = c.downsample; ts->downsample_passes = c.downsample_passes; @@ -646,9 +653,29 @@ void frequency_range(char *arg, struct misc_settings *ms) for (j=0; jwindow_coefs[j] = (int)(256*ms->window_fn(j, length)); } - final_bins = c.bw_wanted / c.bin_spec; - ts->crop_i1 = length/2 - final_bins/2; - ts->crop_i2 = ts->crop_i1 + final_bins - 1; + /* calculate unique values */ + ts->freq_low = lower_edge; + hop_bins = c.bw_wanted / c.bin_spec; + actual_bw = hop_bins * c.bin_spec; + ts->freq_high = lower_edge + actual_bw; + upper_perfect = c.lower + (i+1) * c.bw_wanted; + if (ts->freq_high + c.bin_spec <= upper_perfect) { + hop_bins += 1; + actual_bw = hop_bins * c.bin_spec; + ts->freq_high = lower_edge + actual_bw; + } + remainder = planned_bins - logged_bins - hop_bins; + if (i == c.hops-1 && remainder > 0) { + hop_bins += remainder; + actual_bw = hop_bins * c.bin_spec; + ts->freq_high = lower_edge + actual_bw; + } + logged_bins += hop_bins; + ts->crop_i1 = (length - hop_bins) / 2; + ts->crop_i2 = ts->crop_i1 + hop_bins - 1; + ts->freq = (lower_edge - ts->crop_i1 * c.bin_spec) + c.bw_needed/2; + /* prep for next hop */ + lower_edge = ts->freq_high; } tune_count += c.hops; /* report */ @@ -657,8 +684,7 @@ void frequency_range(char *arg, struct misc_settings *ms) fprintf(stderr, "Downsampling by: %ix\n", c.downsample); fprintf(stderr, "Cropping by: %0.2f%%\n", c.crop*100); fprintf(stderr, "Total FFT bins: %i\n", c.hops * (1<crop)); - bw2 = (int)(((double)ts->rate * (double)bin_count) / (len * 2 * ds)); - fprintf(file, "%i, %i, %.2f, %i, ", ts->freq - bw2, ts->freq + bw2, - (double)ts->rate / (double)(len*ds), ts->samples); + fprintf(file, "%i, %i, %i, %i, ", ts->freq_low, ts->freq_high, + ts->bin_spec, ts->samples); // something seems off with the dbm math for (i=ts->crop_i1; i<=ts->crop_i2; i++) { if (i == ts->crop_i2) {