From 3a24bdf1da4906c6adee1f2de98e1d7502bc05bb Mon Sep 17 00:00:00 2001 From: f4exb Date: Tue, 5 Mar 2019 01:01:38 +0100 Subject: [PATCH] libfreedv: use in FreeDV demod --- libfreedv/CMakeLists.txt | 24 + libfreedv/_kiss_fft_guts.h | 159 ++ libfreedv/codec2_fft.cpp | 163 ++ libfreedv/codec2_fft.h | 27 +- libfreedv/cohpsk.cpp | 1394 ++++++++++++ libfreedv/cohpsk_defs.h | 9 + libfreedv/cohpsk_internal.h | 131 ++ libfreedv/fdmdv.cpp | 1995 +++++++++++++++++ libfreedv/freedv_data_channel.cpp | 320 +++ libfreedv/freedv_filter.cpp | 286 +++ libfreedv/freedv_filter_coef.h | 166 ++ libfreedv/freedv_vhf_framing.cpp | 867 +++++++ libfreedv/fsk.cpp | 1251 +++++++++++ libfreedv/hanning.h | 649 ++++++ libfreedv/kiss_fft.cpp | 413 ++++ libfreedv/kiss_fft.h | 5 +- libfreedv/kiss_fftr.h | 2 +- libfreedv/linreg.cpp | 110 + libfreedv/linreg.h | 40 + libfreedv/machdep.h | 57 + libfreedv/modem_probe.h | 47 +- libfreedv/os.h | 57 + libfreedv/pilot_coeff.h | 46 + libfreedv/pilots_coh.h | 11 + libfreedv/rn.h | 969 ++++++++ libfreedv/rn_coh.h | 609 +++++ libfreedv/rxdec_coeff.h | 40 + libfreedv/test_bits.h | 170 ++ libfreedv/test_bits_coh.h | 569 +++++ plugins/channelrx/demodfreedv/CMakeLists.txt | 4 +- plugins/channelrx/demodfreedv/freedvdemod.cpp | 36 +- plugins/channelrx/demodfreedv/freedvdemod.h | 7 +- 32 files changed, 10581 insertions(+), 52 deletions(-) create mode 100644 libfreedv/_kiss_fft_guts.h create mode 100644 libfreedv/codec2_fft.cpp create mode 100644 libfreedv/cohpsk.cpp create mode 100644 libfreedv/cohpsk_defs.h create mode 100644 libfreedv/cohpsk_internal.h create mode 100644 libfreedv/fdmdv.cpp create mode 100644 libfreedv/freedv_data_channel.cpp create mode 100644 libfreedv/freedv_filter.cpp create mode 100644 libfreedv/freedv_filter_coef.h create mode 100644 libfreedv/freedv_vhf_framing.cpp create mode 100644 libfreedv/fsk.cpp create mode 100644 libfreedv/hanning.h create mode 100644 libfreedv/kiss_fft.cpp create mode 100644 libfreedv/linreg.cpp create mode 100644 libfreedv/linreg.h create mode 100644 libfreedv/machdep.h create mode 100644 libfreedv/os.h create mode 100644 libfreedv/pilot_coeff.h create mode 100644 libfreedv/pilots_coh.h create mode 100644 libfreedv/rn.h create mode 100644 libfreedv/rn_coh.h create mode 100644 libfreedv/rxdec_coeff.h create mode 100644 libfreedv/test_bits.h create mode 100644 libfreedv/test_bits_coh.h diff --git a/libfreedv/CMakeLists.txt b/libfreedv/CMakeLists.txt index 86e76b5f7..94d558a20 100644 --- a/libfreedv/CMakeLists.txt +++ b/libfreedv/CMakeLists.txt @@ -1,7 +1,16 @@ project(freedv) set(freedv_SOURCES + codec2_fft.cpp + cohpsk.cpp + fdmdv.cpp freedv_api.cpp + freedv_data_channel.cpp + freedv_filter.cpp + freedv_vhf_framing.cpp + fsk.cpp + kiss_fft.cpp + linreg.cpp ) set(freedv_HEADERS @@ -9,24 +18,39 @@ set(freedv_HEADERS codec2_fdmdv.h codec2_fft.h codec2_ofdm.h + cohpsk_defs.h + cohpsk_internal.h defines.h fdmdv_internal.h fdv_arm_math.h fmfsk.h freedv_api_internal.h freedv_data_channel.h + freedv_filter_coef.h freedv_filter.h freedv_vhf_framing.h fsk.h gp_interleaver.h + hanning.h interldpc.h + _kiss_fft_guts.h kiss_fft.h kiss_fftr.h libfreedv.h + linreg.h + machdep.h modem_probe.h modem_stats.h mpdecode_core.h ofdm_internal.h + os.h + pilot_coeff.h + pilots_coh.h + rn_coh.h + rn.h + rxdec_coeff.h + test_bits_coh.h + test_bits.h ) include_directories( diff --git a/libfreedv/_kiss_fft_guts.h b/libfreedv/_kiss_fft_guts.h new file mode 100644 index 000000000..4a89ef6a6 --- /dev/null +++ b/libfreedv/_kiss_fft_guts.h @@ -0,0 +1,159 @@ +/* +Copyright (c) 2003-2010, Mark Borgerding + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/* kiss_fft.h + defines kiss_fft_scalar as either short or a float type + and defines + typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */ +#include "kiss_fft.h" +#include + +#define MAXFACTORS 32 +/* e.g. an fft of length 128 has 4 factors + as far as kissfft is concerned + 4*4*4*2 + */ + +namespace FreeDV +{ + +struct kiss_fft_state{ + int nfft; + int inverse; + int factors[2*MAXFACTORS]; + kiss_fft_cpx twiddles[1]; +}; + +/* + Explanation of macros dealing with complex math: + + C_MUL(m,a,b) : m = a*b + C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise + C_SUB( res, a,b) : res = a - b + C_SUBFROM( res , a) : res -= a + C_ADDTO( res , a) : res += a + * */ +#ifdef FIXED_POINT +#if (FIXED_POINT==32) +# define FRACBITS 31 +# define SAMPPROD int64_t +#define SAMP_MAX 2147483647 +#else +# define FRACBITS 15 +# define SAMPPROD int32_t +#define SAMP_MAX 32767 +#endif + +#define SAMP_MIN -SAMP_MAX + +#if defined(CHECK_OVERFLOW) +# define CHECK_OVERFLOW_OP(a,op,b) \ + if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \ + fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); } +#endif + + +# define smul(a,b) ( (SAMPPROD)(a)*(b) ) +# define sround( x ) (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS ) + +# define S_MUL(a,b) sround( smul(a,b) ) + +# define C_MUL(m,a,b) \ + do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \ + (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0) + +# define DIVSCALAR(x,k) \ + (x) = sround( smul( x, SAMP_MAX/k ) ) + +# define C_FIXDIV(c,div) \ + do { DIVSCALAR( (c).r , div); \ + DIVSCALAR( (c).i , div); }while (0) + +# define C_MULBYSCALAR( c, s ) \ + do{ (c).r = sround( smul( (c).r , s ) ) ;\ + (c).i = sround( smul( (c).i , s ) ) ; }while(0) + +#else /* not FIXED_POINT*/ + +# define S_MUL(a,b) ( (a)*(b) ) +#define C_MUL(m,a,b) \ + do{ (m).r = (a).r*(b).r - (a).i*(b).i;\ + (m).i = (a).r*(b).i + (a).i*(b).r; }while(0) +# define C_FIXDIV(c,div) /* NOOP */ +# define C_MULBYSCALAR( c, s ) \ + do{ (c).r *= (s);\ + (c).i *= (s); }while(0) +#endif + +#ifndef CHECK_OVERFLOW_OP +# define CHECK_OVERFLOW_OP(a,op,b) /* noop */ +#endif + +#define C_ADD( res, a,b)\ + do { \ + CHECK_OVERFLOW_OP((a).r,+,(b).r)\ + CHECK_OVERFLOW_OP((a).i,+,(b).i)\ + (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \ + }while(0) +#define C_SUB( res, a,b)\ + do { \ + CHECK_OVERFLOW_OP((a).r,-,(b).r)\ + CHECK_OVERFLOW_OP((a).i,-,(b).i)\ + (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \ + }while(0) +#define C_ADDTO( res , a)\ + do { \ + CHECK_OVERFLOW_OP((res).r,+,(a).r)\ + CHECK_OVERFLOW_OP((res).i,+,(a).i)\ + (res).r += (a).r; (res).i += (a).i;\ + }while(0) + +#define C_SUBFROM( res , a)\ + do {\ + CHECK_OVERFLOW_OP((res).r,-,(a).r)\ + CHECK_OVERFLOW_OP((res).i,-,(a).i)\ + (res).r -= (a).r; (res).i -= (a).i; \ + }while(0) + + +#ifdef FIXED_POINT +# define KISS_FFT_COS(phase) floorf(.5+SAMP_MAX * cosf (phase)) +# define KISS_FFT_SIN(phase) floorf(.5+SAMP_MAX * sinf (phase)) +# define HALF_OF(x) ((x)>>1) +#elif defined(USE_SIMD) +# define KISS_FFT_COS(phase) _mm_set1_ps( cosf(phase) ) +# define KISS_FFT_SIN(phase) _mm_set1_ps( sinf(phase) ) +# define HALF_OF(x) ((x)*_mm_set1_ps(.5)) +#else +# define KISS_FFT_COS(phase) (kiss_fft_scalar) cosf(phase) +# define KISS_FFT_SIN(phase) (kiss_fft_scalar) sinf(phase) +# define HALF_OF(x) ((x)*.5) +#endif + +#define kf_cexp(x,phase) \ + do{ \ + (x)->r = KISS_FFT_COS(phase);\ + (x)->i = KISS_FFT_SIN(phase);\ + }while(0) + + +/* a debugging function */ +#define pcpx(c)\ + fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) ) + + +#define KISS_FFT_TMP_ALLOC(nbytes) KISS_FFT_MALLOC(nbytes) +#define KISS_FFT_TMP_FREE(ptr) KISS_FFT_FREE(ptr) + +} // FreeDV diff --git a/libfreedv/codec2_fft.cpp b/libfreedv/codec2_fft.cpp new file mode 100644 index 000000000..a3734694d --- /dev/null +++ b/libfreedv/codec2_fft.cpp @@ -0,0 +1,163 @@ +/* + * codec2_fft.c + * + * Created on: 24.09.2016 + * Author: danilo + */ + +#include "codec2_fft.h" + +#ifdef USE_KISS_FFT +#include "_kiss_fft_guts.h" +#endif + +namespace FreeDV +{ + +#ifdef USE_KISS_FFT +#else +#if 0 +// caching constants in RAM did not seem to have an effect on performance +// TODO: Decide what to with this code +#define FFT_INIT_CACHE_SIZE 4 +const arm_cfft_instance_f32* fft_init_cache[FFT_INIT_CACHE_SIZE]; + +static const arm_cfft_instance_f32* arm_fft_instance2ram(const arm_cfft_instance_f32* in) +{ + + arm_cfft_instance_f32* out = malloc(sizeof(arm_cfft_instance_f32)); + + if (out) { + memcpy(out,in,sizeof(arm_cfft_instance_f32)); + out->pBitRevTable = malloc(out->bitRevLength * sizeof(uint16_t)); + out->pTwiddle = malloc(out->fftLen * sizeof(float32_t)); + memcpy((void*)out->pBitRevTable,in->pBitRevTable,out->bitRevLength * sizeof(uint16_t)); + memcpy((void*)out->pTwiddle,in->pTwiddle,out->fftLen * sizeof(float32_t)); + } + return out; +} + + +static const arm_cfft_instance_f32* arm_fft_cache_get(const arm_cfft_instance_f32* romfft) +{ + const arm_cfft_instance_f32* retval = NULL; + static int used = 0; + for (int i = 0; fft_init_cache[i] != NULL && i < used; i++) + { + if (romfft->fftLen == fft_init_cache[i]->fftLen) + { + retval = fft_init_cache[i]; + break; + } + } + if (retval == NULL && used < FFT_INIT_CACHE_SIZE) + { + retval = arm_fft_instance2ram(romfft); + fft_init_cache[used++] = retval; + } + if (retval == NULL) + { + retval = romfft; + } + return retval; +} +#endif +#endif + +void codec2_fft_free(codec2_fft_cfg cfg) +{ +#ifdef USE_KISS_FFT + KISS_FFT_FREE(cfg); +#else + FREE(cfg); +#endif +} + +codec2_fft_cfg codec2_fft_alloc(int nfft, int inverse_fft, void* mem, std::size_t* lenmem) +{ + codec2_fft_cfg retval; +#ifdef USE_KISS_FFT + retval = kiss_fft_alloc(nfft, inverse_fft, mem, lenmem); +#else + retval = MALLOC(sizeof(codec2_fft_struct)); + retval->inverse = inverse_fft; + switch(nfft) + { + case 128: + retval->instance = &arm_cfft_sR_f32_len128; + break; + case 256: + retval->instance = &arm_cfft_sR_f32_len256; + break; + case 512: + retval->instance = &arm_cfft_sR_f32_len512; + break; +// case 1024: +// retval->instance = &arm_cfft_sR_f32_len1024; +// break; + default: + abort(); + } + // retval->instance = arm_fft_cache_get(retval->instance); +#endif + return retval; +} + +codec2_fftr_cfg codec2_fftr_alloc(int nfft, int inverse_fft, void* mem, std::size_t* lenmem) +{ + codec2_fftr_cfg retval; +#ifdef USE_KISS_FFT + retval = kiss_fftr_alloc(nfft, inverse_fft, mem, lenmem); +#else + retval = MALLOC(sizeof(codec2_fftr_struct)); + retval->inverse = inverse_fft; + retval->instance = MALLOC(sizeof(arm_rfft_fast_instance_f32)); + arm_rfft_fast_init_f32(retval->instance,nfft); + // memcpy(&retval->instance->Sint,arm_fft_cache_get(&retval->instance->Sint),sizeof(arm_cfft_instance_f32)); +#endif + return retval; +} +void codec2_fftr_free(codec2_fftr_cfg cfg) +{ +#ifdef USE_KISS_FFT + KISS_FFT_FREE(cfg); +#else + FREE(cfg->instance); + FREE(cfg); +#endif +} + +// there is a little overhead for inplace kiss_fft but this is +// on the powerful platforms like the Raspberry or even x86 PC based ones +// not noticeable +// the reduced usage of RAM and increased performance on STM32 platforms +// should be worth it. +void codec2_fft_inplace(codec2_fft_cfg cfg, codec2_fft_cpx* inout) +{ + +#ifdef USE_KISS_FFT + kiss_fft_cpx in[512]; + // decide whether to use the local stack based buffer for in + // or to allow kiss_fft to allocate RAM + // second part is just to play safe since first method + // is much faster and uses less RAM + if (cfg->nfft <= 512) + { + memcpy(in,inout,cfg->nfft*sizeof(kiss_fft_cpx)); + kiss_fft(cfg, in, (kiss_fft_cpx*)inout); + } + else + { + kiss_fft(cfg, (kiss_fft_cpx*)inout, (kiss_fft_cpx*)inout); + } +#else + arm_cfft_f32(cfg->instance,(float*)inout,cfg->inverse,1); + if (cfg->inverse) + { + arm_scale_f32((float*)inout,cfg->instance->fftLen,(float*)inout,cfg->instance->fftLen*2); + } + +#endif +} + +} // FreeDV diff --git a/libfreedv/codec2_fft.h b/libfreedv/codec2_fft.h index 1e269e1c9..8651c24fb 100644 --- a/libfreedv/codec2_fft.h +++ b/libfreedv/codec2_fft.h @@ -14,8 +14,9 @@ #include #include -namespace FreeDV -{ +#include "codec2/comp.h" +#include "defines.h" +#include "kiss_fftr.h" #ifdef FDV_ARM_MATH #include "fdv_arm_math.h" @@ -23,15 +24,16 @@ namespace FreeDV #define USE_KISS_FFT #endif -#include "defines.h" -#include "codec2/comp.h" - - -typedef COMP codec2_fft_cpx; -#include "kiss_fftr.h" - #ifdef USE_KISS_FFT #include "kiss_fft.h" +#endif + +namespace FreeDV +{ + +typedef COMP codec2_fft_cpx; + +#ifdef USE_KISS_FFT typedef kiss_fftr_cfg codec2_fftr_cfg; typedef kiss_fft_cfg codec2_fft_cfg; typedef kiss_fft_scalar codec2_fft_scalar; @@ -51,11 +53,8 @@ typedef COMP codec2_fft_cpx; typedef codec2_fft_struct* codec2_fft_cfg; #endif - - static inline void codec2_fftr(codec2_fftr_cfg cfg, codec2_fft_scalar* in, codec2_fft_cpx* out) { - #ifdef USE_KISS_FFT kiss_fftr(cfg, in, (kiss_fft_cpx*)out); #else @@ -75,8 +74,8 @@ static inline void codec2_fftri(codec2_fftr_cfg cfg, codec2_fft_cpx* in, codec2_ } -codec2_fft_cfg codec2_fft_alloc(int nfft, int inverse_fft, void* mem, size_t* lenmem); -codec2_fftr_cfg codec2_fftr_alloc(int nfft, int inverse_fft, void* mem, size_t* lenmem); +codec2_fft_cfg codec2_fft_alloc(int nfft, int inverse_fft, void* mem, std::size_t* lenmem); +codec2_fftr_cfg codec2_fftr_alloc(int nfft, int inverse_fft, void* mem, std::size_t* lenmem); void codec2_fft_free(codec2_fft_cfg cfg); void codec2_fftr_free(codec2_fftr_cfg cfg); diff --git a/libfreedv/cohpsk.cpp b/libfreedv/cohpsk.cpp new file mode 100644 index 000000000..1db247335 --- /dev/null +++ b/libfreedv/cohpsk.cpp @@ -0,0 +1,1394 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: cohpsk.c + AUTHOR......: David Rowe + DATE CREATED: March 2015 + + Functions that implement a coherent PSK FDM modem. + +\*---------------------------------------------------------------------------*/ + +/* + Copyright (C) 2015 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program 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 Lesser General Public License + along with this program; if not, see . +*/ + +/*---------------------------------------------------------------------------*\ + + INCLUDES + +\*---------------------------------------------------------------------------*/ + +#include +#include +#include +#include +#include + +#include "codec2_cohpsk.h" +#include "cohpsk_defs.h" +#include "cohpsk_internal.h" +#include "fdmdv_internal.h" +#include "pilots_coh.h" +#include "codec2/comp_prim.h" +#include "kiss_fft.h" +#include "linreg.h" +#include "rn_coh.h" +#include "test_bits_coh.h" + +namespace FreeDV +{ + +static COMP qpsk_mod[] = { + { 1.0, 0.0}, + { 0.0, 1.0}, + { 0.0,-1.0}, + {-1.0, 0.0} +}; + +static int sampling_points[] = {0, 1, 6, 7}; + +void corr_with_pilots_comp(float *corr_out, float *mag_out, struct COHPSK *coh, int t, COMP f_fine); +void update_ct_symb_buf(COMP ct_symb_buf[][COHPSK_NC*ND], COMP ch_symb[][COHPSK_NC*ND]); + +/*---------------------------------------------------------------------------*\ + + FUNCTIONS + +\*---------------------------------------------------------------------------*/ + + +/*--------------------------------------------------------------------------* \ + + FUNCTION....: cohpsk_create + AUTHOR......: David Rowe + DATE CREATED: Marcg 2015 + + Create and initialise an instance of the modem. Returns a pointer + to the modem states or NULL on failure. One set of states is + sufficient for a full duplex modem. + +\*---------------------------------------------------------------------------*/ + +struct COHPSK *cohpsk_create(void) +{ + struct COHPSK *coh; + struct FDMDV *fdmdv; + int r,c,p,i; + float freq_hz; + + assert(COHPSK_NC == PILOTS_NC); + assert(COHPSK_NOM_SAMPLES_PER_FRAME == (COHPSK_M*NSYMROWPILOT)); + assert(COHPSK_MAX_SAMPLES_PER_FRAME == (COHPSK_M*NSYMROWPILOT+COHPSK_M/P)); + assert(COHPSK_ND == ND); + assert(COHPSK_NSYM == NSYM); /* as we want to use the tx sym mem on fdmdv */ + assert(COHPSK_NT == NT); + + coh = (struct COHPSK*) malloc(sizeof(struct COHPSK)); + if (coh == NULL) + return NULL; + + /* set up buffer of tx pilot symbols for coh demod on rx */ + + for(r=0; r<2*NPILOTSFRAME; ) { + for(p=0; ppilot2[r][c] = pilots_coh[p][c]; + } + } + } + + /* Clear symbol buffer memory */ + + for (r=0; rct_symb_buf[r][c].real = 0.0; + coh->ct_symb_buf[r][c].imag = 0.0; + } + } + + coh->ff_phase.real = 1.0; coh->ff_phase.imag = 0.0; + coh->sync = 0; + coh->frame = 0; + coh->ratio = 0.0; + coh->nin = COHPSK_M; + + /* clear sync window buffer */ + + for (i=0; ich_fdm_frame_buf[i].real = 0.0; + coh->ch_fdm_frame_buf[i].imag = 0.0; + } + + /* set up fdmdv states so we can use those modem functions */ + + fdmdv = fdmdv_create(COHPSK_NC*ND - 1); + fdmdv->fsep = COHPSK_RS*(1.0 + COHPSK_EXCESS_BW); + for(c=0; cphase_tx[c].real = 1.0; + fdmdv->phase_tx[c].imag = 0.0; + + /* note non-linear carrier spacing to help PAPR, works v well in conjunction with CLIP */ + + freq_hz = fdmdv->fsep*( -(COHPSK_NC*ND)/2 - 0.5 + pow(c + 1.0, 0.98) ); + + fdmdv->freq[c].real = cosf(2.0*M_PI*freq_hz/COHPSK_FS); + fdmdv->freq[c].imag = sinf(2.0*M_PI*freq_hz/COHPSK_FS); + fdmdv->freq_pol[c] = 2.0*M_PI*freq_hz/COHPSK_FS; + + //printf("c: %d %f %f\n",c,freq_hz,fdmdv->freq_pol[c]); + for(i=0; irx_filter_memory[c][i].real = 0.0; + coh->rx_filter_memory[c][i].imag = 0.0; + } + + /* optional per-carrier amplitude weighting for testing */ + + coh->carrier_ampl[c] = 1.0; + } + fdmdv->fbb_rect.real = cosf(2.0*PI*FDMDV_FCENTRE/COHPSK_FS); + fdmdv->fbb_rect.imag = sinf(2.0*PI*FDMDV_FCENTRE/COHPSK_FS); + fdmdv->fbb_pol = 2.0*PI*FDMDV_FCENTRE/COHPSK_FS; + + coh->fdmdv = fdmdv; + + coh->sig_rms = coh->noise_rms = 0.0; + + for(c=0; crx_symb[r][c].real = 0.0; + coh->rx_symb[r][c].imag = 0.0; + } + } + + coh->verbose = 0; + + /* disable optional logging by default */ + + coh->rx_baseband_log = NULL; + coh->rx_baseband_log_col_index = 0; + coh->rx_filt_log = NULL; + coh->rx_filt_log_col_index = 0; + coh->ch_symb_log = NULL; + coh->ch_symb_log_r = 0; + coh->rx_timing_log = NULL; + coh->rx_timing_log_index = 0; + + /* test frames */ + + coh->ptest_bits_coh_tx = coh->ptest_bits_coh_rx[0] = coh->ptest_bits_coh_rx[1] = (int*)test_bits_coh; + coh->ptest_bits_coh_end = (int*)test_bits_coh + sizeof(test_bits_coh)/sizeof(int); + + /* Disable 'reduce' frequency estimation mode */ + coh->freq_est_mode_reduced = 0; + + return coh; +} + + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: cohpsk_destroy + AUTHOR......: David Rowe + DATE CREATED: March 2015 + + Destroy an instance of the modem. + +\*---------------------------------------------------------------------------*/ + +void cohpsk_destroy(struct COHPSK *coh) +{ + assert(coh != NULL); + fdmdv_destroy(coh->fdmdv); + free(coh); +} + + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: bits_to_qpsk_symbols() + AUTHOR......: David Rowe + DATE CREATED: March 2015 + + Rate Rs modulator. Maps bits to parallel DQPSK symbols and inserts pilot symbols. + +\*---------------------------------------------------------------------------*/ + +void bits_to_qpsk_symbols(COMP tx_symb[][COHPSK_NC*ND], int tx_bits[], int nbits) +{ + int i, r, c, p_r, data_r, d, diversity; + short bits; + + /* check allowed number of bits supplied matches number of QPSK + symbols in the frame */ + + assert( (NSYMROW*COHPSK_NC*2 == nbits) || (NSYMROW*COHPSK_NC*2*ND == nbits)); + + /* if we input twice as many bits we don't do diversity */ + + if (NSYMROW*COHPSK_NC*2 == nbits) { + diversity = 1; /* diversity mode */ + } + else { + diversity = 2; /* twice as many bits, non diversity mode */ + } + + /* + Insert two rows of Nc pilots at beginning of data frame. + + Organise QPSK symbols into a NSYMBROWS rows by PILOTS_NC*ND cols matrix, + each column is a carrier, time flows down the cols...... + + Note: the "& 0x1" prevents and non binary tx_bits[] screwing up + our lives. Call me defensive. + + sqrtf(ND) term ensures the same energy/symbol for different + diversity factors. + */ + + r = 0; + for(p_r=0; p_r<2; p_r++) { + for(c=0; cpilot2[p][pc], ct_symb_buf[sampling_points[p]][c]); + } + + linreg(&m, &b, x, y, NPILOTSFRAME+2); + for(r=0; rphi_[r][c] = atan2(yfit.imag, yfit.real); + } + + /* amplitude estimation */ + + mag = 0.0; + for(p=0; pamp_[r][c] = amp_; + } + } + + /* now correct phase of data symbols */ + + for(c=0; cphi_[r][c]); phi_rect.imag = -sinf(coh->phi_[r][c]); + coh->rx_symb[r][c] = cmult(ct_symb_buf[NPILOTSFRAME + r][c], phi_rect); + i = c*NSYMROW + r; + rx_symb_linear[i] = coh->rx_symb[r][c]; + } + } + + /* and finally optional diversity combination, note output is soft decn a "1" is < 0 */ + + for(c=0; crx_symb[r][c]; + for (d=1; drx_symb[r][c + COHPSK_NC*d]); + } + rot = cmult(div_symb, pi_on_4); + i = c*NSYMROW + r; + rx_bits[2*i+1] = rot.real; + rx_bits[2*i] = rot.imag; + + /* demodulate bits from upper and lower carriers separately for test purposes */ + + assert(ND == 2); + + i = c*NSYMROW + r; + rot = cmult(coh->rx_symb[r][c], pi_on_4); + coh->rx_bits_lower[2*i+1] = rot.real; + coh->rx_bits_lower[2*i] = rot.imag; + rot = cmult(coh->rx_symb[r][c + COHPSK_NC], pi_on_4); + coh->rx_bits_upper[2*i+1] = rot.real; + coh->rx_bits_upper[2*i] = rot.imag; + } + } + + + /* estimate RMS signal and noise */ + + mag = 0.0; + for(i=0; isig_rms = mag/(NSYMROW*COHPSK_NC*ND); + + sum_x = 0; + sum_xx = 0; + n = 0; + for (i=0; i coh->sig_rms) { + sum_x += s.imag; + sum_xx += s.imag*s.imag; + n++; + } + } + + noise_var = 0; + if (n > 1) { + noise_var = (n*sum_xx - sum_x*sum_x)/(n*(n-1)); + } + coh->noise_rms = sqrtf(noise_var); + +} + + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: tx_filter_and_upconvert_coh() + AUTHOR......: David Rowe + DATE CREATED: May 2015 + + Given NC symbols construct M samples (1 symbol) of NC filtered + and upconverted symbols. + + TODO: work out a way to merge with fdmdv version, e.g. run time define M/NSYM, + and run unittests on fdmdv and cohpsk modem afterwards. + +\*---------------------------------------------------------------------------*/ + +void tx_filter_and_upconvert_coh(COMP tx_fdm[], int Nc,const COMP tx_symbols[], + COMP tx_filter_memory[COHPSK_NC*ND][COHPSK_NSYM], + COMP phase_tx[], COMP freq[], + COMP *fbb_phase, COMP fbb_rect) +{ + int c; + int i,j,k; + COMP gain; + COMP tx_baseband; + COMP two = {2.0, 0.0}; + float mag; + + gain.real = sqrtf(2.0)/2.0; + gain.imag = 0.0; + + for(i=0; ireal /= mag; + fbb_phase->imag /= mag; + + /* shift memory, inserting zeros at end */ + + for(i=0; ict_symb_buf[t+sampling_points[p]][c]); + pc = c % COHPSK_NC; + acorr = cadd(acorr, fcmult(coh->pilot2[p][pc], f_corr)); + mag += cabsolute(f_corr); + } + corr += cabsolute(acorr); + } + + *corr_out = corr; + *mag_out = mag; +} + + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: frame_sync_fine_freq_est() + AUTHOR......: David Rowe + DATE CREATED: April 2015 + + Returns an estimate of frame sync (coarse timing) offset and fine + frequency offset, advances to next sync state if we have a reliable + match for frame sync. + +\*---------------------------------------------------------------------------*/ + +void frame_sync_fine_freq_est(struct COHPSK *coh, COMP ch_symb[][COHPSK_NC*ND], int sync, int *next_sync) +{ + int t; + float f_fine, mag, max_corr, max_mag, corr, delta_f_fine, f_fine_range ; + COMP f_fine_d_ph; + + if(coh->freq_est_mode_reduced){ + delta_f_fine = 1.3; + f_fine_range = 10; + }else{ + delta_f_fine = .25; + f_fine_range = 20; + } + + /* Represent f_fine scan as delta2-phase */ + const COMP f_fine_d2_ph = comp_exp_j(2*M_PI*delta_f_fine/COHPSK_RS); + + f_fine = -f_fine_range; + + update_ct_symb_buf(coh->ct_symb_buf, ch_symb); + /* sample pilots at start of this frame and start of next frame */ + + if (sync == 0) { + + /* Represent f_fine as complex delta-phase instead of frequency */ + f_fine_d_ph = comp_exp_j(2*M_PI*f_fine/COHPSK_RS); + + + /* sample correlation over 2D grid of time and fine freq points */ + max_corr = max_mag = 0; + for (f_fine=-f_fine_range; f_fine<=f_fine_range; f_fine+=delta_f_fine) { + for (t=0; t= max_corr) { + max_corr = corr; + max_mag = mag; + coh->ct = t; + coh->f_fine_est = f_fine; + } + } + /* Advance f_fine */ + f_fine_d_ph = cmult(f_fine_d_ph,f_fine_d2_ph); + } + + + coh->ff_rect.real = cosf(coh->f_fine_est*2.0*M_PI/COHPSK_RS); + coh->ff_rect.imag = -sinf(coh->f_fine_est*2.0*M_PI/COHPSK_RS); + if (coh->verbose) + fprintf(stderr, " [%d] fine freq f: %6.2f max_ratio: %f ct: %d\n", coh->frame, (double)coh->f_fine_est, (double)(max_corr/max_mag), coh->ct); + + if (max_corr/max_mag > 0.9) { + if (coh->verbose) + fprintf(stderr, " [%d] encouraging sync word!\n", coh->frame); + coh->sync_timer = 0; + *next_sync = 1; + } + else { + *next_sync = 0; + } + coh->ratio = max_corr/max_mag; + } +} + + +void update_ct_symb_buf(COMP ct_symb_buf[][COHPSK_NC*ND], COMP ch_symb[][COHPSK_NC*ND]) +{ + int r, c, i; + + /* update memory in symbol buffer */ + + for(r=0; rct, comp_exp_j(2*M_PI*coh->f_fine_est/COHPSK_RS)); + coh->ratio = fabsf(corr)/mag; + + // printf("%f\n", cabsolute(corr)/mag); + + if (fabsf(corr)/mag < 0.8) + coh->sync_timer++; + else + coh->sync_timer = 0; + + if (coh->sync_timer == 10) { + if (coh->verbose) + fprintf(stderr," [%d] lost sync ....\n", coh->frame); + next_sync = 0; + } + } + + sync = next_sync; + + return sync; +} + + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: cohpsk_mod() + AUTHOR......: David Rowe + DATE CREATED: 5/4/2015 + + COHPSK modulator, take a frame of COHPSK_BITS_PER_FRAME or + 2*COHPSK_BITS_PER_FRAME bits and generates a frame of + COHPSK_NOM_SAMPLES_PER_FRAME modulated symbols. + + if nbits == COHPSK_BITS_PER_FRAME, diveristy mode is used, if nbits + == 2*COHPSK_BITS_PER_FRAME diversity mode is not used. + + The output signal is complex to support single sided frequency + shifting, for example when testing frequency offsets in channel + simulation. + +\*---------------------------------------------------------------------------*/ + +void cohpsk_mod(struct COHPSK *coh, COMP tx_fdm[], int tx_bits[], int nbits) +{ + struct FDMDV *fdmdv = coh->fdmdv; + COMP tx_symb[NSYMROWPILOT][COHPSK_NC*ND]; + COMP tx_onesym[COHPSK_NC*ND]; + int r,c; + + bits_to_qpsk_symbols(tx_symb, tx_bits, nbits); + + for(r=0; rcarrier_ampl[c], tx_symb[r][c]); + tx_filter_and_upconvert_coh(&tx_fdm[r*COHPSK_M], COHPSK_NC*ND , tx_onesym, fdmdv->tx_filter_memory, + fdmdv->phase_tx, fdmdv->freq, &fdmdv->fbb_phase_tx, fdmdv->fbb_rect); + } +} + + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: cohpsk_clip() + AUTHOR......: David Rowe + DATE CREATED: May 2015 + + Hard clips a cohpsk modulator signal to improve PAPR, CLIP threshold + hard coded and will need to be changed if NC*ND does. + +\*---------------------------------------------------------------------------*/ + +void cohpsk_clip(COMP tx_fdm[], float clip_thresh, int n) +{ + COMP sam; + float mag; + int i; + + for(i=0; i clip_thresh) { + sam = fcmult(clip_thresh/mag, sam); + } + tx_fdm[i] = sam; + } + } + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: fdm_downconvert_coh + AUTHOR......: David Rowe + DATE CREATED: May 2015 + + Frequency shift each modem carrier down to NC baseband signals. + + TODO: try to combine with fdmdv version, carefully re-test fdmdv modem. + +\*---------------------------------------------------------------------------*/ + +void fdm_downconvert_coh(COMP rx_baseband[COHPSK_NC][COHPSK_M+COHPSK_M/P], int Nc, COMP rx_fdm[], COMP phase_rx[], COMP freq[], int nin) +{ + int i,c; + float mag; + + /* maximum number of input samples to demod */ + + assert(nin <= (COHPSK_M+COHPSK_M/P)); + + /* downconvert */ + + for (c=0; creal /= mag; + foff_phase_rect->imag /= mag; +} + + +void rate_Fs_rx_processing(struct COHPSK *coh, COMP ch_symb[][COHPSK_NC*ND], COMP ch_fdm_frame[], float *f_est, int nsymb, int nin, int freq_track) +{ + struct FDMDV *fdmdv = coh->fdmdv; + int r, c, i, ch_fdm_frame_index; + COMP rx_fdm_frame_bb[COHPSK_M+COHPSK_M/P]; + COMP rx_baseband[COHPSK_NC*ND][COHPSK_M+COHPSK_M/P]; + COMP rx_filt[COHPSK_NC*ND][P+1]; + float env[NT*P], rx_timing; + COMP rx_onesym[COHPSK_NC*ND]; + float beta, g; + COMP adiff, amod_strip, mod_strip; + + ch_fdm_frame_index = 0; + rx_timing = 0; + + for (r=0; rfbb_phase_rx, nin); + ch_fdm_frame_index += nin; + fdm_downconvert_coh(rx_baseband, COHPSK_NC*ND, rx_fdm_frame_bb, fdmdv->phase_rx, fdmdv->freq, nin); + rx_filter_coh(rx_filt, COHPSK_NC*ND, rx_baseband, coh->rx_filter_memory, nin); + rx_timing = rx_est_timing(rx_onesym, fdmdv->Nc, rx_filt, fdmdv->rx_filter_mem_timing, env, nin, COHPSK_M); + + for(c=0; cNc+1; c++) { + //printf("rx_onesym[%d] %f %f prev_rx_symbols[%d] %f %f\n", c, rx_onesym[c].real, rx_onesym[c].imag, + // fdmdv->prev_rx_symbols[c].real, fdmdv->prev_rx_symbols[c].imag); + adiff = cmult(rx_onesym[c], cconj(fdmdv->prev_rx_symbols[c])); + fdmdv->prev_rx_symbols[c] = rx_onesym[c]; + + /* 4th power strips QPSK modulation, by multiplying phase by 4 + Using the abs value of the real coord was found to help + non-linear issues when noise power was large. */ + + amod_strip = cmult(adiff, adiff); + amod_strip = cmult(amod_strip, amod_strip); + amod_strip.real = fabsf(amod_strip.real); + mod_strip = cadd(mod_strip, amod_strip); + } + //printf("modstrip: %f %f\n", mod_strip.real, mod_strip.imag); + + /* loop filter made up of 1st order IIR plus integrator. Integerator + was found to be reqd */ + + fdmdv->foff_filt = (1.0-beta)*fdmdv->foff_filt + beta*atan2(mod_strip.imag, mod_strip.real); + //printf("foff_filt: %f angle: %f\n", fdmdv->foff_filt, atan2(mod_strip.imag, mod_strip.real)); + *f_est += g*fdmdv->foff_filt; + } + + /* Optional logging used for testing against Octave version */ + + if (coh->rx_baseband_log) { + assert(nin <= (COHPSK_M+COHPSK_M/P)); + for(c=0; crx_baseband_log[c*coh->rx_baseband_log_col_sz + coh->rx_baseband_log_col_index + i] = rx_baseband[c][i]; + } + } + coh->rx_baseband_log_col_index += nin; + assert(coh->rx_baseband_log_col_index <= coh->rx_baseband_log_col_sz); + } + + if (coh->rx_filt_log) { + for(c=0; crx_filt_log[c*coh->rx_filt_log_col_sz + coh->rx_filt_log_col_index + i] = rx_filt[c][i]; + } + } + coh->rx_filt_log_col_index += nin/(COHPSK_M/P); + } + + if (coh->ch_symb_log) { + for(c=0; cch_symb_log[coh->ch_symb_log_r*COHPSK_NC*ND + c] = ch_symb[r][c]; + } + coh->ch_symb_log_r++; + } + + if (coh->rx_timing_log) { + coh->rx_timing_log[coh->rx_timing_log_index] = rx_timing; + coh->rx_timing_log_index++; + //printf("rx_timing_log_index: %d\n", coh->rx_timing_log_index); + } + + /* we only allow a timing shift on one symbol per frame */ + + if (nin != COHPSK_M) + nin = COHPSK_M; + } + + coh->rx_timing = rx_timing; +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: cohpsk_set_freq_est_mode() + AUTHOR......: Brady O'Brien + DATE CREATED: 12 Dec 2017 + + Enables or disables a 'simple' frequency estimation mode. Simple frequency + estimation uses substantially less CPU when cohpsk modem is not sunk than + default mode, but may take many frames to sync. + +\*---------------------------------------------------------------------------*/ +void cohpsk_set_freq_est_mode(struct COHPSK *coh, int use_simple_mode){ + if(use_simple_mode){ + coh->freq_est_mode_reduced = 1; + }else{ + coh->freq_est_mode_reduced = 0; + } +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: cohpsk_demod() + AUTHOR......: David Rowe + DATE CREATED: 5/4/2015 + + COHPSK demodulator, takes an array of (nominally) nin_frame = + COHPSK_NOM_SAMPLES_PER_FRAME modulated samples, returns an array of + COHPSK_BITS_PER_FRAME bits. + + The input signal is complex to support single sided frequency shifting + before the demod input (e.g. click to tune feature). + +\*---------------------------------------------------------------------------*/ + +void cohpsk_demod(struct COHPSK *coh, float rx_bits[], int *sync_good, COMP rx_fdm[], int *nin_frame) +{ + COMP ch_symb[NSW*NSYMROWPILOT][COHPSK_NC*ND]; + int i, j, sync, anext_sync, next_sync, nin, r, c, ns_done; + float max_ratio, f_est; + + assert(*nin_frame <= COHPSK_MAX_SAMPLES_PER_FRAME); + + next_sync = sync = coh->sync; + + for (i=0; ich_fdm_frame_buf[i] = coh->ch_fdm_frame_buf[i+*nin_frame]; + //printf("nin_frame: %d i: %d i+nin_frame: %d\n", *nin_frame, i, i+*nin_frame); + for (j=0; ich_fdm_frame_buf[i] = rx_fdm[j]; + //printf("i: %d j: %d rx_fdm[0]: %f %f\n", i,j, rx_fdm[0].real, rx_fdm[0].imag); + + /* if out of sync do Initial Freq offset estimation using NSW frames to flush out filter memories */ + + if (sync == 0) { + + + max_ratio = 0.0; + f_est = 0.0; + + coh->f_est -= 20; + if(coh->f_est < FDMDV_FCENTRE - 60.0){ + coh->f_est = FDMDV_FCENTRE + 60; + } + + if(!coh->freq_est_mode_reduced){ + coh->f_est = FDMDV_FCENTRE-40.0; + } + + ns_done = 0; + //for (coh->f_est = FDMDV_FCENTRE-40.0; coh->f_est <= FDMDV_FCENTRE+40.0; coh->f_est += 40.0) + while(!ns_done){ + + /* Use slower freq estimator; only do one chunk of freq range */ + if(coh->freq_est_mode_reduced){ + coh->f_est -= 20; + if(coh->f_est < FDMDV_FCENTRE - 60.0){ + coh->f_est = FDMDV_FCENTRE + 60; + } + ns_done = 1; + }else{ + /* we can test +/- 20Hz, so we break this up into 3 tests to cover +/- 60Hz */ + if(coh->f_est > FDMDV_FCENTRE+40.0) ns_done = 1; + } + + if (coh->verbose) + fprintf(stderr, " [%d] acohpsk.f_est: %f +/- 20\n", coh->frame, (double)coh->f_est); + + /* we are out of sync so reset f_est and process two frames to clean out memories */ + + rate_Fs_rx_processing(coh, ch_symb, coh->ch_fdm_frame_buf, &coh->f_est, NSW*NSYMROWPILOT, COHPSK_M, 0); + for (i=0; ict_symb_buf, &ch_symb[i*NSYMROWPILOT]); + } + frame_sync_fine_freq_est(coh, &ch_symb[(NSW-1)*NSYMROWPILOT], sync, &anext_sync); + + if (anext_sync == 1) { + //printf(" [%d] acohpsk.ratio: %f\n", f, coh->ratio); + if (coh->ratio > max_ratio) { + max_ratio = coh->ratio; + f_est = coh->f_est - coh->f_fine_est; + next_sync = anext_sync; + } + } + + if(!coh->freq_est_mode_reduced){ + coh->f_est += 40; + } + } + + if (next_sync == 1) { + + /* we've found a sync candidate! + re-process last NSW frames with adjusted f_est then check again */ + + coh->f_est = f_est; + + if (coh->verbose) + fprintf(stderr, " [%d] trying sync and f_est: %f\n", coh->frame, (double)coh->f_est); + + rate_Fs_rx_processing(coh, ch_symb, coh->ch_fdm_frame_buf, &coh->f_est, NSW*NSYMROWPILOT, COHPSK_M, 0); + for (i=0; ict_symb_buf, &ch_symb[i*NSYMROWPILOT]); + } + /* + for(i=0; ict_symb_buf[i][0].real, coh->ct_symb_buf[i][0].imag); + } + */ + frame_sync_fine_freq_est(coh, &ch_symb[(NSW-1)*NSYMROWPILOT], sync, &next_sync); + + if (fabs(coh->f_fine_est) > 2.0) { + if (coh->verbose) + fprintf(stderr, " [%d] Hmm %f is a bit big :(\n", coh->frame, (double)coh->f_fine_est); + next_sync = 0; + } + } + + if (next_sync == 1) { + /* OK we are in sync! + demodulate first frame (demod completed below) */ + + if (coh->verbose) + fprintf(stderr, " [%d] in sync! f_est: %f ratio: %f \n", coh->frame, (double)coh->f_est, (double)coh->ratio); + for(r=0; rct_symb_ff_buf[r][c] = coh->ct_symb_buf[coh->ct+r][c]; + } + } + + /* If in sync just do sample rate processing on latest frame */ + + if (sync == 1) { + rate_Fs_rx_processing(coh, ch_symb, rx_fdm, &coh->f_est, NSYMROWPILOT, coh->nin, 1); + frame_sync_fine_freq_est(coh, ch_symb, sync, &next_sync); + + for(r=0; r<2; r++) + for(c=0; cct_symb_ff_buf[r][c] = coh->ct_symb_ff_buf[r+NSYMROWPILOT][c]; + for(; rct_symb_ff_buf[r][c] = coh->ct_symb_buf[coh->ct+r][c]; + } + + /* if we are in sync complete demodulation with symbol rate processing */ + + *sync_good = 0; + if ((next_sync == 1) || (sync == 1)) { + qpsk_symbols_to_bits(coh, rx_bits, coh->ct_symb_ff_buf); + *sync_good = 1; + } + + sync = sync_state_machine(coh, sync, next_sync); + coh->sync = sync; + + /* work out how many samples we need for the next call to account + for differences in tx and rx sample clocks */ + + nin = COHPSK_M; + if (sync == 1) { + if (coh->rx_timing > COHPSK_M/P) + nin = COHPSK_M + COHPSK_M/P; + if (coh->rx_timing < -COHPSK_M/P) + nin = COHPSK_M - COHPSK_M/P; + } + coh->nin = nin; + *nin_frame = (NSYMROWPILOT-1)*COHPSK_M + nin; + //if (coh->verbose) + // fprintf(stderr, "%f %d %d\n", coh->rx_timing, nin, *nin_frame); +} + + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: cohpsk_fs_offset() + AUTHOR......: David Rowe + DATE CREATED: May 2015 + + Simulates small Fs offset between mod and demod. + +\*---------------------------------------------------------------------------*/ + +int cohpsk_fs_offset(COMP out[], COMP in[], int n, float sample_rate_ppm) +{ + double tin, f; + int tout, t1, t2; + + tin = 0.0; tout = 0; + while (tin < n) { + t1 = floor(tin); + t2 = ceil(tin); + f = tin - t1; + out[tout].real = (1.0-f)*in[t1].real + f*in[t2].real; + out[tout].imag = (1.0-f)*in[t1].imag + f*in[t2].imag; + tout += 1; + tin += 1.0 + sample_rate_ppm/1E6; + //printf("tin: %f tout: %d f: %f\n", tin, tout, f); + } + + return tout; +} + + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: cohpsk_get_demod_stats() + AUTHOR......: David Rowe + DATE CREATED: 14 June 2015 + + Fills stats structure with a bunch of demod information. + +\*---------------------------------------------------------------------------*/ + +void cohpsk_get_demod_stats(struct COHPSK *coh, struct MODEM_STATS *stats) +{ + int c,r; + COMP pi_4; + float new_snr_est; + + pi_4.real = cosf(M_PI/4.0); + pi_4.imag = sinf(M_PI/4.0); + + stats->Nc = COHPSK_NC*ND; + assert(stats->Nc <= MODEM_STATS_NC_MAX); + new_snr_est = 20*log10((coh->sig_rms+1E-6)/(coh->noise_rms+1E-6)) - 10*log10(3000.0/700.0); + stats->snr_est = 0.9*stats->snr_est + 0.1*new_snr_est; + + //fprintf(stderr, "sig_rms: %f noise_rms: %f snr_est: %f\n", coh->sig_rms, coh->noise_rms, stats->snr_est); + stats->sync = coh->sync; + stats->foff = coh->f_est - FDMDV_FCENTRE; + stats->rx_timing = coh->rx_timing; + stats->clock_offset = 0.0; /* TODO - implement clock offset estimation */ + + assert(NSYMROW <= MODEM_STATS_NR_MAX); + stats->nr = NSYMROW; + for(c=0; crx_symbols[r][c] = cmult(coh->rx_symb[r][c], pi_4); + } + } +} + + +void cohpsk_set_verbose(struct COHPSK *coh, int verbose) +{ + assert(coh != NULL); + coh->verbose = verbose; +} + + +void cohpsk_set_frame(struct COHPSK *coh, int frame) +{ + assert(coh != NULL); + coh->frame = frame; +} + + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: cohpsk_get_test_bits() + AUTHOR......: David Rowe + DATE CREATED: June 2015 + + Returns a frame of known test bits. + +\*---------------------------------------------------------------------------*/ + +void cohpsk_get_test_bits(struct COHPSK *coh, int rx_bits[]) +{ + memcpy(rx_bits, coh->ptest_bits_coh_tx, sizeof(int)*COHPSK_BITS_PER_FRAME); + coh->ptest_bits_coh_tx += COHPSK_BITS_PER_FRAME; + if (coh->ptest_bits_coh_tx >=coh->ptest_bits_coh_end) { + coh->ptest_bits_coh_tx = (int*)test_bits_coh; + } +} + + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: cohpsk_put_test_bits() + AUTHOR......: David Rowe + DATE CREATED: June 2015 + + Accepts bits from demod and attempts to sync with the known + test_bits sequence. When synced measures bit errors. + + Has states to track two separate received test sequences based on + channel 0 or 1. + +\*---------------------------------------------------------------------------*/ + +void cohpsk_put_test_bits(struct COHPSK *coh, int *state, short error_pattern[], + int *bit_errors, char rx_bits_char[], int channel) +{ + int i, next_state, anerror; + int rx_bits[COHPSK_BITS_PER_FRAME]; + + assert((channel == 0) || (channel == 1)); + int *ptest_bits_coh_rx = coh->ptest_bits_coh_rx[channel]; + + for(i=0; i 1)) { + fprintf(stderr, "i: %d rx_bits: %d ptest_bits_coh_rx: %d\n", i, rx_bits[i], ptest_bits_coh_rx[i]); + } + *bit_errors += anerror; + error_pattern[i] = anerror; + } + + /* state logic */ + + next_state = *state; + + if (*state == 0) { + if (*bit_errors < 4) { + next_state = 1; + ptest_bits_coh_rx += COHPSK_BITS_PER_FRAME; + if (ptest_bits_coh_rx >= coh->ptest_bits_coh_end) { + ptest_bits_coh_rx = (int*)test_bits_coh; + } + } + } + + /* if 5 frames with large BER reset test frame sync */ + + if (*state > 0) { + if (*bit_errors > 8) { + if (*state == 6) + next_state = 0; + else + next_state = *state+1; + } + else + next_state = 1; + } + + if (*state > 0) { + ptest_bits_coh_rx += COHPSK_BITS_PER_FRAME; + if (ptest_bits_coh_rx >= coh->ptest_bits_coh_end) { + ptest_bits_coh_rx = (int*)test_bits_coh; + } + } + + //fprintf(stderr, "state: %d next_state: %d bit_errors: %d\n", *state, next_state, *bit_errors); + + *state = next_state; + coh->ptest_bits_coh_rx[channel] = ptest_bits_coh_rx; +} + + +int cohpsk_error_pattern_size(void) { + return COHPSK_BITS_PER_FRAME; +} + + +float *cohpsk_get_rx_bits_lower(struct COHPSK *coh) { + return coh->rx_bits_lower; +} + +float *cohpsk_get_rx_bits_upper(struct COHPSK *coh) { + return coh->rx_bits_upper; +} + +void cohpsk_set_carrier_ampl(struct COHPSK *coh, int c, float ampl) { + assert(c < COHPSK_NC*ND); + coh->carrier_ampl[c] = ampl; + fprintf(stderr, "cohpsk_set_carrier_ampl: %d %f\n", c, (double)ampl); +} + +} // FreeDV diff --git a/libfreedv/cohpsk_defs.h b/libfreedv/cohpsk_defs.h new file mode 100644 index 000000000..28b756e41 --- /dev/null +++ b/libfreedv/cohpsk_defs.h @@ -0,0 +1,9 @@ +/* Generated by write_pilot_file() Octave function */ + +#define NSYMROW 4 /* number of data symbols per carrier (number of rows) */ +#define NS 4 /* number of data symbols between pilots */ +#define NPILOTSFRAME 2 /* number of pilot symbols per carrier */ +#define PILOTS_NC 7 /* number of carriers */ + +#define NSYMROWPILOT 6 /* number of rows after pilots inserted */ + diff --git a/libfreedv/cohpsk_internal.h b/libfreedv/cohpsk_internal.h new file mode 100644 index 000000000..f3221c778 --- /dev/null +++ b/libfreedv/cohpsk_internal.h @@ -0,0 +1,131 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: cohpsk_internal.h + AUTHOR......: David Rowe + DATE CREATED: March 2015 + + Functions that implement a coherent PSK FDM modem. + +\*---------------------------------------------------------------------------*/ + +/* + Copyright (C) 2015 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program 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 Lesser General Public License + along with this program; if not, see . +*/ + +#ifndef __COHPSK_INTERNAL__ +#define __COHPSK_INTERNAL__ + +#define NCT_SYMB_BUF (2*NSYMROWPILOT+2) +#define ND 2 /* diversity factor ND 1 is no diveristy, ND we have orginal plus + one copy */ +#define NSW 4 /* number of sync window frames */ +#define COHPSK_ND 2 /* diversity factor */ +#define COHPSK_M 100 /* oversampling rate */ +#define COHPSK_NSYM 6 +#define COHPSK_NFILTER (COHPSK_NSYM*COHPSK_M) +#define COHPSK_EXCESS_BW 0.5 /* excess BW factor of root nyq filter */ +#define COHPSK_NT 5 /* number of symbols we estimate timing over */ + +#include "fdmdv_internal.h" +#include "kiss_fft.h" + +namespace FreeDV +{ + +struct COHPSK { + COMP ch_fdm_frame_buf[NSW*NSYMROWPILOT*COHPSK_M]; /* buffer of several frames of symbols from channel */ + float pilot2[2*NPILOTSFRAME][COHPSK_NC]; + float phi_[NSYMROWPILOT][COHPSK_NC*ND]; /* phase estimates for this frame of rx data symbols */ + float amp_[NSYMROW][COHPSK_NC*ND]; /* amplitude estimates for this frame of rx data symbols */ + COMP rx_symb[NSYMROWPILOT][COHPSK_NC*ND]; /* demodulated symbols */ + float f_est; + COMP rx_filter_memory[COHPSK_NC*ND][COHPSK_NFILTER]; + COMP ct_symb_buf[NCT_SYMB_BUF][COHPSK_NC*ND]; + int ct; /* coarse timing offset in symbols */ + float rx_timing; /* fine timing for last symbol in frame */ + int nin; /* number of samples to input for next symbol */ + float f_fine_est; + COMP ff_rect; + COMP ff_phase; + COMP ct_symb_ff_buf[NSYMROWPILOT+2][COHPSK_NC*ND]; + int sync; + int sync_timer; + + int frame; + float ratio; + + float sig_rms; + float noise_rms; + + struct FDMDV *fdmdv; + + int verbose; + + int *ptest_bits_coh_tx; + int *ptest_bits_coh_rx[2]; + int *ptest_bits_coh_end; + + /* counting bit errors using pilots */ + + int npilotbits; + int npilotbiterrors; + + /* optional log variables used for testing Octave to C port */ + + COMP *rx_baseband_log; + int rx_baseband_log_col_index; + int rx_baseband_log_col_sz; + + COMP *rx_filt_log; + int rx_filt_log_col_index; + int rx_filt_log_col_sz; + + COMP *ch_symb_log; + int ch_symb_log_r; + int ch_symb_log_col_sz; + + float *rx_timing_log; + int rx_timing_log_index; + + /* demodulated bits before diversity combination for test/instrumentation purposes */ + + float rx_bits_lower[COHPSK_BITS_PER_FRAME]; + float rx_bits_upper[COHPSK_BITS_PER_FRAME]; + + /* tx amplitude weights for each carrier for test/instrumentation */ + + float carrier_ampl[COHPSK_NC*ND]; + + /* Flag enabling simple freq est mode */ + int freq_est_mode_reduced; +}; + +void bits_to_qpsk_symbols(COMP tx_symb[][COHPSK_NC*COHPSK_ND], int tx_bits[], int nbits); +void qpsk_symbols_to_bits(struct COHPSK *coh, float rx_bits[], COMP ct_symb_buf[][COHPSK_NC*COHPSK_ND]); +void tx_filter_and_upconvert_coh(COMP tx_fdm[], int Nc, const COMP tx_symbols[], + COMP tx_filter_memory[COHPSK_NC][COHPSK_NSYM], + COMP phase_tx[], COMP freq[], + COMP *fbb_phase, COMP fbb_rect); +void fdm_downconvert_coh(COMP rx_baseband[COHPSK_NC][COHPSK_M+COHPSK_M/P], int Nc, COMP rx_fdm[], COMP phase_rx[], COMP freq[], int nin); +void rx_filter_coh(COMP rx_filt[COHPSK_NC+1][P+1], int Nc, COMP rx_baseband[COHPSK_NC+1][COHPSK_M+COHPSK_M/P], COMP rx_filter_memory[COHPSK_NC+1][COHPSK_NFILTER], int nin); +void frame_sync_fine_freq_est(struct COHPSK *coh, COMP ch_symb[][COHPSK_NC*COHPSK_ND], int sync, int *next_sync); +void fine_freq_correct(struct COHPSK *coh, int sync, int next_sync); +int sync_state_machine(struct COHPSK *coh, int sync, int next_sync); +int cohpsk_fs_offset(COMP out[], COMP in[], int n, float sample_rate_ppm); + +} // FreeDV + +#endif diff --git a/libfreedv/fdmdv.cpp b/libfreedv/fdmdv.cpp new file mode 100644 index 000000000..1efa2ef49 --- /dev/null +++ b/libfreedv/fdmdv.cpp @@ -0,0 +1,1995 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: fdmdv.c + AUTHOR......: David Rowe + DATE CREATED: April 14 2012 + + Functions that implement the FDMDV modem. + +\*---------------------------------------------------------------------------*/ + +/* + Copyright (C) 2012 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program 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 Lesser General Public License + along with this program; if not, see . +*/ + +/*---------------------------------------------------------------------------*\ + + INCLUDES + +\*---------------------------------------------------------------------------*/ + +#include +#include +#include +#include +#include + +#include "fdv_arm_math.h" + +#include "fdmdv_internal.h" +#include "codec2_fdmdv.h" +#include "codec2/comp_prim.h" +#include "rn.h" +#include "rxdec_coeff.h" +#include "test_bits.h" +#include "pilot_coeff.h" +#include "codec2_fft.h" +#include "hanning.h" +#include "os.h" +#include "machdep.h" + +namespace FreeDV +{ + +static int sync_uw[] = {1,-1,1,-1,1,-1}; +#ifdef __EMBEDDED__ +#define printf gdb_stdio_printf +#endif + +static const COMP pi_on_4 = { .70710678118654752439, .70710678118654752439 }; // COSF(PI/4) , SINF(PI/4) + + +/*--------------------------------------------------------------------------* \ + + FUNCTION....: fdmdv_create + AUTHOR......: David Rowe + DATE CREATED: 16/4/2012 + + Create and initialise an instance of the modem. Returns a pointer + to the modem states or NULL on failure. One set of states is + sufficient for a full duplex modem. + +\*---------------------------------------------------------------------------*/ + +struct FDMDV * fdmdv_create(int Nc) +{ + struct FDMDV *f; + int c, i, k; + + assert(NC == FDMDV_NC_MAX); /* check public and private #defines match */ + assert(Nc <= NC); + assert(FDMDV_NOM_SAMPLES_PER_FRAME == M_FAC); + assert(FDMDV_MAX_SAMPLES_PER_FRAME == (M_FAC+M_FAC/P)); + + f = (struct FDMDV*) malloc(sizeof(struct FDMDV)); + if (f == NULL) + return NULL; + + f->Nc = Nc; + + f->ntest_bits = Nc*NB*4; + f->current_test_bit = 0; + f->rx_test_bits_mem = (int*) malloc(sizeof(int)*f->ntest_bits); + assert(f->rx_test_bits_mem != NULL); + for(i=0; intest_bits; i++) + f->rx_test_bits_mem[i] = 0; + assert((sizeof(test_bits)/sizeof(int)) >= f->ntest_bits); + + f->old_qpsk_mapping = 0; + + f->tx_pilot_bit = 0; + + for(c=0; cprev_tx_symbols[c].real = 1.0; + f->prev_tx_symbols[c].imag = 0.0; + f->prev_rx_symbols[c].real = 1.0; + f->prev_rx_symbols[c].imag = 0.0; + + for(k=0; ktx_filter_memory[c][k].real = 0.0; + f->tx_filter_memory[c][k].imag = 0.0; + } + + /* Spread initial FDM carrier phase out as far as possible. + This helped PAPR for a few dB. We don't need to adjust rx + phase as DQPSK takes care of that. */ + + f->phase_tx[c].real = COSF(2.0*PI*c/(Nc+1)); + f->phase_tx[c].imag = SINF(2.0*PI*c/(Nc+1)); + + f->phase_rx[c].real = 1.0; + f->phase_rx[c].imag = 0.0; + + for(k=0; krx_filter_mem_timing[c][k].real = 0.0; + f->rx_filter_mem_timing[c][k].imag = 0.0; + } + } + f->prev_tx_symbols[Nc].real = 2.0; + + fdmdv_set_fsep(f, FSEP); + f->freq[Nc].real = COSF(2.0*PI*0.0/FS); + f->freq[Nc].imag = SINF(2.0*PI*0.0/FS); + f->freq_pol[Nc] = 2.0*PI*0.0/FS; + + f->fbb_rect.real = COSF(2.0*PI*FDMDV_FCENTRE/FS); + f->fbb_rect.imag = SINF(2.0*PI*FDMDV_FCENTRE/FS); + f->fbb_pol = 2.0*PI*FDMDV_FCENTRE/FS; + f->fbb_phase_tx.real = 1.0; + f->fbb_phase_tx.imag = 0.0; + f->fbb_phase_rx.real = 1.0; + f->fbb_phase_rx.imag = 0.0; + + /* Generate DBPSK pilot Look Up Table (LUT) */ + + generate_pilot_lut(f->pilot_lut, &f->freq[Nc]); + + /* freq Offset estimation states */ + + f->fft_pilot_cfg = codec2_fft_alloc (MPILOTFFT, 0, NULL, NULL); + assert(f->fft_pilot_cfg != NULL); + + for(i=0; ipilot_baseband1[i].real = f->pilot_baseband2[i].real = 0.0; + f->pilot_baseband1[i].imag = f->pilot_baseband2[i].imag = 0.0; + } + f->pilot_lut_index = 0; + f->prev_pilot_lut_index = 3*M_FAC; + + for(i=0; irxdec_lpf_mem[i].real = 0.0; + f->rxdec_lpf_mem[i].imag = 0.0; + } + + for(i=0; ipilot_lpf1[i].real = f->pilot_lpf2[i].real = 0.0; + f->pilot_lpf1[i].imag = f->pilot_lpf2[i].imag = 0.0; + } + + f->foff = 0.0; + f->foff_phase_rect.real = 1.0; + f->foff_phase_rect.imag = 0.0; + + for(i=0; irx_fdm_mem[i].real = 0.0; + f->rx_fdm_mem[i].imag = 0.0; + } + + f->fest_state = 0; + f->sync = 0; + f->timer = 0; + for(i=0; isync_mem[i] = 0; + + for(c=0; csig_est[c] = 0.0; + f->noise_est[c] = 0.0; + } + + f->sig_pwr_av = 0.0; + f->foff_filt = 0.0; + + return f; +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: fdmdv_destroy + AUTHOR......: David Rowe + DATE CREATED: 16/4/2012 + + Destroy an instance of the modem. + +\*---------------------------------------------------------------------------*/ + +void fdmdv_destroy(struct FDMDV *fdmdv) +{ + assert(fdmdv != NULL); + codec2_fft_free(fdmdv->fft_pilot_cfg); + free(fdmdv->rx_test_bits_mem); + free(fdmdv); +} + + +void fdmdv_use_old_qpsk_mapping(struct FDMDV *fdmdv) { + fdmdv->old_qpsk_mapping = 1; +} + + +int fdmdv_bits_per_frame(struct FDMDV *fdmdv) +{ + return (fdmdv->Nc * NB); +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: fdmdv_get_test_bits() + AUTHOR......: David Rowe + DATE CREATED: 16/4/2012 + + Generate a frame of bits from a repeating sequence of random data. OK so + it's not very random if it repeats but it makes syncing at the demod easier + for test purposes. + +\*---------------------------------------------------------------------------*/ + +void fdmdv_get_test_bits(struct FDMDV *f, int tx_bits[]) +{ + int i; + int bits_per_frame = fdmdv_bits_per_frame(f); + + for(i=0; icurrent_test_bit]; + f->current_test_bit++; + if (f->current_test_bit > (f->ntest_bits-1)) + f->current_test_bit = 0; + } +} + +float fdmdv_get_fsep(struct FDMDV *f) +{ + return f->fsep; +} + +void fdmdv_set_fsep(struct FDMDV *f, float fsep) { + int c; + float carrier_freq; + + f->fsep = fsep; + + /* Set up frequency of each carrier */ + + for(c=0; cNc/2; c++) { + carrier_freq = (-f->Nc/2 + c)*f->fsep; + f->freq[c].real = COSF(2.0*PI*carrier_freq/FS); + f->freq[c].imag = SINF(2.0*PI*carrier_freq/FS); + f->freq_pol[c] = 2.0*PI*carrier_freq/FS; + } + + for(c=f->Nc/2; cNc; c++) { + carrier_freq = (-f->Nc/2 + c + 1)*f->fsep; + f->freq[c].real = COSF(2.0*PI*carrier_freq/FS); + f->freq[c].imag = SINF(2.0*PI*carrier_freq/FS); + f->freq_pol[c] = 2.0*PI*carrier_freq/FS; + } +} + + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: bits_to_dqpsk_symbols() + AUTHOR......: David Rowe + DATE CREATED: 16/4/2012 + + Maps bits to parallel DQPSK symbols. Generate Nc+1 QPSK symbols from + vector of (1,Nc*Nb) input tx_bits. The Nc+1 symbol is the +1 -1 +1 + .... BPSK sync carrier. + +\*---------------------------------------------------------------------------*/ + +void bits_to_dqpsk_symbols(COMP tx_symbols[], int Nc, COMP prev_tx_symbols[], int tx_bits[], int *pilot_bit, int old_qpsk_mapping) +{ + int c, msb, lsb; + COMP j = {0.0,1.0}; + + /* Map tx_bits to to Nc DQPSK symbols. Note legacy support for + old (suboptimal) V0.91 FreeDV mapping */ + + for(c=0; creal /= mag; + fbb_phase->imag /= mag; + + /* shift memory, inserting zeros at end */ + + for(i=0; ireal /= mag; + fbb_phase->imag /= mag; +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: fdmdv_mod() + AUTHOR......: David Rowe + DATE CREATED: 26/4/2012 + + FDMDV modulator, take a frame of FDMDV_BITS_PER_FRAME bits and + generates a frame of FDMDV_SAMPLES_PER_FRAME modulated symbols. + Sync bit is returned to aid alignment of your next frame. + + The sync_bit value returned will be used for the _next_ frame. + + The output signal is complex to support single sided frequency + shifting, for example when testing frequency offsets in channel + simulation. + +\*---------------------------------------------------------------------------*/ + +void fdmdv_mod(struct FDMDV *fdmdv, COMP tx_fdm[], int tx_bits[], int *sync_bit) +{ + COMP tx_symbols[NC+1]; + PROFILE_VAR(mod_start, tx_filter_and_upconvert_start); + + PROFILE_SAMPLE(mod_start); + bits_to_dqpsk_symbols(tx_symbols, fdmdv->Nc, fdmdv->prev_tx_symbols, tx_bits, &fdmdv->tx_pilot_bit, fdmdv->old_qpsk_mapping); + memcpy(fdmdv->prev_tx_symbols, tx_symbols, sizeof(COMP)*(fdmdv->Nc+1)); + PROFILE_SAMPLE_AND_LOG(tx_filter_and_upconvert_start, mod_start, " bits_to_dqpsk_symbols"); + tx_filter_and_upconvert(tx_fdm, fdmdv->Nc, tx_symbols, fdmdv->tx_filter_memory, + fdmdv->phase_tx, fdmdv->freq, &fdmdv->fbb_phase_tx, fdmdv->fbb_rect); + PROFILE_SAMPLE_AND_LOG2(tx_filter_and_upconvert_start, " tx_filter_and_upconvert"); + + *sync_bit = fdmdv->tx_pilot_bit; +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: generate_pilot_fdm() + AUTHOR......: David Rowe + DATE CREATED: 19/4/2012 + + Generate M_FAC samples of DBPSK pilot signal for Freq offset estimation. + +\*---------------------------------------------------------------------------*/ + +void generate_pilot_fdm(COMP *pilot_fdm, int *bit, float *symbol, + float *filter_mem, COMP *phase, COMP *freq) +{ + int i,j,k; + float tx_baseband[M_FAC]; + + /* +1 -1 +1 -1 DBPSK sync carrier, once filtered becomes (roughly) + two spectral lines at +/- RS/2 */ + + if (*bit) + *symbol = -*symbol; + + if (*bit) + *bit = 0; + else + *bit = 1; + + /* filter DPSK symbol to create M_FAC baseband samples */ + + filter_mem[NFILTER-1] = (sqrtf(2)/2) * *symbol; + for(i=0; ireal; + pilot_fdm[i].imag = sqrtf(2)*2*tx_baseband[i] * phase->imag; + } +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: generate_pilot_lut() + AUTHOR......: David Rowe + DATE CREATED: 19/4/2012 + + Generate a 4M sample vector of DBPSK pilot signal. As the pilot signal + is periodic in 4M samples we can then use this vector as a look up table + for pilot signal generation in the demod. + +\*---------------------------------------------------------------------------*/ + +void generate_pilot_lut(COMP pilot_lut[], COMP *pilot_freq) +{ + int pilot_rx_bit = 0; + float pilot_symbol = sqrtf(2.0); + COMP pilot_phase = {1.0, 0.0}; + float pilot_filter_mem[NFILTER]; + COMP pilot[M_FAC]; + int i,f; + + for(i=0; i= 4) + memcpy(&pilot_lut[M_FAC*(f-4)], pilot, M_FAC*sizeof(COMP)); + } + + // create complex conjugate since we need this and only this later on + for (f=0;f<4*M_FAC;f++) + { + pilot_lut[f] = cconj(pilot_lut[f]); + } + +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: lpf_peak_pick() + AUTHOR......: David Rowe + DATE CREATED: 20/4/2012 + + LPF and peak pick part of freq est, put in a function as we call it twice. + +\*---------------------------------------------------------------------------*/ + +void lpf_peak_pick(float *foff, float *max, COMP pilot_baseband[], + COMP pilot_lpf[], codec2_fft_cfg fft_pilot_cfg, COMP S[], int nin, + int do_fft) +{ + int i,j,k; + int mpilot; + float mag, imax; + int ix; + float r; + + /* LPF cutoff 200Hz, so we can handle max +/- 200 Hz freq offset */ + + for(i=0; i imax) { + imax = mag; + ix = i; + } + } + r = 2.0*200.0/MPILOTFFT; /* maps FFT bin to frequency in Hz */ + + if (ix >= MPILOTFFT/2) + *foff = (ix - MPILOTFFT)*r; + else + *foff = (ix)*r; + } + + *max = imax; + +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: rx_est_freq_offset() + AUTHOR......: David Rowe + DATE CREATED: 19/4/2012 + + Estimate frequency offset of FDM signal using BPSK pilot. Note that + this algorithm is quite sensitive to pilot tone level wrt other + carriers, so test variations to the pilot amplitude carefully. + +\*---------------------------------------------------------------------------*/ + +float rx_est_freq_offset(struct FDMDV *f, COMP rx_fdm[], int nin, int do_fft) +{ + int i; +#ifndef FDV_ARM_MATH + int j; +#endif + COMP pilot[M_FAC+M_FAC/P]; + COMP prev_pilot[M_FAC+M_FAC/P]; + float foff, foff1, foff2; + float max1, max2; + + assert(nin <= M_FAC+M_FAC/P); + + /* get pilot samples used for correlation/down conversion of rx signal */ + + for (i=0; ipilot_lut[f->pilot_lut_index]; + f->pilot_lut_index++; + if (f->pilot_lut_index >= 4*M_FAC) + f->pilot_lut_index = 0; + + prev_pilot[i] = f->pilot_lut[f->prev_pilot_lut_index]; + f->prev_pilot_lut_index++; + if (f->prev_pilot_lut_index >= 4*M_FAC) + f->prev_pilot_lut_index = 0; + } + + /* + Down convert latest M_FAC samples of pilot by multiplying by ideal + BPSK pilot signal we have generated locally. The peak of the + resulting signal is sensitive to the time shift between the + received and local version of the pilot, so we do it twice at + different time shifts and choose the maximum. + */ + + for(i=0; ipilot_baseband1[i] = f->pilot_baseband1[i+nin]; + f->pilot_baseband2[i] = f->pilot_baseband2[i+nin]; + } + +#ifndef FDV_ARM_MATH + for(i=0,j=NPILOTBASEBAND-nin; ipilot_baseband1[j] = cmult(rx_fdm[i], pilot[i]); + f->pilot_baseband2[j] = cmult(rx_fdm[i], prev_pilot[i]); + } +#else + // TODO: Maybe a handwritten mult taking advantage of rx_fdm[0] being + // used twice would be faster but this is for sure faster than + // the implementation above in any case. + arm_cmplx_mult_cmplx_f32(&rx_fdm[0].real,&pilot[0].real,&f->pilot_baseband1[NPILOTBASEBAND-nin].real,nin); + arm_cmplx_mult_cmplx_f32(&rx_fdm[0].real,&prev_pilot[0].real,&f->pilot_baseband2[NPILOTBASEBAND-nin].real,nin); +#endif + + lpf_peak_pick(&foff1, &max1, f->pilot_baseband1, f->pilot_lpf1, f->fft_pilot_cfg, f->S1, nin, do_fft); + lpf_peak_pick(&foff2, &max2, f->pilot_baseband2, f->pilot_lpf2, f->fft_pilot_cfg, f->S2, nin, do_fft); + + if (max1 > max2) + foff = foff1; + else + foff = foff2; + + return foff; +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: fdmdv_freq_shift() + AUTHOR......: David Rowe + DATE CREATED: 26/4/2012 + + Frequency shift modem signal. The use of complex input and output allows + single sided frequency shifting (no images). + +\*---------------------------------------------------------------------------*/ + +void fdmdv_freq_shift(COMP rx_fdm_fcorr[], COMP rx_fdm[], float foff, + COMP *foff_phase_rect, int nin) +{ + COMP foff_rect; + float mag; + int i; + + foff_rect.real = COSF(2.0*PI*foff/FS); + foff_rect.imag = SINF(2.0*PI*foff/FS); + for(i=0; ireal /= mag; + foff_phase_rect->imag /= mag; +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: fdm_downconvert + AUTHOR......: David Rowe + DATE CREATED: 22/4/2012 + + Frequency shift each modem carrier down to Nc+1 baseband signals. + +\*---------------------------------------------------------------------------*/ + +void fdm_downconvert(COMP rx_baseband[NC+1][M_FAC+M_FAC/P], int Nc, COMP rx_fdm[], COMP phase_rx[], COMP freq[], int nin) +{ + int i,c; + float mag; + + /* maximum number of input samples to demod */ + + assert(nin <= (M_FAC+M_FAC/P)); + + /* downconvert */ + + for (c=0; c + nin + |--------------------------|---------| + 1 | + phase_rx(c) + + This means winding phase(c) back from this point + to ensure phase continuity. + + */ + + //PROFILE_SAMPLE(windback_start); + windback_phase = -freq_pol[c]*NFILTER; + windback_phase_rect.real = COSF(windback_phase); + windback_phase_rect.imag = SINF(windback_phase); + phase_rx[c] = cmult(phase_rx[c],windback_phase_rect); + //PROFILE_SAMPLE_AND_LOG(downconvert_start, windback_start, " windback"); + + /* down convert all samples in buffer */ + + st = NRX_FDM_MEM-1; /* end of buffer */ + st -= nin-1; /* first new sample */ + st -= NFILTER; /* first sample used in filtering */ + + /* freq shift per dec_rate step is dec_rate times original shift */ + + f_rect = freq[c]; + for(i=0; i P) + rx_timing -= P; + if (rx_timing < -P) + rx_timing += P; + + /* rx_filter_mem_timing contains Nt*P samples (Nt symbols at rate + P), where Nt is odd. Lets use linear interpolation to resample + in the centre of the timing estimation window .*/ + + rx_timing += floorf(NT/2.0)*P; + low_sample = floorf(rx_timing); + fract = rx_timing - low_sample; + high_sample = ceilf(rx_timing); + + //printf("rx_timing: %f low_sample: %d high_sample: %d fract: %f\n", rx_timing, low_sample, high_sample, fract); + + for(c=0; c= 0) && (d.imag >= 0)) { + msb = 0; lsb = 0; + } + if ((d.real < 0) && (d.imag >= 0)) { + msb = 0; lsb = 1; + } + if ((d.real < 0) && (d.imag < 0)) { + if (old_qpsk_mapping) { + msb = 1; lsb = 0; + } else { + msb = 1; lsb = 1; + } + } + if ((d.real >= 0) && (d.imag < 0)) { + if (old_qpsk_mapping) { + msb = 1; lsb = 1; + } else { + msb = 1; lsb = 0; + } + } + rx_bits[2*c] = msb; + rx_bits[2*c+1] = lsb; + } + + /* Extract DBPSK encoded Sync bit and fine freq offset estimate */ + + norm = 1.0/(cabsolute(prev_rx_symbols[Nc])+1E-6); + phase_difference[Nc] = cmult(rx_symbols[Nc], fcmult(norm, cconj(prev_rx_symbols[Nc]))); + if (phase_difference[Nc].real < 0) { + *sync_bit = 1; + ferr = phase_difference[Nc].imag*norm; /* make f_err magnitude insensitive */ + } + else { + *sync_bit = 0; + ferr = -phase_difference[Nc].imag*norm; + } + + /* pilot carrier gets an extra pi/4 rotation to make it consistent + with other carriers, as we need it for snr_update and scatter + diagram */ + + phase_difference[Nc] = cmult(phase_difference[Nc], pi_on_4); + + return ferr; +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: snr_update() + AUTHOR......: David Rowe + DATE CREATED: 17 May 2012 + + Given phase differences update estimates of signal and noise levels. + +\*---------------------------------------------------------------------------*/ + +void snr_update(float sig_est[], float noise_est[], int Nc, COMP phase_difference[]) +{ + float s[NC+1]; + COMP refl_symbols[NC+1]; + float n[NC+1]; + int c; + + + /* mag of each symbol is distance from origin, this gives us a + vector of mags, one for each carrier. */ + + for(c=0; cntest_bits; +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: fdmdv_put_test_bits() + AUTHOR......: David Rowe + DATE CREATED: 24/4/2012 + + Accepts nbits from rx and attempts to sync with test_bits sequence. + If sync OK measures bit errors. + +\*---------------------------------------------------------------------------*/ + +void fdmdv_put_test_bits(struct FDMDV *f, int *sync, short error_pattern[], + int *bit_errors, int *ntest_bits, int rx_bits[]) +{ + int i,j; + float ber; + int bits_per_frame = fdmdv_bits_per_frame(f); + + /* Append to our memory */ + + for(i=0,j=bits_per_frame; intest_bits-bits_per_frame; i++,j++) + f->rx_test_bits_mem[i] = f->rx_test_bits_mem[j]; + for(i=f->ntest_bits-bits_per_frame,j=0; intest_bits; i++,j++) + f->rx_test_bits_mem[i] = rx_bits[j]; + + /* see how many bit errors we get when checked against test sequence */ + + *bit_errors = 0; + for(i=0; intest_bits; i++) { + error_pattern[i] = test_bits[i] ^ f->rx_test_bits_mem[i]; + *bit_errors += error_pattern[i]; + //printf("%d %d %d %d\n", i, test_bits[i], f->rx_test_bits_mem[i], test_bits[i] ^ f->rx_test_bits_mem[i]); + } + + /* if less than a thresh we are aligned and in sync with test sequence */ + + ber = (float)*bit_errors/f->ntest_bits; + + *sync = 0; + if (ber < 0.2) + *sync = 1; + + *ntest_bits = f->ntest_bits; + +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: freq_state(() + AUTHOR......: David Rowe + DATE CREATED: 24/4/2012 + + Freq offset state machine. Moves between coarse and fine states + based on BPSK pilot sequence. Freq offset estimator occasionally + makes mistakes when used continuously. So we use it until we have + acquired the BPSK pilot, then switch to a more robust "fine" + tracking algorithm. If we lose sync we switch back to coarse mode + for fast re-acquisition of large frequency offsets. + + The sync state is also useful for higher layers to determine when + there is valid FDMDV data for decoding. We want to reliably and + quickly get into sync, stay in sync even on fading channels, and + fall out of sync quickly if tx stops or it's a false sync. + + In multipath fading channels the BPSK sync carrier may be pushed + down in the noise, despite other carriers being at full strength. + We want to avoid loss of sync in these cases. + +\*---------------------------------------------------------------------------*/ + +int freq_state(int *reliable_sync_bit, int sync_bit, int *state, int *timer, int *sync_mem) +{ + int next_state, sync, unique_word, i, corr; + + /* look for 6 symbols (120ms) 101010 of sync sequence */ + + unique_word = 0; + for(i=0; ifbb_phase_rx, *nin); + + /* freq offset estimation and correction */ + + PROFILE_SAMPLE(demod_start); + foff_coarse = rx_est_freq_offset(fdmdv, rx_fdm_bb, *nin, !fdmdv->sync); + PROFILE_SAMPLE_AND_LOG(fdmdv_freq_shift_start, demod_start, " rx_est_freq_offset"); + + if (fdmdv->sync == 0) + fdmdv->foff = foff_coarse; + fdmdv_freq_shift(rx_fdm_fcorr, rx_fdm_bb, -fdmdv->foff, &fdmdv->foff_phase_rect, *nin); + PROFILE_SAMPLE_AND_LOG(down_convert_and_rx_filter_start, fdmdv_freq_shift_start, " fdmdv_freq_shift"); + + /* baseband processing */ + + rxdec_filter(rx_fdm_filter, rx_fdm_fcorr, fdmdv->rxdec_lpf_mem, *nin); + down_convert_and_rx_filter(rx_filt, fdmdv->Nc, rx_fdm_filter, fdmdv->rx_fdm_mem, fdmdv->phase_rx, fdmdv->freq, + fdmdv->freq_pol, *nin, M_FAC/Q); + PROFILE_SAMPLE_AND_LOG(rx_est_timing_start, down_convert_and_rx_filter_start, " down_convert_and_rx_filter"); + fdmdv->rx_timing = rx_est_timing(rx_symbols, fdmdv->Nc, rx_filt, fdmdv->rx_filter_mem_timing, env, *nin, M_FAC); + PROFILE_SAMPLE_AND_LOG(qpsk_to_bits_start, rx_est_timing_start, " rx_est_timing"); + + /* Adjust number of input samples to keep timing within bounds */ + + *nin = M_FAC; + + if (fdmdv->rx_timing > M_FAC/P) + *nin += M_FAC/P; + + if (fdmdv->rx_timing < -M_FAC/P) + *nin -= M_FAC/P; + + foff_fine = qpsk_to_bits(rx_bits, &sync_bit, fdmdv->Nc, fdmdv->phase_difference, fdmdv->prev_rx_symbols, rx_symbols, + fdmdv->old_qpsk_mapping); + memcpy(fdmdv->prev_rx_symbols, rx_symbols, sizeof(COMP)*(fdmdv->Nc+1)); + PROFILE_SAMPLE_AND_LOG(snr_update_start, qpsk_to_bits_start, " qpsk_to_bits"); + snr_update(fdmdv->sig_est, fdmdv->noise_est, fdmdv->Nc, fdmdv->phase_difference); + PROFILE_SAMPLE_AND_LOG(freq_state_start, snr_update_start, " snr_update"); + + /* freq offset estimation state machine */ + + fdmdv->sync = freq_state(reliable_sync_bit, sync_bit, &fdmdv->fest_state, &fdmdv->timer, fdmdv->sync_mem); + PROFILE_SAMPLE_AND_LOG2(freq_state_start, " freq_state"); + fdmdv->foff -= TRACK_COEFF*foff_fine; +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: calc_snr() + AUTHOR......: David Rowe + DATE CREATED: 17 May 2012 + + Calculate current SNR estimate (3000Hz noise BW) + +\*---------------------------------------------------------------------------*/ + +float calc_snr(int Nc, float sig_est[], float noise_est[]) +{ + float S, SdB; + float mean, N50, N50dB, N3000dB; + float snr_dB; + int c; + + S = 0.0; + for(c=0; cNc <= MODEM_STATS_NC_MAX); + + stats->Nc = fdmdv->Nc; + stats->snr_est = calc_snr(fdmdv->Nc, fdmdv->sig_est, fdmdv->noise_est); + stats->sync = fdmdv->sync; + stats->foff = fdmdv->foff; + stats->rx_timing = fdmdv->rx_timing; + stats->clock_offset = 0.0; /* TODO - implement clock offset estimation */ + + stats->nr = 1; + for(c=0; cNc+1; c++) { + stats->rx_symbols[0][c] = fdmdv->phase_difference[c]; + } +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: fdmdv_8_to_16() + AUTHOR......: David Rowe + DATE CREATED: 9 May 2012 + + Changes the sample rate of a signal from 8 to 16 kHz. Support function for + SM1000. + +\*---------------------------------------------------------------------------*/ + +void fdmdv_8_to_16(float out16k[], float in8k[], int n) +{ + int i,k,l; + float acc; + + /* make sure n is an integer multiple of the oversampling rate, ow + this function breaks */ + + assert((n % FDMDV_OS) == 0); + + /* this version unrolled for specific FDMDV_OS */ + + assert(FDMDV_OS == 2); + + for(i=0; iNc; i++) + fprintf(stderr," %1.3f", (double)cabsolute(f->phase_tx[i])); + fprintf(stderr,"\nfreq[]:\n"); + for(i=0; i<=f->Nc; i++) + fprintf(stderr," %1.3f", (double)cabsolute(f->freq[i])); + fprintf(stderr,"\nfoff_phase_rect: %1.3f", (double)cabsolute(f->foff_phase_rect)); + fprintf(stderr,"\nphase_rx[]:\n"); + for(i=0; i<=f->Nc; i++) + fprintf(stderr," %1.3f", (double)cabsolute(f->phase_rx[i])); + fprintf(stderr, "\n\n"); +} + + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: randn() + AUTHOR......: David Rowe + DATE CREATED: 2 August 2014 + + Simple approximation to normal (gaussian) random number generator + with 0 mean and unit variance. + +\*---------------------------------------------------------------------------*/ + +#define RANDN_IT 12 /* This magic number of iterations gives us a + unit variance. I think beacuse var = + (b-a)^2/12 for one uniform random variable, so + for a sum of n random variables it's + n(b-a)^2/12, or for b=1, a = 0, n=12, we get + var = 12(1-0)^2/12 = 1 */ + +static float randn() { + int i; + float rn = 0.0; + + for(i=0; isig_pwr_av: %e target_snr_linear: %f noise_pwr_4000Hz: %e noise_gain: %e\n", + sig_pwr, f->sig_pwr_av, target_snr_linear, noise_pwr_4000Hz, noise_gain); + */ +} + +} // FreeDV + diff --git a/libfreedv/freedv_data_channel.cpp b/libfreedv/freedv_data_channel.cpp new file mode 100644 index 000000000..8b51440f8 --- /dev/null +++ b/libfreedv/freedv_data_channel.cpp @@ -0,0 +1,320 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: freedv_data_channel.c + AUTHOR......: Jeroen Vreeken + DATE CREATED: 03 March 2016 + + Data channel for ethernet like packets in freedv VHF frames. + Currently designed for- + * 2 control bits per frame + * 4 byte counter bits per frame + * 64 bits of data per frame +\*---------------------------------------------------------------------------*/ + +/* + Copyright (C) 2016 Jeroen Vreeken + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program 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 Lesser General Public License + along with this program; if not, see . +*/ + +#include "freedv_data_channel.h" + +#include +#include +#include + +namespace FreeDV +{ + +static unsigned char fdc_header_bcast[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; + +/* CCIT CRC table (0x1201 polynomal) */ +static unsigned short fdc_crc_table[256] = { + 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, + 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, + 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, + 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, + 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, + 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, + 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, + 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, + 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, + 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, + 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, + 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, + 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, + 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, + 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, + 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, + 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, + 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, + 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, + 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, + 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, + 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, + 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, + 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, + 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, + 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, + 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, + 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, + 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, + 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, + 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, + 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 +}; + +static unsigned short fdc_crc(unsigned char *buffer, std::size_t len) +{ + unsigned short crc = 0xffff; + std::size_t i; + + for (i = 0; i < len; i++, buffer++) { + crc = (crc >> 8) ^ fdc_crc_table[(crc ^ *buffer) & 0xff]; + } + + return crc ^ 0xffff; +} + +/* CRC4 0x03 polynomal */ +static unsigned char fdc_crc4(unsigned char *buffer, std::size_t len) +{ + unsigned char crc = 0x0f; + std::size_t i; + + for (i = 0; i < len; i++, buffer++) { + int shift; + + for (shift = 7; shift <= 0; shift--) { + crc <<= 1; + if ((*buffer >> shift) & 0x1) + crc |= 1; + if (crc & 0x10) + crc ^= 0x03; + } + } + + return crc & 0x0f; +} + +struct freedv_data_channel *freedv_data_channel_create(void) +{ + struct freedv_data_channel *fdc; + + fdc = (freedv_data_channel*) malloc(sizeof(struct freedv_data_channel)); + if (!fdc) + return nullptr; + + fdc->cb_rx = nullptr; + fdc->cb_tx = nullptr; + fdc->packet_tx_size = 0; + + freedv_data_set_header(fdc, fdc_header_bcast); + + memcpy(fdc->rx_header, fdc->tx_header, 8); + + return fdc; +} + +void freedv_data_channel_destroy(struct freedv_data_channel *fdc) +{ + free(fdc); +} + + +void freedv_data_set_cb_rx(struct freedv_data_channel *fdc, freedv_data_callback_rx cb, void *state) +{ + fdc->cb_rx = cb; + fdc->cb_rx_state = state; +} + +void freedv_data_set_cb_tx(struct freedv_data_channel *fdc, freedv_data_callback_tx cb, void *state) +{ + fdc->cb_tx = cb; + fdc->cb_tx_state = state; +} + +void freedv_data_channel_rx_frame(struct freedv_data_channel *fdc, unsigned char *data, std::size_t size, int from_bit, int bcast_bit, int crc_bit, int end_bits) +{ + int copy_bits; + if (end_bits) { + copy_bits = end_bits; + } else { + copy_bits = size; + } + + /* New packet? */ + if (fdc->packet_rx_cnt == 0) { + /* Does the packet have a compressed from field? */ + if (from_bit) { + /* Compressed from: take the previously received header */ + memcpy(fdc->packet_rx + fdc->packet_rx_cnt, fdc->rx_header, 6); + fdc->packet_rx_cnt += 6; + } + if (bcast_bit) { + if (!from_bit) { + /* Copy from header and modify size and end_bits accordingly */ + memcpy(fdc->packet_rx + fdc->packet_rx_cnt, data, 6); + fdc->packet_rx_cnt += 6; + copy_bits -= 6; + if (copy_bits < 0) + copy_bits = 0; + data += 6; + } + /* Compressed to: fill in broadcast address */ + memcpy(fdc->packet_rx + fdc->packet_rx_cnt, fdc_header_bcast, sizeof(fdc_header_bcast)); + fdc->packet_rx_cnt += 6; + } + if (crc_bit) { + unsigned char calc_crc = fdc_crc4(data, size); + if (calc_crc == end_bits) { + /* It is a single header field, remember it for later */ + memcpy(fdc->packet_rx + 6, data, 6); + memcpy(fdc->packet_rx, fdc_header_bcast, 6); + if (fdc->cb_rx) { + fdc->cb_rx(fdc->cb_rx_state, fdc->packet_rx, 12); + } + } + fdc->packet_rx_cnt = 0; + return; + } + } + + if (fdc->packet_rx_cnt + copy_bits >= FREEDV_DATA_CHANNEL_PACKET_MAX) { + /* Something went wrong... this can not be a real packet */ + fdc->packet_rx_cnt = 0; + return; + } + + memcpy(fdc->packet_rx + fdc->packet_rx_cnt, data, copy_bits); + fdc->packet_rx_cnt += copy_bits; + + if (end_bits != 0 && fdc->packet_rx_cnt >= 2) { + unsigned short calc_crc = fdc_crc(fdc->packet_rx, fdc->packet_rx_cnt - 2); + unsigned short rx_crc; + rx_crc = fdc->packet_rx[fdc->packet_rx_cnt - 1] << 8; + rx_crc |= fdc->packet_rx[fdc->packet_rx_cnt - 2]; + + if (rx_crc == calc_crc) { + if ((std::size_t) fdc->packet_rx_cnt == size) { + /* It is a single header field, remember it for later */ + memcpy(fdc->rx_header, fdc->packet_rx, 6); + } + + /* callback */ + if (fdc->cb_rx) { + unsigned char tmp[6]; + memcpy(tmp, fdc->packet_rx, 6); + memcpy(fdc->packet_rx, fdc->packet_rx + 6, 6); + memcpy(fdc->packet_rx + 6, tmp, 6); + + std::size_t size = fdc->packet_rx_cnt - 2; + if (size < 12) + size = 12; + fdc->cb_rx(fdc->cb_rx_state, fdc->packet_rx, size); + } + } + fdc->packet_rx_cnt = 0; + } +} + +void freedv_data_channel_tx_frame(struct freedv_data_channel *fdc, unsigned char *data, std::size_t size, int *from_bit, int *bcast_bit, int *crc_bit, int *end_bits) +{ + *from_bit = 0; + *bcast_bit = 0; + *crc_bit = 0; + + if (!fdc->packet_tx_size) { + fdc->packet_tx_cnt = 0; + + if (fdc->cb_tx) { + fdc->packet_tx_size = FREEDV_DATA_CHANNEL_PACKET_MAX; + fdc->cb_tx(fdc->cb_tx_state, fdc->packet_tx, &fdc->packet_tx_size); + } + if (!fdc->packet_tx_size) { + /* Nothing to send, insert a header frame */ + memcpy(fdc->packet_tx, fdc->tx_header, size); + if (size < 8) { + *end_bits = fdc_crc4(fdc->tx_header, size); + *crc_bit = 1; + memcpy(data, fdc->tx_header, size); + + return; + } else { + fdc->packet_tx_size = size; + } + } else { + /* new packet */ + unsigned short crc; + unsigned char tmp[6]; + + *from_bit = !memcmp(fdc->packet_tx + 6, fdc->tx_header, 6); + *bcast_bit = !memcmp(fdc->packet_tx, fdc_header_bcast, 6); + + memcpy(tmp, fdc->packet_tx, 6); + memcpy(fdc->packet_tx, fdc->packet_tx + 6, 6); + memcpy(fdc->packet_tx + 6, tmp, 6); + + crc = fdc_crc(fdc->packet_tx, fdc->packet_tx_size); + + fdc->packet_tx[fdc->packet_tx_size] = crc & 0xff; + fdc->packet_tx_size++; + fdc->packet_tx[fdc->packet_tx_size] = (crc >> 8) & 0xff; + fdc->packet_tx_size++; + + if (*from_bit) { + fdc->packet_tx_cnt = 6; + } else { + if (*bcast_bit) { + memcpy(fdc->packet_tx + 6, fdc->packet_tx, 6); + } + } + if (*bcast_bit) { + fdc->packet_tx_cnt += 6; + } + } + } + if (fdc->packet_tx_size) { + std::size_t copy = fdc->packet_tx_size - fdc->packet_tx_cnt; + + if (copy > size) { + copy = size; + *end_bits = 0; + } else { + *end_bits = copy; + fdc->packet_tx_size = 0; + } + memcpy(data, fdc->packet_tx + fdc->packet_tx_cnt, copy); + fdc->packet_tx_cnt += copy; + } +} + +void freedv_data_set_header(struct freedv_data_channel *fdc, unsigned char *header) +{ + unsigned short crc = fdc_crc(header, 6); + + memcpy(fdc->tx_header, header, 6); + fdc->tx_header[6] = crc & 0xff; + fdc->tx_header[7] = (crc >> 8) & 0xff; +} + +int freedv_data_get_n_tx_frames(struct freedv_data_channel *fdc, std::size_t size) +{ + if (fdc->packet_tx_size == 0) + return 0; + /* packet will be send in 'size' byte frames */ + return (fdc->packet_tx_size - fdc->packet_tx_cnt + size-1) / size; +} + +} // FreeDV diff --git a/libfreedv/freedv_filter.cpp b/libfreedv/freedv_filter.cpp new file mode 100644 index 000000000..032db43c2 --- /dev/null +++ b/libfreedv/freedv_filter.cpp @@ -0,0 +1,286 @@ +/* + Copyright (C) 2018 James C. Ahlstrom + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program 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 Lesser General Public License + along with this program; if not, see . +*/ + +#include +#include +#include +#include + +#include "freedv_filter.h" +#include "freedv_filter_coef.h" + +#include "fdv_arm_math.h" + +#define cmplx(value) (COSF(value) + SINF(value) * I) + +namespace FreeDV +{ + +/* + * This is a library of filter functions. They were copied from Quisk and converted to single precision. + */ + +/*---------------------------------------------------------------------------*\ + + FUNCTIONS...: quisk_filt_cfInit + AUTHOR......: Jim Ahlstrom + DATE CREATED: 27 August 2015 + MODIFIED: 4 June 2018 + + Initialize a FIR filter that has complex samples, and either real or complex coefficients. + +\*---------------------------------------------------------------------------*/ + +void quisk_filt_cfInit(struct quisk_cfFilter * filter, float * coefs, int taps) { + // Prepare a new filter using coefs and taps. Samples are complex. Coefficients can + // be real or complex. + filter->dCoefs = coefs; + filter->cpxCoefs = NULL; + filter->cSamples = (std::complex *) malloc(taps * sizeof(std::complex)); + memset(filter->cSamples, 0, taps * sizeof(std::complex)); + filter->ptcSamp = filter->cSamples; + filter->nTaps = taps; + filter->cBuf = NULL; + filter->nBuf = 0; + filter->decim_index = 0; +} + +/*---------------------------------------------------------------------------*\ + + FUNCTIONS...: quisk_filt_destroy + AUTHOR......: Jim Ahlstrom + DATE CREATED: 27 August 2015 + MODIFIED: 4 June 2018 + + Destroy the FIR filter and free all resources. + +\*---------------------------------------------------------------------------*/ + +void quisk_filt_destroy(struct quisk_cfFilter * filter) { + if (filter->cSamples) { + free(filter->cSamples); + filter->cSamples = NULL; + } + + if (filter->cBuf) { + free(filter->cBuf); + filter->cBuf = NULL; + } + + if (filter->cpxCoefs) { + free(filter->cpxCoefs); + filter->cpxCoefs = NULL; + } +} + +/*---------------------------------------------------------------------------*\ + + FUNCTIONS...: quisk_cfInterpDecim + AUTHOR......: Jim Ahlstrom + DATE CREATED: 27 August 2015 + MODIFIED: 4 June 2018 + + Take an array of samples cSamples of length count, multiply the sample rate + by interp, and then divide the sample rate by decim. Return the new number + of samples. Each specific interp and decim will require its own custom + low pass FIR filter with real coefficients. + +\*---------------------------------------------------------------------------*/ + +int quisk_cfInterpDecim(std::complex * cSamples, int count, struct quisk_cfFilter * filter, int interp, int decim) { + // Interpolate by interp, and then decimate by decim. + // This uses the float coefficients of filter (not the complex). Samples are complex. + int i, k, nOut; + float * ptCoef; + std::complex *ptSample; + std::complex csample; + + if (count > filter->nBuf) { // increase size of sample buffer + filter->nBuf = count * 2; + + if (filter->cBuf) + free(filter->cBuf); + + filter->cBuf = (std::complex *) malloc(filter->nBuf * sizeof(std::complex)); + } + + memcpy(filter->cBuf, cSamples, count * sizeof(std::complex)); + nOut = 0; + + for (i = 0; i < count; i++) { + // Put samples into buffer left to right. Use samples right to left. + *filter->ptcSamp = filter->cBuf[i]; + + while (filter->decim_index < interp) { + ptSample = filter->ptcSamp; + ptCoef = filter->dCoefs + filter->decim_index; + csample = 0; + + for (k = 0; k < filter->nTaps / interp; k++, ptCoef += interp) { + csample += *ptSample * *ptCoef; + + if (--ptSample < filter->cSamples) + ptSample = filter->cSamples + filter->nTaps - 1; + } + + cSamples[nOut] = csample * (float) interp; + nOut++; + filter->decim_index += decim; + } + + if (++filter->ptcSamp >= filter->cSamples + filter->nTaps) + filter->ptcSamp = filter->cSamples; + + filter->decim_index = filter->decim_index - interp; + } + + return nOut; +} + +/*---------------------------------------------------------------------------*\ + + FUNCTIONS...: quisk_ccfInterpDecim + AUTHOR......: Jim Ahlstrom + DATE CREATED: 7 June 2018 + + Take an array of samples cSamples of length count, multiply the sample rate + by interp, and then divide the sample rate by decim. Return the new number + of samples. Each specific interp and decim will require its own custom + low pass FIR filter with complex coefficients. This filter can be tuned. + + This filter is not currently used. + +\*---------------------------------------------------------------------------*/ +#if 0 +int quisk_ccfInterpDecim(complex float * cSamples, int count, struct quisk_cfFilter * filter, int interp, int decim) { + // Interpolate by interp, and then decimate by decim. + // This uses the complex coefficients of filter (not the real). Samples are complex. + int i, k, nOut; + complex float * ptCoef; + complex float * ptSample; + complex float csample; + + if (count > filter->nBuf) { // increase size of sample buffer + filter->nBuf = count * 2; + if (filter->cBuf) + FREE(filter->cBuf); + filter->cBuf = (complex float *)MALLOC(filter->nBuf * sizeof(complex float)); + } + + memcpy(filter->cBuf, cSamples, count * sizeof(complex float)); + nOut = 0; + + for (i = 0; i < count; i++) { + // Put samples into buffer left to right. Use samples right to left. + *filter->ptcSamp = filter->cBuf[i]; + + while (filter->decim_index < interp) { + ptSample = filter->ptcSamp; + ptCoef = filter->cpxCoefs + filter->decim_index; + csample = 0; + + for (k = 0; k < filter->nTaps / interp; k++, ptCoef += interp) { + csample += *ptSample * *ptCoef; + + if (--ptSample < filter->cSamples) + ptSample = filter->cSamples + filter->nTaps - 1; + } + + cSamples[nOut] = csample * interp; + nOut++; + filter->decim_index += decim; + } + + if (++filter->ptcSamp >= filter->cSamples + filter->nTaps) + filter->ptcSamp = filter->cSamples; + + filter->decim_index = filter->decim_index - interp; + } + + return nOut; +} +#endif + +/*---------------------------------------------------------------------------*\ + + FUNCTIONS...: quisk_cfTune + AUTHOR......: Jim Ahlstrom + DATE CREATED: 4 June 2018 + + Tune a low pass filter with float coefficients into an analytic I/Q bandpass filter + with complex coefficients. The "freq" is the center frequency / sample rate. + If the float coefs represent a low pass filter with bandwidth 1 kHz, the new bandpass + filter has width 2 kHz. The filter can be re-tuned repeatedly. + +\*---------------------------------------------------------------------------*/ + +void quisk_cfTune(struct quisk_cfFilter * filter, float freq) { + float D, tune; + int i; + + if ( ! filter->cpxCoefs) + filter->cpxCoefs = (std::complex *) malloc(filter->nTaps * sizeof(std::complex)); + + tune = 2.0 * M_PI * freq; + D = (filter->nTaps - 1.0) / 2.0; + + for (i = 0; i < filter->nTaps; i++) { + float tval = tune * (i - D); + filter->cpxCoefs[i] = cmplx(tval) * filter->dCoefs[i]; + } +} + +/*---------------------------------------------------------------------------*\ + + FUNCTIONS...: quisk_ccfFilter + AUTHOR......: Jim Ahlstrom + DATE CREATED: 4 June 2018 + + Filter complex samples using complex coefficients. The inSamples and outSamples may be + the same array. The loop runs forward over coefficients but backwards over samples. + Therefore, the coefficients must be reversed unless they are created by quisk_cfTune. + Low pass filter coefficients are symmetrical, so this does not usually matter. + +\*---------------------------------------------------------------------------*/ + +void quisk_ccfFilter(std::complex *inSamples, std::complex *outSamples, int count, struct quisk_cfFilter * filter) { + int i, k; + std::complex *ptSample; + std::complex *ptCoef; + std::complex accum; + + for (i = 0; i < count; i++) { + *filter->ptcSamp = inSamples[i]; + accum = 0; + ptSample = filter->ptcSamp; + ptCoef = filter->cpxCoefs; + + for (k = 0; k < filter->nTaps; k++, ptCoef++) { + accum += *ptSample * *ptCoef; + + if (--ptSample < filter->cSamples) + ptSample = filter->cSamples + filter->nTaps - 1; + } + + outSamples[i] = accum; + + if (++filter->ptcSamp >= filter->cSamples + filter->nTaps) + filter->ptcSamp = filter->cSamples; + } +} + +} // freeDV diff --git a/libfreedv/freedv_filter_coef.h b/libfreedv/freedv_filter_coef.h new file mode 100644 index 000000000..9694eb5c1 --- /dev/null +++ b/libfreedv/freedv_filter_coef.h @@ -0,0 +1,166 @@ +/* + Copyright (C) 2018 James C. Ahlstrom + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program 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 Lesser General Public License + along with this program; if not, see . +*/ + +/* + These are the coefficients for various FIR filters. A declaration of these filter coefficients is in filter.h. + Multiple filters can use these coefficients because they are read-only. + + Although a sample rate is specified, the filters may be used at other sample rates. For example, if + filtP750S1040 is used at 48000 sps, the pass and stop frequencies are 4500 and 6240 hz. +*/ + +namespace FreeDV +{ + +// Low pass filter, sample rate 8000 hz, 0.2 dB ripple, 100 dB atten, pass 550 hz, stop 750 hz. +float filtP550S750[160]={ + 0.000001500540125945, 0.000020553368071006, 0.000052842049763802, 0.000112071233638701, + 0.000202565299657164, 0.000325476960438197, 0.000474396686568771, 0.000633746562372497, 0.000778858561033731, + 0.000878592697224500, 0.000900611877226272, 0.000818750000130019, 0.000621157718914443, 0.000317269738067462, +-0.000058614729046822, -0.000448959090901751, -0.000780751290682747, -0.000978953969922609, -0.000983534965413392, +-0.000766540799920385, -0.000344938705664714, 0.000214927788687815, 0.000804118320944653, 0.001289527679116282, + 0.001541598437149897, 0.001466078039230554, 0.001032493743140772, 0.000291727467744814, -0.000623607913580581, +-0.001518948630011706, -0.002175907515711935, -0.002402252989524116, -0.002082876981631170, -0.001219318501019004, + 0.000053915753894017, 0.001483599323867600, 0.002743518309691092, 0.003504691193108974, 0.003515993126242027, + 0.002676486805582815, 0.001080325423865147, -0.000980649349095093, -0.003062866925046052, -0.004660487490214220, +-0.005321805637618908, -0.004767235761853469, -0.002979877569160189, -0.000242864453416682, 0.002892365745006815, + 0.005707645107750651, 0.007473145256589892, 0.007624527169837005, 0.005921569713871673, 0.002547381438730890, +-0.001883079571618079, -0.006418195698900790, -0.009958090016198632, -0.011502199858687428, -0.010403943660694560, +-0.006572745274759415, -0.000569370325758693, 0.006440667006225166, 0.012881777376768124, 0.017083918451421990, + 0.017661533458054445, 0.013877952730549446, 0.005912685575826365, -0.005037640104142052, -0.016864250576905999, +-0.026855876467499887, -0.032168177048912679, -0.030370760878632559, -0.019967289813872333, -0.000782327027950076, + 0.025871098651626040, 0.057290144048617792, 0.089743290905422241, 0.119038289777397190, 0.141198609990722840, + 0.153125933205703250, 0.153125933205703250, 0.141198609990722840, 0.119038289777397190, 0.089743290905422241, + 0.057290144048617792, 0.025871098651626040, -0.000782327027950076, -0.019967289813872333, -0.030370760878632559, +-0.032168177048912679, -0.026855876467499887, -0.016864250576905999, -0.005037640104142052, 0.005912685575826365, + 0.013877952730549446, 0.017661533458054445, 0.017083918451421990, 0.012881777376768124, 0.006440667006225166, +-0.000569370325758693, -0.006572745274759415, -0.010403943660694560, -0.011502199858687428, -0.009958090016198632, +-0.006418195698900790, -0.001883079571618079, 0.002547381438730890, 0.005921569713871673, 0.007624527169837005, + 0.007473145256589892, 0.005707645107750651, 0.002892365745006815, -0.000242864453416682, -0.002979877569160189, +-0.004767235761853469, -0.005321805637618908, -0.004660487490214220, -0.003062866925046052, -0.000980649349095093, + 0.001080325423865147, 0.002676486805582815, 0.003515993126242027, 0.003504691193108974, 0.002743518309691092, + 0.001483599323867600, 0.000053915753894017, -0.001219318501019004, -0.002082876981631170, -0.002402252989524116, +-0.002175907515711935, -0.001518948630011706, -0.000623607913580581, 0.000291727467744814, 0.001032493743140772, + 0.001466078039230554, 0.001541598437149897, 0.001289527679116282, 0.000804118320944653, 0.000214927788687815, +-0.000344938705664714, -0.000766540799920385, -0.000983534965413392, -0.000978953969922609, -0.000780751290682747, +-0.000448959090901751, -0.000058614729046822, 0.000317269738067462, 0.000621157718914443, 0.000818750000130019, + 0.000900611877226272, 0.000878592697224500, 0.000778858561033731, 0.000633746562372497, 0.000474396686568771, + 0.000325476960438197, 0.000202565299657164, 0.000112071233638701, 0.000052842049763802, 0.000020553368071006, + 0.000001500540125945 +}; + +// FIR filter suitable for changing rates 7500 to/from 8000 +// Sample 120000 Hz, pass 2700, stop 3730, ripple 0.1dB, atten 100 dB. Stop 0.03108. +float quiskFilt120t480[480] = { -0.000005050567303837, -0.000000267011791999, 0.000000197734700398, 0.000001038946634000, + 0.000002322193058869, 0.000004115682735322, 0.000006499942123311, 0.000009551098482930, 0.000013350669444763, + 0.000017966192635412, 0.000023463361155584, 0.000029885221425020, 0.000037271082107518, 0.000045630720487935, + 0.000054970017069384, 0.000065233162392019, 0.000076360900545177, 0.000088271373315159, 0.000100818605854714, + 0.000113853476544409, 0.000127174196746337, 0.000140558396336177, 0.000153744508371709, 0.000166450784469067, + 0.000178368313347299, 0.000189176709991702, 0.000198541881389953, 0.000206128795372885, 0.000211604878787747, + 0.000214655997661182, 0.000214994859281552, 0.000212358734245594, 0.000206539880117977, 0.000197379393194548, + 0.000184780318878738, 0.000168719942655099, 0.000149250512353807, 0.000126511346757621, 0.000100726393185629, + 0.000072210925236429, 0.000041365841965015, 0.000008680571408025, -0.000025277165852799, -0.000059865389594949, +-0.000094384355854646, -0.000128080670195777, -0.000160170174848483, -0.000189854272533545, -0.000216333899003825, +-0.000238836419299503, -0.000256632149501508, -0.000269058714331757, -0.000275541485292432, -0.000275614059005332, +-0.000268937472718753, -0.000255317038867589, -0.000234717772155001, -0.000207273956099563, -0.000173297342436372, +-0.000133280012107173, -0.000087895370243821, -0.000037986085678081, 0.000015440388211825, 0.000071232572821451, + 0.000128114399130489, 0.000184710477990398, 0.000239577162514028, 0.000291234779803098, 0.000338204791740229, + 0.000379047713684221, 0.000412403761615261, 0.000437031818051652, 0.000451848709179591, 0.000455966225408344, + 0.000448726371643413, 0.000429729020814434, 0.000398857326863837, 0.000356297600912998, 0.000302547334727027, + 0.000238422248479072, 0.000165048886226905, 0.000083853091464077, -0.000003462782744354, -0.000094949813106744, +-0.000188451833293202, -0.000281651282503015, -0.000372121907291206, -0.000457387566635848, -0.000534985542936898, +-0.000602532044011899, -0.000657788245032425, -0.000698728981427767, -0.000723604675185869, -0.000731002305621048, +-0.000719899536922384, -0.000689709694056092, -0.000640319946685634, -0.000572115873292030, -0.000485996080304965, +-0.000383371840261246, -0.000266155252511831, -0.000136731311264191, 0.000002082667095075, 0.000147092077716480, + 0.000294790953130229, 0.000441441918072383, 0.000583164190168290, 0.000716029226064227, 0.000836164238172957, + 0.000939856052624227, 0.001023657909064450, 0.001084492755093968, 0.001119751426837743, 0.001127383039339373, + 0.001105974243787613, 0.001054815583369999, 0.000973950761085690, 0.000864209315714227, 0.000727219011746881, + 0.000565398080608305, 0.000381924396468366, 0.000180685902835315, -0.000033793183292569, -0.000256444114966522, +-0.000481764526566339, -0.000703946352348464, -0.000917016099829735, -0.001114986581270253, -0.001292014799874503, +-0.001442563411804926, -0.001561559957317790, -0.001644551048567398, -0.001687846581475964, -0.001688649703502788, +-0.001645167889846890, -0.001556702802350076, -0.001423714708648073, -0.001247857669697092, -0.001031986722557201, +-0.000780131048444402, -0.000497436825078657, -0.000190077210351809, 0.000134868279325909, 0.000469563533327739, + 0.000805591531546815, 0.001134152328775355, 0.001446279849797673, 0.001733071409562941, 0.001985924997799762, + 0.002196778054604388, 0.002358342626407065, 0.002464328098407475, 0.002509648218888532, 0.002490604086803692, + 0.002405037734357425, 0.002252452724297770, 0.002034094661603120, 0.001752990365583534, 0.001413941154886139, + 0.001023470495638453, 0.000589723521647734, 0.000122320866350319, -0.000367832138027160, -0.000868777013398284, +-0.001367771151677059, -0.001851587344265625, -0.002306838088978190, -0.002720317947026380, -0.003079353614002113, +-0.003372155891804708, -0.003588162376578369, -0.003718362558663737, -0.003755596511143005, -0.003694818131674599, +-0.003533315298404129, -0.003270878754553819, -0.002909914962857412, -0.002455496391464944, -0.001915346645364514, +-0.001299757227227888, -0.000621437066532776, 0.000104706515738248, 0.000861849931067767, 0.001631595707499856, + 0.002394368911341672, 0.003129858565588139, 0.003817496679992245, 0.004436963307209760, 0.004968707287606522, + 0.005394469536085115, 0.005697797543539088, 0.005864537618023589, 0.005883292537600076, 0.005745832319314692, + 0.005447447099071761, 0.004987231255534477, 0.004368289529377007, 0.003597859022418248, 0.002687338851256991, + 0.001652226293162047, 0.000511956075882180, -0.000710356149138656, -0.001988263330091648, -0.003292424566049982, +-0.004591123342747130, -0.005850857852106148, -0.007036991266043732, -0.008114450164977267, -0.009048456200082230, +-0.009805276478965942, -0.010352975302354198, -0.010662152577592631, -0.010706650669328861, -0.010464214075017983, +-0.009917087295446811, -0.009052534679222271, -0.007863270920348924, -0.006347789704693751, -0.004510582323649121, +-0.002362238055733795, 0.000080576968834213, 0.002795265196543707, 0.005753566158586979, 0.008921944932552510, + 0.012262093950265378, 0.015731539846483594, 0.019284344624007944, 0.022871886384520687, 0.026443706729191677, + 0.029948406200633094, 0.033334570666910354, 0.036551709955124537, 0.039551189200810140, 0.042287133974308874, + 0.044717290029466283, 0.046803820535016104, 0.048514022996355009, 0.049820951883635139, 0.050703932928426454, + 0.051148959210315710, 0.051148959210315710, 0.050703932928426454, 0.049820951883635139, 0.048514022996355009, + 0.046803820535016104, 0.044717290029466283, 0.042287133974308874, 0.039551189200810140, 0.036551709955124537, + 0.033334570666910354, 0.029948406200633094, 0.026443706729191677, 0.022871886384520687, 0.019284344624007944, + 0.015731539846483594, 0.012262093950265378, 0.008921944932552510, 0.005753566158586979, 0.002795265196543707, + 0.000080576968834213, -0.002362238055733795, -0.004510582323649121, -0.006347789704693751, -0.007863270920348924, +-0.009052534679222271, -0.009917087295446811, -0.010464214075017983, -0.010706650669328861, -0.010662152577592631, +-0.010352975302354198, -0.009805276478965942, -0.009048456200082230, -0.008114450164977267, -0.007036991266043732, +-0.005850857852106148, -0.004591123342747130, -0.003292424566049982, -0.001988263330091648, -0.000710356149138656, + 0.000511956075882180, 0.001652226293162047, 0.002687338851256991, 0.003597859022418248, 0.004368289529377007, + 0.004987231255534477, 0.005447447099071761, 0.005745832319314692, 0.005883292537600076, 0.005864537618023589, + 0.005697797543539088, 0.005394469536085115, 0.004968707287606522, 0.004436963307209760, 0.003817496679992245, + 0.003129858565588139, 0.002394368911341672, 0.001631595707499856, 0.000861849931067767, 0.000104706515738248, +-0.000621437066532776, -0.001299757227227888, -0.001915346645364514, -0.002455496391464944, -0.002909914962857412, +-0.003270878754553819, -0.003533315298404129, -0.003694818131674599, -0.003755596511143005, -0.003718362558663737, +-0.003588162376578369, -0.003372155891804708, -0.003079353614002113, -0.002720317947026380, -0.002306838088978190, +-0.001851587344265625, -0.001367771151677059, -0.000868777013398284, -0.000367832138027160, 0.000122320866350319, + 0.000589723521647734, 0.001023470495638453, 0.001413941154886139, 0.001752990365583534, 0.002034094661603120, + 0.002252452724297770, 0.002405037734357425, 0.002490604086803692, 0.002509648218888532, 0.002464328098407475, + 0.002358342626407065, 0.002196778054604388, 0.001985924997799762, 0.001733071409562941, 0.001446279849797673, + 0.001134152328775355, 0.000805591531546815, 0.000469563533327739, 0.000134868279325909, -0.000190077210351809, +-0.000497436825078657, -0.000780131048444402, -0.001031986722557201, -0.001247857669697092, -0.001423714708648073, +-0.001556702802350076, -0.001645167889846890, -0.001688649703502788, -0.001687846581475964, -0.001644551048567398, +-0.001561559957317790, -0.001442563411804926, -0.001292014799874503, -0.001114986581270253, -0.000917016099829735, +-0.000703946352348464, -0.000481764526566339, -0.000256444114966522, -0.000033793183292569, 0.000180685902835315, + 0.000381924396468366, 0.000565398080608305, 0.000727219011746881, 0.000864209315714227, 0.000973950761085690, + 0.001054815583369999, 0.001105974243787613, 0.001127383039339373, 0.001119751426837743, 0.001084492755093968, + 0.001023657909064450, 0.000939856052624227, 0.000836164238172957, 0.000716029226064227, 0.000583164190168290, + 0.000441441918072383, 0.000294790953130229, 0.000147092077716480, 0.000002082667095075, -0.000136731311264191, +-0.000266155252511831, -0.000383371840261246, -0.000485996080304965, -0.000572115873292030, -0.000640319946685634, +-0.000689709694056092, -0.000719899536922384, -0.000731002305621048, -0.000723604675185869, -0.000698728981427767, +-0.000657788245032425, -0.000602532044011899, -0.000534985542936898, -0.000457387566635848, -0.000372121907291206, +-0.000281651282503015, -0.000188451833293202, -0.000094949813106744, -0.000003462782744354, 0.000083853091464077, + 0.000165048886226905, 0.000238422248479072, 0.000302547334727027, 0.000356297600912998, 0.000398857326863837, + 0.000429729020814434, 0.000448726371643413, 0.000455966225408344, 0.000451848709179591, 0.000437031818051652, + 0.000412403761615261, 0.000379047713684221, 0.000338204791740229, 0.000291234779803098, 0.000239577162514028, + 0.000184710477990398, 0.000128114399130489, 0.000071232572821451, 0.000015440388211825, -0.000037986085678081, +-0.000087895370243821, -0.000133280012107173, -0.000173297342436372, -0.000207273956099563, -0.000234717772155001, +-0.000255317038867589, -0.000268937472718753, -0.000275614059005332, -0.000275541485292432, -0.000269058714331757, +-0.000256632149501508, -0.000238836419299503, -0.000216333899003825, -0.000189854272533545, -0.000160170174848483, +-0.000128080670195777, -0.000094384355854646, -0.000059865389594949, -0.000025277165852799, 0.000008680571408025, + 0.000041365841965015, 0.000072210925236429, 0.000100726393185629, 0.000126511346757621, 0.000149250512353807, + 0.000168719942655099, 0.000184780318878738, 0.000197379393194548, 0.000206539880117977, 0.000212358734245594, + 0.000214994859281552, 0.000214655997661182, 0.000211604878787747, 0.000206128795372885, 0.000198541881389953, + 0.000189176709991702, 0.000178368313347299, 0.000166450784469067, 0.000153744508371709, 0.000140558396336177, + 0.000127174196746337, 0.000113853476544409, 0.000100818605854714, 0.000088271373315159, 0.000076360900545177, + 0.000065233162392019, 0.000054970017069384, 0.000045630720487935, 0.000037271082107518, 0.000029885221425020, + 0.000023463361155584, 0.000017966192635412, 0.000013350669444763, 0.000009551098482930, 0.000006499942123311, + 0.000004115682735322, 0.000002322193058869, 0.000001038946634000, 0.000000197734700398, -0.000000267011791999, +-0.000005050567303837 }; + +} // FreeDV diff --git a/libfreedv/freedv_vhf_framing.cpp b/libfreedv/freedv_vhf_framing.cpp new file mode 100644 index 000000000..c329e9e84 --- /dev/null +++ b/libfreedv/freedv_vhf_framing.cpp @@ -0,0 +1,867 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: fsk.c + AUTHOR......: Brady O'Brien + DATE CREATED: 11 February 2016 + + Framer and deframer for VHF FreeDV modes 'A' and 'B' + Currently designed for- + * 40ms ota modem frames + * 40ms Codec2 1300 frames + * 52 bits of Codec2 per frame + * 16 bits of unique word per frame + * 28 'spare' bits per frame + * - 4 spare bits at front and end of frame (8 total) for padding + * - 20 'protocol' bits, either for higher layers of 'protocol' or + * - 18 'protocol' bits and 2 vericode sidechannel bits + +\*---------------------------------------------------------------------------*/ + +/* + Copyright (C) 2016 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program 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 Lesser General Public License + along with this program; if not, see . +*/ + + +#include +#include +#include +#include +#include +#include "freedv_vhf_framing.h" + +namespace FreeDV +{ + +/* The voice UW of the VHF type A frame */ +static const uint8_t A_uw_v[] = {0,1,1,0,0,1,1,1, + 1,0,1,0,1,1,0,1}; + +/* The data UW of the VHF type A frame */ +static const uint8_t A_uw_d[] = {1,1,1,1,0,0,0,1, + 1,1,1,1,1,1,0,0}; + +/* Blank VHF type A frame */ +static const uint8_t A_blank[] = {1,0,1,0,0,1,1,1, /* Padding[0:3] Proto[0:3] */ + 1,0,1,0,0,1,1,1, /* Proto[4:11] */ + 0,0,0,0,0,0,0,0, /* Voice[0:7] */ + 0,0,0,0,0,0,0,0, /* Voice[8:15] */ + 0,0,0,0,0,0,0,0, /* Voice[16:23] */ + 0,1,1,0,0,1,1,1, /* UW[0:7] */ + 1,0,1,0,1,1,0,1, /* UW[8:15] */ + 0,0,0,0,0,0,0,0, /* Voice[24:31] */ + 0,0,0,0,0,0,0,0, /* Voice[32:39] */ + 0,0,0,0,0,0,0,0, /* Voice[40:47] */ + 0,0,0,0,0,0,1,0, /* Voice[48:51] Proto[12:15] */ + 0,1,1,1,0,0,1,0};/* Proto[16:19] Padding[4:7] */ + +/* Blank VHF type AT (A for TDMA; padding bits not transmitted) frame */ +static const uint8_t AT_blank[] = { 0,1,1,1, /* Proto[0:3] */ + 1,0,1,0,0,1,1,1, /* Proto[4:11] */ + 0,0,0,0,0,0,0,0, /* Voice[0:7] */ + 0,0,0,0,0,0,0,0, /* Voice[8:15] */ + 0,0,0,0,0,0,0,0, /* Voice[16:23] */ + 0,1,1,0,0,1,1,1, /* UW[0:7] */ + 1,0,1,0,1,1,0,1, /* UW[8:15] */ + 0,0,0,0,0,0,0,0, /* Voice[24:31] */ + 0,0,0,0,0,0,0,0, /* Voice[32:39] */ + 0,0,0,0,0,0,0,0, /* Voice[40:47] */ + 0,0,0,0,0,0,1,0, /* Voice[48:51] Proto[12:15] */ + 0,1,1,1 };/* Proto[16:19] */ + +/* HF Type B voice UW */ +static const uint8_t B_uw_v[] = {0,1,1,0,0,1,1,1}; + +/* HF Type B data UW */ +static const uint8_t B_uw_d[] = {1,1,1,1,0,0,1,0}; + +/* Blank HF type B frame */ +static const uint8_t B_blank[] = {0,1,1,0,0,1,1,1, /* UW[0:7] */ + 0,0,0,0,0,0,0,0, /* Voice1[0:7] */ + 0,0,0,0,0,0,0,0, /* Voice1[8:15] */ + 0,0,0,0,0,0,0,0, /* Voice1[16:23] */ + 0,0,0,0,0,0,0,0, /* Voice1[24:28] Voice2[0:3] */ + 0,0,0,0,0,0,0,0, /* Voice2[4:11] */ + 0,0,0,0,0,0,0,0, /* Voice2[12:19] */ + 0,0,0,0,0,0,0,0};/* Voice2[20:28] */ + +/* States */ +#define ST_NOSYNC 0 /* Not synchronized */ +#define ST_SYNC 1 /* Synchronized */ + +/* Get a single bit out of an MSB-first packed byte array */ +#define UNPACK_BIT_MSBFIRST(bytes,bitidx) ((bytes)[(bitidx)>>3]>>(7-((bitidx)&0x7)))&0x1 + +enum frame_payload_type { + FRAME_PAYLOAD_TYPE_VOICE, + FRAME_PAYLOAD_TYPE_DATA, +}; + +/* Place codec and other bits into a frame */ +void fvhff_frame_bits( int frame_type, + uint8_t bits_out[], + uint8_t codec2_in[], + uint8_t proto_in[], + uint8_t vc_in[]){ + int i,ibit; + if(frame_type == FREEDV_VHF_FRAME_A){ + /* Fill out frame with blank frame prototype */ + for(i=0; i<96; i++) + bits_out[i] = A_blank[i]; + + /* Fill in protocol bits, if present */ + if(proto_in!=NULL){ + ibit = 0; + /* First half of protocol bits */ + /* Extract and place in frame, MSB first */ + for(i=4 ; i<16; i++){ + bits_out[i] = UNPACK_BIT_MSBFIRST(proto_in,ibit); + ibit++; + } + /* Last set of protocol bits */ + for(i=84; i<92; i++){ + bits_out[i] = UNPACK_BIT_MSBFIRST(proto_in,ibit); + ibit++; + } + } + + /* Fill in varicode bits, if present */ + if(vc_in!=NULL){ + bits_out[90] = vc_in[0]; + bits_out[91] = vc_in[1]; + } + + /* Fill in codec2 bits, present or not */ + ibit = 0; + for(i=16; i<40; i++){ /* First half */ + bits_out[i] = UNPACK_BIT_MSBFIRST(codec2_in,ibit); + ibit++; + } + for(i=56; i<84; i++){ /* Second half */ + bits_out[i] = UNPACK_BIT_MSBFIRST(codec2_in,ibit); + ibit++; + } + }else if(frame_type == FREEDV_HF_FRAME_B){ + /* Pointers to both c2 frames so the bit unpack macro works */ + uint8_t * codec2_in1 = &codec2_in[0]; + uint8_t * codec2_in2 = &codec2_in[4]; + /* Fill out frame with blank prototype */ + for(i=0; i<64; i++) + bits_out[i] = B_blank[i]; + + /* Fill out first codec2 block */ + ibit=0; + for(i=8; i<36; i++){ + bits_out[i] = UNPACK_BIT_MSBFIRST(codec2_in1,ibit); + ibit++; + } + /* Fill out second codec2 block */ + ibit=0; + for(i=36; i<64; i++){ + bits_out[i] = UNPACK_BIT_MSBFIRST(codec2_in2,ibit); + ibit++; + } + }else if(frame_type == FREEDV_VHF_FRAME_AT){ + /* Fill out frame with blank frame prototype */ + for(i=0; i<88; i++) + bits_out[i] = AT_blank[i]; + + /* Fill in protocol bits, if present */ + if(proto_in!=NULL){ + ibit = 0; + /* First half of protocol bits */ + /* Extract and place in frame, MSB first */ + for(i=0 ; i<12; i++){ + bits_out[i] = UNPACK_BIT_MSBFIRST(proto_in,ibit); + ibit++; + } + /* Last set of protocol bits */ + for(i=80; i<88; i++){ + bits_out[i] = UNPACK_BIT_MSBFIRST(proto_in,ibit); + ibit++; + } + } + + /* Fill in varicode bits, if present */ + if(vc_in!=NULL){ + bits_out[86] = vc_in[0]; + bits_out[87] = vc_in[1]; + } + + /* Fill in codec2 bits, present or not */ + ibit = 0; + for(i=12; i<36; i++){ /* First half */ + bits_out[i] = UNPACK_BIT_MSBFIRST(codec2_in,ibit); + ibit++; + } + for(i=52; i<80; i++){ /* Second half */ + bits_out[i] = UNPACK_BIT_MSBFIRST(codec2_in,ibit); + ibit++; + } + } +} + +/* Place data and other bits into a frame */ +void fvhff_frame_data_bits(struct freedv_vhf_deframer * def, int frame_type, + uint8_t bits_out[]){ + int i,ibit; + if(frame_type == FREEDV_VHF_FRAME_A){ + uint8_t data[8]; + int end_bits; + int from_bit; + int bcast_bit; + int crc_bit; + + /* Fill out frame with blank frame prototype */ + for(i=0; i<4; i++) + bits_out[i] = A_blank[i]; + for(i=92; i<96; i++) + bits_out[i] = A_blank[i]; + + /* UW data */ + for (i=0; i < 16; i++) + bits_out[40 + i] = A_uw_d[i]; + + if (def->fdc) + freedv_data_channel_tx_frame(def->fdc, data, 8, &from_bit, &bcast_bit, &crc_bit, &end_bits); + else + return; + + bits_out[4] = from_bit; + bits_out[5] = bcast_bit; + bits_out[6] = 0; /* unused */ + bits_out[7] = 0; /* unused */ + + /* Fill in data bits */ + ibit = 0; + for(i=8; i<40; i++){ /* First half */ + bits_out[i] = UNPACK_BIT_MSBFIRST(data,ibit); + ibit++; + } + for(i=56; i<88; i++){ /* Second half */ + bits_out[i] = UNPACK_BIT_MSBFIRST(data,ibit); + ibit++; + } + + for (i = 0; i < 4; i++) + bits_out[88 + i] = (end_bits >> (3-i)) & 0x1; + } else if (frame_type == FREEDV_HF_FRAME_B){ + uint8_t data[6]; + int end_bits; + int from_bit; + int bcast_bit; + int crc_bit; + + /* Fill out frame with blank prototype */ + for(i=0; i<64; i++) + bits_out[i] = B_blank[i]; + + /* UW data */ + for (i=0; i < 8; i++) + bits_out[0 + i] = B_uw_d[i]; + + if (def->fdc) + freedv_data_channel_tx_frame(def->fdc, data, 6, &from_bit, &bcast_bit, &crc_bit, &end_bits); + else + return; + + bits_out[56] = from_bit; + bits_out[57] = bcast_bit; + bits_out[58] = crc_bit; + bits_out[59] = 0; /* unused */ + + /* Fill in data bits */ + ibit = 0; + for(i=8; i<56; i++){ /* First half */ + bits_out[i] = UNPACK_BIT_MSBFIRST(data,ibit); + ibit++; + } + for (i = 0; i < 4; i++) + bits_out[60 + i] = (end_bits >> (3-i)) & 0x1; + } +} + +/* Init and allocate memory for a freedv-vhf framer/deframer */ +struct freedv_vhf_deframer * fvhff_create_deframer(uint8_t frame_type, int enable_bit_flip){ + struct freedv_vhf_deframer * deframer; + uint8_t *bits,*invbits; + int frame_size; + int uw_size; + + assert( (frame_type == FREEDV_VHF_FRAME_A) || (frame_type == FREEDV_HF_FRAME_B) ); + + /* It's a Type A frame */ + if(frame_type == FREEDV_VHF_FRAME_A){ + frame_size = 96; + uw_size = 16; + }else if(frame_type == FREEDV_HF_FRAME_B){ + frame_size = 64; + uw_size = 8; + }else{ + return NULL; + } + + /* Allocate memory for the thing */ + deframer = (freedv_vhf_deframer*) malloc(sizeof(struct freedv_vhf_deframer)); + if(deframer == NULL) + return NULL; + + /* Allocate the not-bit buffer */ + if(enable_bit_flip){ + invbits = (uint8_t*) malloc(sizeof(uint8_t)*frame_size); + if(invbits == NULL) { + free(deframer); + return NULL; + } + }else{ + invbits = NULL; + } + + /* Allocate the bit buffer */ + bits = (uint8_t*) malloc(sizeof(uint8_t)*frame_size); + if(bits == NULL) { + free(deframer); + return NULL; + } + + deframer->bits = bits; + deframer->invbits = invbits; + deframer->ftype = frame_type; + deframer->state = ST_NOSYNC; + deframer->bitptr = 0; + deframer->last_uw = 0; + deframer->miss_cnt = 0; + deframer->frame_size = frame_size; + deframer->uw_size = uw_size; + deframer->on_inv_bits = 0; + deframer->sym_size = 1; + + deframer->ber_est = 0; + deframer->total_uw_bits = 0; + deframer->total_uw_err = 0; + + deframer->fdc = NULL; + + return deframer; +} + +/* Get size of frame in bits */ +int fvhff_get_frame_size(struct freedv_vhf_deframer * def){ + return def->frame_size; +} + +/* Codec2 size in bytes */ +int fvhff_get_codec2_size(struct freedv_vhf_deframer * def){ + if(def->ftype == FREEDV_VHF_FRAME_A){ + return 7; + } else if(def->ftype == FREEDV_HF_FRAME_B){ + return 8; + } else{ + return 0; + } +} + +/* Protocol bits in bits */ +int fvhff_get_proto_size(struct freedv_vhf_deframer * def){ + if(def->ftype == FREEDV_VHF_FRAME_A){ + return 20; + } else if(def->ftype == FREEDV_HF_FRAME_B){ + return 0; + } else{ + return 0; + } +} + +/* Varicode bits in bits */ +int fvhff_get_varicode_size(struct freedv_vhf_deframer * def){ + if(def->ftype == FREEDV_VHF_FRAME_A){ + return 2; + } else if(def->ftype == FREEDV_HF_FRAME_B){ + return 0; + } else{ + return 0; + } +} + +void fvhff_destroy_deframer(struct freedv_vhf_deframer * def){ + freedv_data_channel_destroy(def->fdc); + free(def->bits); + free(def); +} + +int fvhff_synchronized(struct freedv_vhf_deframer * def){ + return (def->state) == ST_SYNC; +} + +/* Search for a complete UW in a buffer of bits */ +std::size_t fvhff_search_uw(const uint8_t bits[], std::size_t nbits, + const uint8_t uw[], std::size_t uw_len, + std::size_t * delta_out, std::size_t bits_per_sym){ + + std::size_t ibits,iuw; + std::size_t delta_min = uw_len; + std::size_t delta; + std::size_t offset_min = 0; + /* Walk through buffer bits */ + for(ibits = 0; ibits < nbits-uw_len; ibits+=bits_per_sym){ + delta = 0; + for(iuw = 0; iuw < uw_len; iuw++){ + if(bits[ibits+iuw] != uw[iuw]) delta++; + } + if( delta < delta_min ){ + delta_min = delta; + offset_min = ibits; + } + } + if(delta_out != NULL) *delta_out = delta_min; + return offset_min; +} + +/* See if the UW is where it should be, to within a tolerance, in a bit buffer */ +static int fvhff_match_uw(struct freedv_vhf_deframer * def,uint8_t bits[],int tol,int *rdiff, enum frame_payload_type *pt){ + int frame_type = def->ftype; + int bitptr = def->bitptr; + int frame_size = def->frame_size; + int uw_len = def->uw_size; + int iuw,ibit; + const uint8_t * uw[2]; + int uw_offset; + int diff[2] = { 0, 0 }; + int i; + int match[2]; + int r; + + /* defaults to make compiler happy on -O3 */ + + *pt = FRAME_PAYLOAD_TYPE_VOICE; + *rdiff = 0; + + /* Set up parameters for the standard type of frame */ + if(frame_type == FREEDV_VHF_FRAME_A){ + uw[0] = A_uw_v; + uw[1] = A_uw_d; + uw_len = 16; + uw_offset = 40; + } else if(frame_type == FREEDV_HF_FRAME_B){ + uw[0] = B_uw_v; + uw[1] = B_uw_d; + uw_len = 8; + uw_offset = 0; + } else { + return 0; + } + + /* Check both the voice and data UWs */ + for (i = 0; i < 2; i++) { + /* Start bit pointer where UW should be */ + ibit = bitptr + uw_offset; + if(ibit >= frame_size) ibit -= frame_size; + /* Walk through and match bits in frame with bits of UW */ + for(iuw=0; iuw= frame_size) ibit = 0; + } + match[i] = diff[i] <= tol; + } + /* Pick the best matching UW */ + + if (diff[0] < diff[1]) { + r = match[0]; + *rdiff = diff[0]; + *pt = FRAME_PAYLOAD_TYPE_VOICE; + } else { + r = match[1]; + *rdiff = diff[1]; + *pt = FRAME_PAYLOAD_TYPE_DATA; + } + + return r; +} + +static void fvhff_extract_frame_voice(struct freedv_vhf_deframer * def,uint8_t bits[], + uint8_t codec2_out[],uint8_t proto_out[],uint8_t vc_out[]){ + int frame_type = def->ftype; + int bitptr = def->bitptr; + int frame_size = def->frame_size; + int iframe,ibit; + + if(frame_type == FREEDV_VHF_FRAME_A){ + /* Extract codec2 bits */ + memset(codec2_out,0,7); + ibit = 0; + /* Extract and pack first half, MSB first */ + iframe = bitptr+16; + if(iframe >= frame_size) iframe-=frame_size; + for(;ibit<24;ibit++){ + codec2_out[ibit>>3] |= (bits[iframe]&0x1)<<(7-(ibit&0x7)); + iframe++; + if(iframe >= frame_size) iframe=0; + } + + /* Extract and pack last half, MSB first */ + iframe = bitptr+56; + if(iframe >= frame_size) iframe-=frame_size; + for(;ibit<52;ibit++){ + codec2_out[ibit>>3] |= (bits[iframe]&0x1)<<(7-(ibit&0x7)); + iframe++; + if(iframe >= frame_size) iframe=0; + } + /* Extract varicode bits, if wanted */ + if(vc_out!=NULL){ + iframe = bitptr+90; + if(iframe >= frame_size) iframe-=frame_size; + vc_out[0] = bits[iframe]; + iframe++; + vc_out[1] = bits[iframe]; + } + /* Extract protocol bits, if proto is passed through */ + if(proto_out!=NULL){ + /* Clear protocol bit array */ + memset(proto_out,0,3); + ibit = 0; + /* Extract and pack first half, MSB first */ + iframe = bitptr+4; + if(iframe >= frame_size) iframe-=frame_size; + for(;ibit<12;ibit++){ + proto_out[ibit>>3] |= (bits[iframe]&0x1)<<(7-(ibit&0x7)); + iframe++; + if(iframe >= frame_size) iframe=0; + } + + /* Extract and pack last half, MSB first */ + iframe = bitptr+84; + if(iframe >= frame_size) iframe-=frame_size; + for(;ibit<20;ibit++){ + proto_out[ibit>>3] |= (bits[iframe]&0x1)<<(7-(ibit&0x7)); + iframe++; + if(iframe >= frame_size) iframe=0; + } + } + + }else if(frame_type == FREEDV_HF_FRAME_B){ + /* Pointers to both c2 frames */ + uint8_t * codec2_out1 = &codec2_out[0]; + uint8_t * codec2_out2 = &codec2_out[4]; + + /* Extract codec2 bits */ + memset(codec2_out,0,8); + ibit = 0; + + /* Extract and pack first c2 frame, MSB first */ + iframe = bitptr+8; + if(iframe >= frame_size) iframe-=frame_size; + for(;ibit<28;ibit++){ + codec2_out1[ibit>>3] |= (bits[iframe]&0x1)<<(7-(ibit&0x7)); + iframe++; + if(iframe >= frame_size) iframe=0; + } + + /* Extract and pack second c2 frame, MSB first */ + iframe = bitptr+36; + ibit = 0; + if(iframe >= frame_size) iframe-=frame_size; + for(;ibit<28;ibit++){ + codec2_out2[ibit>>3] |= (bits[iframe]&0x1)<<(7-(ibit&0x7)); + iframe++; + if(iframe >= frame_size) iframe=0; + } + }else if(frame_type == FREEDV_VHF_FRAME_AT){ + /* Extract codec2 bits */ + memset(codec2_out,0,7); + ibit = 0; + /* Extract and pack first half, MSB first */ + iframe = bitptr+12; + if(iframe >= frame_size) iframe-=frame_size; + for(;ibit<24;ibit++){ + codec2_out[ibit>>3] |= (bits[iframe]&0x1)<<(7-(ibit&0x7)); + iframe++; + if(iframe >= frame_size) iframe=0; + } + + /* Extract and pack last half, MSB first */ + iframe = bitptr+52; + if(iframe >= frame_size) iframe-=frame_size; + for(;ibit<52;ibit++){ + codec2_out[ibit>>3] |= (bits[iframe]&0x1)<<(7-(ibit&0x7)); + iframe++; + if(iframe >= frame_size) iframe=0; + } + /* Extract varicode bits, if wanted */ + if(vc_out!=NULL){ + iframe = bitptr+86; + if(iframe >= frame_size) iframe-=frame_size; + vc_out[0] = bits[iframe]; + iframe++; + vc_out[1] = bits[iframe]; + } + /* Extract protocol bits, if proto is passed through */ + if(proto_out!=NULL){ + /* Clear protocol bit array */ + memset(proto_out,0,3); + ibit = 0; + /* Extract and pack first half, MSB first */ + iframe = bitptr+4; + if(iframe >= frame_size) iframe-=frame_size; + for(;ibit<12;ibit++){ + proto_out[ibit>>3] |= (bits[iframe]&0x1)<<(7-(ibit&0x7)); + iframe++; + if(iframe >= frame_size) iframe=0; + } + + /* Extract and pack last half, MSB first */ + iframe = bitptr+84; + if(iframe >= frame_size) iframe-=frame_size; + for(;ibit<20;ibit++){ + proto_out[ibit>>3] |= (bits[iframe]&0x1)<<(7-(ibit&0x7)); + iframe++; + if(iframe >= frame_size) iframe=0; + } + } + + } +} + +static void fvhff_extract_frame_data(struct freedv_vhf_deframer * def,uint8_t bits[]){ + int frame_type = def->ftype; + int bitptr = def->bitptr; + int frame_size = def->frame_size; + int iframe,ibit; + + if(frame_type == FREEDV_VHF_FRAME_A){ + uint8_t data[8]; + int end_bits = 0; + int from_bit; + int bcast_bit; + + iframe = bitptr+4; + if(iframe >= frame_size) iframe-=frame_size; + from_bit = bits[iframe]; + iframe++; + if(iframe >= frame_size) iframe-=frame_size; + bcast_bit = bits[iframe]; + + /* Extract data bits */ + memset(data,0,8); + ibit = 0; + /* Extract and pack first half, MSB first */ + iframe = bitptr+8; + if(iframe >= frame_size) iframe-=frame_size; + for(;ibit<32;ibit++){ + data[ibit>>3] |= (bits[iframe]&0x1)<<(7-(ibit&0x7)); + iframe++; + if(iframe >= frame_size) iframe=0; + } + + /* Extract and pack last half, MSB first */ + iframe = bitptr+56; + if(iframe >= frame_size) iframe-=frame_size; + for(;ibit<64;ibit++){ + data[ibit>>3] |= (bits[iframe]&0x1)<<(7-(ibit&0x7)); + iframe++; + if(iframe >= frame_size) iframe=0; + } + + /* Extract endbits value, MSB first*/ + iframe = bitptr+88; + ibit = 0; + if(iframe >= frame_size) iframe-=frame_size; + for(;ibit<4;ibit++){ + end_bits |= (bits[iframe]&0x1)<<(3-(ibit)); + iframe++; + if(iframe >= frame_size) iframe=0; + } + + if (def->fdc) { + freedv_data_channel_rx_frame(def->fdc, data, 8, from_bit, bcast_bit, 0, end_bits); + } + } else if(frame_type == FREEDV_HF_FRAME_B){ + uint8_t data[6]; + int end_bits = 0; + int from_bit; + int bcast_bit; + int crc_bit; + + ibit = 0; + memset(data,0,6); + + /* Extract and pack first c2 frame, MSB first */ + iframe = bitptr+8; + if(iframe >= frame_size) iframe-=frame_size; + for(;ibit<48;ibit++){ + data[ibit>>3] |= (bits[iframe]&0x1)<<(7-(ibit&0x7)); + iframe++; + if(iframe >= frame_size) iframe=0; + } + + iframe = bitptr+56; + if(iframe >= frame_size) iframe-=frame_size; + from_bit = bits[iframe]; + iframe++; + if(iframe >= frame_size) iframe-=frame_size; + bcast_bit = bits[iframe]; + iframe++; + if(iframe >= frame_size) iframe-=frame_size; + crc_bit = bits[iframe]; + + /* Extract endbits value, MSB first*/ + iframe = bitptr+60; + ibit = 0; + if(iframe >= frame_size) iframe-=frame_size; + for(;ibit<4;ibit++){ + end_bits |= (bits[iframe]&0x1)<<(3-(ibit)); + iframe++; + if(iframe >= frame_size) iframe=0; + } + + if (def->fdc) { + freedv_data_channel_rx_frame(def->fdc, data, 6, from_bit, bcast_bit, crc_bit, end_bits); + } + } +} + +static void fvhff_extract_frame(struct freedv_vhf_deframer * def,uint8_t bits[],uint8_t codec2_out[], + uint8_t proto_out[],uint8_t vc_out[],enum frame_payload_type pt){ + switch (pt) { + case FRAME_PAYLOAD_TYPE_VOICE: + fvhff_extract_frame_voice(def, bits, codec2_out, proto_out, vc_out); + break; + case FRAME_PAYLOAD_TYPE_DATA: + fvhff_extract_frame_data(def, bits); + break; + } +} + +/* + * Try to find the UW and extract codec/proto/vc bits in def->frame_size bits + */ +int fvhff_deframe_bits(struct freedv_vhf_deframer * def,uint8_t codec2_out[],uint8_t proto_out[], + uint8_t vc_out[],uint8_t bits_in[]){ + uint8_t * strbits = def->bits; + uint8_t * invbits = def->invbits; + uint8_t * bits; + int on_inv_bits = def->on_inv_bits; + int frame_type = def->ftype; + int state = def->state; + int bitptr = def->bitptr; + int last_uw = def->last_uw; + int miss_cnt = def->miss_cnt; + int frame_size = def->frame_size; + int uw_size = def->uw_size; + int uw_diff; + int i; + int uw_first_tol; + int uw_sync_tol; + int miss_tol; + int extracted_frame = 0; + enum frame_payload_type pt = FRAME_PAYLOAD_TYPE_VOICE; + + /* Possibly set up frame-specific params here */ + if(frame_type == FREEDV_VHF_FRAME_A){ + uw_first_tol = 1; /* The UW bit-error tolerance for the first frame */ + uw_sync_tol = 3; /* The UW bit error tolerance for frames after sync */ + miss_tol = 4; /* How many UWs may be missed before going into the de-synced state */ + }else if(frame_type == FREEDV_HF_FRAME_B){ + uw_first_tol = 0; /* The UW bit-error tolerance for the first frame */ + uw_sync_tol = 1; /* The UW bit error tolerance for frames after sync */ + miss_tol = 3; /* How many UWs may be missed before going into the de-synced state */ + }else{ + return 0; + } + /* Skip N bits for multi-bit symbol modems */ + for(i=0; i= frame_size) bitptr -= frame_size; + def->bitptr = bitptr; + /* Enter state machine */ + if(state==ST_SYNC){ + /* Already synchronized, just wait till UW is back where it should be */ + last_uw++; + if(invbits!=NULL){ + if(on_inv_bits) + bits = invbits; + else + bits = strbits; + }else{ + bits=strbits; + } + /* UW should be here. We're sunk, so deframe anyway */ + if(last_uw == frame_size){ + last_uw = 0; + + if(!fvhff_match_uw(def,bits,uw_sync_tol,&uw_diff, &pt)) + miss_cnt++; + else + miss_cnt=0; + + /* If we go over the miss tolerance, go into no-sync */ + if(miss_cnt>miss_tol){ + state = ST_NOSYNC; + } + /* Extract the bits */ + extracted_frame = 1; + fvhff_extract_frame(def,bits,codec2_out,proto_out,vc_out,pt); + + /* Update BER estimate */ + def->ber_est = (.995*def->ber_est) + (.005*((float)uw_diff)/((float)uw_size)); + def->total_uw_bits += uw_size; + def->total_uw_err += uw_diff; + } + /* Not yet sunk */ + }else{ + /* It's a sync!*/ + if(invbits!=NULL){ + if(fvhff_match_uw(def,invbits,uw_first_tol, &uw_diff, &pt)){ + state = ST_SYNC; + last_uw = 0; + miss_cnt = 0; + extracted_frame = 1; + on_inv_bits = 1; + fvhff_extract_frame(def,invbits,codec2_out,proto_out,vc_out,pt); + /* Update BER estimate */ + def->ber_est = (.995*def->ber_est) + (.005*((float)uw_diff)/((float)uw_size)); + def->total_uw_bits += uw_size; + def->total_uw_err += uw_diff; + } + } + if(fvhff_match_uw(def,strbits,uw_first_tol, &uw_diff, &pt)){ + state = ST_SYNC; + last_uw = 0; + miss_cnt = 0; + extracted_frame = 1; + on_inv_bits = 0; + fvhff_extract_frame(def,strbits,codec2_out,proto_out,vc_out,pt); + /* Update BER estimate */ + def->ber_est = (.995*def->ber_est) + (.005*((float)uw_diff)/((float)uw_size)); + def->total_uw_bits += uw_size; + def->total_uw_err += uw_diff; + } + } + } + def->state = state; + def->last_uw = last_uw; + def->miss_cnt = miss_cnt; + def->on_inv_bits = on_inv_bits; + /* return zero for data frames, they are already handled by callback */ + return extracted_frame && pt == FRAME_PAYLOAD_TYPE_VOICE; +} + +} // FreeDV diff --git a/libfreedv/fsk.cpp b/libfreedv/fsk.cpp new file mode 100644 index 000000000..043d55961 --- /dev/null +++ b/libfreedv/fsk.cpp @@ -0,0 +1,1251 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: fsk.c + AUTHOR......: Brady O'Brien + DATE CREATED: 7 January 2016 + + C Implementation of 2/4FSK modulator/demodulator, based on octave/fsk_horus.m + +\*---------------------------------------------------------------------------*/ + +/* + Copyright (C) 2016 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program 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 Lesser General Public License + along with this program; if not, see . +*/ + +/*---------------------------------------------------------------------------*\ + + DEFINES + +\*---------------------------------------------------------------------------*/ + +/* P oversampling rate constant -- should probably be init-time configurable */ +#define horus_P 8 + +/* Define this to enable EbNodB estimate */ +/* This needs square roots, may take more cpu time than it's worth */ +#define EST_EBNO + +/* This is a flag for the freq. estimator to use a precomputed/rt computed hann window table + On platforms with slow cosf, this will produce a substantial speedup at the cost of a small + amount of memory +*/ +#define USE_HANN_TABLE + +/* This flag turns on run-time hann table generation. If USE_HANN_TABLE is unset, + this flag has no effect. If USE_HANN_TABLE is set and this flag is set, the + hann table will be allocated and generated when fsk_init or fsk_init_hbr is + called. If this flag is not set, a hann function table of size fsk->Ndft MUST + be provided. On small platforms, this can be used with a precomputed table to + save memory at the cost of flash space. +*/ +#define GENERATE_HANN_TABLE_RUNTIME + +/* Turn off table generation if on cortex M4 to save memory */ +#ifdef CORTEX_M4 +#undef USE_HANN_TABLE +#endif + +/*---------------------------------------------------------------------------*\ + + INCLUDES + +\*---------------------------------------------------------------------------*/ + +#include +#include +#include +#include + +#include "fsk.h" +#include "codec2/comp_prim.h" +#include "kiss_fftr.h" +#include "modem_probe.h" + +namespace FreeDV +{ + +/*---------------------------------------------------------------------------*\ + + FUNCTIONS + +\*---------------------------------------------------------------------------*/ + +static void stats_init(struct FSK *fsk); + +#ifdef USE_HANN_TABLE +/* + This is used by fsk_create and fsk_create_hbr to generate a hann function + table +*/ +static void fsk_generate_hann_table(struct FSK* fsk){ + int Ndft = fsk->Ndft; + int i; + + /* Set up complex oscilator to calculate hann function */ + COMP dphi = comp_exp_j((2*M_PI)/((float)Ndft-1)); + COMP rphi = {.5,0}; + + rphi = cmult(cconj(dphi),rphi); + + for (i = 0; i < Ndft; i++) + { + rphi = cmult(dphi,rphi); + float hannc = .5-rphi.real; + //float hann = .5-(.5*cosf((2*M_PI*(float)(i))/((float)Ndft-1))); + fsk->hann_table[i] = hannc; + } +} +#endif + + + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: fsk_create_hbr + AUTHOR......: Brady O'Brien + DATE CREATED: 11 February 2016 + + Create and initialize an instance of the FSK modem. Returns a pointer + to the modem state/config struct. One modem config struct may be used + for both mod and demod. returns NULL on failure. + +\*---------------------------------------------------------------------------*/ + +struct FSK * fsk_create_hbr(int Fs, int Rs,int P,int M, int tx_f1, int tx_fs) +{ + struct FSK *fsk; + int i; + int memold; + int Ndft = 0; + /* Number of symbols in a processing frame */ + int nsyms = 48; + /* Check configuration validity */ + assert(Fs > 0 ); + assert(Rs > 0 ); + assert(tx_f1 > 0); + assert(tx_fs > 0); + assert(P > 0); + /* Ts (Fs/Rs) must be an integer */ + assert( (Fs%Rs) == 0 ); + /* Ts/P (Fs/Rs/P) must be an integer */ + assert( ((Fs/Rs)%P) == 0 ); + assert( M==2 || M==4); + + fsk = (struct FSK*) malloc(sizeof(struct FSK)); + if(fsk == NULL) return NULL; + + + /* Set constant config parameters */ + fsk->Fs = Fs; + fsk->Rs = Rs; + fsk->Ts = Fs/Rs; + fsk->burst_mode = 0; + fsk->N = fsk->Ts*nsyms; + fsk->P = P; + fsk->Nsym = nsyms; + fsk->Nmem = fsk->N+(2*fsk->Ts); + fsk->f1_tx = tx_f1; + fsk->fs_tx = tx_fs; + fsk->nin = fsk->N; + fsk->mode = M==2 ? MODE_2FSK : MODE_4FSK; + fsk->Nbits = M==2 ? fsk->Nsym : fsk->Nsym*2; + + /* Find smallest 2^N value that fits Fs for efficient FFT */ + /* It would probably be better to use KISS-FFt's routine here */ + for(i=1; i; i<<=1) + if((fsk->N)&i) + Ndft = i; + + fsk->Ndft = Ndft; + + fsk->est_min = Rs/4; + if(fsk->est_min<0) fsk->est_min = 0; + + fsk->est_max = (Fs/2)-Rs/4; + + fsk->est_space = Rs-(Rs/5); + + /* Set up rx state */ + + for( i=0; iphi_c[i] = comp_exp_j(0); + + memold = (4*fsk->Ts); + + fsk->nstash = memold; + fsk->samp_old = (COMP*) malloc(sizeof(COMP)*memold); + if(fsk->samp_old == NULL){ + free(fsk); + return NULL; + } + + for(i=0;isamp_old[i].real = 0; + fsk->samp_old[i].imag = 0; + } + + fsk->fft_cfg = kiss_fft_alloc(fsk->Ndft,0,NULL,NULL); + if(fsk->fft_cfg == NULL){ + free(fsk->samp_old); + free(fsk); + return NULL; + } + + fsk->fft_est = (float*)malloc(sizeof(float)*fsk->Ndft/2); + if(fsk->fft_est == NULL){ + free(fsk->samp_old); + free(fsk->fft_cfg); + free(fsk); + return NULL; + } + + #ifdef USE_HANN_TABLE + #ifdef GENERATE_HANN_TABLE_RUNTIME + fsk->hann_table = (float*)malloc(sizeof(float)*fsk->Ndft); + if(fsk->hann_table == NULL){ + free(fsk->fft_est); + free(fsk->samp_old); + free(fsk->fft_cfg); + free(fsk); + return NULL; + } + fsk_generate_hann_table(fsk); + #else + fsk->hann_table = NULL; + #endif + #endif + + for(i=0;iNdft/2;i++)fsk->fft_est[i] = 0; + + fsk->norm_rx_timing = 0; + + /* Set up tx state */ + fsk->tx_phase_c = comp_exp_j(0); + + /* Set up demod stats */ + fsk->EbNodB = 0; + + for( i=0; if_est[i] = 0; + + fsk->ppm = 0; + + fsk->stats = (struct MODEM_STATS*)malloc(sizeof(struct MODEM_STATS)); + if(fsk->stats == NULL){ + free(fsk->fft_est); + free(fsk->samp_old); + free(fsk->fft_cfg); + free(fsk); + return NULL; + } + stats_init(fsk); + fsk->normalise_eye = 1; + + return fsk; +} + + +#define HORUS_MIN 800 +#define HORUS_MAX 2500 +#define HORUS_MIN_SPACING 100 + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: fsk_create + AUTHOR......: Brady O'Brien + DATE CREATED: 7 January 2016 + + Create and initialize an instance of the FSK modem. Returns a pointer + to the modem state/config struct. One modem config struct may be used + for both mod and demod. returns NULL on failure. + +\*---------------------------------------------------------------------------*/ + +struct FSK * fsk_create(int Fs, int Rs,int M, int tx_f1, int tx_fs) +{ + struct FSK *fsk; + int i; + int Ndft = 0; + int memold; + + /* Check configuration validity */ + assert(Fs > 0 ); + assert(Rs > 0 ); + assert(tx_f1 > 0); + assert(tx_fs > 0); + assert(horus_P > 0); + /* Ts (Fs/Rs) must be an integer */ + assert( (Fs%Rs) == 0 ); + /* Ts/P (Fs/Rs/P) must be an integer */ + assert( ((Fs/Rs)%horus_P) == 0 ); + assert( M==2 || M==4); + + fsk = (struct FSK*) malloc(sizeof(struct FSK)); + if(fsk == NULL) return NULL; + + Ndft = 1024; + + /* Set constant config parameters */ + fsk->Fs = Fs; + fsk->Rs = Rs; + fsk->Ts = Fs/Rs; + fsk->N = Fs; + fsk->burst_mode = 0; + fsk->P = horus_P; + fsk->Nsym = fsk->N/fsk->Ts; + fsk->Ndft = Ndft; + fsk->Nmem = fsk->N+(2*fsk->Ts); + fsk->f1_tx = tx_f1; + fsk->fs_tx = tx_fs; + fsk->nin = fsk->N; + fsk->mode = M==2 ? MODE_2FSK : MODE_4FSK; + fsk->Nbits = M==2 ? fsk->Nsym : fsk->Nsym*2; + fsk->est_min = HORUS_MIN; + fsk->est_max = HORUS_MAX; + fsk->est_space = HORUS_MIN_SPACING; + + /* Set up rx state */ + for( i=0; iphi_c[i] = comp_exp_j(0); + + memold = (4*fsk->Ts); + + fsk->nstash = memold; + fsk->samp_old = (COMP*) malloc(sizeof(COMP)*memold); + if(fsk->samp_old == NULL){ + free(fsk); + return NULL; + } + + for(i=0;isamp_old[i].real = 0.0; + fsk->samp_old[i].imag = 0.0; + } + + fsk->fft_cfg = kiss_fft_alloc(Ndft,0,NULL,NULL); + if(fsk->fft_cfg == NULL){ + free(fsk->samp_old); + free(fsk); + return NULL; + } + + fsk->fft_est = (float*)malloc(sizeof(float)*fsk->Ndft/2); + if(fsk->fft_est == NULL){ + free(fsk->samp_old); + free(fsk->fft_cfg); + free(fsk); + return NULL; + } + + #ifdef USE_HANN_TABLE + #ifdef GENERATE_HANN_TABLE_RUNTIME + fsk->hann_table = (float*)malloc(sizeof(float)*fsk->Ndft); + if(fsk->hann_table == NULL){ + free(fsk->fft_est); + free(fsk->samp_old); + free(fsk->fft_cfg); + free(fsk); + return NULL; + } + fsk_generate_hann_table(fsk); + #else + fsk->hann_table = NULL; + #endif + #endif + + for(i=0;ifft_est[i] = 0; + + fsk->norm_rx_timing = 0; + + /* Set up tx state */ + fsk->tx_phase_c = comp_exp_j(0); + + /* Set up demod stats */ + fsk->EbNodB = 0; + + for( i=0; if_est[i] = 0; + + fsk->ppm = 0; + + fsk->stats = (struct MODEM_STATS*)malloc(sizeof(struct MODEM_STATS)); + + if(fsk->stats == NULL){ + free(fsk->fft_est); + free(fsk->samp_old); + free(fsk->fft_cfg); + free(fsk); + return NULL; + } + stats_init(fsk); + fsk->normalise_eye = 1; + + return fsk; +} + +/* make sure stats have known values in case monitoring process reads stats before they are set */ + +static void stats_init(struct FSK *fsk) { + /* Take a sample for the eye diagrams */ + int i,j,m; + int P = fsk->P; + int M = fsk->mode; + + /* due to oversample rate P, we have too many samples for eye + trace. So lets output a decimated version */ + + /* asserts below as we found some problems over-running eye matrix */ + + /* TODO: refactor eye tracing code here and in fsk_demod */ + + int neyesamp_dec = ceil(((float)P*2)/MODEM_STATS_EYE_IND_MAX); + int neyesamp = (P*2)/neyesamp_dec; + assert(neyesamp <= MODEM_STATS_EYE_IND_MAX); + fsk->stats->neyesamp = neyesamp; + + int eye_traces = MODEM_STATS_ET_MAX/M; + + fsk->stats->neyetr = fsk->mode*eye_traces; + for(i=0; istats->rx_eye[i*M+m][j] = 0; + } + } + } + + fsk->stats->rx_timing = fsk->stats->snr_est = 0; + +} + + +void fsk_set_nsym(struct FSK *fsk,int nsyms){ + assert(nsyms>0); + int Ndft,i; + Ndft = 0; + + /* Set constant config parameters */ + fsk->N = fsk->Ts*nsyms; + fsk->Nsym = nsyms; + fsk->Nmem = fsk->N+(2*fsk->Ts); + fsk->nin = fsk->N; + fsk->Nbits = fsk->mode==2 ? fsk->Nsym : fsk->Nsym*2; + + /* Find smallest 2^N value that fits Fs for efficient FFT */ + /* It would probably be better to use KISS-FFt's routine here */ + for(i=1; i; i<<=1) + if((fsk->N)&i) + Ndft = i; + + fsk->Ndft = Ndft; + + free(fsk->fft_cfg); + free(fsk->fft_est); + + fsk->fft_cfg = kiss_fft_alloc(Ndft,0,NULL,NULL); + fsk->fft_est = (float*)malloc(sizeof(float)*fsk->Ndft/2); + + for(i=0;ifft_est[i] = 0; + +} + +/* Set the FSK modem into burst demod mode */ + +void fsk_enable_burst_mode(struct FSK *fsk,int nsyms){ + fsk_set_nsym(fsk,nsyms); + fsk->nin = fsk->N; + fsk->burst_mode = 1; +} + +void fsk_clear_estimators(struct FSK *fsk){ + int i; + /* Clear freq estimator state */ + for(i=0; i < (fsk->Ndft/2); i++){ + fsk->fft_est[i] = 0; + } + /* Reset timing diff correction */ + fsk->nin = fsk->N; +} + +uint32_t fsk_nin(struct FSK *fsk){ + return (uint32_t)fsk->nin; +} + +void fsk_destroy(struct FSK *fsk){ + free(fsk->fft_cfg); + free(fsk->samp_old); + free(fsk->stats); + free(fsk); +} + +void fsk_get_demod_stats(struct FSK *fsk, struct MODEM_STATS *stats){ + /* copy from internal stats, note we can't overwrite stats completely + as it has other states rqd by caller, also we want a consistent + interface across modem types for the freedv_api. + */ + + stats->clock_offset = fsk->stats->clock_offset; + stats->snr_est = fsk->stats->snr_est; // TODO: make this SNR not Eb/No + stats->rx_timing = fsk->stats->rx_timing; + stats->foff = fsk->stats->foff; + + stats->neyesamp = fsk->stats->neyesamp; + stats->neyetr = fsk->stats->neyetr; + memcpy(stats->rx_eye, fsk->stats->rx_eye, sizeof(stats->rx_eye)); + memcpy(stats->f_est, fsk->stats->f_est, fsk->mode*sizeof(float)); + + /* these fields not used for FSK so set to something sensible */ + + stats->sync = 0; + stats->nr = fsk->stats->nr; + stats->Nc = fsk->stats->Nc; +} + +/* + * Set the minimum and maximum frequencies at which the freq. estimator can find tones + */ +void fsk_set_est_limits(struct FSK *fsk,int est_min, int est_max){ + + fsk->est_min = est_min; + if(fsk->est_min<0) fsk->est_min = 0; + + fsk->est_max = est_max; +} + +/* + * Internal function to estimate the frequencies of the two tones within a block of samples. + * This is split off because it is fairly complicated, needs a bunch of memory, and probably + * takes more cycles than the rest of the demod. + * Parameters: + * fsk - FSK struct from demod containing FSK config + * fsk_in - block of samples in this demod cycles, must be nin long + * freqs - Array for the estimated frequencies + * M - number of frequency peaks to find + */ +void fsk_demod_freq_est(struct FSK *fsk, COMP fsk_in[],float *freqs,int M){ + int Ndft = fsk->Ndft; + int Fs = fsk->Fs; + int nin = fsk->nin; + int i,j; + float hann; + float max; + float tc; + int imax; + kiss_fft_cfg fft_cfg = fsk->fft_cfg; + int *freqi = new int[M]; + int f_min,f_max,f_zero; + + /* Array to do complex FFT from using kiss_fft */ + kiss_fft_cpx *fftin = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx)*Ndft); + kiss_fft_cpx *fftout = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx)*Ndft); + + #ifndef USE_HANN_TABLE + COMP dphi = comp_exp_j((2*M_PI)/((float)Ndft-1)); + COMP rphi = {.5,0}; + rphi = cmult(cconj(dphi),rphi); + #endif + + f_min = (fsk->est_min*Ndft)/Fs; + f_max = (fsk->est_max*Ndft)/Fs; + f_zero = (fsk->est_space*Ndft)/Fs; + + /* scale averaging time constant based on number of samples */ + tc = 0.95*Ndft/Fs; + + int samps; + int fft_samps; + int fft_loops = nin / Ndft; + + for (j = 0; j < fft_loops; j++) + { + /* 48000 sample rate (for example) will have a spare */ + /* 896 samples besides the 46 "Ndft" samples, so adjust */ + + samps = (nin - ((j + 1) * Ndft)); + fft_samps = (samps >= Ndft) ? Ndft : samps; + + /* Copy FSK buffer into reals of FFT buffer and apply a hann window */ + for(i=0; ihann_table[i]; + #else + //hann = 1-cosf((2*M_PI*(float)(i))/((float)fft_samps-1)); + rphi = cmult(dphi,rphi); + hann = .5-rphi.real; + #endif + fftin[i].r = hann*fsk_in[i+Ndft*j].real; + fftin[i].i = hann*fsk_in[i+Ndft*j].imag; + } + + /* Zero out the remaining slots on spare samples */ + for(; ifft_est[i] = (fsk->fft_est[i]*(1-tc)) + (sqrtf(fftout[i].r)*tc); + fftout[i].i = fsk->fft_est[i]; + } + } + + modem_probe_samp_f("t_fft_est", fsk->fft_est, Ndft/2); + + max = 0; + /* Find the M frequency peaks here */ + for(i=0; i max){ + max = fftout[j].i; + imax = j; + } + } + /* Blank out FMax +/-Fspace/2 */ + f_min = imax - f_zero; + f_min = f_min < 0 ? 0 : f_min; + f_max = imax + f_zero; + f_max = f_max > Ndft ? Ndft : f_max; + for(j=f_min; j= freqi[i-1]) i++; + else{ + j = freqi[i]; + freqi[i] = freqi[i-1]; + freqi[i-1] = j; + if(i>1) i--; + } + } + + /* Convert freqs from indices to frequencies */ + for(i=0; iN; + int Ts = fsk->Ts; + int Rs = fsk->Rs; + int Fs = fsk->Fs; + int nsym = fsk->Nsym; + int nin = fsk->nin; + int P = fsk->P; + int Nmem = fsk->Nmem; + int M = fsk->mode; + int i, j, m, dc_i, cbuf_i; + float ft1; + int nstash = fsk->nstash; + + COMP* *f_int = new COMP*[M]; /* Filtered and downsampled symbol tones */ + COMP *t = new COMP[M]; /* complex number temps */ + COMP t_c; /* another complex temp */ + COMP *phi_c = new COMP[M]; + COMP phi_ft; + int nold = Nmem-nin; + + COMP *dphi = new COMP[M]; + COMP dphift; + float rx_timing,norm_rx_timing,old_norm_rx_timing,d_norm_rx_timing,appm; + int using_old_samps; + + COMP* sample_src; + COMP* f_intbuf_m; + + float *f_est = new float[M]; + float fc_avg, fc_tx; + float meanebno,stdebno,eye_max; + int neyesamp,neyeoffset; + + #ifdef MODEMPROBE_ENABLE + char mp_name_tmp[20]; /* Temporary string for modem probe trace names */ + #endif + + //for(size_t jj = 0; jjphi_c[m]; + } + + /* Estimate tone frequencies */ + fsk_demod_freq_est(fsk,fsk_in,f_est,M); + modem_probe_samp_f("t_f_est",f_est,M); + + + /* Allocate circular buffer for integration */ + f_intbuf_m = (COMP*) malloc(sizeof(COMP)*Ts); + + /* allocate memory for the integrated samples */ + for( m=0; mf_est[0]<1){ + for( m=0; mf_est[m] = f_est[m]; + } + + /* Initalize downmixers for each symbol tone */ + for( m=0; mf_est[m])/(float)(Fs))); + phi_c[m] = cmult(dphi[m],phi_c[m]); + //fprintf(stderr,"F%d = %f",m,fsk->f_est[m]); + + /* Figure out how much to nudge each sample downmixer for every sample */ + dphi[m] = comp_exp_j(2*M_PI*((fsk->f_est[m])/(float)(Fs))); + } + + /* Integrate and downsample for symbol tones */ + for(m=0; msamp_old[nstash-nold]); + using_old_samps = 1; + + /* Pre-fill integration buffer */ + for(dc_i=0; dc_i=nold && using_old_samps){ + sample_src = &fsk_in[0]; + dc_i = 0; + using_old_samps = 0; + + /* Recalculate delta-phi after switching to new sample source */ + phi_c[m] = comp_normalize(phi_c[m]); + dphi_m = comp_exp_j(2*M_PI*((f_est_m)/(float)(Fs))); + } + /* Downconvert and place into integration buffer */ + f_intbuf_m[dc_i]=cmult(sample_src[dc_i],cconj(phi_c[m])); + + #ifdef MODEMPROBE_ENABLE + snprintf(mp_name_tmp,19,"t_f%zd_dc",m+1); + modem_probe_samp_c(mp_name_tmp,&f_intbuf_m[dc_i],1); + #endif + /* Spin downconversion phases */ + phi_c[m] = cmult(phi_c[m],dphi_m); + } + cbuf_i = dc_i; + + /* Integrate over Ts at offsets of Ts/P */ + for(i=0; i<(nsym+1)*P; i++){ + /* Downconvert and Place Ts/P samples in the integration buffers */ + for(j=0; j<(Ts/P); j++,dc_i++){ + /* Switch sample source to new samples when we run out of old ones */ + if(dc_i>=nold && using_old_samps){ + sample_src = &fsk_in[0]; + dc_i = 0; + using_old_samps = 0; + + /* Recalculate delta-phi after switching to new sample source */ + phi_c[m] = comp_normalize(phi_c[m]); + dphi_m = comp_exp_j(2*M_PI*((f_est_m)/(float)(Fs))); + } + /* Downconvert and place into integration buffer */ + f_intbuf_m[cbuf_i+j]=cmult(sample_src[dc_i],cconj(phi_c[m])); + + #ifdef MODEMPROBE_ENABLE + snprintf(mp_name_tmp,19,"t_f%zd_dc",m+1); + modem_probe_samp_c(mp_name_tmp,&f_intbuf_m[cbuf_i+j],1); + #endif + /* Spin downconversion phases */ + phi_c[m] = cmult(phi_c[m],dphi_m); + + } + + /* Dump internal samples */ + cbuf_i += Ts/P; + if(cbuf_i>=Ts) cbuf_i = 0; + + /* Integrate over the integration buffers, save samples */ + float it_r = 0; + float it_i = 0; + for(j=0; jphi_c[m] = phi_c[m]; + fsk->f_est[m] = f_est[m]; + } + + /* Stash samples away in the old sample buffer for the next round of bit getting */ + memcpy((void*)&(fsk->samp_old[0]),(void*)&(fsk_in[nin-nstash]),sizeof(COMP)*nstash); + + /* Fine Timing Estimation */ + /* Apply magic nonlinearity to f1_int and f2_int, shift down to 0, + * extract angle */ + + /* Figure out how much to spin the oscillator to extract magic spectral line */ + dphift = comp_exp_j(2*M_PI*((float)(Rs)/(float)(P*Rs))); + phi_ft.real = 1; + phi_ft.imag = 0; + t_c=comp0(); + for(i=0; i<(nsym+1)*P; i++){ + /* Get abs^2 of fx_int[i], and add 'em */ + ft1 = 0; + for( m=0; mnorm_rx_timing; + fsk->norm_rx_timing = norm_rx_timing; + + /* Estimate sample clock offset */ + d_norm_rx_timing = norm_rx_timing - old_norm_rx_timing; + + /* Filter out big jumps in due to nin change */ + if(fabsf(d_norm_rx_timing) < .2){ + appm = 1e6*d_norm_rx_timing/(float)nsym; + fsk->ppm = .9*fsk->ppm + .1*appm; + } + + /* Figure out how many samples are needed the next modem cycle */ + /* Unless we're in burst mode */ + if(!fsk->burst_mode){ + if(norm_rx_timing > 0.25) + fsk->nin = N+Ts/2; + else if(norm_rx_timing < -0.25) + fsk->nin = N-Ts/2; + else + fsk->nin = N; + } + + modem_probe_samp_f("t_norm_rx_timing",&(norm_rx_timing),1); + modem_probe_samp_i("t_nin",&(fsk->nin),1); + + /* Re-sample the integrators with linear interpolation magic */ + int low_sample = (int)floorf(rx_timing); + float fract = rx_timing - (float)low_sample; + int high_sample = (int)ceilf(rx_timing); + + /* Vars for finding the max-of-4 for each bit */ + float *tmax = new float[M]; + + #ifdef EST_EBNO + meanebno = 0; + stdebno = 0; + #endif + + /* FINALLY, THE BITS */ + /* also, resample fx_int */ + for(i = 0; i < nsym; i++) + { + int st = (i+1)*P; + for( m=0; mmax){ + max = tmax[m]; + sym = m; + } + if(tmax[m]>1; + } + } + + /* Produce soft decision symbols */ + if(rx_sd != NULL){ + /* Convert symbols from max^2 into max */ + for( m=0; m 0.0) { + stdebno = sqrt(stdebno); + } else { + stdebno = 0.0; + } + + fsk->EbNodB = -6+(20*log10f((1e-6+meanebno)/(1e-6+stdebno))); + #else + fsk->EbNodB = 1; + #endif + + /* Write some statistics to the stats struct */ + + /* Save clock offset in ppm */ + fsk->stats->clock_offset = fsk->ppm; + + /* Calculate and save SNR from EbNodB estimate */ + + fsk->stats->snr_est = .5*fsk->stats->snr_est + .5*fsk->EbNodB;//+ 10*log10f(((float)Rs)/((float)Rs*M)); + + /* Save rx timing */ + fsk->stats->rx_timing = (float)rx_timing; + + /* Estimate and save frequency offset */ + fc_avg = (f_est[0]+f_est[1])/2; + fc_tx = (fsk->f1_tx+fsk->f1_tx+fsk->fs_tx)/2; + fsk->stats->foff = fc_tx-fc_avg; + + /* Take a sample for the eye diagrams ---------------------------------- */ + + /* due to oversample rate P, we have too many samples for eye + trace. So lets output a decimated version. We use 2P + as we want two symbols worth of samples in trace */ + + int neyesamp_dec = ceil(((float)P*2)/MODEM_STATS_EYE_IND_MAX); + neyesamp = (P*2)/neyesamp_dec; + assert(neyesamp <= MODEM_STATS_EYE_IND_MAX); + fsk->stats->neyesamp = neyesamp; + + #ifdef I_DONT_UNDERSTAND + neyeoffset = high_sample+1+(P*28); /* WTF this line? Where does "28" come from ? */ + #endif /* ifdef-ed out as I am afraid it will index out of memory as P changes */ + neyeoffset = high_sample+1; + + int eye_traces = MODEM_STATS_ET_MAX/M; + int ind; + + fsk->stats->neyetr = fsk->mode*eye_traces; + for( i=0; iMODEM_STATS_EYE_IND_MAX advance through integrated + samples newamp_dec at a time so we dont overflow rx_eye[][] + */ + ind = 2*P*i + neyeoffset + j*neyesamp_dec; + assert((i*M+m) < MODEM_STATS_ET_MAX); + assert(ind < (nsym+1)*P); + fsk->stats->rx_eye[i*M+m][j] = cabsolute(f_int[m][ind]); + } + } + } + + if (fsk->normalise_eye) { + eye_max = 0; + /* Normalize eye to +/- 1 */ + for(i=0; istats->rx_eye[i][j])>eye_max) + eye_max = fabsf(fsk->stats->rx_eye[i][j]); + + for(i=0; istats->rx_eye[i][j] = fsk->stats->rx_eye[i][j]/eye_max; + } + + fsk->stats->nr = 0; + fsk->stats->Nc = 0; + + for(i=0; istats->f_est[i] = f_est[i]; + } + + /* Dump some internal samples */ + modem_probe_samp_f("t_EbNodB",&(fsk->EbNodB),1); + modem_probe_samp_f("t_ppm",&(fsk->ppm),1); + modem_probe_samp_f("t_rx_timing",&(rx_timing),1); + + #ifdef MODEMPROBE_ENABLE + for( m=0; mtx_phase_c; /* Current complex TX phase */ + int f1_tx = fsk->f1_tx; /* '0' frequency */ + int fs_tx = fsk->fs_tx; /* space between frequencies */ + int Ts = fsk->Ts; /* samples-per-symbol */ + int Fs = fsk->Fs; /* sample freq */ + int M = fsk->mode; + COMP *dosc_f = new COMP[M]; /* phase shift per sample */ + COMP dph; /* phase shift of current bit */ + int i, j, m, bit_i, sym; + + /* Init the per sample phase shift complex numbers */ + for( m=0; mNsym; i++){ + sym = 0; + /* Pack the symbol number from the bit stream */ + for( m=M; m>>=1; ){ + uint8_t bit = tx_bits[bit_i]; + bit = (bit==1)?1:0; + sym = (sym<<1)|bit; + bit_i++; + } + /* Look up symbol phase shift */ + dph = dosc_f[sym]; + /* Spin the oscillator for a symbol period */ + for(j=0; jtx_phase_c = tx_phase_c; + + delete[] dosc_f; +} + +void fsk_mod_c(struct FSK *fsk,COMP fsk_out[],uint8_t tx_bits[]){ + COMP tx_phase_c = fsk->tx_phase_c; /* Current complex TX phase */ + int f1_tx = fsk->f1_tx; /* '0' frequency */ + int fs_tx = fsk->fs_tx; /* space between frequencies */ + int Ts = fsk->Ts; /* samples-per-symbol */ + int Fs = fsk->Fs; /* sample freq */ + int M = fsk->mode; + COMP *dosc_f = new COMP[M]; /* phase shift per sample */ + COMP dph; /* phase shift of current bit */ + int i, j, bit_i, sym; + int m; + + /* Init the per sample phase shift complex numbers */ + for( m=0; mNsym; i++){ + sym = 0; + /* Pack the symbol number from the bit stream */ + for( m=M; m>>=1; ){ + uint8_t bit = tx_bits[bit_i]; + bit = (bit==1)?1:0; + sym = (sym<<1)|bit; + bit_i++; + } + /* Look up symbol phase shift */ + dph = dosc_f[sym]; + /* Spin the oscillator for a symbol period */ + for(j=0; jtx_phase_c = tx_phase_c; + + delete[] dosc_f; +} + + +/* Modulator that assume an external VCO. The output is a voltage + that changes for each symbol */ + +void fsk_mod_ext_vco(struct FSK *fsk, float vco_out[], uint8_t tx_bits[]) { + int f1_tx = fsk->f1_tx; /* '0' frequency */ + int fs_tx = fsk->fs_tx; /* space between frequencies */ + int Ts = fsk->Ts; /* samples-per-symbol */ + int M = fsk->mode; + int i, j, m, sym, bit_i; + + bit_i = 0; + for(i=0; iNsym; i++) { + /* generate the symbol number from the bit stream, + e.g. 0,1 for 2FSK, 0,1,2,3 for 4FSK */ + + sym = 0; + + /* unpack the symbol number from the bit stream */ + + for( m=M; m>>=1; ){ + uint8_t bit = tx_bits[bit_i]; + bit = (bit==1)?1:0; + sym = (sym<<1)|bit; + bit_i++; + } + + /* + Map 'sym' to VCO frequency + Note: drive is inverted, a higher tone drives VCO voltage lower + */ + + //fprintf(stderr, "i: %d sym: %d freq: %f\n", i, sym, f1_tx + fs_tx*(float)sym); + for(j=0; jnormalise_eye = normalise_enable; +} + +} // freeDV + + + + + diff --git a/libfreedv/hanning.h b/libfreedv/hanning.h new file mode 100644 index 000000000..167a4e32e --- /dev/null +++ b/libfreedv/hanning.h @@ -0,0 +1,649 @@ +/* Generated by hanning_file() Octave function */ + +namespace FreeDV +{ + +const float hanning[]={ + 0, + 2.4171e-05, + 9.66816e-05, + 0.000217525, + 0.000386689, + 0.000604158, + 0.00086991, + 0.00118392, + 0.00154616, + 0.00195659, + 0.00241517, + 0.00292186, + 0.00347661, + 0.00407937, + 0.00473008, + 0.00542867, + 0.00617507, + 0.00696922, + 0.00781104, + 0.00870045, + 0.00963736, + 0.0106217, + 0.0116533, + 0.0127322, + 0.0138581, + 0.0150311, + 0.0162509, + 0.0175175, + 0.0188308, + 0.0201906, + 0.0215968, + 0.0230492, + 0.0245478, + 0.0260923, + 0.0276826, + 0.0293186, + 0.0310001, + 0.032727, + 0.034499, + 0.036316, + 0.0381779, + 0.0400844, + 0.0420354, + 0.0440307, + 0.04607, + 0.0481533, + 0.0502802, + 0.0524506, + 0.0546643, + 0.056921, + 0.0592206, + 0.0615627, + 0.0639473, + 0.0663741, + 0.0688427, + 0.0713531, + 0.0739048, + 0.0764978, + 0.0791318, + 0.0818064, + 0.0845214, + 0.0872767, + 0.0900718, + 0.0929066, + 0.0957807, + 0.0986939, + 0.101646, + 0.104636, + 0.107665, + 0.110732, + 0.113836, + 0.116978, + 0.120156, + 0.123372, + 0.126624, + 0.129912, + 0.133235, + 0.136594, + 0.139989, + 0.143418, + 0.146881, + 0.150379, + 0.153911, + 0.157476, + 0.161074, + 0.164705, + 0.168368, + 0.172063, + 0.17579, + 0.179549, + 0.183338, + 0.187158, + 0.191008, + 0.194888, + 0.198798, + 0.202737, + 0.206704, + 0.2107, + 0.214724, + 0.218775, + 0.222854, + 0.226959, + 0.231091, + 0.235249, + 0.239432, + 0.243641, + 0.247874, + 0.252132, + 0.256414, + 0.260719, + 0.265047, + 0.269398, + 0.273772, + 0.278167, + 0.282584, + 0.287021, + 0.29148, + 0.295958, + 0.300456, + 0.304974, + 0.30951, + 0.314065, + 0.318638, + 0.323228, + 0.327835, + 0.332459, + 0.3371, + 0.341756, + 0.346427, + 0.351113, + 0.355814, + 0.360528, + 0.365256, + 0.369997, + 0.374751, + 0.379516, + 0.384293, + 0.389082, + 0.393881, + 0.398691, + 0.40351, + 0.408338, + 0.413176, + 0.418022, + 0.422876, + 0.427737, + 0.432605, + 0.43748, + 0.44236, + 0.447247, + 0.452138, + 0.457034, + 0.461935, + 0.466839, + 0.471746, + 0.476655, + 0.481568, + 0.486481, + 0.491397, + 0.496313, + 0.501229, + 0.506145, + 0.511061, + 0.515976, + 0.520889, + 0.5258, + 0.530708, + 0.535614, + 0.540516, + 0.545414, + 0.550308, + 0.555197, + 0.560081, + 0.564958, + 0.56983, + 0.574695, + 0.579552, + 0.584402, + 0.589244, + 0.594077, + 0.598901, + 0.603715, + 0.60852, + 0.613314, + 0.618097, + 0.622868, + 0.627628, + 0.632375, + 0.63711, + 0.641831, + 0.646538, + 0.651232, + 0.655911, + 0.660574, + 0.665222, + 0.669855, + 0.67447, + 0.679069, + 0.683651, + 0.688215, + 0.69276, + 0.697287, + 0.701795, + 0.706284, + 0.710752, + 0.7152, + 0.719627, + 0.724033, + 0.728418, + 0.73278, + 0.73712, + 0.741437, + 0.74573, + 0.75, + 0.754246, + 0.758467, + 0.762663, + 0.766833, + 0.770978, + 0.775097, + 0.779189, + 0.783254, + 0.787291, + 0.791301, + 0.795283, + 0.799236, + 0.80316, + 0.807055, + 0.810921, + 0.814756, + 0.81856, + 0.822334, + 0.826077, + 0.829788, + 0.833468, + 0.837115, + 0.840729, + 0.844311, + 0.847859, + 0.851374, + 0.854855, + 0.858301, + 0.861713, + 0.86509, + 0.868431, + 0.871737, + 0.875007, + 0.87824, + 0.881437, + 0.884598, + 0.887721, + 0.890806, + 0.893854, + 0.896864, + 0.899835, + 0.902768, + 0.905661, + 0.908516, + 0.911331, + 0.914106, + 0.916841, + 0.919536, + 0.92219, + 0.924804, + 0.927376, + 0.929907, + 0.932397, + 0.934845, + 0.93725, + 0.939614, + 0.941935, + 0.944213, + 0.946448, + 0.94864, + 0.950789, + 0.952894, + 0.954955, + 0.956972, + 0.958946, + 0.960874, + 0.962759, + 0.964598, + 0.966393, + 0.968142, + 0.969846, + 0.971505, + 0.973118, + 0.974686, + 0.976207, + 0.977683, + 0.979112, + 0.980495, + 0.981832, + 0.983122, + 0.984365, + 0.985561, + 0.986711, + 0.987813, + 0.988868, + 0.989876, + 0.990837, + 0.99175, + 0.992616, + 0.993434, + 0.994204, + 0.994927, + 0.995601, + 0.996228, + 0.996807, + 0.997337, + 0.99782, + 0.998255, + 0.998641, + 0.998979, + 0.999269, + 0.999511, + 0.999704, + 0.999849, + 0.999946, + 0.999994, + 0.999994, + 0.999946, + 0.999849, + 0.999704, + 0.999511, + 0.999269, + 0.998979, + 0.998641, + 0.998255, + 0.99782, + 0.997337, + 0.996807, + 0.996228, + 0.995601, + 0.994927, + 0.994204, + 0.993434, + 0.992616, + 0.99175, + 0.990837, + 0.989876, + 0.988868, + 0.987813, + 0.986711, + 0.985561, + 0.984365, + 0.983122, + 0.981832, + 0.980495, + 0.979112, + 0.977683, + 0.976207, + 0.974686, + 0.973118, + 0.971505, + 0.969846, + 0.968142, + 0.966393, + 0.964598, + 0.962759, + 0.960874, + 0.958946, + 0.956972, + 0.954955, + 0.952894, + 0.950789, + 0.94864, + 0.946448, + 0.944213, + 0.941935, + 0.939614, + 0.93725, + 0.934845, + 0.932397, + 0.929907, + 0.927376, + 0.924804, + 0.92219, + 0.919536, + 0.916841, + 0.914106, + 0.911331, + 0.908516, + 0.905661, + 0.902768, + 0.899835, + 0.896864, + 0.893854, + 0.890806, + 0.887721, + 0.884598, + 0.881437, + 0.87824, + 0.875007, + 0.871737, + 0.868431, + 0.86509, + 0.861713, + 0.858301, + 0.854855, + 0.851374, + 0.847859, + 0.844311, + 0.840729, + 0.837115, + 0.833468, + 0.829788, + 0.826077, + 0.822334, + 0.81856, + 0.814756, + 0.810921, + 0.807055, + 0.80316, + 0.799236, + 0.795283, + 0.791301, + 0.787291, + 0.783254, + 0.779189, + 0.775097, + 0.770978, + 0.766833, + 0.762663, + 0.758467, + 0.754246, + 0.75, + 0.74573, + 0.741437, + 0.73712, + 0.73278, + 0.728418, + 0.724033, + 0.719627, + 0.7152, + 0.710752, + 0.706284, + 0.701795, + 0.697287, + 0.69276, + 0.688215, + 0.683651, + 0.679069, + 0.67447, + 0.669855, + 0.665222, + 0.660574, + 0.655911, + 0.651232, + 0.646538, + 0.641831, + 0.63711, + 0.632375, + 0.627628, + 0.622868, + 0.618097, + 0.613314, + 0.60852, + 0.603715, + 0.598901, + 0.594077, + 0.589244, + 0.584402, + 0.579552, + 0.574695, + 0.56983, + 0.564958, + 0.560081, + 0.555197, + 0.550308, + 0.545414, + 0.540516, + 0.535614, + 0.530708, + 0.5258, + 0.520889, + 0.515976, + 0.511061, + 0.506145, + 0.501229, + 0.496313, + 0.491397, + 0.486481, + 0.481568, + 0.476655, + 0.471746, + 0.466839, + 0.461935, + 0.457034, + 0.452138, + 0.447247, + 0.44236, + 0.43748, + 0.432605, + 0.427737, + 0.422876, + 0.418022, + 0.413176, + 0.408338, + 0.40351, + 0.398691, + 0.393881, + 0.389082, + 0.384293, + 0.379516, + 0.374751, + 0.369997, + 0.365256, + 0.360528, + 0.355814, + 0.351113, + 0.346427, + 0.341756, + 0.3371, + 0.332459, + 0.327835, + 0.323228, + 0.318638, + 0.314065, + 0.30951, + 0.304974, + 0.300456, + 0.295958, + 0.29148, + 0.287021, + 0.282584, + 0.278167, + 0.273772, + 0.269398, + 0.265047, + 0.260719, + 0.256414, + 0.252132, + 0.247874, + 0.243641, + 0.239432, + 0.235249, + 0.231091, + 0.226959, + 0.222854, + 0.218775, + 0.214724, + 0.2107, + 0.206704, + 0.202737, + 0.198798, + 0.194888, + 0.191008, + 0.187158, + 0.183338, + 0.179549, + 0.17579, + 0.172063, + 0.168368, + 0.164705, + 0.161074, + 0.157476, + 0.153911, + 0.150379, + 0.146881, + 0.143418, + 0.139989, + 0.136594, + 0.133235, + 0.129912, + 0.126624, + 0.123372, + 0.120156, + 0.116978, + 0.113836, + 0.110732, + 0.107665, + 0.104636, + 0.101646, + 0.0986939, + 0.0957807, + 0.0929066, + 0.0900718, + 0.0872767, + 0.0845214, + 0.0818064, + 0.0791318, + 0.0764978, + 0.0739048, + 0.0713531, + 0.0688427, + 0.0663741, + 0.0639473, + 0.0615627, + 0.0592206, + 0.056921, + 0.0546643, + 0.0524506, + 0.0502802, + 0.0481533, + 0.04607, + 0.0440307, + 0.0420354, + 0.0400844, + 0.0381779, + 0.036316, + 0.034499, + 0.032727, + 0.0310001, + 0.0293186, + 0.0276826, + 0.0260923, + 0.0245478, + 0.0230492, + 0.0215968, + 0.0201906, + 0.0188308, + 0.0175175, + 0.0162509, + 0.0150311, + 0.0138581, + 0.0127322, + 0.0116533, + 0.0106217, + 0.00963736, + 0.00870045, + 0.00781104, + 0.00696922, + 0.00617507, + 0.00542867, + 0.00473008, + 0.00407937, + 0.00347661, + 0.00292186, + 0.00241517, + 0.00195659, + 0.00154616, + 0.00118392, + 0.00086991, + 0.000604158, + 0.000386689, + 0.000217525, + 9.66816e-05, + 2.4171e-05, + 0 +}; + +} // FreeDV diff --git a/libfreedv/kiss_fft.cpp b/libfreedv/kiss_fft.cpp new file mode 100644 index 000000000..44c02e9db --- /dev/null +++ b/libfreedv/kiss_fft.cpp @@ -0,0 +1,413 @@ +/* +Copyright (c) 2003-2010, Mark Borgerding + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#include "_kiss_fft_guts.h" +/* The guts header contains all the multiplication and addition macros that are defined for + fixed or floating point complex numbers. It also delares the kf_ internal functions. + */ + +namespace FreeDV +{ + +static void kf_bfly2( + kiss_fft_cpx * Fout, + const std::size_t fstride, + const kiss_fft_cfg st, + int m + ) +{ + kiss_fft_cpx * Fout2; + kiss_fft_cpx * tw1 = st->twiddles; + kiss_fft_cpx t; + Fout2 = Fout + m; + do{ + C_FIXDIV(*Fout,2); C_FIXDIV(*Fout2,2); + + C_MUL (t, *Fout2 , *tw1); + tw1 += fstride; + C_SUB( *Fout2 , *Fout , t ); + C_ADDTO( *Fout , t ); + ++Fout2; + ++Fout; + }while (--m); +} + +static void kf_bfly4( + kiss_fft_cpx * Fout, + const std::size_t fstride, + const kiss_fft_cfg st, + const std::size_t m + ) +{ + kiss_fft_cpx *tw1,*tw2,*tw3; + kiss_fft_cpx scratch[6]; + std::size_t k=m; + const std::size_t m2=2*m; + const std::size_t m3=3*m; + + + tw3 = tw2 = tw1 = st->twiddles; + + do { + C_FIXDIV(*Fout,4); C_FIXDIV(Fout[m],4); C_FIXDIV(Fout[m2],4); C_FIXDIV(Fout[m3],4); + + C_MUL(scratch[0],Fout[m] , *tw1 ); + C_MUL(scratch[1],Fout[m2] , *tw2 ); + C_MUL(scratch[2],Fout[m3] , *tw3 ); + + C_SUB( scratch[5] , *Fout, scratch[1] ); + C_ADDTO(*Fout, scratch[1]); + C_ADD( scratch[3] , scratch[0] , scratch[2] ); + C_SUB( scratch[4] , scratch[0] , scratch[2] ); + C_SUB( Fout[m2], *Fout, scratch[3] ); + tw1 += fstride; + tw2 += fstride*2; + tw3 += fstride*3; + C_ADDTO( *Fout , scratch[3] ); + + if(st->inverse) { + Fout[m].r = scratch[5].r - scratch[4].i; + Fout[m].i = scratch[5].i + scratch[4].r; + Fout[m3].r = scratch[5].r + scratch[4].i; + Fout[m3].i = scratch[5].i - scratch[4].r; + }else{ + Fout[m].r = scratch[5].r + scratch[4].i; + Fout[m].i = scratch[5].i - scratch[4].r; + Fout[m3].r = scratch[5].r - scratch[4].i; + Fout[m3].i = scratch[5].i + scratch[4].r; + } + ++Fout; + }while(--k); +} + +static void kf_bfly3( + kiss_fft_cpx * Fout, + const std::size_t fstride, + const kiss_fft_cfg st, + std::size_t m + ) +{ + std::size_t k=m; + const std::size_t m2 = 2*m; + kiss_fft_cpx *tw1,*tw2; + kiss_fft_cpx scratch[5]; + kiss_fft_cpx epi3; + epi3 = st->twiddles[fstride*m]; + + tw1=tw2=st->twiddles; + + do{ + C_FIXDIV(*Fout,3); C_FIXDIV(Fout[m],3); C_FIXDIV(Fout[m2],3); + + C_MUL(scratch[1],Fout[m] , *tw1); + C_MUL(scratch[2],Fout[m2] , *tw2); + + C_ADD(scratch[3],scratch[1],scratch[2]); + C_SUB(scratch[0],scratch[1],scratch[2]); + tw1 += fstride; + tw2 += fstride*2; + + Fout[m].r = Fout->r - HALF_OF(scratch[3].r); + Fout[m].i = Fout->i - HALF_OF(scratch[3].i); + + C_MULBYSCALAR( scratch[0] , epi3.i ); + + C_ADDTO(*Fout,scratch[3]); + + Fout[m2].r = Fout[m].r + scratch[0].i; + Fout[m2].i = Fout[m].i - scratch[0].r; + + Fout[m].r -= scratch[0].i; + Fout[m].i += scratch[0].r; + + ++Fout; + }while(--k); +} + +static void kf_bfly5( + kiss_fft_cpx * Fout, + const std::size_t fstride, + const kiss_fft_cfg st, + int m + ) +{ + kiss_fft_cpx *Fout0,*Fout1,*Fout2,*Fout3,*Fout4; + int u; + kiss_fft_cpx scratch[13]; + kiss_fft_cpx * twiddles = st->twiddles; + kiss_fft_cpx *tw; + kiss_fft_cpx ya,yb; + ya = twiddles[fstride*m]; + yb = twiddles[fstride*2*m]; + + Fout0=Fout; + Fout1=Fout0+m; + Fout2=Fout0+2*m; + Fout3=Fout0+3*m; + Fout4=Fout0+4*m; + + tw=st->twiddles; + for ( u=0; ur += scratch[7].r + scratch[8].r; + Fout0->i += scratch[7].i + scratch[8].i; + + scratch[5].r = scratch[0].r + S_MUL(scratch[7].r,ya.r) + S_MUL(scratch[8].r,yb.r); + scratch[5].i = scratch[0].i + S_MUL(scratch[7].i,ya.r) + S_MUL(scratch[8].i,yb.r); + + scratch[6].r = S_MUL(scratch[10].i,ya.i) + S_MUL(scratch[9].i,yb.i); + scratch[6].i = -S_MUL(scratch[10].r,ya.i) - S_MUL(scratch[9].r,yb.i); + + C_SUB(*Fout1,scratch[5],scratch[6]); + C_ADD(*Fout4,scratch[5],scratch[6]); + + scratch[11].r = scratch[0].r + S_MUL(scratch[7].r,yb.r) + S_MUL(scratch[8].r,ya.r); + scratch[11].i = scratch[0].i + S_MUL(scratch[7].i,yb.r) + S_MUL(scratch[8].i,ya.r); + scratch[12].r = - S_MUL(scratch[10].i,yb.i) + S_MUL(scratch[9].i,ya.i); + scratch[12].i = S_MUL(scratch[10].r,yb.i) - S_MUL(scratch[9].r,ya.i); + + C_ADD(*Fout2,scratch[11],scratch[12]); + C_SUB(*Fout3,scratch[11],scratch[12]); + + ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4; + } +} + +/* perform the butterfly for one stage of a mixed radix FFT */ +static void kf_bfly_generic( + kiss_fft_cpx * Fout, + const std::size_t fstride, + const kiss_fft_cfg st, + int m, + int p + ) +{ + int u,k,q1,q; + kiss_fft_cpx * twiddles = st->twiddles; + kiss_fft_cpx t; + int Norig = st->nfft; + + kiss_fft_cpx * scratch = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC(sizeof(kiss_fft_cpx)*p); + + for ( u=0; u=Norig) twidx-=Norig; + C_MUL(t,scratch[q] , twiddles[twidx] ); + C_ADDTO( Fout[ k ] ,t); + } + k += m; + } + } + KISS_FFT_TMP_FREE(scratch); +} + +static +void kf_work( + kiss_fft_cpx * Fout, + const kiss_fft_cpx * f, + const std::size_t fstride, + int in_stride, + int * factors, + const kiss_fft_cfg st + ) +{ + kiss_fft_cpx * Fout_beg=Fout; + const int p=*factors++; /* the radix */ + const int m=*factors++; /* stage's fft length/p */ + const kiss_fft_cpx * Fout_end = Fout + p*m; + +#ifdef _OPENMP + // use openmp extensions at the + // top-level (not recursive) + if (fstride==1 && p<=5) + { + int k; + + // execute the p different work units in different threads +# pragma omp parallel for + for (k=0;k floor_sqrt) + p = n; /* no more factors, skip to end */ + } + n /= p; + *facbuf++ = p; + *facbuf++ = n; + } while (n > 1); +} + +/* + * + * User-callable function to allocate all necessary storage space for the fft. + * + * The return value is a contiguous block of memory, allocated with malloc. As such, + * It can be freed with free(), rather than a kiss_fft-specific function. + * */ +kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem, std::size_t * lenmem ) +{ + kiss_fft_cfg st=NULL; + std::size_t memneeded = sizeof(struct kiss_fft_state) + + sizeof(kiss_fft_cpx)*(nfft-1); /* twiddle factors*/ + + if ( lenmem==NULL ) { + st = ( kiss_fft_cfg)KISS_FFT_MALLOC( memneeded ); + }else{ + if (mem != NULL && *lenmem >= memneeded) + st = (kiss_fft_cfg)mem; + *lenmem = memneeded; + } + if (st) { + int i; + st->nfft=nfft; + st->inverse = inverse_fft; + + for (i=0;iinverse) + phase *= -1; + kf_cexp(st->twiddles+i, phase ); + } + + kf_factor(nfft,st->factors); + } + return st; +} + + +void kiss_fft_stride(kiss_fft_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int in_stride) +{ + if (fin == fout) { + //NOTE: this is not really an in-place FFT algorithm. + //It just performs an out-of-place FFT into a temp buffer + kiss_fft_cpx * tmpbuf = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC( sizeof(kiss_fft_cpx)*st->nfft); + kf_work(tmpbuf,fin,1,in_stride, st->factors,st); + memcpy(fout,tmpbuf,sizeof(kiss_fft_cpx)*st->nfft); + KISS_FFT_TMP_FREE(tmpbuf); + }else{ + kf_work( fout, fin, 1,in_stride, st->factors,st ); + } +} + +void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout) +{ + kiss_fft_stride(cfg,fin,fout,1); +} + + +void kiss_fft_cleanup(void) +{ + // nothing needed any more +} + +int kiss_fft_next_fast_size(int n) +{ + while(1) { + int m=n; + while ( (m%2) == 0 ) m/=2; + while ( (m%3) == 0 ) m/=3; + while ( (m%5) == 0 ) m/=5; + if (m<=1) + break; /* n is completely factorable by twos, threes, and fives */ + n++; + } + return n; +} + +} // FreeDV diff --git a/libfreedv/kiss_fft.h b/libfreedv/kiss_fft.h index ca4933aae..66beae128 100644 --- a/libfreedv/kiss_fft.h +++ b/libfreedv/kiss_fft.h @@ -20,6 +20,9 @@ in the tools/ directory. */ +namespace FreeDV +{ + #ifdef USE_SIMD # include # define kiss_fft_scalar __m128 @@ -45,8 +48,6 @@ # endif #endif -namespace FreeDV -{ typedef struct { kiss_fft_scalar r; diff --git a/libfreedv/kiss_fftr.h b/libfreedv/kiss_fftr.h index 916c1ff63..0816802e2 100644 --- a/libfreedv/kiss_fftr.h +++ b/libfreedv/kiss_fftr.h @@ -39,6 +39,6 @@ void kiss_fftri(kiss_fftr_cfg cfg,const kiss_fft_cpx *freqdata,kiss_fft_scalar * } // FreeDV -#define kiss_fftr_free free +//#define kiss_fftr_free free #endif diff --git a/libfreedv/linreg.cpp b/libfreedv/linreg.cpp new file mode 100644 index 000000000..479b38c45 --- /dev/null +++ b/libfreedv/linreg.cpp @@ -0,0 +1,110 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: linreg.c + AUTHOR......: David Rowe + DATE CREATED: April 2015 + + Linear regression C module based on: + + http://stackoverflow.com/questions/5083465/fast-efficient-least-squares-fit-algorithm-in-c + + Use: + + $ gcc linreg.c -o linreg -D__UNITTEST__ -Wall + $ ./linreg + + Then compare yfit results with octave/tlinreg.m + +\*---------------------------------------------------------------------------*/ + +/* + Copyright (C) 2015 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program 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 Lesser General Public License + along with this program; if not, see . +*/ + +#include +#include +#include + +#include "linreg.h" +#include "codec2/comp_prim.h" + +namespace FreeDV +{ + +void linreg(COMP *m, COMP *b, float x[], COMP y[], int n) +{ + float sumx = 0.0; /* sum of x */ + float sumx2 = 0.0; /* sum of x^2 */ + COMP sumxy = {0.0,0.0}; /* sum of x * y */ + COMP sumy = {0.0,0.0}; /* sum of y */ + COMP sumy2 = {0.0,0.0}; /* sum of y**2 */ + float denom; + COMP zero; + int i; + + for (i=0; i. +*/ + +#ifndef __LINREG__ +#define __LINREG__ + +#include "codec2/comp.h" + +namespace FreeDV +{ + +void linreg(COMP *m, COMP *b, float x[], COMP y[], int n); + +} // FreeDV + +#endif diff --git a/libfreedv/machdep.h b/libfreedv/machdep.h new file mode 100644 index 000000000..496468368 --- /dev/null +++ b/libfreedv/machdep.h @@ -0,0 +1,57 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: machdep.h + AUTHOR......: David Rowe + DATE CREATED: May 2 2013 + + Machine dependant functions, e.g. profiling that requires access to a clock + counter register. + +\*---------------------------------------------------------------------------*/ + +/* + Copyright (C) 2013 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program 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 Lesser General Public License + along with this program; if not, see . +*/ + +#ifndef __MACHDEP__ +#define __MACHDEP__ + +namespace FreeDV +{ + +#ifdef PROFILE +#define PROFILE_VAR(...) unsigned int __VA_ARGS__ +#define PROFILE_SAMPLE(timestamp) timestamp = machdep_profile_sample() +#define PROFILE_SAMPLE_AND_LOG(timestamp, prev_timestamp, label) \ + timestamp = machdep_profile_sample_and_log(prev_timestamp, label) +#define PROFILE_SAMPLE_AND_LOG2(prev_timestamp, label) \ + machdep_profile_sample_and_log(prev_timestamp, label) +#else +#define PROFILE_VAR(...) +#define PROFILE_SAMPLE(timestamp) +#define PROFILE_SAMPLE_AND_LOG(timestamp, prev_timestamp, label) +#define PROFILE_SAMPLE_AND_LOG2(prev_timestamp, label) +#endif + +void machdep_profile_init(void); +void machdep_profile_reset(void); +unsigned int machdep_profile_sample(void); +unsigned int machdep_profile_sample_and_log(unsigned int start, char s[]); +void machdep_profile_print_logged_samples(void); + +} // FreeDV + +#endif diff --git a/libfreedv/modem_probe.h b/libfreedv/modem_probe.h index e5ba6ac42..826051ccc 100644 --- a/libfreedv/modem_probe.h +++ b/libfreedv/modem_probe.h @@ -29,8 +29,8 @@ #define __MODEMPROBE_H #include #include -#include -#include "comp.h" +#include +#include "codec2/comp.h" namespace FreeDV { @@ -103,28 +103,47 @@ static inline void modem_probe_samp_cft(char *tracename,complex float samp[],siz #else -static inline void modem_probe_init(char *modname,char *runname){ - return; +static inline void modem_probe_init(const char *modname, char *runname) +{ + (void) modname; + (void) runname; + return; } -static inline void modem_probe_close(){ - return; +static inline void modem_probe_close() { + return; } -static inline void modem_probe_samp_i(char *name,int samp[],size_t sampcnt){ - return; +static inline void modem_probe_samp_i(const char *name, int samp[], std::size_t sampcnt) +{ + (void) name; + (void) samp; + (void) sampcnt; + return; } -static inline void modem_probe_samp_f(char *name,float samp[],size_t cnt){ - return; +static inline void modem_probe_samp_f(const char *name, float samp[], std::size_t cnt) +{ + (void) name; + (void) samp; + (void) cnt; + return; } -static inline void modem_probe_samp_c(char *name,COMP samp[],size_t cnt){ - return; +static inline void modem_probe_samp_c(const char *name, COMP samp[], std::size_t cnt) +{ + (void) name; + (void) samp; + (void) cnt; + return; } -static inline void modem_probe_samp_cft(char *name,complex float samp[],size_t cnt){ - return; +static inline void modem_probe_samp_cft(const char *name, std::complex samp[], std::size_t cnt) +{ + (void) name; + (void) samp; + (void) cnt; + return; } #endif diff --git a/libfreedv/os.h b/libfreedv/os.h new file mode 100644 index 000000000..0922622e3 --- /dev/null +++ b/libfreedv/os.h @@ -0,0 +1,57 @@ +/* Generate using fir1(47,1/2) in Octave */ + +namespace FreeDV +{ + +static const float fdmdv_os_filter[]= { + -0.0008215855034550382, + -0.0007833023901802921, + 0.001075563790768233, + 0.001199092367787555, + -0.001765309502928316, + -0.002055372115328064, + 0.002986877604154257, + 0.003462567920638414, + -0.004856570111126334, + -0.005563143845031497, + 0.007533613299748122, + 0.008563932468880897, + -0.01126857129039911, + -0.01280782411693687, + 0.01651443896361847, + 0.01894875110322284, + -0.02421604439474981, + -0.02845107338464062, + 0.03672973563400258, + 0.04542046150312214, + -0.06189165826716491, + -0.08721876380763803, + 0.1496157094199961, + 0.4497962274137046, + 0.4497962274137046, + 0.1496157094199961, + -0.08721876380763803, + -0.0618916582671649, + 0.04542046150312216, + 0.03672973563400257, + -0.02845107338464062, + -0.02421604439474984, + 0.01894875110322284, + 0.01651443896361848, + -0.01280782411693687, + -0.0112685712903991, + 0.008563932468880899, + 0.007533613299748123, + -0.005563143845031501, + -0.004856570111126346, + 0.003462567920638419, + 0.002986877604154259, + -0.002055372115328063, + -0.001765309502928318, + 0.001199092367787557, + 0.001075563790768233, + -0.0007833023901802925, + -0.0008215855034550383 +}; + +} // FreeDV diff --git a/libfreedv/pilot_coeff.h b/libfreedv/pilot_coeff.h new file mode 100644 index 000000000..63320a4cb --- /dev/null +++ b/libfreedv/pilot_coeff.h @@ -0,0 +1,46 @@ +/* Generated by pilot_coeff_file() Octave function */ + +// const removed since this provides gain +// on the STM32F4 platform + +namespace FreeDV +{ + +#ifdef CORTEX_M4 +/* const */ float pilot_coeff[]={ +#else +const float pilot_coeff[]={ +#endif + 0.00223001, + 0.00301037, + 0.00471258, + 0.0075934, + 0.0118145, + 0.0174153, + 0.0242969, + 0.0322204, + 0.0408199, + 0.0496286, + 0.0581172, + 0.0657392, + 0.0719806, + 0.0764066, + 0.0787022, + 0.0787022, + 0.0764066, + 0.0719806, + 0.0657392, + 0.0581172, + 0.0496286, + 0.0408199, + 0.0322204, + 0.0242969, + 0.0174153, + 0.0118145, + 0.0075934, + 0.00471258, + 0.00301037, + 0.00223001 +}; + +} // FreeDV diff --git a/libfreedv/pilots_coh.h b/libfreedv/pilots_coh.h new file mode 100644 index 000000000..00f4816a4 --- /dev/null +++ b/libfreedv/pilots_coh.h @@ -0,0 +1,11 @@ +/* Generated by write_pilot_file() Octave function */ + +namespace FreeDV +{ + +float pilots_coh[][PILOTS_NC]={ + { 1.000000, -1.000000, 1.000000, -1.000000, 1.000000, -1.000000, -1.000000}, + { -1.000000, 1.000000, 1.000000, -1.000000, 1.000000, 1.000000, 1.000000} +}; + +} // FreeDV diff --git a/libfreedv/rn.h b/libfreedv/rn.h new file mode 100644 index 000000000..d10c239d0 --- /dev/null +++ b/libfreedv/rn.h @@ -0,0 +1,969 @@ +/* Generated by rn_file() Octave function */ + +namespace FreeDV +{ + +const float gt_alpha5_root[]={ + 2.86997e-05, + 2.2286e-05, + 1.82863e-05, + 1.42303e-05, + 1.04905e-05, + 6.70859e-06, + 3.05918e-06, + -6.22187e-07, + -4.22748e-06, + -7.85603e-06, + -1.14317e-05, + -1.50227e-05, + -1.85712e-05, + -2.21275e-05, + -2.56455e-05, + -2.91642e-05, + -3.26453e-05, + -3.61199e-05, + -3.95556e-05, + -4.29778e-05, + -4.63581e-05, + -4.97179e-05, + -5.3032e-05, + -5.63184e-05, + -5.95548e-05, + -6.27565e-05, + -6.59032e-05, + -6.90085e-05, + -7.20538e-05, + -7.50509e-05, + -7.7983e-05, + -8.08605e-05, + -8.36678e-05, + -8.64141e-05, + -8.9085e-05, + -9.16888e-05, + -9.42119e-05, + -9.66619e-05, + -9.9026e-05, + -0.000101311, + -0.000103505, + -0.000105614, + -0.000107627, + -0.00010955, + -0.000111372, + -0.000113099, + -0.00011472, + -0.000116241, + -0.000117652, + -0.000118959, + -0.000120152, + -0.000121235, + -0.000122201, + -0.000123053, + -0.000123784, + -0.000124397, + -0.000124884, + -0.00012525, + -0.000125487, + -0.000125598, + -0.000125578, + -0.000125428, + -0.000125145, + -0.000124729, + -0.000124185, + -0.000123518, + -0.000122709, + -0.000121766, + -0.000120685, + -0.000119471, + -0.000118119, + -0.000116633, + -0.000115009, + -0.000113251, + -0.000111356, + -0.000109326, + -0.00010716, + -0.00010486, + -0.000102424, + -9.98553e-05, + -9.71528e-05, + -9.43199e-05, + -9.13551e-05, + -8.82623e-05, + -8.50404e-05, + -8.16936e-05, + -7.82211e-05, + -7.46271e-05, + -7.09109e-05, + -6.70773e-05, + -6.31256e-05, + -5.90607e-05, + -5.48823e-05, + -5.05954e-05, + -4.62001e-05, + -4.17016e-05, + -3.71002e-05, + -3.24015e-05, + -2.7606e-05, + -2.27195e-05, + -1.77428e-05, + -1.2682e-05, + -7.53795e-06, + -2.31702e-06, + 2.97965e-06, + 8.34567e-06, + 1.37796e-05, + 1.9275e-05, + 2.483e-05, + 3.04382e-05, + 3.60975e-05, + 4.18011e-05, + 4.75467e-05, + 5.33273e-05, + 5.91403e-05, + 6.49787e-05, + 7.08393e-05, + 7.67152e-05, + 8.26029e-05, + 8.84957e-05, + 9.43895e-05, + 0.000100278, + 0.000106157, + 0.00011202, + 0.000117864, + 0.000123681, + 0.000129468, + 0.000135218, + 0.000140929, + 0.000146583, + 0.000152183, + 0.000157725, + 0.000163202, + 0.000168608, + 0.000173938, + 0.000179183, + 0.00018434, + 0.0001894, + 0.00019436, + 0.000199211, + 0.000203949, + 0.000208568, + 0.000213063, + 0.000217426, + 0.000221654, + 0.00022574, + 0.000229678, + 0.000233463, + 0.000237089, + 0.000240551, + 0.000243843, + 0.000246959, + 0.000249895, + 0.000252644, + 0.000255202, + 0.000257562, + 0.000259721, + 0.000261672, + 0.000263411, + 0.000264933, + 0.000266234, + 0.000267308, + 0.000268152, + 0.00026876, + 0.000269128, + 0.000269253, + 0.000269129, + 0.000268754, + 0.000268123, + 0.000267232, + 0.000266079, + 0.000264658, + 0.000262968, + 0.000261006, + 0.000258767, + 0.000256251, + 0.000253453, + 0.000250373, + 0.000247007, + 0.000243354, + 0.000239412, + 0.00023518, + 0.000230655, + 0.000225837, + 0.000220723, + 0.000215314, + 0.000209608, + 0.000203605, + 0.000197304, + 0.000190706, + 0.000183812, + 0.000176621, + 0.000169145, + 0.000161363, + 0.000153275, + 0.000144895, + 0.000136224, + 0.000127266, + 0.00011802, + 0.000108491, + 9.8679e-05, + 8.85877e-05, + 7.82196e-05, + 6.7577e-05, + 5.66636e-05, + 4.54822e-05, + 3.40369e-05, + 2.23311e-05, + 1.03695e-05, + -1.844e-06, + -1.43041e-05, + -2.70061e-05, + -3.99444e-05, + -5.31139e-05, + -6.65082e-05, + -8.01218e-05, + -9.39481e-05, + -0.000107981, + -0.000122213, + -0.000136638, + -0.000151248, + -0.000166036, + -0.000180995, + -0.000196115, + -0.00021139, + -0.000226811, + -0.000242369, + -0.000258056, + -0.000273861, + -0.000289776, + -0.000305792, + -0.000321898, + -0.000338084, + -0.000354342, + -0.00037066, + -0.000387027, + -0.000403434, + -0.00041987, + -0.000436324, + -0.000452784, + -0.00046924, + -0.00048568, + -0.000502091, + -0.000518464, + -0.000534785, + -0.000551043, + -0.000567225, + -0.000583319, + -0.000599314, + -0.000615196, + -0.000630955, + -0.000646575, + -0.000662049, + -0.000677361, + -0.000692506, + -0.000707464, + -0.00072229, + -0.000736922, + -0.000751266, + -0.000765372, + -0.000779217, + -0.000792798, + -0.000806094, + -0.000819098, + -0.000831793, + -0.000844168, + -0.000856207, + -0.000867898, + -0.000879227, + -0.00089018, + -0.000900744, + -0.000910906, + -0.000920652, + -0.00092997, + -0.000938844, + -0.000947263, + -0.000955214, + -0.000962682, + -0.000969654, + -0.000976119, + -0.000982062, + -0.00098747, + -0.000992332, + -0.000996634, + -0.00100036, + -0.00100351, + -0.00100606, + -0.001008, + -0.00100932, + -0.00101, + -0.00101005, + -0.00100943, + -0.00100816, + -0.0010062, + -0.00100356, + -0.00100021, + -0.000996162, + -0.000991392, + -0.000985892, + -0.000979654, + -0.000972668, + -0.000964925, + -0.000956415, + -0.000947131, + -0.000937065, + -0.000926208, + -0.000914552, + -0.00090209, + -0.000888816, + -0.000874721, + -0.0008598, + -0.000844046, + -0.000827453, + -0.000810015, + -0.000791726, + -0.000772581, + -0.000752576, + -0.000731704, + -0.000709965, + -0.00068735, + -0.000663865, + -0.000639509, + -0.000614269, + -0.000588146, + -0.000561139, + -0.000533246, + -0.000504468, + -0.000474802, + -0.000444251, + -0.000412813, + -0.00038049, + -0.000347281, + -0.000313189, + -0.000278215, + -0.000242361, + -0.000205629, + -0.000168024, + -0.000129546, + -9.02024e-05, + -4.99954e-05, + -8.93026e-06, + 3.2988e-05, + 7.57537e-05, + 0.000119361, + 0.000163804, + 0.000209075, + 0.000255167, + 0.000302074, + 0.000349786, + 0.000398297, + 0.000447596, + 0.000497676, + 0.000548526, + 0.000600136, + 0.000652497, + 0.000705598, + 0.000759427, + 0.000813972, + 0.000869223, + 0.000925166, + 0.000981789, + 0.00103908, + 0.00109702, + 0.00115561, + 0.00121482, + 0.00127464, + 0.00133505, + 0.00139605, + 0.00145762, + 0.00151973, + 0.00158238, + 0.00164555, + 0.00170922, + 0.00177337, + 0.00183799, + 0.00190305, + 0.00196854, + 0.00203445, + 0.00210075, + 0.00216742, + 0.00223445, + 0.00230181, + 0.00236949, + 0.00243747, + 0.00250572, + 0.00257423, + 0.00264296, + 0.00271192, + 0.00278107, + 0.00285039, + 0.00291986, + 0.00298947, + 0.00305918, + 0.00312898, + 0.00319884, + 0.00326874, + 0.00333866, + 0.00340857, + 0.00347846, + 0.00354831, + 0.00361808, + 0.00368775, + 0.00375731, + 0.00382673, + 0.00389599, + 0.00396506, + 0.00403393, + 0.00410256, + 0.00417094, + 0.00423904, + 0.00430684, + 0.00437431, + 0.00444144, + 0.0045082, + 0.00457457, + 0.00464052, + 0.00470603, + 0.00477108, + 0.00483565, + 0.00489972, + 0.00496325, + 0.00502623, + 0.00508865, + 0.00515046, + 0.00521166, + 0.00527223, + 0.00533213, + 0.00539135, + 0.00544987, + 0.00550766, + 0.00556472, + 0.005621, + 0.00567651, + 0.00573121, + 0.00578508, + 0.00583811, + 0.00589028, + 0.00594157, + 0.00599196, + 0.00604143, + 0.00608996, + 0.00613754, + 0.00618415, + 0.00622977, + 0.00627439, + 0.00631798, + 0.00636054, + 0.00640204, + 0.0064425, + 0.00648186, + 0.00652009, + 0.00655722, + 0.00659322, + 0.00662808, + 0.00666179, + 0.00669433, + 0.00672571, + 0.00675589, + 0.00678488, + 0.00681266, + 0.00683921, + 0.00686454, + 0.00688863, + 0.00691147, + 0.00693305, + 0.00695336, + 0.0069724, + 0.00699016, + 0.00700663, + 0.00702181, + 0.00703569, + 0.00704826, + 0.00705952, + 0.00706947, + 0.00707809, + 0.0070854, + 0.00709138, + 0.00709604, + 0.00709937, + 0.00710136, + 0.00710203, + 0.00710136, + 0.00709937, + 0.00709604, + 0.00709138, + 0.0070854, + 0.00707809, + 0.00706947, + 0.00705952, + 0.00704826, + 0.00703569, + 0.00702181, + 0.00700663, + 0.00699016, + 0.0069724, + 0.00695336, + 0.00693305, + 0.00691147, + 0.00688863, + 0.00686454, + 0.00683921, + 0.00681266, + 0.00678488, + 0.00675589, + 0.00672571, + 0.00669433, + 0.00666179, + 0.00662808, + 0.00659322, + 0.00655722, + 0.00652009, + 0.00648186, + 0.0064425, + 0.00640204, + 0.00636054, + 0.00631798, + 0.00627439, + 0.00622977, + 0.00618415, + 0.00613754, + 0.00608996, + 0.00604143, + 0.00599196, + 0.00594157, + 0.00589028, + 0.00583811, + 0.00578508, + 0.00573121, + 0.00567651, + 0.005621, + 0.00556472, + 0.00550766, + 0.00544987, + 0.00539135, + 0.00533213, + 0.00527223, + 0.00521166, + 0.00515046, + 0.00508865, + 0.00502623, + 0.00496325, + 0.00489972, + 0.00483565, + 0.00477108, + 0.00470603, + 0.00464052, + 0.00457457, + 0.0045082, + 0.00444144, + 0.00437431, + 0.00430684, + 0.00423904, + 0.00417094, + 0.00410256, + 0.00403393, + 0.00396506, + 0.00389599, + 0.00382673, + 0.00375731, + 0.00368775, + 0.00361808, + 0.00354831, + 0.00347846, + 0.00340857, + 0.00333866, + 0.00326874, + 0.00319884, + 0.00312898, + 0.00305918, + 0.00298947, + 0.00291986, + 0.00285039, + 0.00278107, + 0.00271192, + 0.00264296, + 0.00257423, + 0.00250572, + 0.00243747, + 0.00236949, + 0.00230181, + 0.00223445, + 0.00216742, + 0.00210075, + 0.00203445, + 0.00196854, + 0.00190305, + 0.00183799, + 0.00177337, + 0.00170922, + 0.00164555, + 0.00158238, + 0.00151973, + 0.00145762, + 0.00139605, + 0.00133505, + 0.00127464, + 0.00121482, + 0.00115561, + 0.00109702, + 0.00103908, + 0.000981789, + 0.000925166, + 0.000869223, + 0.000813972, + 0.000759427, + 0.000705598, + 0.000652497, + 0.000600136, + 0.000548526, + 0.000497676, + 0.000447596, + 0.000398297, + 0.000349786, + 0.000302074, + 0.000255167, + 0.000209075, + 0.000163804, + 0.000119361, + 7.57537e-05, + 3.2988e-05, + -8.93026e-06, + -4.99954e-05, + -9.02024e-05, + -0.000129546, + -0.000168024, + -0.000205629, + -0.000242361, + -0.000278215, + -0.000313189, + -0.000347281, + -0.00038049, + -0.000412813, + -0.000444251, + -0.000474802, + -0.000504468, + -0.000533246, + -0.000561139, + -0.000588146, + -0.000614269, + -0.000639509, + -0.000663865, + -0.00068735, + -0.000709965, + -0.000731704, + -0.000752576, + -0.000772581, + -0.000791726, + -0.000810015, + -0.000827453, + -0.000844046, + -0.0008598, + -0.000874721, + -0.000888816, + -0.00090209, + -0.000914552, + -0.000926208, + -0.000937065, + -0.000947131, + -0.000956415, + -0.000964925, + -0.000972668, + -0.000979654, + -0.000985892, + -0.000991392, + -0.000996162, + -0.00100021, + -0.00100356, + -0.0010062, + -0.00100816, + -0.00100943, + -0.00101005, + -0.00101, + -0.00100932, + -0.001008, + -0.00100606, + -0.00100351, + -0.00100036, + -0.000996634, + -0.000992332, + -0.00098747, + -0.000982062, + -0.000976119, + -0.000969654, + -0.000962682, + -0.000955214, + -0.000947263, + -0.000938844, + -0.00092997, + -0.000920652, + -0.000910906, + -0.000900744, + -0.00089018, + -0.000879227, + -0.000867898, + -0.000856207, + -0.000844168, + -0.000831793, + -0.000819098, + -0.000806094, + -0.000792798, + -0.000779217, + -0.000765372, + -0.000751266, + -0.000736922, + -0.00072229, + -0.000707464, + -0.000692506, + -0.000677361, + -0.000662049, + -0.000646575, + -0.000630955, + -0.000615196, + -0.000599314, + -0.000583319, + -0.000567225, + -0.000551043, + -0.000534785, + -0.000518464, + -0.000502091, + -0.00048568, + -0.00046924, + -0.000452784, + -0.000436324, + -0.00041987, + -0.000403434, + -0.000387027, + -0.00037066, + -0.000354342, + -0.000338084, + -0.000321898, + -0.000305792, + -0.000289776, + -0.000273861, + -0.000258056, + -0.000242369, + -0.000226811, + -0.00021139, + -0.000196115, + -0.000180995, + -0.000166036, + -0.000151248, + -0.000136638, + -0.000122213, + -0.000107981, + -9.39481e-05, + -8.01218e-05, + -6.65082e-05, + -5.31139e-05, + -3.99444e-05, + -2.70061e-05, + -1.43041e-05, + -1.844e-06, + 1.03695e-05, + 2.23311e-05, + 3.40369e-05, + 4.54822e-05, + 5.66636e-05, + 6.7577e-05, + 7.82196e-05, + 8.85877e-05, + 9.8679e-05, + 0.000108491, + 0.00011802, + 0.000127266, + 0.000136224, + 0.000144895, + 0.000153275, + 0.000161363, + 0.000169145, + 0.000176621, + 0.000183812, + 0.000190706, + 0.000197304, + 0.000203605, + 0.000209608, + 0.000215314, + 0.000220723, + 0.000225837, + 0.000230655, + 0.00023518, + 0.000239412, + 0.000243354, + 0.000247007, + 0.000250373, + 0.000253453, + 0.000256251, + 0.000258767, + 0.000261006, + 0.000262968, + 0.000264658, + 0.000266079, + 0.000267232, + 0.000268123, + 0.000268754, + 0.000269129, + 0.000269253, + 0.000269128, + 0.00026876, + 0.000268152, + 0.000267308, + 0.000266234, + 0.000264933, + 0.000263411, + 0.000261672, + 0.000259721, + 0.000257562, + 0.000255202, + 0.000252644, + 0.000249895, + 0.000246959, + 0.000243843, + 0.000240551, + 0.000237089, + 0.000233463, + 0.000229678, + 0.00022574, + 0.000221654, + 0.000217426, + 0.000213063, + 0.000208568, + 0.000203949, + 0.000199211, + 0.00019436, + 0.0001894, + 0.00018434, + 0.000179183, + 0.000173938, + 0.000168608, + 0.000163202, + 0.000157725, + 0.000152183, + 0.000146583, + 0.000140929, + 0.000135218, + 0.000129468, + 0.000123681, + 0.000117864, + 0.00011202, + 0.000106157, + 0.000100278, + 9.43895e-05, + 8.84957e-05, + 8.26029e-05, + 7.67152e-05, + 7.08393e-05, + 6.49787e-05, + 5.91403e-05, + 5.33273e-05, + 4.75467e-05, + 4.18011e-05, + 3.60975e-05, + 3.04382e-05, + 2.483e-05, + 1.9275e-05, + 1.37796e-05, + 8.34567e-06, + 2.97965e-06, + -2.31702e-06, + -7.53795e-06, + -1.2682e-05, + -1.77428e-05, + -2.27195e-05, + -2.7606e-05, + -3.24015e-05, + -3.71002e-05, + -4.17016e-05, + -4.62001e-05, + -5.05954e-05, + -5.48823e-05, + -5.90607e-05, + -6.31256e-05, + -6.70773e-05, + -7.09109e-05, + -7.46271e-05, + -7.82211e-05, + -8.16936e-05, + -8.50404e-05, + -8.82623e-05, + -9.13551e-05, + -9.43199e-05, + -9.71528e-05, + -9.98553e-05, + -0.000102424, + -0.00010486, + -0.00010716, + -0.000109326, + -0.000111356, + -0.000113251, + -0.000115009, + -0.000116633, + -0.000118119, + -0.000119471, + -0.000120685, + -0.000121766, + -0.000122709, + -0.000123518, + -0.000124185, + -0.000124729, + -0.000125145, + -0.000125428, + -0.000125578, + -0.000125598, + -0.000125487, + -0.00012525, + -0.000124884, + -0.000124397, + -0.000123784, + -0.000123053, + -0.000122201, + -0.000121235, + -0.000120152, + -0.000118959, + -0.000117652, + -0.000116241, + -0.00011472, + -0.000113099, + -0.000111372, + -0.00010955, + -0.000107627, + -0.000105614, + -0.000103505, + -0.000101311, + -9.9026e-05, + -9.66619e-05, + -9.42119e-05, + -9.16888e-05, + -8.9085e-05, + -8.64141e-05, + -8.36678e-05, + -8.08605e-05, + -7.7983e-05, + -7.50509e-05, + -7.20538e-05, + -6.90085e-05, + -6.59032e-05, + -6.27565e-05, + -5.95548e-05, + -5.63184e-05, + -5.3032e-05, + -4.97179e-05, + -4.63581e-05, + -4.29778e-05, + -3.95556e-05, + -3.61199e-05, + -3.26453e-05, + -2.91642e-05, + -2.56455e-05, + -2.21275e-05, + -1.85712e-05, + -1.50227e-05, + -1.14317e-05, + -7.85603e-06, + -4.22748e-06, + -6.22187e-07, + 3.05918e-06, + 6.70859e-06, + 1.04905e-05, + 1.42303e-05, + 1.82863e-05, + 2.2286e-05 +}; + +} // FreeDV diff --git a/libfreedv/rn_coh.h b/libfreedv/rn_coh.h new file mode 100644 index 000000000..53d4598bf --- /dev/null +++ b/libfreedv/rn_coh.h @@ -0,0 +1,609 @@ +/* Generated by rn_file() Octave function */ + +namespace FreeDV +{ + +const float gt_alpha5_root_coh[]={ + 4.05576e-05, + 2.58255e-05, + 1.58964e-05, + 5.78773e-06, + -3.71244e-06, + -1.33229e-05, + -2.2664e-05, + -3.20611e-05, + -4.12734e-05, + -5.04935e-05, + -5.9545e-05, + -6.85565e-05, + -7.73902e-05, + -8.6137e-05, + -9.46835e-05, + -0.000103097, + -0.000111281, + -0.000119289, + -0.000127034, + -0.000134559, + -0.000141789, + -0.000148756, + -0.000155393, + -0.000161723, + -0.000167689, + -0.000173315, + -0.00017854, + -0.000183382, + -0.000187794, + -0.000191793, + -0.000195333, + -0.000198429, + -0.000201038, + -0.000203173, + -0.000204797, + -0.000205922, + -0.000206515, + -0.00020659, + -0.000206117, + -0.000205109, + -0.000203541, + -0.000201427, + -0.000198743, + -0.000195505, + -0.000191693, + -0.000187324, + -0.000182382, + -0.000176885, + -0.000170822, + -0.00016421, + -0.000157041, + -0.000149335, + -0.000141089, + -0.000132323, + -0.000123038, + -0.000113258, + -0.000102985, + -9.22439e-05, + -8.10442e-05, + -6.94109e-05, + -5.73536e-05, + -4.49012e-05, + -3.20661e-05, + -1.88794e-05, + -5.35615e-06, + 8.47105e-06, + 2.25833e-05, + 3.69472e-05, + 5.15418e-05, + 6.63317e-05, + 8.12934e-05, + 9.63895e-05, + 0.000111594, + 0.000126869, + 0.000142183, + 0.000157497, + 0.000172781, + 0.000187996, + 0.000203111, + 0.000218088, + 0.000232892, + 0.000247474, + 0.000261806, + 0.000275847, + 0.000289559, + 0.000302903, + 0.000315839, + 0.00032833, + 0.000340339, + 0.000351824, + 0.000362751, + 0.00037308, + 0.000382774, + 0.000391795, + 0.000400108, + 0.000407675, + 0.000414464, + 0.000420437, + 0.000425565, + 0.000429812, + 0.000433151, + 0.000435544, + 0.000436975, + 0.000437401, + 0.000436865, + 0.000435237, + 0.00043246, + 0.000428592, + 0.000423608, + 0.000417497, + 0.00041024, + 0.000401823, + 0.000392231, + 0.000381449, + 0.000369471, + 0.000356284, + 0.000341885, + 0.000326267, + 0.00030943, + 0.000291373, + 0.000272099, + 0.000251612, + 0.000229921, + 0.000207034, + 0.000182964, + 0.000157726, + 0.000131338, + 0.000103821, + 7.51956e-05, + 4.54842e-05, + 1.4721e-05, + -1.7067e-05, + -4.98479e-05, + -8.35883e-05, + -0.000118248, + -0.00015379, + -0.000190167, + -0.000227336, + -0.000265248, + -0.000303856, + -0.000343104, + -0.000382942, + -0.00042331, + -0.000464152, + -0.000505403, + -0.000547003, + -0.000588883, + -0.000630979, + -0.000673218, + -0.000715533, + -0.000757849, + -0.000800092, + -0.000842187, + -0.000884054, + -0.000925613, + -0.000966788, + -0.00100749, + -0.00104765, + -0.00108717, + -0.00112597, + -0.00116397, + -0.00120108, + -0.0012372, + -0.00127227, + -0.00130617, + -0.00133884, + -0.00137017, + -0.00140008, + -0.00142848, + -0.00145528, + -0.0014804, + -0.00150374, + -0.00152522, + -0.00154475, + -0.00156225, + -0.00157763, + -0.00159081, + -0.00160171, + -0.00161024, + -0.00161633, + -0.0016199, + -0.00162088, + -0.00161917, + -0.00161472, + -0.00160744, + -0.00159729, + -0.00158419, + -0.00156807, + -0.00154888, + -0.00152655, + -0.00150103, + -0.00147227, + -0.00144021, + -0.00140482, + -0.00136604, + -0.00132384, + -0.00127818, + -0.00122903, + -0.00117635, + -0.00112013, + -0.00106033, + -0.000996946, + -0.000929956, + -0.000859348, + -0.000785117, + -0.000707261, + -0.000625779, + -0.00054068, + -0.000451952, + -0.000359651, + -0.000263788, + -0.00016436, + -6.13947e-05, + 4.5076e-05, + 0.000155016, + 0.000268384, + 0.000385134, + 0.000505217, + 0.000628582, + 0.000755171, + 0.000884923, + 0.00101777, + 0.00115366, + 0.00129249, + 0.00143421, + 0.00157873, + 0.00172596, + 0.00187583, + 0.00202822, + 0.00218306, + 0.00234023, + 0.00249965, + 0.00266119, + 0.00282475, + 0.00299023, + 0.00315749, + 0.00332643, + 0.00349691, + 0.00366882, + 0.00384202, + 0.00401639, + 0.0041918, + 0.0043681, + 0.00454516, + 0.00472285, + 0.00490101, + 0.00507951, + 0.00525821, + 0.00543695, + 0.0056156, + 0.005794, + 0.00597201, + 0.00614947, + 0.00632623, + 0.00650216, + 0.00667708, + 0.00685086, + 0.00702335, + 0.00719439, + 0.00736383, + 0.00753153, + 0.00769734, + 0.00786111, + 0.00802269, + 0.00818194, + 0.00833872, + 0.00849289, + 0.0086443, + 0.00879283, + 0.00893832, + 0.00908066, + 0.00921971, + 0.00935534, + 0.00948743, + 0.00961585, + 0.00974049, + 0.00986123, + 0.00997795, + 0.0100905, + 0.0101989, + 0.0103029, + 0.0104025, + 0.0104976, + 0.0105881, + 0.0106738, + 0.0107548, + 0.010831, + 0.0109022, + 0.0109684, + 0.0110295, + 0.0110855, + 0.0111364, + 0.011182, + 0.0112224, + 0.0112575, + 0.0112872, + 0.0113115, + 0.0113305, + 0.0113441, + 0.0113522, + 0.0113549, + 0.0113522, + 0.0113441, + 0.0113305, + 0.0113115, + 0.0112872, + 0.0112575, + 0.0112224, + 0.011182, + 0.0111364, + 0.0110855, + 0.0110295, + 0.0109684, + 0.0109022, + 0.010831, + 0.0107548, + 0.0106738, + 0.0105881, + 0.0104976, + 0.0104025, + 0.0103029, + 0.0101989, + 0.0100905, + 0.00997795, + 0.00986123, + 0.00974049, + 0.00961585, + 0.00948743, + 0.00935534, + 0.00921971, + 0.00908066, + 0.00893832, + 0.00879283, + 0.0086443, + 0.00849289, + 0.00833872, + 0.00818194, + 0.00802269, + 0.00786111, + 0.00769734, + 0.00753153, + 0.00736383, + 0.00719439, + 0.00702335, + 0.00685086, + 0.00667708, + 0.00650216, + 0.00632623, + 0.00614947, + 0.00597201, + 0.005794, + 0.0056156, + 0.00543695, + 0.00525821, + 0.00507951, + 0.00490101, + 0.00472285, + 0.00454516, + 0.0043681, + 0.0041918, + 0.00401639, + 0.00384202, + 0.00366882, + 0.00349691, + 0.00332643, + 0.00315749, + 0.00299023, + 0.00282475, + 0.00266119, + 0.00249965, + 0.00234023, + 0.00218306, + 0.00202822, + 0.00187583, + 0.00172596, + 0.00157873, + 0.00143421, + 0.00129249, + 0.00115366, + 0.00101777, + 0.000884923, + 0.000755171, + 0.000628582, + 0.000505217, + 0.000385134, + 0.000268384, + 0.000155016, + 4.5076e-05, + -6.13947e-05, + -0.00016436, + -0.000263788, + -0.000359651, + -0.000451952, + -0.00054068, + -0.000625779, + -0.000707261, + -0.000785117, + -0.000859348, + -0.000929956, + -0.000996946, + -0.00106033, + -0.00112013, + -0.00117635, + -0.00122903, + -0.00127818, + -0.00132384, + -0.00136604, + -0.00140482, + -0.00144021, + -0.00147227, + -0.00150103, + -0.00152655, + -0.00154888, + -0.00156807, + -0.00158419, + -0.00159729, + -0.00160744, + -0.00161472, + -0.00161917, + -0.00162088, + -0.0016199, + -0.00161633, + -0.00161024, + -0.00160171, + -0.00159081, + -0.00157763, + -0.00156225, + -0.00154475, + -0.00152522, + -0.00150374, + -0.0014804, + -0.00145528, + -0.00142848, + -0.00140008, + -0.00137017, + -0.00133884, + -0.00130617, + -0.00127227, + -0.0012372, + -0.00120108, + -0.00116397, + -0.00112597, + -0.00108717, + -0.00104765, + -0.00100749, + -0.000966788, + -0.000925613, + -0.000884054, + -0.000842187, + -0.000800092, + -0.000757849, + -0.000715533, + -0.000673218, + -0.000630979, + -0.000588883, + -0.000547003, + -0.000505403, + -0.000464152, + -0.00042331, + -0.000382942, + -0.000343104, + -0.000303856, + -0.000265248, + -0.000227336, + -0.000190167, + -0.00015379, + -0.000118248, + -8.35883e-05, + -4.98479e-05, + -1.7067e-05, + 1.4721e-05, + 4.54842e-05, + 7.51956e-05, + 0.000103821, + 0.000131338, + 0.000157726, + 0.000182964, + 0.000207034, + 0.000229921, + 0.000251612, + 0.000272099, + 0.000291373, + 0.00030943, + 0.000326267, + 0.000341885, + 0.000356284, + 0.000369471, + 0.000381449, + 0.000392231, + 0.000401823, + 0.00041024, + 0.000417497, + 0.000423608, + 0.000428592, + 0.00043246, + 0.000435237, + 0.000436865, + 0.000437401, + 0.000436975, + 0.000435544, + 0.000433151, + 0.000429812, + 0.000425565, + 0.000420437, + 0.000414464, + 0.000407675, + 0.000400108, + 0.000391795, + 0.000382774, + 0.00037308, + 0.000362751, + 0.000351824, + 0.000340339, + 0.00032833, + 0.000315839, + 0.000302903, + 0.000289559, + 0.000275847, + 0.000261806, + 0.000247474, + 0.000232892, + 0.000218088, + 0.000203111, + 0.000187996, + 0.000172781, + 0.000157497, + 0.000142183, + 0.000126869, + 0.000111594, + 9.63895e-05, + 8.12934e-05, + 6.63317e-05, + 5.15418e-05, + 3.69472e-05, + 2.25833e-05, + 8.47105e-06, + -5.35615e-06, + -1.88794e-05, + -3.20661e-05, + -4.49012e-05, + -5.73536e-05, + -6.94109e-05, + -8.10442e-05, + -9.22439e-05, + -0.000102985, + -0.000113258, + -0.000123038, + -0.000132323, + -0.000141089, + -0.000149335, + -0.000157041, + -0.00016421, + -0.000170822, + -0.000176885, + -0.000182382, + -0.000187324, + -0.000191693, + -0.000195505, + -0.000198743, + -0.000201427, + -0.000203541, + -0.000205109, + -0.000206117, + -0.00020659, + -0.000206515, + -0.000205922, + -0.000204797, + -0.000203173, + -0.000201038, + -0.000198429, + -0.000195333, + -0.000191793, + -0.000187794, + -0.000183382, + -0.00017854, + -0.000173315, + -0.000167689, + -0.000161723, + -0.000155393, + -0.000148756, + -0.000141789, + -0.000134559, + -0.000127034, + -0.000119289, + -0.000111281, + -0.000103097, + -9.46835e-05, + -8.6137e-05, + -7.73902e-05, + -6.85565e-05, + -5.9545e-05, + -5.04935e-05, + -4.12734e-05, + -3.20611e-05, + -2.2664e-05, + -1.33229e-05, + -3.71244e-06, + 5.78773e-06, + 1.58964e-05, + 2.58255e-05 +}; + +} // FreeDV diff --git a/libfreedv/rxdec_coeff.h b/libfreedv/rxdec_coeff.h new file mode 100644 index 000000000..e0600cd71 --- /dev/null +++ b/libfreedv/rxdec_coeff.h @@ -0,0 +1,40 @@ +/* Generated by rxdec_file() Octave function */ + +namespace FreeDV +{ + +const float rxdec_coeff[]={ + -0.00125472, + -0.00204605, + -0.0019897, + 0.000163906, + 0.00490937, + 0.00986375, + 0.0096718, + -0.000480351, + -0.019311, + -0.0361822, + -0.0341251, + 0.000827866, + 0.0690577, + 0.152812, + 0.222115, + 0.249004, + 0.222115, + 0.152812, + 0.0690577, + 0.000827866, + -0.0341251, + -0.0361822, + -0.019311, + -0.000480351, + 0.0096718, + 0.00986375, + 0.00490937, + 0.000163906, + -0.0019897, + -0.00204605, + -0.00125472 +}; + +} // FreeDV diff --git a/libfreedv/test_bits.h b/libfreedv/test_bits.h new file mode 100644 index 000000000..861add362 --- /dev/null +++ b/libfreedv/test_bits.h @@ -0,0 +1,170 @@ +/* Generated by test_bits_file() Octave function */ + +namespace FreeDV +{ + +const int test_bits[]={ + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 1 +}; + +} // FreeDV + diff --git a/libfreedv/test_bits_coh.h b/libfreedv/test_bits_coh.h new file mode 100644 index 000000000..e80183b6f --- /dev/null +++ b/libfreedv/test_bits_coh.h @@ -0,0 +1,569 @@ +/* Generated by test_bits_coh_file() Octave function */ + +namespace FreeDV +{ + +const int test_bits_coh[]={ + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 1, + 0, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 1, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 1, + 0, + 1, + 0, + 1, + 1, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 1, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 0, + 1, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 0, + 1, + 0, + 1, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 0 +}; + +} // FreeDV diff --git a/plugins/channelrx/demodfreedv/CMakeLists.txt b/plugins/channelrx/demodfreedv/CMakeLists.txt index 059071e2d..04c4b2fa5 100644 --- a/plugins/channelrx/demodfreedv/CMakeLists.txt +++ b/plugins/channelrx/demodfreedv/CMakeLists.txt @@ -23,7 +23,8 @@ set(freedv_FORMS include_directories( . ${CMAKE_CURRENT_BINARY_DIR} - ${CMAKE_SOURCE_DIR}/swagger/sdrangel/code/qt5/client + ${CMAKE_SOURCE_DIR}/swagger/sdrangel/code/qt5/client + ${CMAKE_SOURCE_DIR}/libfreedv ${CODEC2_INCLUDE_DIR} ) @@ -44,6 +45,7 @@ target_link_libraries(demodfreedv sdrbase sdrgui swagger + freedv ${CODEC2_LIBRARIES} ) diff --git a/plugins/channelrx/demodfreedv/freedvdemod.cpp b/plugins/channelrx/demodfreedv/freedvdemod.cpp index dee62ae3b..b2c2a9506 100644 --- a/plugins/channelrx/demodfreedv/freedvdemod.cpp +++ b/plugins/channelrx/demodfreedv/freedvdemod.cpp @@ -22,7 +22,7 @@ #include #include -#include "codec2/freedv_api.h" +#include "libfreedv.h" #include "SWGChannelSettings.h" #include "SWGFreeDVDemodSettings.h" @@ -67,12 +67,12 @@ void FreeDVDemod::FreeDVStats::init() m_fps = 1; } -void FreeDVDemod::FreeDVStats::collect(struct freedv *freeDV) +void FreeDVDemod::FreeDVStats::collect(struct FreeDV::freedv *freeDV) { - struct MODEM_STATS stats; + struct FreeDV::MODEM_STATS stats; - freedv_get_modem_extended_stats(freeDV, &stats); - m_totalBitErrors = freedv_get_total_bit_errors(freeDV); + FreeDV::freedv_get_modem_extended_stats(freeDV, &stats); + m_totalBitErrors = FreeDV::freedv_get_total_bit_errors(freeDV); m_clockOffset = stats.clock_offset; m_freqOffset = stats.foff; m_syncMetric = stats.sync_metric; @@ -150,7 +150,7 @@ FreeDVDemod::FreeDVDemod(DeviceSourceAPI *deviceAPI) : m_lowCutoff(0), m_volume(2), m_spanLog2(3), - m_sum(0), + m_sum(fftfilt::cmplx{0,0}), m_inputSampleRate(48000), m_modemSampleRate(48000), m_speechSampleRate(8000), // fixed 8 kS/s @@ -387,7 +387,7 @@ bool FreeDVDemod::handleMessage(const Message& cmd) { qDebug("FreeDVDemod::handleMessage: MsgResyncFreeDVDemod"); m_settingsMutex.lock(); - freedv_set_sync(m_freeDV, unsync); + FreeDV::freedv_set_sync(m_freeDV, FreeDV::unsync); m_settingsMutex.unlock(); return true; } @@ -593,27 +593,27 @@ void FreeDVDemod::applyFreeDVMode(FreeDVDemodSettings::FreeDVMode mode) if (fdv_mode == FREEDV_MODE_700D) { - struct freedv_advanced adv; + struct FreeDV::freedv_advanced adv; adv.interleave_frames = 1; m_freeDV = freedv_open_advanced(fdv_mode, &adv); } else { - m_freeDV = freedv_open(fdv_mode); + m_freeDV = FreeDV::freedv_open(fdv_mode); } if (m_freeDV) { - freedv_set_test_frames(m_freeDV, 0); - freedv_set_snr_squelch_thresh(m_freeDV, -100.0); - freedv_set_squelch_en(m_freeDV, 0); - freedv_set_clip(m_freeDV, 0); - freedv_set_ext_vco(m_freeDV, 0); - freedv_set_sync(m_freeDV, manualsync); + FreeDV::freedv_set_test_frames(m_freeDV, 0); + FreeDV::freedv_set_snr_squelch_thresh(m_freeDV, -100.0); + FreeDV::freedv_set_squelch_en(m_freeDV, 0); + FreeDV::freedv_set_clip(m_freeDV, 0); + FreeDV::freedv_set_ext_vco(m_freeDV, 0); + FreeDV::freedv_set_sync(m_freeDV, FreeDV::manualsync); - freedv_set_callback_txt(m_freeDV, nullptr, nullptr, nullptr); - freedv_set_callback_protocol(m_freeDV, nullptr, nullptr, nullptr); - freedv_set_callback_data(m_freeDV, nullptr, nullptr, nullptr); + FreeDV::freedv_set_callback_txt(m_freeDV, nullptr, nullptr, nullptr); + FreeDV::freedv_set_callback_protocol(m_freeDV, nullptr, nullptr, nullptr); + FreeDV::freedv_set_callback_data(m_freeDV, nullptr, nullptr, nullptr); int nSpeechSamples = freedv_get_n_speech_samples(m_freeDV); int nMaxModemSamples = freedv_get_n_max_modem_samples(m_freeDV); diff --git a/plugins/channelrx/demodfreedv/freedvdemod.h b/plugins/channelrx/demodfreedv/freedvdemod.h index ea367701c..7ba885fd9 100644 --- a/plugins/channelrx/demodfreedv/freedvdemod.h +++ b/plugins/channelrx/demodfreedv/freedvdemod.h @@ -43,7 +43,10 @@ class QNetworkReply; class DeviceSourceAPI; class ThreadedBasebandSampleSink; class DownChannelizer; + +namespace FreeDV { struct freedv; +} class FreeDVDemod : public BasebandSampleSink, public ChannelSinkAPI { Q_OBJECT @@ -208,7 +211,7 @@ private: { FreeDVStats(); void init(); - void collect(struct freedv *freedv); + void collect(struct FreeDV::freedv *freedv); bool m_sync; float m_snrEst; @@ -381,7 +384,7 @@ private: QNetworkAccessManager *m_networkManager; QNetworkRequest m_networkRequest; - struct freedv *m_freeDV; + struct FreeDV::freedv *m_freeDV; int m_nSpeechSamples; int m_nMaxModemSamples; int m_nin;