NanoVNA/dsp.c

144 lines
2.9 KiB
C
Raw Normal View History

/*
* Copyright (c) 2014-2015, TAKAHASHI Tomohiro (TTRFTECH) edy555@gmail.com
* All rights reserved.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* The software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
2016-09-18 03:11:18 +02:00
#include <arm_math.h>
#include "nanovna.h"
#ifdef ENABLED_DUMP
int16_t samp_buf[SAMPLE_LEN];
2016-09-18 03:11:18 +02:00
int16_t ref_buf[SAMPLE_LEN];
#endif
2016-09-18 03:11:18 +02:00
float acc_samp_s;
float acc_samp_c;
float acc_ref_s;
float acc_ref_c;
2016-09-19 02:31:50 +02:00
2016-09-18 03:11:18 +02:00
void
dsp_process(int16_t *capture, size_t length)
{
uint32_t *p = (uint32_t*)capture;
uint32_t len = length / 2;
2019-10-17 16:48:37 +02:00
uint32_t i, j;
int32_t samp_s = 0;
int32_t samp_c = 0;
int32_t ref_s = 0;
int32_t ref_c = 0;
2019-10-16 15:27:26 +02:00
// quadrature steps for if=12kHz on fs=48kHz
2019-10-17 16:48:37 +02:00
for (i = 0, j = 0; i < len && j < 12; j++) {
2019-10-16 15:27:26 +02:00
uint32_t sr;
int16_t ref, smp;
sr = *p++;
ref = sr & 0xffff;
smp = (sr>>16) & 0xffff;
#ifdef ENABLED_DUMP
ref_buf[i] = ref;
samp_buf[i] = smp;
#endif
i++;
samp_s += smp;
ref_s += ref;
sr = *p++;
ref = sr & 0xffff;
smp = (sr>>16) & 0xffff;
#ifdef ENABLED_DUMP
ref_buf[i] = ref;
samp_buf[i] = smp;
#endif
i++;
samp_c += smp;
ref_c += ref;
sr = *p++;
ref = sr & 0xffff;
smp = (sr>>16) & 0xffff;
#ifdef ENABLED_DUMP
ref_buf[i] = ref;
samp_buf[i] = smp;
#endif
2019-10-16 15:27:26 +02:00
i++;
samp_s -= smp;
ref_s -= ref;
sr = *p++;
ref = sr & 0xffff;
smp = (sr>>16) & 0xffff;
#ifdef ENABLED_DUMP
ref_buf[i] = ref;
samp_buf[i] = smp;
#endif
2019-10-16 15:27:26 +02:00
i++;
samp_c -= smp;
ref_c -= ref;
2016-09-18 03:11:18 +02:00
}
acc_samp_s += samp_s;
acc_samp_c += samp_c;
acc_ref_s += ref_s;
acc_ref_c += ref_c;
}
2016-09-18 03:11:18 +02:00
void
calculate_gamma(float gamma[2])
{
2019-08-10 10:24:09 +02:00
#if 1
2019-08-10 07:13:56 +02:00
// calculate reflection coeff. by samp divide by ref
float rs = acc_ref_s;
float rc = acc_ref_c;
float rr = rs * rs + rc * rc;
2017-09-15 15:13:17 +02:00
//rr = sqrtf(rr) * 1e8;
float ss = acc_samp_s;
float sc = acc_samp_c;
2017-01-26 16:11:24 +01:00
gamma[0] = (sc * rc + ss * rs) / rr;
gamma[1] = (ss * rc - sc * rs) / rr;
2019-08-10 10:24:09 +02:00
#elif 0
gamma[0] = acc_samp_s;
gamma[1] = acc_samp_c;
#else
gamma[0] = acc_ref_s;
gamma[1] = acc_ref_c;
#endif
2016-09-18 03:11:18 +02:00
}
2016-09-19 02:31:50 +02:00
void
fetch_amplitude(float gamma[2])
{
gamma[0] = acc_samp_s * 1e-9;
gamma[1] = acc_samp_c * 1e-9;
}
void
fetch_amplitude_ref(float gamma[2])
{
gamma[0] = acc_ref_s * 1e-9;
gamma[1] = acc_ref_c * 1e-9;
}
void
reset_dsp_accumerator(void)
{
acc_ref_s = 0;
acc_ref_c = 0;
acc_samp_s = 0;
acc_samp_c = 0;
}