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

📄 sic.c

📁 This a framework to test new ideas in transmission technology. Actual development is a LDPC-coder in
💻 C
字号:
/*************************************************************************** *    sic.c  - Successive Interference Cancellation 2 x 2  *                           ------------------- *   begin                : 20 Aug 2003  *   authors              : Selvavinayagam Gunabalan *   emails               : selvan@kth.se ***************************************************************************//*************************************************************************** *                                Changes *                                ------- * date - name - description * 08/20/2003 - selvan - Begin * 09/16/2003 - Information bits are 'buffered out' in every iteration * 04/03/08 - 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.                                   * *                                                                         * ***************************************************************************//** * Successive Interference Cancellation (SIC) :-) */#include "spc.h"#include "std.h"#include <math.h>#define DBG_LVL 0typedef struct {  // The first ChEst id  int chest_1; // -1  // The second ChEst id  int chest_2; // -1  // The id of the ldpc-decoder  int ldpc; // -1  // Number of times the soft-values are iterated with LDPC before decision  int num_of_outer_loops; // 4  // Whether the data-port should be outputted on each iteration  // (for debugging and tracing purposes)  int data_always; // 0}config_t;typedef struct {  // The calculated SNR from ChEst 1  double snr_1;  // The calculated SNR from ChEst 2  double snr_2;  // The real part of some internal var  double int_re;  // The imag part of some internal var  double int_im;  int iterations;}stats_t;typedef struct {  int chest_1;  int chest_2;  int num_of_outer_loops;  int dnodes;  int iterations;  int ldpc;  int data_always;}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  config->chest_1            = -1;  config->chest_2            = -1;  config->ldpc               = -1;  config->num_of_outer_loops = 4;  config->data_always        = 0;  stats->iterations          = 0;  // 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;  swr_sdb_get_config_struct( context->id, (void**)&config );  if (config->chest_1 != -1 && config->chest_2 != -1) {    private->chest_1 = config->chest_1;    private->chest_2 = config->chest_2;  } else {    PR_DBG( 4, "Channel Estimates Mandatory for this block\n" );  }  private->ldpc = config->ldpc;  private->data_always = config->data_always;  private->num_of_outer_loops = config->num_of_outer_loops;  // Definition - don't touch  swr_sdb_free_config_struct( context->id, (void**)&config );  return 0;}int spc_configure_outputs( swr_sdb_t *context ) {  config_t *config;  swr_sdb_get_config_struct( context->id, (void**)&config );  if ( size_in(0) == 0) //LDPC decoder not yet initialised  {    swr_sdb_free_config_struct( context->id, (void**)&config );    return 0;  } else {    if ( config->ldpc >= 0 ) {      private->dnodes = swr_sdb_get_stats_int( config->ldpc, "dnodes" );      PR_DBG( 4, "size_in(2) = %i, size_out(0) = %i, dnodes = %i, new_size_out = %i\n",              size_in(2), size_out(0), private->dnodes,              private->dnodes / 2 );      size_out(0) = private->dnodes / 2;    } else {      size_out(0) = 0;    }    size_out(1) = size_in(0);  }  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;  SYMBOL_COMPLEX *in_ant1, *in_ant2, *out_data, *out_decode;  SYMBOL_COMPLEX h11,h12,h21,h22;  double_complex *in_ldpc;  double int1mag,int1_re, int1_im,a,b,c;  double var_int_noise1, var_int_noise2;  double var_xhat_1, var_xhat_2;  int re1, im1, re2, im2;  block_t h1,h2;  int i;  double snr_1 = 1.;  double snr_2 = 1.;  int noise_var1 = 10;  int noise_var2 = 10;  PR_DBG( 4, "Inputs: %i, %i, %i\n",          data_available(0), data_available(1), data_available(2) );  if((!data_available(1) || !data_available(2))  && !data_available(0) ) {    PR_DBG( 3, "No data on any of the inputs\n" );    return 0;  }  in_ldpc = buffer_in( 0 );  if( data_available(1) ) {    //This cond is true only in the first iteration.    //Bcos of the flag reset by the following buffer_in macro    PR_DBG( 3, "Going to clear memory\n" );    memset(in_ldpc, 0, size_in(0) * sizeof( double_complex ) );    private->iterations = 0;    // This resets the ldpc-decoder    swr_sdb_send_msg( private->ldpc, SUBS_MSG_RECONFIG, NULL, -1 );  }  in_ant1 = buffer_in( 1 );  in_ant2 = buffer_in( 2 );  //Get the channel coefficients h11,h12,h21,h22,noise variances from the chest blocks  //Data will not be available on the first output in the first iteration  //Since the first iteration does not contain the estimated symbol  out_data = NULL;  out_decode = NULL;  //this if is necessary for the termination of the outer loop  PR_DBG( 4, "Doing iteration %i\n", private->iterations);  h1 = swr_sdb_get_stats_block( private->chest_1, "channel" );  h2 = swr_sdb_get_stats_block( private->chest_2, "channel" );  noise_var1 = swr_sdb_get_stats_int( private->chest_1, "noise_var" );  noise_var2 = swr_sdb_get_stats_int( private->chest_2, "noise_var" );  h11.real = ((SYMBOL_COMPLEX *)h1.data)->real;  h11.imag = ((SYMBOL_COMPLEX *)h1.data)->imag;  //16 -> channel Length, to be Generalized  h12.real = ((SYMBOL_COMPLEX *)h1.data + 16)->real;  h12.imag = ((SYMBOL_COMPLEX *)h1.data + 16)->imag;  h21.real = ((SYMBOL_COMPLEX *)h2.data)->real;  h21.imag = ((SYMBOL_COMPLEX *)h2.data)->imag;  h22.real = ((SYMBOL_COMPLEX *)h2.data + 16)->real;  h22.imag = ((SYMBOL_COMPLEX *)h2.data + 16)->imag;  int1_re =  h11.real * h12.real + h11.imag * h12.imag +             h21.real * h22.real + h21.imag * h22.imag;  int1_im =  h11.real * h12.imag - h11.imag * h12.real +             h21.real * h22.imag - h21.imag * h22.real;  PR_DBG(4,"h11=%i+j%i  h12=%i+j%i  h21=%i+j%i  h22=%i+j%i\n",         h11.real,h11.imag,h12.real,h12.imag,h21.real,h21.imag,h22.real,h22.imag);  //dumb loops, MMX,sse optimizations later  if ( ++private->iterations <= private->num_of_outer_loops ) {    PR_DBG( 4, "Calculating loop: %i\n", private->iterations );    out_decode = buffer_out(1);    for (i = 0; i < size_out( 1 )/2 ; i++ ) {      a  = h11.real * in_ant1[ i ].real + h11.imag * in_ant1[ i ].imag;      b  = h21.real * in_ant2[ i ].real + h21.imag * in_ant2[ i ].imag;      c  = in_ldpc[ 2*i + 1].real;      re1 = a + b - c;      PR_DBG(4,"Real:\na = %g\n b =%g\n c =%g\n", a, b, c);      a  = h11.real * in_ant1[ i ].imag - h11.imag * in_ant1[ i ].real;      b  = h21.real * in_ant2[ i ].imag - h21.imag * in_ant2[ i ].real;      c  = in_ldpc[ 2*i + 1].imag;      im1 = a + b - c;      PR_DBG(4,"Img:\na = %g\n b =%g\n c =%g\n", a, b, c);      re2 = h12.real * in_ant1[ i ].real + h12.imag * in_ant1[ i ].imag            + h22.real * in_ant2[ i ].real + h22.imag * in_ant2[ i ].imag            - in_ldpc[ 2*i ].real;      im2 = h12.real * in_ant1[ i ].imag - h12.imag * in_ant1[ i ].real            + h22.real * in_ant2[ i ].imag - h22.imag * in_ant2[ i ].real            - in_ldpc[ 2*i ].imag;      out_decode[2*i].real = re1 >> 12;      out_decode[2*i].imag = im1 >> 12;      out_decode[2*i+1].real = re2 >> 12;      out_decode[2*i+1].imag = im2 >> 12;    }    // If it's the last iteration, or if we have data_always, we fill the    // data-output    if ( ( private->iterations == private->num_of_outer_loops ) ||         ( private->data_always ) ) {      out_data = buffer_out(0);      memcpy(out_data,out_decode,size_out(0)*sizeof(SYMBOL_COMPLEX));      PR_DBG(4,"In sic module for loop, %i,%i",out_data[0].real,private->iterations);    }    // If it's the last iteration, don't allow the decoder to work    if ( private->iterations == private->num_of_outer_loops ) {      buffer_out_clr( 1 );    }  }  var_xhat_1 = swr_sdb_get_stats_double( private->ldpc, "var_xhat_1" );  var_xhat_2 = swr_sdb_get_stats_double( private->ldpc, "var_xhat_2" );  int1mag = int1_re * int1_re  + int1_im * int1_im;  a  = ( 1 - var_xhat_2) * int1mag;  b  = (double)noise_var1 * ( h11.real * h11.real + h11.imag * h11.imag );  c  = (double)noise_var2 * ( h21.real * h21.real + h21.imag * h21.imag );  var_int_noise1  = a + b + c;  PR_DBG(4,"\n a = %g \n b =%g \n c=%g \n noise_var= %i \n"         " h11.real = %i \n h11.imag = %i \n h21.real =%i \n h21.imag = %i \n"         " var_int_noise1 = %g\n bagain = %g\n",         a,b,c,noise_var1,h11.real,h11.imag,h21.real,h21.imag,         var_int_noise1, (double)noise_var1 * ( h11.real * h11.real +                                                h11.imag * h11.imag ));  a  = ( 1 - var_xhat_1) * int1mag;  b  = (double)noise_var1 * ( h12.real * h12.real + h12.imag * h12.imag );  c  = (double)noise_var2 * ( h22.real * h22.real + h22.imag * h22.imag );  var_int_noise2  = a + b + c;  PR_DBG(4,"\n a = %g \n b =%g \n c=%g \n var_int_noise2 = %g\n",         a,b,c, var_int_noise2);  snr_1   = (h11.real * h11.real + h11.imag * h11.imag +             h21.real * h21.real + h21.imag * h21.imag);  snr_2   = (h12.real * h12.real + h12.imag * h12.imag +             h22.real * h22.real + h22.imag * h22.imag);  PR_DBG( 4," Var_xhat_1 = %g \n Var_xhat_2 = %g \n snr_1 = %g\n snr_2 = %g\n  int1mag = %g\n noise_var1 = %i\n noise_var2 = %i\n",var_xhat_1,var_xhat_2,snr_1,snr_2,int1mag,noise_var1,noise_var2);  PR_DBG( 3," %g \t  %g ;\n",var_xhat_1,var_xhat_2);  swr_sdb_get_stats_struct( context->id, (void**)&stats );  stats->snr_1 = snr_1 / var_int_noise1;  stats->snr_2 = snr_2 / var_int_noise2;  stats->int_re = int1_re;  stats->int_im = int1_im;  stats->iterations = private->iterations;  PR_DBG( 4,"snr_1 = %g   snr_2 = %g\n", stats->snr_1,stats->snr_2);  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( 3, 2, 5, 5 );  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( "chest_1" );  UM_CONFIG_INT( "chest_2" );  UM_CONFIG_INT( "ldpc" );  UM_CONFIG_INT( "num_of_outer_loops" );  UM_CONFIG_INT( "data_always" );  UM_STATS_DOUBLE( "snr_1" );  UM_STATS_DOUBLE( "snr_2" );  UM_STATS_DOUBLE( "int_re" );  UM_STATS_DOUBLE( "int_im" );  UM_STATS_INT( "iterations" );  /*   * 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_DOUBLE_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 );  // 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_outputs = spc_configure_outputs;  // And register the module in the SPM. Change the name!  cdb_id = swr_cdb_register_spc( &desc, "sic" );  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 + -