📄 ldpc_decode.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/08/19 - selvan - Removed BSC part. Incorporated MIMO. 2 x 2 * 04/03/05 - ineiti adjusted the 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-MIMO * * Successive Interference cancellation is effected by the change in variance * for every 'outer loop' iteration. The variance is sum of the variance of * thermal noise and the variance of other user(interferer). * * The variance is collected from 'sic' module. * * snr1 = H1'H1/var(interference)+var(noise1) * snr2 = H2'H2/var(interference)+var(noise2) */#include "spc.h"#include "graphcreate.h"#include "decodehelpers.h"#include "std.h"#include <math.h>#define DBG_LVL 0#define IN_BLOCKS 1#define limit(a,x,b) ( x < a ? a : ( x > b ? b : x ) )#define llr(x) limit(-25,x,25)typedef struct { // How many iterations are done in this module int iterations; // 30 // The code for the ldcp. Only 1 is working, as it's the // only one that is a multiple of 8 int ldpc_code_id; // 1 //Block-id of the SIC block. This will provide snr1, snr2. int sic; // -1}config_t;typedef struct { // How many data-bits int dnodes; // The variation on antenna 1 double var_xhat_1; // The variation on antenna 2 double var_xhat_2;}stats_t;typedef struct { // ADD HERE anything you want! graphs *graph; int *iphi; int gap; int ldpc_code_id; int bits_per_symbol; int iterations; int sic;}private_t;/* * The initialisation function, or constructor, * is called the each time this module is instantiated. */int rcv_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->ldpc_code_id = 1; config->iterations = 30; config->sic = -1; stats->dnodes = 0; stats->var_xhat_1 = 0; stats->var_xhat_2 = 0; 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; // 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 ) { // Definition of variables - don't touch config_t *config; stats_t *stats; int vnodenum=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 // 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; size_out(0) = vnodenum / 2; stats->dnodes = vnodenum - private->graph->cnodenum; PR_DBG( 2, "Put output-size to %i bytes\n", size_out(0) ); } } private->iterations =config->iterations; private->sic =config->sic; stats->var_xhat_1 = 0; stats->var_xhat_2 = 0; // 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; double_complex *out; int vnodenum=0, cnodenum=0, dnodenum=0; int *decisions; double *dec_in; int i,j,k,socket; double snr_1=0., snr_2=0., int_re=0., int_im=0.; vnode *vnode_ptr; double out_real_1, out_real_2, out_imag_1, out_imag_2; double var_xhat_1, var_xhat_2; double sum[4]; // If we have a matched-filter, then get the correct values if ( private->sic >= 0 ) { snr_1 = swr_sdb_get_stats_double( private->sic, "snr_1" ); snr_2 = swr_sdb_get_stats_double( private->sic, "snr_2" ); int_re = swr_sdb_get_stats_double( private->sic, "int_re" ); int_im = swr_sdb_get_stats_double( private->sic, "int_im" ); } PR_DBG( 4, "snr: %g/%g, interference: %g/%g\n", snr_1, snr_2, int_re, int_im ); if ( !data_available( 0 ) ) { PR_DBG( 2, "Missing data on port %i\n", 0 ); return 0; } in=buffer_in(0); out=buffer_out(0); vnodenum=(*private->graph).vnodenum; cnodenum=(*private->graph).cnodenum; // d is data nodes dnodenum=vnodenum - cnodenum; //PR_DBG(3, "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); dec_in=(double*)swr_malloc(sizeof(double)*size_in(0)*2); j = 0; //compute LLRs for symbols from 'in' to the decoder input vector 'dec_in' if( size_in(0)*2 >= vnodenum ) { for( i = 0; i < size_in(0) / 2; i++ ) { dec_in[ 4*i ] = llr( -2 * ( in[ 2*i ].real << 12 ) * snr_1 ); dec_in[ 4*i + 1 ] = llr( -2 * ( in[ 2*i ].imag << 12 ) * snr_1 ); dec_in[ 4*i + 2 ] = llr( -2 * ( in[ 2*i + 1 ].real << 12 ) * snr_2 ); dec_in[ 4*i + 3 ] = llr( -2 * ( in[ 2*i + 1 ].imag << 12 ) * snr_2 ); if ( i < 1 ) { PR_DBG( 4, "dec_in[%i] = %f\n", 4*i, dec_in[4*i] ); PR_DBG( 4, "dec_in[%i] = %f\n", 4*i+1, dec_in[4*i+1] ); PR_DBG( 4, "dec_in[%i] = %f\n", 4*i+2, dec_in[4*i+2] ); PR_DBG( 4, "dec_in[%i] = %f\n,snr1 = %f, snr2 = %f", 4*i+3, dec_in[4*i+3],snr_1,snr_2 ); } } } else { PR_DBG( 0, "size_in(0)*2<vnodenum ( %i*2<%i ) => unable to decode\n", size_in(0),vnodenum ); return 0; } initializegraph(private->graph, dec_in); for ( i=0; i<private->iterations; i++ ) { variablemessagemap(private->graph); checkmessagemap(private->graph); } //makedecisions(private->graph, decisions); //convert the LLR back to estimates vnode_ptr = (private->graph)->vnodelist; var_xhat_1 = 0.; var_xhat_2 = 0.; for( i = 0; i < vnodenum / 4 ; i++) { //sqrt(2)->normalises the QPSK symbols with unit magnitude for( k = 0; k < 4; k++){ sum[k] = vnode_ptr[ 4*i+k ].value; for( socket = 0; socket < vnode_ptr[4*i+k].degree ; socket++ ){ sum[k] += vnode_ptr[ 4*i+k ].edgelist[socket].message; } } out_real_1 = ( (1 - exp(llr(sum[0])) ) / (1 + exp(llr(sum[0])))) / sqrt(2); out_imag_1 = ( (1 - exp(llr(sum[1])) ) / (1 + exp(llr(sum[1])))) / sqrt(2); out_real_2 = ( (1 - exp(llr(sum[2])) ) / (1 + exp(llr(sum[2])))) / sqrt(2); out_imag_2 = ( (1 - exp(llr(sum[3])) ) / (1 + exp(llr(sum[3])))) / sqrt(2); //The following manipulation out[ 2*i ].real = int_re * out_real_1 - int_im * out_imag_1; out[ 2*i ].imag = int_re * out_imag_1 + int_im * out_real_1; out[ 2*i + 1].real = int_re * out_real_2 - int_im * out_imag_2; out[ 2*i + 1].imag = int_re * out_imag_2 - int_im * out_real_2; var_xhat_1 += pow( out_real_1 , 2 ) + pow( out_imag_1, 2); var_xhat_2 += pow( out_real_2 , 2 ) + pow( out_imag_2, 2); PR_DBG(4,"out_real_1 = %g\n out_imag_1 = %g \n out_real_2 = %g\n out_imag_2 = %g\nint_re = %g\n int_im = %g\n out[2*%i].real = %g\n out[2*%i].imag = %g\n ", out_real_1,out_imag_1,out_real_2,out_imag_2,int_re,int_im,i,out[2*i].real,i,out[2*i].imag); } var_xhat_1 /= ( vnodenum/4 ); var_xhat_2 /= ( vnodenum/4 ); // And calculate the snr for further use swr_sdb_get_stats_struct( context->id, (void**)&stats ); stats->var_xhat_1 = var_xhat_1; stats->var_xhat_2 = var_xhat_2; swr_sdb_free_stats_struct( context->id, (void**)&stats ); swr_free(decisions); swr_free(dec_in); 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( 1, 1, 3, 3 ); 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( "ldpc_code_id" ); UM_CONFIG_INT( "sic" ); UM_STATS_INT( "dnodes" ); UM_STATS_DOUBLE( "var_xhat_1" ); UM_STATS_DOUBLE( "var_xhat_2" ); /** * 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_OUTPUT( SIG_DOUBLE_COMPLEX, 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_soft" ); 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 + -