@@ -46,14 +46,19 @@ def sweep(self, f_min_hz: float, f_max_hz: float, *,
4646 self .print_message (f"[*] mode: { self .mode } " )
4747
4848 lo = int (f_min_hz // 1_000_000 )
49- hi = int (f_max_hz // 1_000_000 )
49+ hi = - int (- f_max_hz // 1_000_000 ) # ceil: never truncate the top
5050 # hackrf_sweep takes integer MHz edges, so sub-MHz precision is lost.
51- # Warn rather than silently shift the band the user asked for.
51+ # Snap OUTWARD (floor the low edge, ceil the high edge) so the swept
52+ # range always CONTAINS the requested band -- flooring both edges
53+ # silently dropped everything above the last whole MHz (e.g.
54+ # 433.9:434.1 swept 433:434 and never covered 434.0-434.1). Warn so
55+ # the user knows the edges moved.
5256 if f_min_hz % 1_000_000 or f_max_hz % 1_000_000 :
5357 self .warn (
54- f"sweep edges snapped to MHz: "
58+ f"sweep edges snapped to MHz (outward) : "
5559 f"{ f_min_hz / 1e6 :g} :{ f_max_hz / 1e6 :g} -> { lo } :{ hi } MHz "
56- f"(hackrf_sweep takes integer MHz)" )
60+ f"(hackrf_sweep takes integer MHz; requested band fully "
61+ f"covered)" )
5762 argv = ["sweep" , "-f" , f"{ lo } :{ hi } " , "-l" , lna , "-g" , vga ,
5863 "-a" , 1 if amp else 0 ]
5964 if bin_width is not None :
@@ -124,13 +129,20 @@ def monitor_frequencies(self, freqs_hz: list[float], *,
124129 t0 = _time .time ()
125130
126131 def _nearest_power (rows_by_low : dict [int , Any ], f : float ) -> Any :
127- # find the sweep segment whose [hz_low, hz_high) covers f, return
128- # the mean dB of that segment's bins (a simple power proxy)
132+ # find the sweep segment whose [hz_low, hz_high) covers f and
133+ # return the dB of the BIN covering f (max of that bin +/-1 for
134+ # tuning slop). Averaging the whole segment diluted a narrowband
135+ # carrier toward the noise floor: in a 5 MHz segment a strong
136+ # signal occupying one bin barely moved the mean.
129137 for low in sorted (rows_by_low ):
130138 r = rows_by_low [low ]
131139 if r ["hz_low" ] <= f < r ["hz_high" ]:
132140 db = r ["db" ]
133- return sum (db ) / len (db ) if db else float ("-inf" )
141+ if not db :
142+ return float ("-inf" )
143+ idx = int ((f - r ["hz_low" ]) / r ["bin_width" ])
144+ idx = max (0 , min (idx , len (db ) - 1 ))
145+ return max (db [max (0 , idx - 1 ):idx + 2 ])
134146 return None
135147
136148 from .._stream_ctx import StreamCtx
@@ -189,7 +201,7 @@ def sweep_to_file(self, f_min_hz: float, f_max_hz: float, out: str, *,
189201 lna = self ._snap_gain ("lna_gain" , lna , C .LNA_GAIN )
190202 vga = self ._snap_gain ("vga_gain" , vga , C .VGA_GAIN )
191203 lo = int (f_min_hz // 1_000_000 )
192- hi = int (f_max_hz // 1_000_000 )
204+ hi = - int (- f_max_hz // 1_000_000 ) # ceil: never truncate the top
193205 argv = ["sweep" , "-f" , f"{ lo } :{ hi } " , "-l" , lna , "-g" , vga ,
194206 "-a" , 1 if amp else 0 , "-r" , out ]
195207 if bin_width is not None :
0 commit comments