📄 block_mimo.c
字号:
/*************************************************************************** * block_mimo.c - Define a MIMO-block with an input or output-size * ------------------- * begin : 03/07/03 * authors : Selvavinayagam Gunabalan * emails : selvan@kth.se ***************************************************************************//*************************************************************************** * Changes * ------- * date - name - description * 03/07/03 - selvan - took from block.c and added MIMO * 04/03/05 - ineiti - adjusted configuration-structure a bit * 04/03/05 - ineiti - adjusted documentation * **************************************************************************//*************************************************************************** * * * 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 block simulates the channel interface between two transmitting * antennas with two receivers. This block also adds noise. * The block implements the following, * y = Hx + n * * where, * 'y = [y1 y2]' ' is the received vector or the output of this module. * * 'H = [a11 a12; * a21 a22] is the channel matrix. * * 'x = [x1 x2]' ' is the transmit vector or the input of this module. * * 'n = [n1 n2]' ' is the AWGN. n1,n2 are independent. */#include <stdlib.h>#include "spc.h"#define DBG_LVL 0#define MALLOC_BORDER 256#define RX_TX_DELAY 0typedef struct { // The size in samples int size; // 2560 * 4 // variance of the gaussian noise double sigma; // 100 // amplitude of the white noise int noise_amplitude; // 0 // Channel Transmission Coefficients double a[2][2]; // [ 1 .25 ; .25 1 ]}config_t;typedef struct {}stats_t;typedef struct { int size; SAMPLE_S12 *in1, *out1; SAMPLE_S12 *in2, *out2; double sigma; int noise_amplitude; unsigned short simrand[3]; double a11; //Channel Transmission Coefficients double a12; double a21; double a22;}private_t;/* * The initialisation function, or constructor, * is called the first time this module is instantiated. */int spc_init( swr_sdb_t *context ) { // Begin system-definitions { 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 ); // } End of system-definitions // Tell the subsystem we don't accept resizes, but only emit them context->status |= SUBS_STATUS_RESIZE_NONE; private->size = 0; // We set the standard UMTS-size for the slot config->size = 2560 * 4; config->noise_amplitude = 0; config->sigma = 100; //New parameters config->a[0][0] = 1; config->a[0][1] = 0.25; config->a[1][0] = 0.25; config->a[1][1] = 1; port_in(0).flags |= SWR_PORT_OWN_MALLOC; port_in(0).data = 0; port_out(0).flags |= SWR_PORT_OWN_MALLOC; port_out(0).data = 0; //verify with Linus port_in(1).flags |= SWR_PORT_OWN_MALLOC; port_in(1).data = 0; port_out(1).flags |= SWR_PORT_OWN_MALLOC; port_out(1).data = 0; private->in1 = private->out1 = 0; private->in2 = private->out2 = 0; srand( (int)get_time_usec()%100000000 ); // initialize the random generator private->simrand[0]=(unsigned short)((get_time_usec()>>00)&0xffff); private->simrand[1]=(unsigned short)((get_time_usec()>>16)&0xffff); private->simrand[2]=(unsigned short)((get_time_usec()>>00)&0xffff); // Begin system-definitions 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 in- and out-puts * this is called when the io-sizes change. */int spc_configure_puts( swr_sdb_t *context, config_t *config ) { // If any size changed, free eventual data-pointers, and allocate // the size PR_DBG( 4, "Size: %i, addr: %p\n", ( private->size + 2 * MALLOC_BORDER ) * sizeof( SAMPLE_S12 ), private->in1 ); // (Re-)allocate the internal data-pointers if ( !private->in1 ) { private->in1 = swr_malloc( ( private->size + 2 * MALLOC_BORDER ) * sizeof( SAMPLE_S12 ) ); port_in(0).data = private->in1 + MALLOC_BORDER; } if ( !private->in2 ) { private->in2 = swr_malloc( ( private->size + 2 * MALLOC_BORDER ) * sizeof( SAMPLE_S12 ) ); port_in(1).data = private->in2 + MALLOC_BORDER; } if ( !private->out1 ) { private->out1 = swr_malloc( ( private->size + 2 * MALLOC_BORDER ) * sizeof( SAMPLE_S12 ) ); port_out(0).data = private->out1 + MALLOC_BORDER; } if ( !private->out2 ) { private->out2 = swr_malloc( ( private->size + 2 * MALLOC_BORDER ) * sizeof( SAMPLE_S12 ) ); port_out(1).data = private->out2 + MALLOC_BORDER; } return 0;}/* * Every time modules from the outside change the value of a configuration parameter, * this function is called. */int spc_reconfig( swr_sdb_t *context ) { // Definition of variables - don't touch config_t *config; swr_sdb_get_config_struct( context->id, (void**)&config ); if ( config->size != private->size ) { PR_DBG( 2, "Re-setting sizes: %i, %i\n", config->size, private->size ); private->size = config->size; size_in(0) = config->size; size_out(0) = config->size; size_out(1) = config->size; size_in(1) = config->size; swr_free( private->in1 ); swr_free( private->out1 ); swr_free( private->in2 ); swr_free( private->out2 ); private->in1 = private->out1 = NULL; private->in2 = private->out2 = NULL; spc_configure_puts( context, config ); } private->noise_amplitude = config->noise_amplitude; private->sigma = config->sigma; private->a11 = config->a[0][0]; private->a12 = config->a[0][1]; private->a21 = config->a[1][0]; private->a22 = config->a[1][1]; 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 spc_pdata( swr_sdb_t *context ) { // Definition of variables - don't touch stats_t *stats; SAMPLE_S12 *in1, *out1; SAMPLE_S12 *in2, *out2; int i,j; double u, v; double alpha, w; double a11,a12,a21,a22; if ( !data_available(0) || !data_available(1) ) { PR_DBG( 4, "Not all data here yet\n" ); return 0; } PR_DBG( 4, "Data is here, going on\n" ); // Just to transmit the PORT_DATA flag from the input // to the output in1 = buffer_in(0); out1 = buffer_out(0); in2 = buffer_in(1); out2 = buffer_out(1); a11 = private->a11; a12 = private->a12; a21 = private->a21; a22 = private->a22; if ( private->size ) { in1 = private->in1; out1 = private->out1; in2 = private->in2; out2 = private->out2; for ( i=0; i<private->size + 2 * MALLOC_BORDER - RX_TX_DELAY; i++ ) { out1[ i ] = ( a11 * in1[ i + RX_TX_DELAY ] + a12 * in2[ i + RX_TX_DELAY ] ) / 16 / 2; //Quantization Error - Linus out2[ i ] = ( a21 * in1[ i + RX_TX_DELAY ] + a22 * in2[ i + RX_TX_DELAY ] ) / 16 / 2; out1[ i ] += ((2.0 * private->noise_amplitude)*rand()/(RAND_MAX+1.0)) - private->noise_amplitude; out2[ i ] += ((2.0 * private->noise_amplitude)*rand()/(RAND_MAX+1.0)) - private->noise_amplitude; // j= 0; do { u = 2*erand48(private->simrand)-1; v = 2*erand48(private->simrand)-1; w = u*u + v*v; j++; } while (w>=1); // if ( j > 1 ){ // PR_DBG( 4, "while-loop done: %i\n", j ); //} alpha = sqrt(-2*log(w)/w); out1[i] += private->sigma*alpha*u; out1[i] = max( out1[i], (short int)-2048 ); out1[i] = min( out1[i], (short int)2047 ); out2[i] += private->sigma*alpha*v; out2[i] = max( out2[i], (short int)-2048 ); out2[i] = min( out2[i], (short int)2047 ); } } swr_sdb_get_stats_struct( context->id, (void**)&stats ); // Put your code here // ADD HERE swr_sdb_free_stats_struct( context->id, (void**)&stats ); return(0);}/* * This is the `destructor'. */int spc_finalize( swr_sdb_t *context ) { // Free the input and output buffers swr_free( private->in1 ); swr_free( private->out1 ); swr_free( private->in2 ); swr_free( private->out2 ); 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 cdb_id;int spc_module_init(void) { swr_spc_desc_t *desc; /** * Get a description-part from SPM * Give the following parameters: * Input-ports, output-ports, config-params, stat-params */ desc = swr_spc_get_new_desc( 2, 2, 7, 0 ); 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" ); */ UM_CONFIG_INT( "size" ); UM_CONFIG_DOUBLE( "sigma" ); UM_CONFIG_INT( "noise_amplitude" ); UM_CONFIG_DOUBLE( "a11" ); UM_CONFIG_DOUBLE( "a12" ); UM_CONFIG_DOUBLE( "a21" ); UM_CONFIG_DOUBLE( "a22" ); /** * 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 ); */ UM_INPUT( SIG_SAMPLE_S12, 0 ); UM_INPUT( SIG_SAMPLE_S12, 0 ); UM_OUTPUT( SIG_SAMPLE_S12, 0 ); UM_OUTPUT( SIG_SAMPLE_S12, 0 ); // Initialise the callback-functions. Delete the ones you don't use desc->fn_init = spc_init; desc->fn_reconfigure = spc_reconfig; desc->fn_process_data = spc_pdata; desc->fn_finalize = spc_finalize; // And register the module in the SPM. Change the name! cdb_id = swr_cdb_register_spc( &desc, "block_mimo" ); if ( cdb_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 spc_module_exit( void ) { PR_DBG( 4, "Freeing id: %i\n", cdb_id ); if ( swr_cdb_unregister_spc( cdb_id ) < 0 ) { PR_DBG( 0, "Still in use somewhere\n" ); }}module_init( spc_module_init );module_exit( spc_module_exit );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -