From 3d11de1839c457c09c65c56f015613329a97148d Mon Sep 17 00:00:00 2001 From: erikkaashoek Date: Tue, 17 Nov 2026 17:17:58 +0100 Subject: [PATCH] Spur check v2: grid-based minimal IF shift, sixth family at 3IF/2, no dynamic table on hw_if units Per point in a family window the LO is planned, the product offset computed for the LO exactly on the target, and the smaller LO move in either direction that puts the product D away is taken; the IF is shifted by that move so the Si4468 needs no compensation. D is 1.5 RBW at >= 300k, 2 RBW at 30k-100k, 2.5 RBW at 3k-10k, 3 RBW below. Shifts stay within 450 kHz so the IF stays in the BPF with IF1 0.5 MHz off; below IF only when the budget does not clear A4 or C3. The 2L x 5th Si4468 harmonic line at 1603.6 MHz becomes the sixth family and the check also runs in the ultra path while spur removal is off. The hw_if dynamic table entries (IF/4 - 400 kHz, 2IF/3 + 160 kHz, 3IF/2) held no line of their own. Also: fast mode no longer loses an offset step after a full retune (old_delta), and the 200 Hz rule that never ran is removed. --- sa_core.c | 155 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 97 insertions(+), 58 deletions(-) diff --git a/sa_core.c b/sa_core.c index c5ab097..2014d58 100644 --- a/sa_core.c +++ b/sa_core.c @@ -96,6 +96,9 @@ int linear_averaging = true; static freq_t old_freq[5] = { 0, 0, 0, 0,0}; static freq_t real_old_freq[5] = { 0, 0, 0, 0,0}; static long real_offset = 0; +#ifdef TINYSA4 +static int old_delta = 0x20000; // Si4468 offset register value set by set_freq() in fast mode +#endif static float old_temp = 0.0; static int LO_spur_shifted; @@ -2660,7 +2663,6 @@ void set_freq(int V, freq_t freq) // translate the requested frequency into a delta = delta >> 1; delta = ((float)delta) / OFFSET_STEP; // Calculate and set the offset register i.s.o programming a new frequency if (delta > - 0x7fff && delta < 0x7fff) { // and requested frequency can be reached by using the offset registers - static int old_delta = 0x20000; if (old_delta != delta) { si_set_offset(delta); // Signal offset changed so RSSI retrieval is delayed for frequency settling old_delta = delta; @@ -2671,6 +2673,7 @@ void set_freq(int V, freq_t freq) // translate the requested frequency into a fast_counter = 0; // Offset tuning not possible real_offset = 0; real_old_freq[V] = SI4463_set_freq(freq); // Not in fast mode + old_delta = 0x20000; // SI4463_set_freq() cleared the offset register } done: #endif @@ -3221,18 +3224,28 @@ static freq_t *spur_table = (freq_t *)static_spur_table; int spur_table_size = STATIC_SPUR_TABLE_SIZE; freq_t spur_IF = static_spur_IF; -// Spur families of the units with the hardware IF (ZS406, ZS407): LO harmonics mixing with the Si4468 VCO -// (4 * (IF - SI4468_LOW_IF)). +// Spur families of the units with the hardware IF (ZS406, ZS407): harmonics of the LO (2L, 3L, 4L) leak into the +// Si4468 and are received through the 3rd, 4th and 5th harmonic of its own LO, which sits SI4468_LOW_IF below +// the receive frequency (bench 2026-11-16/17, doc/spur_reduction_proposal.md). // With the LO error e and the part c of it the Si4468 takes up, a family's product lands at // k1 * (lf + e - f) - kc * c - ks * shift from the receive frequency; f is its input frequency at the -// nominal IF, shift an IF shift on top of that. +// nominal IF, shift an IF shift on top of that, ks = kc - k1. #define SI4468_LOW_IF 468750 // Si4468 receiver IF: 30 MHz / 64 -enum { SPUR_A4, SPUR_C3M, SPUR_C3P, SPUR_B2, SPUR_A3, SPUR_FAMILIES }; // 266.95, 355.78, 356.10, 533.91, 712.50 MHz -static freq_t spur_family_freq[SPUR_FAMILIES]; -static freq_t spur_family_lo[SPUR_FAMILIES], spur_family_hi[SPUR_FAMILIES]; // inputs that can put a product within 2 RBW -static const uint8_t spur_family_k1[SPUR_FAMILIES] = { 4, 3, 3, 2, 3 }; -static const uint8_t spur_family_kc[SPUR_FAMILIES] = { 5, 4, 4, 3, 5 }; -static const uint8_t spur_family_ks[SPUR_FAMILIES] = { 1, 1, 1, 1, 2 }; +#define SPUR_SHIFT_MAX 450000 // largest IF shift: stays inside the BPF with IF1 0.5 MHz off +enum { SPUR_A4, SPUR_C3M, SPUR_C3P, SPUR_B2, SPUR_A3, SPUR_B5, SPUR_FAMILIES }; // 266.95, 355.78, 356.10, 533.91, 712.50, 1603.60 MHz +static uint32_t spur_family_freq[SPUR_FAMILIES]; +static uint32_t spur_family_lo[SPUR_FAMILIES], spur_family_hi[SPUR_FAMILIES]; // inputs that can put a product within D +static const uint8_t spur_family_k1[SPUR_FAMILIES] = { 4, 3, 3, 2, 3, 2 }; +static const uint8_t spur_family_kc[SPUR_FAMILIES] = { 5, 4, 4, 3, 5, 5 }; +static const uint8_t spur_family_ks[SPUR_FAMILIES] = { 1, 1, 1, 1, 2, 3 }; + +// Required distance of a family product from the receive frequency. The RBW filter gives about 47 dB at 2 RBW; +// the strongest family (B2, -73 dBm on the ZS407) needs more where the floor is lower, less at wide RBW. +static int32_t spur_family_D(void) +{ + int d10 = (actual_rbw_x10 >= 3000 ? 15 : actual_rbw_x10 >= 300 ? 20 : actual_rbw_x10 >= 30 ? 25 : 30); // RBW/10 + return (int32_t)actual_rbw_x10 * 10 * d10; // RBW = actual_rbw_x10 * 100 Hz +} #endif @@ -3354,43 +3367,35 @@ void fill_spur_table(void) // dynamic_spur_table[dynamic_spur_table_size++] = 153000000; // dynamic_spur_table[dynamic_spur_table_size++] = 174600000; // dynamic_spur_table[dynamic_spur_table_size++] = 219000000; - if (hw_if) { // these spurs are found from the actual LO setting by spur_family_distance() + if (hw_if) { // all internal spurs of these units are found from the actual LO setting by + // spur_family_offset(); the table stays empty (the former entries IF/4 - 400 kHz, + // 2IF/3 + 160 kHz and 3IF/2 held no line of their own, bench 2026-11-16/17) spur_family_freq[SPUR_A4] = (corr_IF - 4*SI4468_LOW_IF)/4; spur_family_freq[SPUR_C3M] = (corr_IF - 5*SI4468_LOW_IF)/3; spur_family_freq[SPUR_C3P] = (corr_IF - 3*SI4468_LOW_IF)/3; spur_family_freq[SPUR_B2] = (corr_IF - 4*SI4468_LOW_IF)/2; spur_family_freq[SPUR_A3] = (2*corr_IF - 4*SI4468_LOW_IF)/3; - int32_t shift = (actual_rbw_x10 > 2000 ? DEFAULT_SPUR_OFFSET : DEFAULT_SPUR_OFFSET/2); + spur_family_freq[SPUR_B5] = (3*corr_IF - 4*SI4468_LOW_IF)/2; + int32_t D = spur_family_D(); for (int i = 0; i < SPUR_FAMILIES; i++) { - freq_t w = (actual_rbw_x10 * 200 + spur_family_ks[i] * shift) / spur_family_k1[i] + 250000; // also with the IF shift + uint32_t w = (D + spur_family_ks[i] * SPUR_SHIFT_MAX) / spur_family_k1[i] + 250000; // also with the largest IF shift spur_family_lo[i] = spur_family_freq[i] - w; spur_family_hi[i] = spur_family_freq[i] + w; if ((i == SPUR_C3M || i == SPUR_C3P) && actual_rbw_x10 > 1000) // -98 dBm: below the noise above 100 kHz RBW spur_family_hi[i] = 0; } - } else + } else { dynamic_spur_table[dynamic_spur_table_size++] = corr_IF/4 -spur_factor/2; - if (!hw_if) dynamic_spur_table[dynamic_spur_table_size++] = corr_IF/4 -spur_factor/2 + 60000; - else - dynamic_spur_table[dynamic_spur_table_size++] = corr_IF/4 - 400000; - if (!hw_if) { dynamic_spur_table[dynamic_spur_table_size++] = corr_IF/3 -spur_factor/3; dynamic_spur_table[dynamic_spur_table_size++] = corr_IF/2 -spur_factor - 40000; dynamic_spur_table[dynamic_spur_table_size++] = corr_IF/2 -spur_factor - 20000; if (actual_rbw_x10 < 3000) { dynamic_spur_table[dynamic_spur_table_size++] = corr_IF/2 -spur_factor + 10000; } -// if (actual_rbw_x10 < 1000 && hw_if) { -// dynamic_spur_table[dynamic_spur_table_size++] = corr_IF/2 - spur_factor + 120000; -// dynamic_spur_table[dynamic_spur_table_size++] = corr_IF/2 - spur_factor + 170000; -// } dynamic_spur_table[dynamic_spur_table_size++] = corr_IF*2/3 -spur_factor*2/3; + dynamic_spur_table[dynamic_spur_table_size++] = corr_IF*3/2 -spur_factor*3/2; } - if (actual_rbw_x10 < 6000 && hw_if) { - dynamic_spur_table[dynamic_spur_table_size++] = corr_IF*2/3 -spur_factor*2/3 + 160000; - } - dynamic_spur_table[dynamic_spur_table_size++] = corr_IF*3/2 -spur_factor*3/2; spur_table = dynamic_spur_table; spur_table_size = dynamic_spur_table_size; #if 0 @@ -3476,25 +3481,25 @@ static bool spur_family_window(freq_t lf) return false; } -// Distance from the receive frequency of the nearest spur family product with the LO at L, INT32_MAX when +// Signed offset from the receive frequency of the nearest spur family product with the LO at L, INT32_MAX when // none is near. target_f = lf + nominal IF + shift. -static int32_t spur_family_distance(freq_t lf, freq_t target_f, freq_t L, int inverted_f, int32_t shift, int *family) +static int32_t spur_family_offset(freq_t lf, freq_t target_f, freq_t L, int inverted_f, int32_t shift, int *family) { int32_t e = (int32_t)(L - target_f); int32_t c = LO_IF_correction(L, target_f, inverted_f); - int32_t dist = INT32_MAX; + int32_t best = INT32_MAX, dist = INT32_MAX; for (int i = 0; i < SPUR_FAMILIES; i++) { if (lf < spur_family_lo[i] || lf > spur_family_hi[i]) continue; int32_t o = spur_family_k1[i] * ((int32_t)(lf - spur_family_freq[i]) + e) - spur_family_kc[i] * c - spur_family_ks[i] * shift; - if (o < 0) - o = -o; - if (o < dist) { - dist = o; + int32_t a = (o < 0 ? -o : o); + if (a < dist) { + dist = a; + best = o; *family = i; } } - return dist; + return best; } #endif @@ -4744,39 +4749,75 @@ again: // Spur redu } if (signal_path == PATH_LEAKAGE) set_freq(ADF4351_LO, target_f); // Never used as leakage is taken care under direct - else if (hw_if && lf >= spur_family_lo[SPUR_A4] && lf <= spur_family_hi[SPUR_A3] // families sorted by frequency - && spur_family_window(lf) && setting.mode == M_LOW && signal_path == PATH_LOW && !tracking && setting.auto_IF && config.frequency_IF2 == 0 + else if (hw_if && lf >= spur_family_lo[SPUR_A4] && lf <= spur_family_hi[SPUR_B5] // families sorted by frequency + && spur_family_window(lf) && setting.mode == M_LOW && (signal_path == PATH_LOW || signal_path == PATH_ULTRA) + && !tracking && setting.auto_IF && config.frequency_IF2 == 0 && avoid_setting == avoid_auto && !in_selftest && !S_STATE(setting.spur_removal) && setting.below_IF == S_AUTO_OFF && !LO_shifting && !LO_spur_shifted) { - // Plan the LO, avoid the spur family products it would put in the RBW filter, then set it + // Plan the LO, move the spur family products it would put in the RBW filter, then set it ADF4351_plan_t plan; bool retune = LO_retune(target_f); freq_t L = (retune ? ADF4351_plan_frequency(target_f, &plan) : real_old_freq[ADF4351_LO]); int family = -1; - int32_t dist = spur_family_distance(lf, target_f, L, inverted_f, 0, &family); - if (dist < actual_rbw_x10 * 200 && (!debug_avoid || debug_avoid_second)) { // within 2 RBW: less than 50 dB suppression - if (family == SPUR_A4) { // below IF, as the spur table for the lowest spurs - if (debug_avoid) stored_t[i] = -60.0; // Display when to do spur shift in the stored trace + int32_t o = spur_family_offset(lf, target_f, L, inverted_f, 0, &family); + int32_t D = spur_family_D(); + if (o != INT32_MAX && o < D && o > -D && (!debug_avoid || debug_avoid_second)) { + // With the LO exactly at target_f the product sits at o_ref; it moves by -ks for every Hz the LO is set + // higher. Find the LO grid point nearest to target_f, in either direction, that puts the product at least D + // away, and shift the IF by that LO move: the Si4468 then needs no compensation (IF_rx = L' - lf). + int ks = spur_family_ks[family]; + int f2; + int32_t o_ref = spur_family_offset(lf, target_f, target_f, inverted_f, 0, &f2); + int32_t need[2] = { (o_ref + D + ks - 1) / ks, -((D - o_ref + ks - 1) / ks) }; // LO above / below target_f + int32_t nudge = (D / 8 > 2500 ? D / 8 : 2500); + ADF4351_plan_t best_plan = {0}; + freq_t best_f = target_f; + int32_t best_s = 0, best_cost = INT32_MAX, best_a = (o < 0 ? -o : o); + for (int dir = 0; dir < 2; dir++) { + int32_t s = need[dir]; + for (int tries = 0; tries < 3; tries++) { + if (s > SPUR_SHIFT_MAX || s < -SPUR_SHIFT_MAX) + break; + ADF4351_plan_t p; + int32_t dL = (int32_t)(ADF4351_plan_frequency(target_f + s, &p) - target_f); + int32_t o2 = o_ref - ks * dL; + int32_t a2 = (o2 < 0 ? -o2 : o2), cost = (dL < 0 ? -dL : dL); + if (a2 >= D) { + if (cost < best_cost && cost <= SPUR_SHIFT_MAX) { + best_cost = cost; best_s = dL; best_f = target_f + dL; best_plan = p; best_a = a2; + } + break; + } + s += (dir == 0 ? nudge : -nudge); // the grid rounded the LO short of the needed move + } + } + if (best_cost == INT32_MAX) { // nothing within the budget: the budget edge that helps most + for (int dir = 0; dir < 2; dir++) { + int32_t s = (dir == 0 ? SPUR_SHIFT_MAX : -SPUR_SHIFT_MAX); + ADF4351_plan_t p; + int32_t dL = (int32_t)(ADF4351_plan_frequency(target_f + s, &p) - target_f); + int32_t o2 = o_ref - ks * dL; + int32_t a2 = (o2 < 0 ? -o2 : o2); + if (a2 > best_a) { + best_s = dL; best_f = target_f + dL; best_plan = p; best_a = a2; + } + } + } + if (best_a < D && (family == SPUR_A4 || family == SPUR_C3M || family == SPUR_C3P) && lf < local_IF/2 - 10000000) { + // the shift budget does not clear the product: below IF, as the spur table did for the lowest spurs + if (debug_avoid) stored_t[i] = -70.0; // Display when to do spur shift in the stored trace setting.below_IF = S_AUTO_ON; target_f = local_IF - lf; retune = LO_retune(target_f); if (retune) ADF4351_plan_frequency(target_f, &plan); - } else { // shift the IF if that moves the products further away - int32_t shift = (actual_rbw_x10 > 2000 ? DEFAULT_SPUR_OFFSET : DEFAULT_SPUR_OFFSET/2); - freq_t shifted_f = target_f + shift; - ADF4351_plan_t shifted_plan; - bool shifted_retune = LO_retune(shifted_f); - freq_t shifted_L = (shifted_retune ? ADF4351_plan_frequency(shifted_f, &shifted_plan) : real_old_freq[ADF4351_LO]); - if (spur_family_distance(lf, shifted_f, shifted_L, inverted_f, shift, &family) > dist) { - if (debug_avoid) stored_t[i] = -60.0; // Display when to do spur shift in the stored trace - local_IF += shift; - LO_spur_shifted = true; - target_f = shifted_f; - retune = shifted_retune; - if (retune) - plan = shifted_plan; - } + } else if (best_s != 0) { + if (debug_avoid) stored_t[i] = -60.0; // Display when to do spur shift in the stored trace + local_IF += best_s; + LO_spur_shifted = true; + target_f = best_f; + plan = best_plan; + retune = true; // a shifted point programs its planned LO: the hysteresis would keep the old one } } if (retune) { // as set_freq(ADF4351_LO, target_f) @@ -5084,8 +5125,6 @@ again: // Spur redu #ifdef TINYSA4 if (SI4432_step_delay && (ADF4351_frequency_changed || SI4463_frequency_changed)) { int my_step_delay = SI4432_step_delay; - if (lf < LOW_SHIFT_FREQ && actual_rbw_x10 == 3 && !in_step_test) - my_step_delay = my_step_delay * 2; // if (LO_spur_shifted) // || SI4463_offset_changed) // my_step_delay = my_step_delay * 2; #if 0 // Always have some delay before measuring RSSI