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

📄 ldpc_decode.c

📁 This a framework to test new ideas in transmission technology. Actual development is a LDPC-coder in
💻 C
字号:
/*************************************************************************** *    ldpc_decode.c  - LDPC decoding *                           ------------------- *   begin                :  01/2003 *   authors              :  Bernhard Bruhn *   emails               :  bernhard.bruhn@epfl.ch ***************************************************************************//*************************************************************************** *                                Changes *                                ------- * date - name - description * 03/04/25 - ineiti - added snr to the stats, so that one can check what *                     snr has been used when decoding * 03/05/09 - ineiti - added a done-stats to know when it's finished * * 03/09/01 - chiurtu - modified for the 2fold decoding (either multiple taps *                      or multiple antennas) * 10 Dec 2003 - chiurtu - modified for 4 by 4 MIMO * 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.                                   * *                                                                         * ***************************************************************************//** * LDPC-decoder using the 2fold-techinque to decode a stream coming from a * MIMO environment. */#include "spc.h"#include "graphcreate.h"#include "decodehelpers.h"#include "std.h"#include <complex.h>#define DBG_LVL 0#define MAX_NO_RX 4#define MAX_NO_TX 4typedef struct {  // The total iteration-number. For each iteration, iterations_left  // followed by iterations_right are done.  int iterations; // 30  // How many iterations to the left are done per total iteration  int iterations_left; // 1  // How many iterations to the right are done per total iteration  int iterations_right; // 1  // The ID of the LDPC code  int ldpc_code_id; // 1  // How many input channels  int no_tx; // 4  // How many output channels  int no_rx; // 4  //Block-id of the ChEst  blocks. They will provide H  int chest [MAX_NO_RX]; // [-1 -1 -1 -1]} config_t;typedef struct{  // The calculated SNR in case of a AWGN channel  double snr;} stats_t;typedef struct{  graphs *graph;  int *iphi;  int gap;  int ldpc_code_id;  int bits_per_symbol;  int iterations;  int iterations_left;  int iterations_right;  int no_tx;  int no_rx;  int chest[MAX_NO_RX];} private_t;/* * The initialisation function, or constructor, * is called the each time this module is instantiated. */int rcv_init( swr_sdb_t *context ) {  int i;  // 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->ldpc_code_id	= 1;  config->iterations	= 30;  config->iterations_right = 1;  config->iterations_left = 1;    config->no_rx = MAX_NO_RX;  config->no_tx = MAX_NO_TX;  for ( i=0; i<MAX_NO_RX; i++){    config->chest[i] = -1;  }    stats->snr = -2.3;  private->bits_per_symbol= 2;  private->ldpc_code_id = -1;    //allocate memory for graph  private->graph=(graphs *)swr_malloc(sizeof(graphs));  private->graph->vnodenum=0;  private->graph->cnodenum=0;  private->graph->f = NULL;      // 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 rcv_reconfig( swr_sdb_t *context ) {  int i;  // Definition of variables - don't touch  config_t *config;  stats_t *stats;  int vnodenum=0, cnodenum=0;  swr_sdb_get_config_struct( context->id, (void**)&config );  swr_sdb_get_stats_struct( context->id, (void**)&stats );  // Put you code here  // ADD HERE       private->graph->bits_per_left_checknode = config->no_tx * 2;  if ( private->graph->bits_per_left_checknode ){    PR_DBG( 4, "BPLCN: %i\n", private->graph->bits_per_left_checknode );    // put new code into private->graph if config->ldpc_code_id has changed    if(config->ldpc_code_id!=-1 && private->ldpc_code_id!=config->ldpc_code_id) {      PR_DBG( 2, "loading ldpc code with ID %i\n", config->ldpc_code_id );      if(private->ldpc_code_id!=-1) freeGraph(private->graph, private->iphi);      private->ldpc_code_id = config->ldpc_code_id;      if ( getGraph(private->ldpc_code_id, private->graph, &(private->gap), &(private->iphi)) ){	private->ldpc_code_id = -1;      } else {	vnodenum=private->graph->vnodenum;	cnodenum=private->graph->cnodenum;	size_out(0) = ( vnodenum - cnodenum ) / 8;	PR_DBG( 2, "Put output-size to %i bytes\n", size_out(0) );      }    }  }  private->iterations	=config->iterations;  private->iterations_left	=config->iterations_left;  private->iterations_right	=config->iterations_right;  private->no_rx = config->no_rx;  private->no_tx = config->no_tx;      for ( i=0; i<config->no_rx; i++){    private->chest[i] = config->chest[i] ;  }    // Definition - don't touch  swr_sdb_free_stats_struct( context->id, (void**)&stats );  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 ) {    stats_t *stats;  SYMBOL_COMPLEX *in[MAX_NO_RX], *ch[MAX_NO_RX];  SYMBOL_COMPLEX y[MAX_NO_RX], h[MAX_NO_RX][MAX_NO_TX];  U8 *out;  int vnodenum=0, cnodenum=0, dnodenum=0;  int *decisions;  int i, j, n, k, p, ch_length;  int var_real[MAX_NO_RX], var_imag[MAX_NO_RX];  double x[private->graph->bits_per_left_checknode];  double f;  complex double u, z, w;  PR_DBG( 4, "Entry\n" );    // If we have a matched-filter, then get the correct values  // Return if not all data available yet  for ( i=0; i<private->no_rx; i++ ){    if ( !data_available( i ) ){      PR_DBG( 3, "Not all data here yet\n" );      return -1;    }  }    for (i = 0; i < private->no_rx; i++){    in[i] =  buffer_in(i);  }  out=buffer_out(0);    vnodenum=(*private->graph).vnodenum;  cnodenum=(*private->graph).cnodenum;  // d is data nodes  dnodenum=vnodenum - cnodenum;    PR_DBG( 1, "vnodenum=%d, cnodenum=%d, size_in=%d bytes\n", 	  vnodenum, cnodenum, size_in(0));    PR_DBG( 2,"allocating %i decisions, dec_in\n", vnodenum);  decisions=(int*)swr_malloc(sizeof(int)*vnodenum);  //AWGN Channel  //compute LLRs for symbols from 'in' to the decoder input vector 'dec_in'  if( size_in(0) * private->graph->bits_per_left_checknode < vnodenum ) {    PR_DBG( 0, "size_in(0)*bits_per_left_checknode<vnodenum ( %i*2<%i ) => "	    "unable to decode\n", 	    size_in(0) * private->graph->bits_per_left_checknode, vnodenum );    return -1;  } else {    // TODO: implement initialisation of the input    // The respectives f_b's are    // graph->f[node * ( 1 << private->graph->bits_per_left_checknode ) + b ]    // in1 comes from rcv-antenna 1    // in2 comes from rcv-antenna 2    // h11 = ch1[0]    // h12 = ch1[ch_length]    // h21 = ch2[0]    // h22 = ch2[ch_length]    // var1 is the variance of rcv-antenna 1    // var2 is the variance of rcv-antenna 2        for ( i=0; i<private->no_rx; i++ ){      if ( private->chest[i] < 0 ){	PR_DBG( 0, "One of the chest's is not defined\n" );	return -1;      }    }        for ( i=0; i<private->no_rx; i++ ){      var_real[i] = max( swr_sdb_get_stats_int( private->chest[i], "noise_var_real" ), 1 );      var_imag[i] = max( swr_sdb_get_stats_int( private->chest[i], "noise_var_imag" ), 1 );      ch[i] = swr_sdb_get_stats_block( private->chest[i], "channel" ).data;    }    ch_length = swr_sdb_get_stats_int( private->chest[0], "ch_length" );    // =============== ME !  PAY ATTENTION !!! :-))    // Here we prepare the table of f's needed for the "left" itterations    // Attention at the mapping! we map [x0 x1 x2 x3] in bar[x3 x2 x1 x0]    // This is because we need them like that in the algorithm in  decodehelpers.c    for ( i=0; i<private->no_rx; i++ ) {      for ( j=0; j<private->no_tx; j++ ) {	h[i][j] = ch[i][j*ch_length];      }    }        // print the channel and the variance to compare with the one which we estimate later    // works for a = 0.01        for ( i=0; i<private->no_rx; i++ ){      for ( j=0; j<private->no_tx; j++ ){	PR_DBG_CL( 4, "est-h%i%i(%i:%ii) ", i, j, 		   h[i][j].real,		   h[i][j].imag );      }    }        for ( i=0; i<private->no_rx; i++ ){      PR_DBG_CL( 4, "est-var%i(%i:%ii) ", i, 		 var_real[i], var_imag[i] );    }    PR_DBG_CL( 4,"\n");    for (n=0; n<vnodenum/private->graph->bits_per_left_checknode; n++){      //    for (n=0; n<20; n++){      for ( i=0; i<private->no_rx; i++ ){	y[i] = in[i][n];	PR_DBG_CL( 4, "y[%i](%i:%ii) ", i, y[i].real, y[i].imag );      }                  for( i  = 0; i < (1 << private->graph->bits_per_left_checknode); i++){	for(j = 0; j < private->graph->bits_per_left_checknode; j++){	  x[j] = ((1 << j) & i) ? -1:1;	  x[j] /= sqrt(2);	  PR_DBG_CL( 4, "%2g ", x[j] );	}		p = n*( 1 << private->graph->bits_per_left_checknode )+i;		// compute y-Hx and sum and get f		f = 0.0;		for ( j=0; j<private->no_rx; j++ ) {	  w = 0;	  for ( k=0; k<private->no_tx; k++ ){	    z = x[2*k] + x[2*k+1] * I;	    u = h[j][k].real + h[j][k].imag * I;	    w += z * u;	  }	  	  f += pow( (y[j].real - creal( w )), 2)/(2*var_real[j]) + 	    pow( (y[j].imag - cimag( w )), 2)/(2*var_imag[j]);	  	}		private->graph->f[p] = -f;		PR_DBG_CL( 4,"f = %g \n", -f)      }          }        PR_DBG_CL( 4," f[0] = %g, f[1] = %g, f[2] = %g, f[3] = %g \n", private->graph->f[0], private->graph->f[1], private->graph->f[2], private->graph->f[3]);     }      initializegraph(private->graph);  for ( i=0; i<private->iterations; i++ ){    for ( j=0; j<private->iterations_left; j++ ){      leftfoldmap( private->graph );    }    for ( j=0; j<private->iterations_right; j++ ){      variablemessagemap(private->graph);      checkmessagemap(private->graph);    }  }  makedecisions(private->graph, decisions);  //copy decoded data to output  j=-1;  if( size_out(0)*8 < dnodenum ){    PR_DBG( 1,"output to small: decoded information (%i) does not fit in %i\n",	    size_out(0) * 8, dnodenum );  }  for ( i=0; i<min( dnodenum, size_out(0)*8 ); i++ ){    if ( !( i % 8 ) ){      j++;      out[j] = 0;    }    out[j] += decisions[i] << ( i % 8 );  }  for ( i=dnodenum; i<size_out(0)*8; i++ ){    if ( !( i % 8 ) ){      j++;      out[j] = 0;    }  }  // Clean up  swr_free (decisions);  // And calculate the snr for further use  swr_sdb_get_stats_struct( context->id, (void**)&stats );  swr_sdb_free_stats_struct( context->id, (void**)&stats );  PR_DBG( 4, "exiting\n" );  return(0);}/** * User messages */int rcv_custom_msg( swr_sdb_t *context, swr_usr_msg_t *data, swr_msgq ret ){  return 0;}/* * This is the `destructor'. */int rcv_finalize( swr_sdb_t *context ){  swr_free( private->graph );  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;//  int i;  /**   * Get a description-part from CDB   * Give the following parameters:   * Input-ports, output-ports, config-params, stat-params   */  desc = swr_spc_get_new_desc( MAX_NO_RX, 1, 10, 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" );   */  UM_CONFIG_INT( "iterations" );  UM_CONFIG_INT( "iterations_left" );  UM_CONFIG_INT( "iterations_right" );  UM_CONFIG_INT( "ldpc_code_id" );  UM_CONFIG_INT( "no_tx" );  UM_CONFIG_INT( "no_rx" );  UM_CONFIG_INT( "chest" );  UM_CONFIG_INT( "chest2" );  UM_CONFIG_INT( "chest3" );  UM_CONFIG_INT( "chest4" );    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 );   */  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_U8, 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_custom_msg        = rcv_custom_msg;  desc->fn_finalize          = rcv_finalize;  // And register the module in the SPM. Change the name!  rcv_id = swr_cdb_register_spc( &desc, "ldpc_decode_2fold_general" );  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 + -