<commit_before>/*---------------------------------------------------------------------------*\
                                                                             
  FILE........: tfdmdv.c
  AUTHOR......: <NAME>  
  DATE CREATED: April 16 2012
                                                                             
  Tests for the C version of the FDMDV modem.  This program outputs a
  file of Octave vectors that are loaded and automatically tested
  against the Octave version of the modem by the Octave script
  tfmddv.m
                                                                             
\*---------------------------------------------------------------------------*/


/*
  Copyright (C) 2012 <NAME>

  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, 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 <http://www.gnu.org/licenses/>.
*/

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "fdmdv_internal.h"
#include "codec2_fdmdv.h"
#include "octave.h"

#define FRAMES 25
#define CHANNEL_BUF_SIZE (10*M)

int main(int argc, char *argv[])
{
    struct FDMDV *fdmdv;
    int           tx_bits[FDMDV_BITS_PER_FRAME];
    COMP          tx_symbols[NC+1];
    COMP          tx_baseband[NC+1][M];
    COMP          tx_fdm[M];
    float         channel[CHANNEL_BUF_SIZE];
    int           channel_count;
    COMP          rx_fdm[M+M/P];
    float         foff_coarse;
    int           nin, next_nin;
    COMP          rx_fdm_fcorr[M+M/P];
    COMP          rx_baseband[NC+1][M+M/P];
    COMP          rx_filt[NC+1][P+1];
    float         rx_timing;
    float         env[NT*P];
    COMP          rx_symbols[NC+1];
    int           rx_bits[FDMDV_BITS_PER_FRAME];
    float         foff_fine;
    int           sync_bit;
    int           fest_state;
    int           bad_sync;

    int           tx_bits_log[FDMDV_BITS_PER_FRAME*FRAMES];
    COMP          tx_symbols_log[(NC+1)*FRAMES];
    COMP          tx_baseband_log[(NC+1)][M*FRAMES];
    COMP          tx_fdm_log[M*FRAMES];
    COMP          pilot_baseband1_log[NPILOTBASEBAND*FRAMES];
    COMP          pilot_baseband2_log[NPILOTBASEBAND*FRAMES];
    COMP          pilot_lpf1_log[NPILOTLPF*FRAMES];
    COMP          pilot_lpf2_log[NPILOTLPF*FRAMES];
    COMP          S1_log[MPILOTFFT*FRAMES];
    COMP          S2_log[MPILOTFFT*FRAMES];
    float         foff_coarse_log[FRAMES];
    float         foff_log[FRAMES];
    COMP          rx_baseband_log[(NC+1)][(M+M/P)*FRAMES];
    int           rx_baseband_log_col_index;
    COMP          rx_filt_log[NC+1][(P+1)*FRAMES];
    int           rx_filt_log_col_index;
    float         env_log[NT*P*FRAMES];
    float         rx_timing_log[FRAMES];
    COMP          rx_symbols_log[NC+1][FRAMES];
    float         sig_est_log[NC+1][FRAMES];
    float         noise_est_log[NC+1][FRAMES];
    int           rx_bits_log[FDMDV_BITS_PER_FRAME*FRAMES];
    float         foff_fine_log[FRAMES];
    int           sync_bit_log[FRAMES];
    int           coarse_fine_log[FRAMES];
    int           nin_log[FRAMES];

    FILE         *fout;
    int           f,c,i,j;

    fdmdv = fdmdv_create(FDMDV_NC);
    next_nin = M;
    channel_count = 0;

    rx_baseband_log_col_index = 0;
    rx_filt_log_col_index = 0;

    printf("sizeof FDMDV states: %d bytes\n", sizeof(struct FDMDV));

    for(f=0; f<FRAMES; f++) {

	/* --------------------------------------------------------*\
	                          Modulator
	\*---------------------------------------------------------*/

	fdmdv_get_test_bits(fdmdv, tx_bits);
	bits_to_dqpsk_symbols(tx_symbols, FDMDV_NC, fdmdv->prev_tx_symbols, tx_bits, &fdmdv->tx_pilot_bit);
	memcpy(fdmdv->prev_tx_symbols, tx_symbols, sizeof(COMP)*(NC+1));
	tx_filter(tx_baseband, FDMDV_NC, tx_symbols, fdmdv->tx_filter_memory);
	fdm_upconvert(tx_fdm, FDMDV_NC, tx_baseband, fdmdv->phase_tx, fdmdv->freq);

	/* --------------------------------------------------------*\
	                          Channel
	\*---------------------------------------------------------*/

	nin = next_nin;
	/*
	if (f == 2)
	    nin = 120;
	if (f == 3)
	    nin = 200;
	if ((f !=2) && (f != 3))
            nin = M;
	*/
	/* add M tx samples to end of buffer */

	assert((channel_count + M) < CHANNEL_BUF_SIZE);
	for(i=0; i<M; i++)
	    channel[channel_count+i] = tx_fdm[i].real;
	channel_count += M;

	/* take nin samples from start of buffer */

	for(i=0; i<nin; i++) {
	    rx_fdm[i].real = channel[i];
            rx_fdm[i].imag = 0;
        }

	/* shift buffer back */

	for(i=0,j=nin; j<channel_count; i++,j++)
	    channel[i] = channel[j];
	channel_count -= nin;
 
	/* --------------------------------------------------------*\
	                        Demodulator
	\*---------------------------------------------------------*/

	/* freq offset estimation and correction */

	foff_coarse = rx_est_freq_offset(fdmdv, rx_fdm, nin);
	if (fdmdv->coarse_fine == COARSE)
	    fdmdv->foff = foff_coarse;
	fdmdv_freq_shift(rx_fdm_fcorr, rx_fdm, fdmdv->foff, &fdmdv->foff_rect, &fdmdv->foff_phase_rect, nin);
	
	/* baseband processing */

	fdm_downconvert(rx_baseband, FDMDV_NC, rx_fdm_fcorr, fdmdv->phase_rx, fdmdv->freq, nin);
	rx_filter(rx_filt, FDMDV_NC, rx_baseband, fdmdv->rx_filter_memory, nin);
	rx_timing = rx_est_timing(rx_symbols, FDMDV_NC, rx_filt, rx_baseband, fdmdv->rx_filter_mem_timing, env, fdmdv->rx_baseband_mem_timing, nin);	 
	foff_fine = qpsk_to_bits(rx_bits, &sync_bit, FDMDV_NC, fdmdv->phase_difference, fdmdv->prev_rx_symbols, rx_symbols);
	snr_update(fdmdv->sig_est, fdmdv->noise_est, FDMDV_NC, fdmdv->phase_difference);
	memcpy(fdmdv->prev_rx_symbols, rx_symbols, sizeof(COMP)*(NC+1));
	
	next_nin = M;
	
	if (rx_timing > 2*M/P)
	    next_nin += M/P;
    
	if (rx_timing < 0)
	    next_nin -= M/P;
	
	fdmdv->coarse_fine = freq_state(sync_bit, &fdmdv->fest_state, &bad_sync);
	fdmdv->foff  -= TRACK_COEFF*foff_fine;

	/* --------------------------------------------------------*\
	                    Log each vector 
	\*---------------------------------------------------------*/

	memcpy(&tx_bits_log[FDMDV_BITS_PER_FRAME*f], tx_bits, sizeof(int)*FDMDV_BITS_PER_FRAME);
	memcpy(&tx_symbols_log[(NC+1)*f], tx_symbols, sizeof(COMP)*(NC+1));<commit_msg>added unittest support for old and new QPSK mapping<commit_after>/*---------------------------------------------------------------------------*\
                                                                             
  FILE........: tfdmdv.c
  AUTHOR......: <NAME>  
  DATE CREATED: April 16 2012
                                                                             
  Tests for the C version of the FDMDV modem.  This program outputs a
  file of Octave vectors that are loaded and automatically tested
  against the Octave version of the modem by the Octave script
  tfmddv.m
                                                                             
\*---------------------------------------------------------------------------*/


/*
  Copyright (C) 2012 <NAME>

  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, 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 <http://www.gnu.org/licenses/>.
*/

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "fdmdv_internal.h"
#include "codec2_fdmdv.h"
#include "octave.h"

#define FRAMES 25
#define CHANNEL_BUF_SIZE (10*M)

int main(int argc, char *argv[])
{
    struct FDMDV *fdmdv;
    int           tx_bits[FDMDV_BITS_PER_FRAME];
    COMP          tx_symbols[NC+1];
    COMP          tx_baseband[NC+1][M];
    COMP          tx_fdm[M];
    float         channel[CHANNEL_BUF_SIZE];
    int           channel_count;
    COMP          rx_fdm[M+M/P];
    float         foff_coarse;
    int           nin, next_nin;
    COMP          rx_fdm_fcorr[M+M/P];
    COMP          rx_baseband[NC+1][M+M/P];
    COMP          rx_filt[NC+1][P+1];
    float         rx_timing;
    float         env[NT*P];
    COMP          rx_symbols[NC+1];
    int           rx_bits[FDMDV_BITS_PER_FRAME];
    float         foff_fine;
    int           sync_bit;
    int           fest_state;
    int           bad_sync;

    int           tx_bits_log[FDMDV_BITS_PER_FRAME*FRAMES];
    COMP          tx_symbols_log[(NC+1)*FRAMES];
    COMP          tx_baseband_log[(NC+1)][M*FRAMES];
    COMP          tx_fdm_log[M*FRAMES];
    COMP          pilot_baseband1_log[NPILOTBASEBAND*FRAMES];
    COMP          pilot_baseband2_log[NPILOTBASEBAND*FRAMES];
    COMP          pilot_lpf1_log[NPILOTLPF*FRAMES];
    COMP          pilot_lpf2_log[NPILOTLPF*FRAMES];
    COMP          S1_log[MPILOTFFT*FRAMES];
    COMP          S2_log[MPILOTFFT*FRAMES];
    float         foff_coarse_log[FRAMES];
    float         foff_log[FRAMES];
    COMP          rx_baseband_log[(NC+1)][(M+M/P)*FRAMES];
    int           rx_baseband_log_col_index;
    COMP          rx_filt_log[NC+1][(P+1)*FRAMES];
    int           rx_filt_log_col_index;
    float         env_log[NT*P*FRAMES];
    float         rx_timing_log[FRAMES];
    COMP          rx_symbols_log[NC+1][FRAMES];
    float         sig_est_log[NC+1][FRAMES];
    float         noise_est_log[NC+1][FRAMES];
    int           rx_bits_log[FDMDV_BITS_PER_FRAME*FRAMES];
    float         foff_fine_log[FRAMES];
    int           sync_bit_log[FRAMES];
    int           coarse_fine_log[FRAMES];
    int           nin_log[FRAMES];

    FILE         *fout;
    int           f,c,i,j;

    fdmdv = fdmdv_create(FDMDV_NC);
    next_nin = M;
    channel_count = 0;

    rx_baseband_log_col_index = 0;
    rx_filt_log_col_index = 0;

    printf("sizeof FDMDV states: %d bytes\n", sizeof(struct FDMDV));

    for(f=0; f<FRAMES; f++) {

	/* --------------------------------------------------------*\
	                          Modulator
	\*---------------------------------------------------------*/

	fdmdv_get_test_bits(fdmdv, tx_bits);
	bits_to_dqpsk_symbols(tx_symbols, FDMDV_NC, fdmdv->prev_tx_symbols, tx_bits, &fdmdv->tx_pilot_bit, 0);
	memcpy(fdmdv->prev_tx_symbols, tx_symbols, sizeof(COMP)*(NC+1));
	tx_filter(tx_baseband, FDMDV_NC, tx_symbols, fdmdv->tx_filter_memory);
	fdm_upconvert(tx_fdm, FDMDV_NC, tx_baseband, fdmdv->phase_tx, fdmdv->freq);

	/* --------------------------------------------------------*\
	                          Channel
	\*---------------------------------------------------------*/

	nin = next_nin;
	/*
	if (f == 2)
	    nin = 120;
	if (f == 3)
	    nin = 200;
	if ((f !=2) && (f != 3))
            nin = M;
	*/
	/* add M tx samples to end of buffer */

	assert((channel_count + M) < CHANNEL_BUF_SIZE);
	for(i=0; i<M; i++)
	    channel[channel_count+i] = tx_fdm[i].real;
	channel_count += M;

	/* take nin samples from start of buffer */

	for(i=0; i<nin; i++) {
	    rx_fdm[i].real = channel[i];
            rx_fdm[i].imag = 0;
        }

	/* shift buffer back */

	for(i=0,j=nin; j<channel_count; i++,j++)
	    channel[i] = channel[j];
	channel_count -= nin;
 
	/* --------------------------------------------------------*\
	                        Demodulator
	\*---------------------------------------------------------*/

	/* freq offset estimation and correction */

	foff_coarse = rx_est_freq_offset(fdmdv, rx_fdm, nin);
	if (fdmdv->coarse_fine == COARSE)
	    fdmdv->foff = foff_coarse;
	fdmdv_freq_shift(rx_fdm_fcorr, rx_fdm, fdmdv->foff, &fdmdv->foff_rect, &fdmdv->foff_phase_rect, nin);
	
	/* baseband processing */

	fdm_downconvert(rx_baseband, FDMDV_NC, rx_fdm_fcorr, fdmdv->phase_rx, fdmdv->freq, nin);
	rx_filter(rx_filt, FDMDV_NC, rx_baseband, fdmdv->rx_filter_memory, nin);
	rx_timing = rx_est_timing(rx_symbols, FDMDV_NC, rx_filt, rx_baseband, fdmdv->rx_filter_mem_timing, env, fdmdv->rx_baseband_mem_timing, nin);	 
	foff_fine = qpsk_to_bits(rx_bits, &sync_bit, FDMDV_NC, fdmdv->phase_difference, fdmdv->prev_rx_symbols, rx_symbols, 0);
	snr_update(fdmdv->sig_est, fdmdv->noise_est, FDMDV_NC, fdmdv->phase_difference);
	memcpy(fdmdv->prev_rx_symbols, rx_symbols, sizeof(COMP)*(NC+1));
	
	next_nin = M;
	
	if (rx_timing > 2*M/P)
	    next_nin += M/P;
    
	if (rx_timing < 0)
	    next_nin -= M/P;
	
	fdmdv->coarse_fine = freq_state(sync_bit, &fdmdv->fest_state, &bad_sync);
	fdmdv->foff  -= TRACK_COEFF*foff_fine;

	/* --------------------------------------------------------*\
	                    Log each vector 
	\*---------------------------------------------------------*/

	memcpy(&tx_bits_log[FDMDV_BITS_PER_FRAME*f], tx_bits, sizeof(int)*FDMDV_BITS_PER_FRAME);
	memcpy(&tx_symbols_log[(NC+1)*f], tx_symbols, sizeof(COMP)*(NC+1));
{'fraction_non_alphanumeric': 0.19375370041444642, 'fraction_numerical': 0.009029011249259917, 'mean_word_length': 1.7753132059971246, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7175380', 'n_tokens_mistral': 4792, 'n_tokens_neox': 4577, 'n_words': 919}