From 69d7cd9595a1bc4568767c74843a32d844b2886e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Hamal=20Dvo=C5=99=C3=A1k?= <mordae@anilinux.org>
Date: Sat, 8 Jun 2024 11:25:02 +0200
Subject: [PATCH] Improve secondary filter

---
 src/main.c | 31 ++++++++++++++++++++++++-------
 1 file changed, 24 insertions(+), 7 deletions(-)

diff --git a/src/main.c b/src/main.c
index 5e87852..71f465d 100644
--- a/src/main.c
+++ b/src/main.c
@@ -37,9 +37,9 @@
 static uint32_t lo_cos[LO_WORDS] __attribute__((__aligned__(1 << LO_BITS_DEPTH)));
 static uint32_t lo_sin[LO_WORDS] __attribute__((__aligned__(1 << LO_BITS_DEPTH)));
 
-#define DECIMATE 2
+#define DECIMATE 4
 #define RX_STRIDE (2 * IQ_SAMPLES * DECIMATE)
-#define RX_BITS_DEPTH 12
+#define RX_BITS_DEPTH 13
 #define RX_WORDS (1 << (RX_BITS_DEPTH - 2))
 
 static_assert(RX_STRIDE * 4 < RX_WORDS, "RX_STRIDE * 4 < RX_WORDS");
@@ -430,7 +430,8 @@ static void rf_rx(void)
 	unsigned pos = 0;
 
 	int64_t dcI = 0, dcQ = 0;
-	int prevI = 0, prevQ = 0;
+	int prevI1 = 0, prevI2 = 0, prevI3 = 0;
+	int prevQ1 = 0, prevQ2 = 0, prevQ3 = 0;
 
 	while (true) {
 		if (multicore_fifo_rvalid()) {
@@ -486,8 +487,16 @@ static void rf_rx(void)
 			cos_pos = *cos_ptr++;
 			int I2 = cos_neg - cos_pos;
 
-			int64_t I = (prevI + I1 + I1 + I2) / 2;
-			prevI = I2;
+			cos_neg = *cos_ptr++;
+			cos_pos = *cos_ptr++;
+			int I3 = cos_neg - cos_pos;
+
+			cos_neg = *cos_ptr++;
+			cos_pos = *cos_ptr++;
+			int I4 = cos_neg - cos_pos;
+
+			int64_t I = (prevI3 + prevI2 * 3 + prevI1 * 4 + I1 * 4 + I2 * 3 + I3) / 4;
+			prevI1 = I4, prevI2 = I3, prevI3 = I2;
 
 			int64_t I16 = I << 16;
 			dcI = ((dcI << 16) - dcI + I16) >> 16;
@@ -511,8 +520,16 @@ static void rf_rx(void)
 			sin_pos = *sin_ptr++;
 			int Q2 = sin_neg - sin_pos;
 
-			int64_t Q = (prevQ + Q1 + Q1 + Q2) / 2;
-			prevQ = Q2;
+			sin_neg = *sin_ptr++;
+			sin_pos = *sin_ptr++;
+			int Q3 = sin_neg - sin_pos;
+
+			sin_neg = *sin_ptr++;
+			sin_pos = *sin_ptr++;
+			int Q4 = sin_neg - sin_pos;
+
+			int64_t Q = (prevQ3 + prevQ2 * 3 + prevQ1 * 4 + Q1 * 4 + Q2 * 3 + Q3) / 4;
+			prevQ1 = Q4, prevQ2 = Q3, prevQ3 = Q2;
 
 			int64_t Q16 = Q << 16;
 			dcQ = ((dcQ << 16) - dcQ + Q16) >> 16;