From 69deebb2f2c4e6856010936a81287882c5e97c84 Mon Sep 17 00:00:00 2001 From: boblark Date: Wed, 30 Mar 2022 18:07:16 -0700 Subject: [PATCH] Correct phase interpolation to correct very minor error --- RadioIQMixer_F32.cpp | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/RadioIQMixer_F32.cpp b/RadioIQMixer_F32.cpp index 260572d..580d6fc 100644 --- a/RadioIQMixer_F32.cpp +++ b/RadioIQMixer_F32.cpp @@ -9,17 +9,19 @@ * and cos of the same frequency. The pair of mixer outputs are * referred to as i and q. The conversion in frequency is either * up or down, and a pair of filters on i and q determine which is allow - * to pass to the output. - * + * to pass to the output. + * * April 2021 - Alternatively, there can be two inputs to 0 (left) and 1 * (right) feeding the two mixers separately. This covers all transmit * and receive situations. - * + * * The sin/cos LO is from synth_sin_cos_f32.cpp See that for details. - * + * * Inputs are either real or I-Q per bool twoChannel. Rev Apr 2021 - * + * * MIT License, Use at your own risk. + * + * Rev 28 Mar 2022 Correctedslight interpolation error. RSL */ #include "RadioIQMixer_F32.h" @@ -47,7 +49,7 @@ void RadioIQMixer_F32::update(void) { // Try to get a pair of blocks for the IQ output blockOut_i = AudioStream_F32::allocate_f32(); - if (!blockOut_i){ // Didn't have any + if (!blockOut_i){ // Didn't have any if(errorPrintIQM) Serial.println("IQMIXER-ERR: No I output memory"); AudioStream_F32::release(blockIn0); if(twoChannel) @@ -78,8 +80,8 @@ void RadioIQMixer_F32::update(void) { a = sinTable512_f32[index]; b = sinTable512_f32[index+1]; // Linear interpolation and multiplying (DBMixer) with input - blockOut_i->data[i] = blockIn0->data[i] * (a + 0.001953125*(b-a)*deltaPhase); - + blockOut_i->data[i] = blockIn0->data[i] * (a + (b-a)*deltaPhase); + /* Repeat for cosine by adding 90 degrees phase */ index = (index + 128) & 0x01ff; /* Read two nearest values of input value from the sin table */ @@ -87,9 +89,9 @@ void RadioIQMixer_F32::update(void) { b = sinTable512_f32[index+1]; /* deltaPhase will be the same as used for sin */ if(twoChannel) - blockOut_q->data[i] = blockIn1->data[i]*(a + 0.001953125*(b-a)*deltaPhase); + blockOut_q->data[i] = blockIn1->data[i]*(a + (b-a)*deltaPhase); else - blockOut_q->data[i] = blockIn0->data[i]*(a + 0.001953125*(b-a)*deltaPhase); + blockOut_q->data[i] = blockIn0->data[i]*(a + (b-a)*deltaPhase); } } else { // Do a more flexible update, i.e., not doSimple @@ -103,8 +105,8 @@ void RadioIQMixer_F32::update(void) { b = sinTable512_f32[index+1]; // We now have a sine value, so multiply with the input data and save // Linear interpolate sine and multiply with the input and amplitude (about 1.0) - blockOut_i->data[i] = amplitude_pk * blockIn0->data[i] * (a + 0.001953125*(b-a)*deltaPhase); - + blockOut_i->data[i] = amplitude_pk * blockIn0->data[i] * (a + (b-a)*deltaPhase); + /* Shift forward phaseS_C and get cos. First, the calculation of index of the table */ phaseC = phaseS + phaseS_C; if (phaseC > 512.0f) phaseC -= 512.0f; @@ -115,9 +117,9 @@ void RadioIQMixer_F32::update(void) { b = sinTable512_f32[index+1]; // Same as sin, but leave amplitude of LO at +/- 1.0 if(twoChannel) - blockOut_q->data[i] = blockIn1->data[i]*(a + 0.001953125*(b-a)*deltaPhase); + blockOut_q->data[i] = blockIn1->data[i]*(a + (b-a)*deltaPhase); else - blockOut_q->data[i] = blockIn0->data[i]*(a + 0.001953125*(b-a)*deltaPhase); + blockOut_q->data[i] = blockIn0->data[i]*(a + (b-a)*deltaPhase); } } AudioStream_F32::release(blockIn0); // Done with this