📄 ldpc_encode.c
字号:
/*************************************************************************** * ldpc_encode.c - LDPC encoding * ------------------- * begin : 01/2003 * authors : Bernhard Bruhn * emails : bernhard.bruhn@epfl.ch ***************************************************************************//*************************************************************************** * Changes * ------- * date - name - description * 03/x/x - nicou - enhanced for the general NxM case where N and M <= 4 * 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. * * * ***************************************************************************//** * Encodes a slot. Needs to have a modulator-module with a QPSK * modulation. */#include "spc.h"#include "graphcreate.h"#include "encodehelpers.h"#include <math.h>#define DBG_LVL 0typedef struct { // The index of the ldpc-code. For the moment, three codes // are implemented: // 1 - rate .25, size 4000 // 2 - rate .5, size 3992 // 3 - rate .75, size 3992 int ldpc_code_id; // 1 // Bits per left check node, for a QPSK transmission, this is // the number of TX-antennas * 2 int bplcn; // 0} config_t;typedef struct{} stats_t;typedef struct{ graphs *graph; int *iphi; int gap; int ldpc_code_id; int error;} private_t;/* * The initialisation function, or constructor, * is called the first time this module is instantiated. */int send_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 private->ldpc_code_id =-1; config->ldpc_code_id =1; config->bplcn = 0; //allocate memory for graph private->graph=(graphs *)swr_malloc(sizeof(graphs)); private->graph->f = NULL; private->graph->vnodenum=0; private->graph->cnodenum=0; private->error = -1; // 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 send_reconfig( swr_sdb_t *context ){ // Definition of variables - don't touch config_t *config; int vnodenum=0, cnodenum=0; swr_sdb_get_config_struct( context->id, (void**)&config ); private->graph->bits_per_left_checknode = config->bplcn; 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->error) freeGraph(private->graph, private->iphi); private->ldpc_code_id=config->ldpc_code_id; private->error = getGraph(private->ldpc_code_id, private->graph, &(private->gap), &(private->iphi) ); if ( !private->error ){ vnodenum=(*private->graph).vnodenum; cnodenum=(*private->graph).cnodenum; size_in(0) = ( vnodenum - cnodenum ) / 8; PR_DBG( 2, "Put input-size to %i bytes\n", size_in(0) ); } } } // Definition - don't touch 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 send_pdata( swr_sdb_t *context ){ // Definition of variables - don't touch// config_t *config; //added by bb 3.1.03 stats_t *stats; U8 *in, *out; int node; int vnodenum=0, cnodenum=0, dnodenum=0; int row, col; int *tempa, *tempb; int *codeword; int i,j,index=0; in = buffer_in(0); if ( private->error ){ PR_DBG( 0, "Some error in the initialisation\n" ); return -1; } out = buffer_out(0); swr_sdb_get_stats_struct( context->id, (void**)&stats ); /***************************** * encode an information word * *****************************/ vnodenum=(*private->graph).vnodenum; cnodenum=(*private->graph).cnodenum; dnodenum=vnodenum-cnodenum; // PR_DBG(3, "vnodenum=%d, cnodenum=%d, size_in=%d bytes\n", vnodenum, cnodenum, size_in(0)); codeword=(int*)swr_malloc(sizeof(int)*vnodenum); //copy bytes from 'in' to bits in 'codeword' if( size_in(0)*8 <= dnodenum ){ for( i=0; i<size_in(0)*8; i++ ){ if ( !(i%8) ){ index = in[ i/8 ]; } codeword[i]=(index&1); index/=2; } for( i=size_in(0)*8; i<dnodenum; i++ ){ codeword[i]=0; } } else { PR_DBG( 1,"input > number of dnodes. Cutting the last %i bits.\n", size_in(0)*8-dnodenum); for( i=0; i<dnodenum; i++ ){ if ( !(i%8) ){ index = in[ i/8 ]; } codeword[i]=(index&1); index/=2; } } // zero the remaining positions for(i=dnodenum; i<vnodenum; i++){ codeword[i]=0; } // allocate and clear temp tempa=(int *)swr_malloc(vnodenum*sizeof(int)); tempb=(int *)swr_malloc(vnodenum*sizeof(int)); cleararray(tempa, vnodenum); cleararray(tempb, vnodenum); // start by determining p1 // A s^T graphmultiply(private->graph, 0, cnodenum-(private->gap), 0, vnodenum-cnodenum, codeword, tempa); PR_DBG( 4,"A s^T\n"); if (DBG_LVL>=4) printmatrix(tempa, 1, cnodenum-(private->gap)); // T^-1 A s^T multiplybytminusone(private->graph, (private->gap), tempa, tempb); PR_DBG( 4,"T^-1 A s^T\n"); if (DBG_LVL>=4) printmatrix(tempb, 1, cnodenum-(private->gap)); // E (T^-1 A s^T) cleararray(tempa, (private->gap)); graphmultiply(private->graph, cnodenum-(private->gap), cnodenum, vnodenum-cnodenum+(private->gap), vnodenum, tempb, tempa); PR_DBG( 4,"E (T^-1 A s^T) \n"); if (DBG_LVL>=4) printmatrix(tempa, 1, (private->gap)); // C s^T + E T^-1 A s^T graphmultiply(private->graph, cnodenum-(private->gap), cnodenum, 0, vnodenum-cnodenum, codeword, tempa); PR_DBG( 4,"C s^T + E T^-1 A s^T \n"); if (DBG_LVL>=4) printmatrix(tempa, 1, (private->gap)); // phi^-1 (C s^T + E T^-1 A s^T) for (row=0; row<(private->gap); row++){ for (col=0; col<(private->gap); col++){ codeword[row+vnodenum-cnodenum]^=(*((private->iphi)+row*(private->gap)+col) & tempa[col]); } } PR_DBG( 4,"phi^-1 (C s^T + E T^-1 A s^T) \n"); if (DBG_LVL>=4) printmatrix(codeword, 1, vnodenum); // now determine p2 // A s^T + B p1^T cleararray(tempa, vnodenum); graphmultiply(private->graph, 0, cnodenum-(private->gap), 0, vnodenum-(private->gap), codeword, tempa); PR_DBG( 4,"A s^T + B p1^T \n"); if (DBG_LVL>=4) printmatrix(tempa, 1, cnodenum-(private->gap)); // T^-1 (A s^T + B p1^T) multiplybytminusone(private->graph, private->gap, tempa, codeword+vnodenum-cnodenum+(private->gap)); PR_DBG( 4,"T^-1 (A s^T + B p1^T) \n"); if (DBG_LVL>=4) printmatrix(codeword, 1, vnodenum); // make sure that all constraints are fulfilled // PR_DBG( 3,"making sure that all constraints are fulfilled \n"); cleararray(tempa, vnodenum); graphmultiply(private->graph, 0, cnodenum, 0, vnodenum, codeword, tempa); for (node=0; node<vnodenum; node++) { if(tempa[node]!=0) { PR_DBG( 0,"tempa[%i]!=0\n",node); // return 0; } } // free allocated space // PR_DBG( 3,"freeing tempa, tempb\n"); swr_free (tempa); swr_free (tempb); //copy encoded data to output j=-1; for ( i=0; i<min(vnodenum,size_out(0)*8); i++ ){ if ( !( i % 8 ) ){ j++; out[j] = 0; } out[j] += codeword[i] << ( i % 8 ); } if( size_out(0)*8 < vnodenum ) { PR_DBG( 0,"output to small: codeword does not fit in\n"); } else { for ( i=vnodenum; i<size_out(0)*8; i++ ){ if ( !( i % 8 ) ){ j++; out[j] = 0; } } } swr_free (codeword); swr_sdb_free_stats_struct( context->id, (void**)&stats ); return(0);}/** * User messages */int send_custom_msg( swr_sdb_t *context, swr_usr_msg_t *data, swr_msgq ret ){ return 0;}/* * This is the `destructor'. */int send_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 send_id;int send_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( 1, 1, 2, 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( "ldpc_code_id" ); UM_CONFIG_INT( "bplcn" ); /** * 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_U8, 0 ); UM_OUTPUT( SIG_U8, 0 ); // Initialise the callback-functions. Delete the ones you don't use desc->fn_init = send_init; desc->fn_reconfigure = send_reconfig; desc->fn_process_data = send_pdata; desc->fn_custom_msg = send_custom_msg; desc->fn_finalize = send_finalize; // And register the module in the SPM. Change the name! send_id = swr_cdb_register_spc( &desc, "ldpc_encode_2fold" ); if ( send_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 send_module_exit( void ){ PR_DBG( 4, "Freeing id: %i\n", send_id ); if ( swr_cdb_unregister_spc( send_id ) < 0 ){ PR_DBG( 0, "Still in use somewhere\n" ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -