⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 block_4by4_complex.c

📁 软件无线电的平台
💻 C
字号:
/*************************************************************************** *    block_4by4_complex.c  - Defines a variable, complex NxM MIMO block *                           ------------------- *   begin                :  03/09/03 *   authors              :  Linus Gasser *   emails               :  linus.gasser@epfl.ch ***************************************************************************//*************************************************************************** *                                Changes *                                ------- * date - name - description * 03/07/03 - ineiti - took block_mimo to make block_complex * 09 Dec 2003 - chiurtu - extended to 4 by 4 system * 04/03/04 - ineiti - auto-detect number of rx and tx ports * 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 is used for simulations of the channel. It implements * a NxM MIMO-system, where N and M are <= 4. *  One can give the size of the block in samples as well as the * precision of the A/D converter. *  The channel-matrix h[n][m] represents the channel between the * tx-antenna (input) 'm' and the rx-antenna (output) 'n'. *  For the receiving-antenna you can give the sigma of the white noise. * Please be aware that the sigma is between -32767 and 32767, and compares * to a signal-amplitude set by the modules (usually around 10000), whereas * the more conventional sigma compares to a signal-amplitude of 1.  */#include <stdlib.h>#include <complex.h>#include "spc.h"#define DBG_LVL 0#define RX_TX_DELAY 0#define MAX_NO_RX 4#define MAX_NO_TX 4typedef struct {  // The number of symbols of the block  int size;      // 2560  // The precision of the A/D converter  int precision; // 12  // channel entries over rows    complex double h[MAX_NO_RX][MAX_NO_TX]; // 0.5 + 0.5j  // The sigma of the gaussian-noise at the receiver  complex double sigma[MAX_NO_RX]; // 100 + 100j}config_t;typedef struct {  // The maximum number of reception antennas  int max_no_rx;  // The maximum number of transmission antennas  int max_no_tx;}stats_t;typedef struct {  int size;  int mask;  // How many input channels  int no_tx;  // How many output channels  int no_rx;    // channel entries over rows    complex double h[MAX_NO_RX][MAX_NO_TX];  // this is for Gaussian  complex double sigma[MAX_NO_RX];  // for the random-number generator  unsigned short simrand[3];}private_t;/* * The initialisation function, or constructor, * is called the first time this module is instantiated. */int spc_init( swr_sdb_t *context ) {  int i, j;  // 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_BOTH;  private->size = 0;  // We set the standard UMTS-size for the slot  config->size = 2560;  config->precision = 16;  stats->max_no_rx = MAX_NO_RX;  stats->max_no_tx = MAX_NO_TX;  // Channel parameters  for(i = 0; i < MAX_NO_RX; i++){    for(j = 0; j < MAX_NO_TX; j++){      config->h[i][j] = 0.5 + 0.5I;    }  }    for(j = 0; j < MAX_NO_RX; j++){    config->sigma[j] = 100 + 100I;  }    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}/* * 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;  int ch;  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;    for ( ch=0; ch<MAX_NO_TX; ch++ ){      size_in(ch) = config->size;    }    for ( ch=0; ch<MAX_NO_RX; ch++ ){      size_out(ch) = config->size;    }  }  private->mask = -1 << ( 16 - config->precision );    // Copy the config-parameters to the private array  memcpy( private->sigma, config->sigma, sizeof( config->sigma ) );  memcpy( private->h, config->h, sizeof( config->h ) );    swr_sdb_free_config_struct( context->id, (void**)&config );  return 0;}int spc_configure_inputs( swr_sdb_t *context ) {  // Definition of variables - don't touch  config_t *config;  int ch;  swr_sdb_get_config_struct( context->id, (void**)&config );  private->no_tx = 0;  for ( ch=0; ch<MAX_NO_TX; ch++ ){    if ( port_in( ch ).sdb_id >= 0 ){      private->no_tx++;    }  }  PR_DBG( 4, "Got %i Tx and %i Rx ports\n",	  private->no_tx, private->no_rx );  // Definition - don't touch  swr_sdb_free_config_struct( context->id, (void**)&config );  return 0;}/* * This is called when the input-sizes change * It configures the outputs */int spc_configure_outputs( swr_sdb_t *context ) {  // Definition of variables - don't touch  config_t *config;  int ch;  swr_sdb_get_config_struct( context->id, (void**)&config );  private->no_rx = 0;  for ( ch=0; ch<MAX_NO_RX; ch++ ){    if ( port_out( ch ).sdb_id >= 0 ){      private->no_rx++;    }  }  PR_DBG( 4, "Got %i Tx and %i Rx ports\n",	  private->no_tx, private->no_rx );  // Definition - don't touch  swr_sdb_free_config_struct( context->id, (void**)&config );  return 0;}double getSigma( swr_sdb_t *context, double amplitude ){  double u, v, w, alpha;  do {    u = 2*erand48(private->simrand)-1;    v = 2*erand48(private->simrand)-1;        w = u*u + v*v;  } while (w>=1);  alpha = sqrt( -2 * log(w)/w);  return alpha * u * amplitude;}complex double getSigmaComplex( swr_sdb_t *context, complex double amplitude ){    return getSigma( context, creal( amplitude ) ) +    getSigma( context, cimag( amplitude ) ) * I;}/* * 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;  SYMBOL_COMPLEX *in[MAX_NO_TX];  SYMBOL_COMPLEX *out[MAX_NO_RX];  int i, j, k, re, im;  double a;  complex double x, y;  //  double plus_real, minus_real,	plus_imag1, plus_imag2;  /* // Return if not all data available yet */    for ( i=0; i<private->no_tx; i++ ){      if ( !data_available( i ) ){      PR_DBG( 3, "Not all data here yet. Data to port %i is missing\n",i );      return 0;    }  }  PR_DBG( 3, "Processing data: size is %i, in(%i), out(%i)\n", 	  private->size, private->no_rx, private->no_tx );#if DBG_LVL >= 3  PR_DBG( 3, "h is " );  for ( i=0; i<private->no_rx; i++ ){    for ( j=0; j<private->no_tx; j++ ){      PR_DBG_CL( 3, "%g+%gi   ", creal( private->h[i][j] ),		 cimag( private->h[i][j] ) );    }  }  PR_DBG_CL( 3, "\n" );  PR_DBG( 3, "sigma is " );  for ( i=0; i<private->no_rx; i++ ){    PR_DBG_CL( 3, "%g+%gi   ", creal( private->sigma[i] ),	       cimag( private->sigma[i] ) );  }  PR_DBG_CL( 3, "\n" );#endif    for (i = 0; i < private->no_tx; i++){    in[i] =  buffer_in(i);  }  for (i = 0; i < private->no_rx; i++){    out[i] = buffer_out(i);  }    // this is still hard coded, to omit a saturation of the channel  a= 0.1;  PR_DBG( 4, "A is %f, mask is %x\n", a, private->mask );    if ( private->size ) {    for ( i=0; i<private->size; i++ ) {      for ( j=0; j<private->no_rx; j++ ) {	y = 0;	for ( k=0; k<private->no_tx; k++ ){	  x = in[k][i].real + in[k][i].imag * I;	  y += x * private->h[j][k];	}	y += getSigmaComplex( context, private->sigma[j] );	y *= a;		re = creal( y );	im = cimag( y );		re = max( re, -1 << 15 );	re = min( re,  1 << 15 );	im = max( im, -1 << 15 );	im = min( im,  1 << 15 );		out[j][i+RX_TX_DELAY].real = re & private->mask;	out[j][i+RX_TX_DELAY].imag = im & private->mask;      }    }  }    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 ) {  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( MAX_NO_TX, MAX_NO_RX, 22, 2 );  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_INT( "precision" );  UM_CONFIG_DOUBLE_COMPLEX( "h" );  UM_CONFIG_DOUBLE_COMPLEX( "h12" );  UM_CONFIG_DOUBLE_COMPLEX( "h13" );  UM_CONFIG_DOUBLE_COMPLEX( "h14" );  UM_CONFIG_DOUBLE_COMPLEX( "h2" );  UM_CONFIG_DOUBLE_COMPLEX( "h22" );  UM_CONFIG_DOUBLE_COMPLEX( "h23" );  UM_CONFIG_DOUBLE_COMPLEX( "h24" );  UM_CONFIG_DOUBLE_COMPLEX( "h3" );  UM_CONFIG_DOUBLE_COMPLEX( "h32" );  UM_CONFIG_DOUBLE_COMPLEX( "h33" );  UM_CONFIG_DOUBLE_COMPLEX( "h34" );  UM_CONFIG_DOUBLE_COMPLEX( "h4" );  UM_CONFIG_DOUBLE_COMPLEX( "h42" );  UM_CONFIG_DOUBLE_COMPLEX( "h43" );  UM_CONFIG_DOUBLE_COMPLEX( "h44" );  UM_CONFIG_DOUBLE_COMPLEX( "sigma" );  UM_CONFIG_DOUBLE_COMPLEX( "sigma2" );  UM_CONFIG_DOUBLE_COMPLEX( "sigma3" );  UM_CONFIG_DOUBLE_COMPLEX( "sigma4" );  UM_STATS_INT( "max_no_rx" );  UM_STATS_INT( "max_no_tx" );  /**   * 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_SYMBOL_COMPLEX, 0 );  UM_INPUT( SIG_SYMBOL_COMPLEX, 0 );  UM_INPUT( SIG_SYMBOL_COMPLEX, 0 );  UM_INPUT( SIG_SYMBOL_COMPLEX, 0 );  UM_OUTPUT( SIG_SYMBOL_COMPLEX, 0 );  UM_OUTPUT( SIG_SYMBOL_COMPLEX, 0 );  UM_OUTPUT( SIG_SYMBOL_COMPLEX, 0 );  UM_OUTPUT( SIG_SYMBOL_COMPLEX, 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;  desc->fn_configure_inputs  = spc_configure_inputs;  desc->fn_configure_outputs = spc_configure_outputs;  // And register the module in the SPM. Change the name!  cdb_id = swr_cdb_register_spc( &desc, "block_4by4_complex" );  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 + -