From a6935d6e33cec77544484cf94f3ea07e3433a6bc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Hamal=20Dvo=C5=99=C3=A1k?= <mordae@anilinux.org>
Date: Mon, 18 Mar 2024 10:44:43 +0100
Subject: [PATCH] Add LO dithering option

Makes receiver susceptible to wide-band interference in exchange for
lower spurs that cause reception at unrelated frequencies.
---
 src/main.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/main.c b/src/main.c
index ea62cda..a6bd2c3 100644
--- a/src/main.c
+++ b/src/main.c
@@ -46,6 +46,7 @@
 #define LPF_ORDER 4
 #define AGC_DECAY_BITS 20
 #define BIAS_STRENGTH 0
+#define LO_DITHER 0
 #endif
 
 /* Digital Data */
@@ -57,6 +58,7 @@
 #define LPF_ORDER 4
 #define AGC_DECAY_BITS 16
 #define BIAS_STRENGTH 3
+#define LO_DITHER 1
 #endif
 
 #define IQ_BLOCK_LEN 32
@@ -318,7 +320,15 @@ static void lo_generate(uint32_t *buf, size_t len, double freq, unsigned phase)
 		unsigned bits = 0;
 
 		for (int j = 0; j < 32; j++) {
+#if LO_DITHER
+			int n0 = rand() - RAND_MAX / 2;
+			int n1 = rand() - RAND_MAX / 2;
+			int noise = (n0 + n1) / 2;
+
+			bits |= (accum + noise) >> 31;
+#else
 			bits |= accum >> 31;
+#endif
 			bits <<= 1;
 			accum += step;
 		}