Oversample baseband more for image rejection

This commit is contained in:
Jan Hamal Dvořák 2024-07-22 19:22:14 +02:00
parent 2ddd7ddd71
commit 558c3a0a41

View file

@ -43,9 +43,9 @@
#define LO_WORDS (1 << (LO_BITS_DEPTH - 2))
static uint32_t lo_cos[LO_WORDS] __attribute__((__aligned__(1 << LO_BITS_DEPTH)));
#define DECIMATE 4
#define DECIMATE 16
#define RX_STRIDE (2 * IQ_SAMPLES * DECIMATE)
#define RX_BITS_DEPTH 13
#define RX_BITS_DEPTH 14
#define RX_WORDS (1 << (RX_BITS_DEPTH - 2))
static_assert(RX_STRIDE * 4 <= RX_WORDS, "RX_STRIDE * 4 <= RX_WORDS");
@ -382,36 +382,71 @@ static void rf_rx_stop(void)
dma_t_samp = -1;
}
inline static int next_sample(const uint32_t *buf, int *h)
struct IQ {
int I, Q;
};
inline static struct IQ next_sample(const uint32_t *buf)
{
int x1 = 2 * (buf[0] - buf[1]);
int x2 = 0;
int x3 = -2 * (buf[4] - buf[5]);
int x4 = 0;
int x00 = buf[0] - buf[1];
int x01 = buf[2] - buf[3];
int x02 = buf[4] - buf[5];
int x03 = buf[6] - buf[7];
int x04 = buf[8] - buf[9];
int x05 = buf[10] - buf[11];
int x06 = buf[12] - buf[13];
int x07 = buf[14] - buf[15];
int x08 = buf[16] - buf[17];
int x09 = buf[18] - buf[19];
int x10 = buf[20] - buf[21];
int x11 = buf[22] - buf[23];
int x12 = buf[24] - buf[25];
int x13 = buf[26] - buf[27];
int x14 = buf[28] - buf[29];
int x15 = buf[30] - buf[31];
int sample = 18 * h[9] + 14 * h[8] - 32 * h[7] - 33 * h[6] + 106 * h[5] + 362 * h[4] +
565 * h[3] + 565 * h[2] + 362 * h[1] + 106 * h[0] - 33 * x1 - 32 * x2 +
14 * x3 + 18 * x4;
int I = 0;
I += 39 * x15;
I += 72 * x14;
I += 94 * x13;
I += 102 * x12;
I += 94 * x11;
I += 72 * x10;
I += 39 * x09;
I += 0 * x08;
I += -39 * x07;
I += -72 * x06;
I += -94 * x05;
I += -102 * x04;
I += -94 * x03;
I += -72 * x02;
I += -39 * x01;
I += 0 * x00;
h[9] = h[5];
h[8] = h[4];
h[7] = h[3];
h[6] = h[2];
h[5] = h[1];
h[4] = h[0];
h[3] = x1;
h[2] = x2;
h[1] = x3;
h[0] = x4;
int Q = 0;
Q += 94 * x15;
Q += 72 * x14;
Q += 39 * x13;
Q += 0 * x12;
Q += -39 * x11;
Q += -72 * x10;
Q += -94 * x09;
Q += -102 * x08;
Q += -94 * x07;
Q += -72 * x06;
Q += -39 * x05;
Q += 0 * x04;
Q += 39 * x03;
Q += 72 * x02;
Q += 94 * x01;
Q += 102 * x00;
return sample >> 10;
return (struct IQ){ gain * I / 1024, gain * Q / 1024 };
}
static void rf_rx(void)
{
const uint32_t base = (uint32_t)rx_cos;
int prevI[10] = { 0 };
int prevQ[10] = { 0 };
int pos = 0;
while (true) {
@ -424,8 +459,6 @@ static void rf_rx(void)
int head = (dma_hw->ch[dma_ch_in_cos].write_addr - base) / 4;
int delta = (head < pos ? head + RX_WORDS : head) - pos;
sleep_us(10);
while (delta < RX_STRIDE) {
sleep_us(1);
head = (dma_hw->ch[dma_ch_in_cos].write_addr - base) / 4;
@ -449,12 +482,14 @@ static void rf_rx(void)
int64_t max_amplitude = CLK_SYS_HZ / 2 / sample_rate;
for (int i = 0; i < IQ_SAMPLES; i++) {
int64_t I = next_sample(cos_ptr + 0, prevI);
int64_t Q = next_sample(cos_ptr + 2, prevQ);
cos_ptr += 8;
struct IQ IQ = next_sample(cos_ptr);
cos_ptr += 2 * DECIMATE;
I *= gain;
I -= (max_amplitude * 181) / 256;
int64_t I = IQ.I;
int64_t Q = IQ.Q;
//I *= gain;
I -= (max_amplitude * 169) / 256;
I /= max_amplitude;
if (I > 127)
@ -464,8 +499,8 @@ static void rf_rx(void)
*blockptr++ = (uint8_t)I + 128;
Q *= gain;
Q -= (max_amplitude * 181) / 256;
//Q *= gain;
Q -= (max_amplitude * 169) / 256;
Q /= max_amplitude;
if (Q > 127)