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

📄 block_complex.c

📁 软件无线电的平台
💻 C
字号:
/*************************************************************************** *    block_mimo.c  - Define a MIMO-block with complex input and output *                           ------------------- *   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 * 04/03/03 - ineiti - added border-zone for input and output * 04/03/04 - ineiti - changed config-structure (arrays instead of variables) * 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 module gives a general 2x2 MIMO block with definable * channel-matrix h and noise-variance sigma. *  Additionally to the size, a precision in bits can be given, * too. This precision is used to cut off the transmitted signal, * which simulates a perfect channel, but with some error due to the * A/D converters. */#include <stdlib.h>#include "spc.h"#define DBG_LVL 0#define RX_TX_DELAY 10#define MALLOC_BORDER 256typedef struct {  // The number of symbols of the block  int size;      // 2560  // The precision of the A/D converter  int precision; // 12    // The variance of the gaussian noise at the receiver  complex double sigma[2]; // 100 + 100j  // The channel matrix  complex double h[2][2]; // identity}config_t;typedef struct {}stats_t;typedef struct {  int size;  int mask;  double sigma_1_real;  double sigma_1_imag;  double sigma_2_real;  double sigma_2_imag;  unsigned short simrand[3];  double h11_real;  double h11_imag;  double h12_real;  double h12_imag;  double h21_real;  double h21_imag;  double h22_real;  double h22_imag;  SYMBOL_COMPLEX *in[2], *out[2];}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;  int i, j;  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;  for ( i=0; i<2; i++ ){    port_in(i).flags |= SWR_PORT_OWN_MALLOC;    port_in(i).data = 0;    port_out(i).flags |= SWR_PORT_OWN_MALLOC;    port_out(i).data = 0;    private->in[i] = private->out[i] = NULL;  }  // We set the standard UMTS-size for the slot  config->size = 2560;  config->precision = 12;  for ( i=0; i<2; i++ ){    config->sigma[i] = 100 + 100 * I;     // Channel parameters    for ( j=0; j<2; j++ ){      config->h[i][j] = i == j;    }  }  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 i;  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 );    for ( i=0; i<2; i++ ){      size_in(i) = config->size;      size_out(i) = config->size;      if ( private->size ){	swr_free( private->in[i] );	swr_free( private->out[i] );      }            private->in[i] = swr_malloc( ( config->size + 2 * MALLOC_BORDER ) *				   sizeof( SYMBOL_COMPLEX ) );      port_in(i).data = private->in[i] + MALLOC_BORDER;            private->out[i] = swr_malloc( ( config->size + 2 * MALLOC_BORDER ) *				 sizeof( SYMBOL_COMPLEX ) );      port_out(i).data = private->out[i] + MALLOC_BORDER;    }    private->size = config->size;  }  private->mask = -1 << ( 16 - config->precision );  private->sigma_1_real = creal( config->sigma[0] );  private->sigma_1_imag = cimag( config->sigma[0] );  private->sigma_2_real = creal( config->sigma[1] );  private->sigma_2_imag = cimag( config->sigma[1] );  private->h11_real   = creal( config->h[0][0] );  private->h11_imag   = cimag( config->h[0][0] );  private->h12_real   = creal( config->h[0][1] );  private->h12_imag   = cimag( config->h[0][1] );  private->h21_real   = creal( config->h[1][0] );  private->h21_imag   = cimag( config->h[1][0] );  private->h22_real   = creal( config->h[1][1] );  private->h22_imag   = cimag( config->h[1][1] );    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;}/* * 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 *in1, *out1;  SYMBOL_COMPLEX *in2, *out2;  int i, re, im;  double h11_real;  double h11_imag;  double h12_real;  double h12_imag;  double h21_real;  double h21_imag;  double h22_real;  double h22_imag;  double a, sigma_max_1, sigma_max_2;  if ( !data_available(0) || !data_available(1) ) {    if ( port_in(0).sdb_id >= 0 && port_in(1).sdb_id >= 0 ){      PR_DBG( 4, "Not all data here yet\n" );      return 0;    } else if ( data_available(0) ){      in1  = in2 = buffer_in(0);      out1 = out2 = buffer_out(0);    } else {      PR_DBG( 4, "No data available. Strange\n" );      return 0;    }  } else {    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);  }  h11_real   = private->h11_real;  h11_imag   = private->h11_imag;  h12_real   = private->h12_real;  h12_imag   = private->h12_imag;  h21_real   = private->h21_real;  h21_imag   = private->h21_imag;  h22_real   = private->h22_real;  h22_imag   = private->h22_imag;  sigma_max_1 = max( private->sigma_1_real, private->sigma_1_imag );  sigma_max_2 = max( private->sigma_2_real, private->sigma_2_imag );  a = ( ( 1 << 15 ) - 3 * max( sigma_max_1, sigma_max_2 ) ) / ( 4 * ( 1 << 15 ) );  PR_DBG( 4, "A is %f, mask is %x\n", a, private->mask );    if ( private->size ) {    for ( i=0; i<private->size; i++ ) {      re = (   ( h11_real * in1->real )	     - ( h11_imag * in1->imag )	     + ( h12_real * in2->real )	     - ( h12_imag * in2->imag ) ) * a	   + getSigma( context, private->sigma_1_real );      im = (   ( h11_imag * in1->real )	     + ( h11_real * in1->imag )	     + ( h12_imag * in2->real )	     + ( h12_real * in2->imag ) ) * a	   + getSigma( context, private->sigma_1_imag );      re = max( re, -1 << 15 );      re = min( re,  1 << 15 );      im = max( im, -1 << 15 );      im = min( im,  1 << 15 );      out1->real = re & private->mask;      out1->imag = im & private->mask;      re = (   ( h21_real * in1->real )	     - ( h21_imag * in1->imag )	     + ( h22_real * in2->real )	     - ( h22_imag * in2->imag ) ) * a	   + getSigma( context, private->sigma_2_real );      im = (   ( h21_imag * in1->real )	     + ( h21_real * in1->imag )	     + ( h22_imag * in2->real )	     + ( h22_real * in2->imag ) ) * a	   + getSigma( context, private->sigma_2_imag );      re = max( re, -1 << 15 );      re = min( re,  1 << 15 );      im = max( im, -1 << 15 );      im = min( im,  1 << 15 );      out2->real = re & private->mask;      out2->imag = im & private->mask;      in1++;      in2++;      out1++;      out2++;    }  }  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( 2, 2, 14, 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_INT( "precision" );  UM_CONFIG_DOUBLE( "sigma_1_real" );  UM_CONFIG_DOUBLE( "sigma_1_imag" );  UM_CONFIG_DOUBLE( "sigma_2_real" );  UM_CONFIG_DOUBLE( "sigma_2_imag" );  UM_CONFIG_DOUBLE( "h11_real" );  UM_CONFIG_DOUBLE( "h11_imag" );  UM_CONFIG_DOUBLE( "h12_real" );  UM_CONFIG_DOUBLE( "h12_imag" );  UM_CONFIG_DOUBLE( "h21_real" );  UM_CONFIG_DOUBLE( "h21_imag" );  UM_CONFIG_DOUBLE( "h22_real" );  UM_CONFIG_DOUBLE( "h22_imag" );  /**   * 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_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;  // And register the module in the SPM. Change the name!  cdb_id = swr_cdb_register_spc( &desc, "block_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 + -