📄 spread_rcv.c
字号:
/*************************************************************************** * spread_rcv.c - A despreading-module * ------------------- * begin : 2003 * authors : ineiti * emails : linus.gasser@epfl.ch ***************************************************************************//*************************************************************************** * Changes * ------- * date - name - description * 03/01/28 - ineiti - begin * 03/02/10 - ineiti - Added more than one output-port * 04/03/05 - ineiti - adjusted description * **************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************//** * This is the despreader, taking the input-signal and doing an * internal convolution. */#include "spc.h"#include "spread.h"#define DBG_LVL 0#define MAX_OUTPUTS 8typedef struct { // The factor is the spreading-sequence length int factor[MAX_OUTPUTS]; // 8 // Each bit is one spreading-symbol. 0 is -1, 1 is 1... int sequence[MAX_OUTPUTS]; // 0x0f}config_t;typedef struct { // The snr of output 0 after despreading double snr;}stats_t;typedef struct { int factor[MAX_OUTPUTS]; int table[MAX_OUTPUTS][ MAX_SPREAD_LEN ];}private_t;/* * The initialisation function, or constructor, * is called the first time this module is instantiated. */int rcv_init( swr_sdb_t *context ) { int i; config_t *config; stats_t *stats; MOD_INC_USE_COUNT; if ( sizeof( private_t ) > 0 ) context->private_data = swr_malloc( sizeof( private_t ) ); swr_sdb_get_config_struct( context->id, (void**)&config ); swr_sdb_get_stats_struct( context->id, (void**)&stats ); for ( i=0; i<MAX_OUTPUTS; i++ ) { private->factor[i] = config->factor[i] = 0; config->sequence[i] = 0; } // Let's define a spreading-sequence with length of 8 config->factor[0] = 8; // The spreading sequence is -1 -1 -1 -1 1 1 1 1 config->sequence[0] = 0x0f; swr_sdb_free_stats_struct( context->id, (void**)&stats ); swr_sdb_free_config_struct( context->id, (void**)&config ); return 0; // End system-definitions}/* * To configure the inputs * this is called when the output-sizes change. */int rcv_configure_inputs( swr_sdb_t *context ) { // Definition of variables - don't touch config_t *config; int i, size_in = 0, s; swr_sdb_get_config_struct( context->id, (void**)&config ); /** * We search for all ports that are * 1) connected and have a size * 2) have a corresponding factor > 0 */ for ( i=0; i<MAX_OUTPUTS; i++ ) { if ( size_out(i) && config->factor[i] && ( port_out(i).sdb_id >= 0 ) ) { s = size_out(i) * config->factor[i]; PR_DBG( 4, "i:%i, s:%i, size_in:%i(%i)\n", i, s, size_in, config->factor[i]); if ( !size_in ) { size_in = s; } else if ( s != size_in ) { PR_DBG( 0, "Asked for different sized input! %i->%i at port %i, *:%i\n", size_in, s, i, config->factor[i] ); size_in = min( size_in, s ); } } } PR_DBG( 3, "Input-size is %i\n", size_in ); size_in(0) = size_in; // Definition - don't touch swr_sdb_free_config_struct( context->id, (void**)&config ); return 0;}/* * To configure the outputs * this is called when the input-sizes change */int rcv_configure_outputs( swr_sdb_t *context ) { // Definition of variables - don't touch config_t *config; int i; swr_sdb_get_config_struct( context->id, (void**)&config ); for ( i=0; i<MAX_OUTPUTS; i++ ) { if ( config->factor[i] ) { size_out(i) = size_in(0) / config->factor[i]; } else { size_out(i) = 0; } PR_DBG( 4, "Setting output-port %i to %i(%i)\n", i, size_out(i), config->factor[i] ); } // Definition - don't touch swr_sdb_free_config_struct( context->id, (void**)&config ); return 0;}/* * Every time modules from the outside change the value of a configuration parameter, * this function is called. */int rcv_reconfig( swr_sdb_t *context ) { // Definition of variables - don't touch config_t *config; int i, j, bits, factor; swr_sdb_get_config_struct( context->id, (void**)&config ); for ( i=0; i<MAX_OUTPUTS; i++ ) { if ( config->factor[i] < 0 ){ config->factor[i] = 0; } factor = private->factor[i] = config->factor[i]; if ( factor > 0 ) { PR_DBG( 4, "Factor %i is %i and consists of %x: ", i, factor, config->sequence[i] ); size_out(i) = size_in(0) / config->factor[i]; for ( j=0, bits=config->sequence[i]; j<factor; j++, bits /= 2 ) { private->table[i][j] = ( bits & 1 ) * 2 - 1; PR_DBG_CL( 4, "%2i ", private->table[i][j] ); } PR_DBG_CL( 4, "\n" ); } } // Definition - don't touch swr_sdb_free_config_struct( context->id, (void**)&config ); return 0;}/* * This is the function that implements the `main method' of the class * Every class has got just ONE method/working-mode. */int rcv_pdata( swr_sdb_t *context ) { // Definition of variables - don't touch stats_t *stats; SYMBOL_COMPLEX *in, *out = NULL; int i, j, s, real, imag; double data_amp, noise_var_symbols; for ( s=0; s<MAX_OUTPUTS; s++ ) { if ( private->factor[s] ) { out = buffer_out(s); memset( out, 0, sizeof( SYMBOL_COMPLEX ) * size_out(s) ); in = buffer_in(0); PR_DBG( 4, "In:%p, Out:%p\n", in, out ); for ( i=0; i<size_in(0) / private->factor[s]; i++ ) { for ( j=real=imag=0; j<private->factor[s]; j++ ) { real += in->real * private->table[s][j]; imag += in->imag * private->table[s][j]; in++; } out->real = real / private->factor[s]; out->imag = imag / private->factor[s]; PR_DBG( 4, "(%5i,%5i)->(%5i,%5i)\n", in[-1].real, in[-1].imag, out->real, out->imag ); out++; } } }#if DBG_LVL == 4 in = buffer_in(0); for ( i=0; i<10; i++ ) { PR_DBG_CL( 4, "(%4i:%4i) ", in[i].real, in[i].imag ); } PR_DBG_CL( 4, "\n" );#endif // Calculate power of the received signal data_amp = 0.; out = buffer_out(0); PR_DBG( 3, "out: %p, factor: %i\n", out, private->factor[0] ); if ( out ){ for ( i=0; i<size_out(0); i++ ){ data_amp += out[i].real * out[i].real + out[i].imag * out[i].imag; } data_amp /= size_out( 0 ); // noise_var is the noise-variance of the received chips, while we need // the variance of the received symbols. // \sig_symbols = \sig_chips / spreading_length noise_var_symbols = ((double)swr_sdb_get_stats_int( port_in(0).sdb_id, "noise_var" )) / private->factor[0]; } else { data_amp = 1; noise_var_symbols = 1; } swr_sdb_get_stats_struct( context->id, (void**)&stats ); // Make sure nothing generates a SIGFAULT if ( ( data_amp <= 0 ) || ( noise_var_symbols <= 0 ) || ( data_amp <= noise_var_symbols ) ){ PR_DBG( 2, "data_amp: %s%i.%i, noise_var_symbols: %s%i.%i\n", swr_ftosii( data_amp ), swr_ftosii( noise_var_symbols ) ); stats->snr = 0; } else { stats->snr = 10 * ( log10( data_amp / noise_var_symbols - 1. ) ); } swr_sdb_free_stats_struct( context->id, (void**)&stats ); return(0);}/* * This is the `destructor'. */int rcv_finalize( swr_sdb_t *context ) { if ( sizeof( private_t ) > 0 ) swr_free( private ); MOD_DEC_USE_COUNT; return 0;}/* * This function is called upon "insmod" and is used to register the * different parts of the module to the SPM. */swr_spc_id_t rcv_id;int rcv_module_init(void) { swr_spc_desc_t *desc; char str[256]; int i; /** * Get a description-part from SPM * Give the following parameters: * Input-ports, output-ports, config-params, stat-params */ desc = swr_spc_get_new_desc( 1, MAX_OUTPUTS, 2 * MAX_OUTPUTS, 1 ); if ( !desc ) { PR_DBG( 0, "Can't initialise the module. This is BAD!\n" ); return -1; } /** * Define the different parts of config and stats. You have to define * them in the same order as they appear in the structures. The names * can be freely chosen. * * UM_CONFIG_{INT,DOUBLE,STRING128,POINTER}( "name" ); * UM_STATS_{INT,DOUBLE,STRING128,POINTER,BLOCK}( "name" ); */ for ( i=0; i<MAX_OUTPUTS; i++ ) { sprintf( str, "factor%02i", i ); UM_CONFIG_INT( str ); } for ( i=0; i<MAX_OUTPUTS; i++ ) { sprintf( str, "sequence%02i", i ); UM_CONFIG_INT( str ); } UM_STATS_DOUBLE( "snr" ); /** * The in- and outputs have also to be defined in the right order. First * port first. The additional flag is not used yet, but it will... * * UM_INPUT( SIG_{U8,SYMBOL_{S16,COMPLEX,MMX},SAMPLE_S12,S32}, 0 ); * UM_OUTPUT( SIG_{U8,SYMBOL_{S16,COMPLEX,MMX},SAMPLE_S12,S32}, 0 ); */ for ( i=0; i<MAX_OUTPUTS; i++ ) { UM_OUTPUT( SIG_SYMBOL_COMPLEX, 0 ); } UM_INPUT( SIG_SYMBOL_COMPLEX, 0 ); // Initialise the callback-functions. Delete the ones you don't use desc->fn_init = rcv_init; desc->fn_reconfigure = rcv_reconfig; desc->fn_process_data = rcv_pdata; desc->fn_configure_inputs = rcv_configure_inputs; desc->fn_configure_outputs = rcv_configure_outputs; desc->fn_finalize = rcv_finalize; // And register the module in the SPM. Change the name! rcv_id = swr_cdb_register_spc( &desc, "despread" ); if ( rcv_id == SWR_SPM_INVALID_ID ) { swr_spc_free_desc( desc ); PR_DBG( 0, "Couldn't register the module!\n" ); return 1; } PR_DBG( 4, "Ready\n" ); return 0;}/* * This is called upon rmmod */void rcv_module_exit( void ) { PR_DBG( 4, "Freeing id: %i\n", rcv_id ); if ( swr_cdb_unregister_spc( rcv_id ) < 0 ) { PR_DBG( 0, "Still in use somewhere\n" ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -