📄 chdecoding.c
字号:
/* | | Copyright disclaimer: | This software was developed at the National Institute of Standards | and Technology by employees of the Federal Government in the course | of their official duties. Pursuant to title 17 Section 105 of the | United States Code this software is not subject to copyright | protection and is in the public domain. | | We would appreciate acknowledgement if the software is used. |*//* | Project: WCDMA simulation environment | Module: Convolutional decoder. | Author: Tommi Makelainen, Nokia/NIST | Date: February 12, 1999 | | History: | February 12, 1999 Tommi Makelainen | Initial version. | | June 11, 1999 Maarit Melvasalo | Corrected error in 'wcdma_chdecoding_free'. | Instance was not marked as INSTANCE_FREE. | */#include <stdio.h>#include <math.h>#include "conversions.h"#include "bitroutines.h"#include "config_wcdma.h"#include "mealy.h"#include "errormsg.h"#include "metrics.h"#include "convenc.h"#include "convdec.h"#include "interleaver.h"/* -------- D A T A S T R U C T U R E S ---------------------------- */static enum instance_state chdec_alloc_list[MAX_CH_CODERS];static int decoder_instances = 0;static general_init_flag = FALSE;static MetricTable *decoding_metrics[MAX_CH_CODERS];static MealyEncoder *encoders[MAX_CH_CODERS];static ViterbiDecoder *decoders[MAX_CH_CODERS];static int *deintr_symbs[MAX_CH_CODERS];static int *demod_alphas[MAX_CH_CODERS];static double *intrl_sbits[MAX_CH_CODERS];static double *demod_metrics[MAX_CH_CODERS];/* -------------------------------------------------------------------- *//* * Function: wcdma_chdecoding_init * Desc.: Convolutional decoding initialization * * Returns: => 0 allocated instance number, -1 if no free decoders. * * Note: */int wcdma_chdecoding_init( int coder_type, /* IN: 1=conv., 2=turbo */ int coding_ratio, /* IN: 2 = 1/2 or 3 = 1/3 */ int input_size, /* IN: Size of input bit estimate vector */ int metric_int_type, /* IN: 0=hard, 1=soft */ int gen_polys[], /* IN: generator polynomials */ double soft_prob_0[], /* IN: probabilities for 0 value */ double soft_prob_1[], /* IN: probabilities for 1 value */ int nSoftProbs) /* IN: length of probability vectors */{ int TailLength = CONVOLUTION_TAIL_LEN; int Decoding_depth = 8; int instance, i; MetricTable *decoding_metric; MealyEncoder *encoder; ViterbiDecoder *decoder; int *deintr_symbols, *demod_conv_alphas; double *intrl_soft_bits, *demod_soft_metrics; int num_encode_stages; enum metric_type_type metric_type; if (decoder_instances < MAX_CH_CODERS) { /* * If first call, initialize static data. */ if (general_init_flag == FALSE) { for (i=0; i < MAX_CH_CODERS; i++) { chdec_alloc_list[i] = FREE_INSTANCE; } /* for */ general_init_flag = TRUE; } /* if general_init_flag */ /* * Find first free instance number. */ instance = -1; for (i=0; i < MAX_CH_CODERS; i++) { if (chdec_alloc_list[i] == FREE_INSTANCE) { instance = i; break; } } /* for */ if (instance == -1) return(-1); /* no free instances */ /* * Convert path metric variable to enumerated type. */ metric_type = metric_int_type == 0 ? HAMMING_METRIC : SOFT_METRIC; /* * Allocate data structures for convolutional encoder and decoder. */ decoding_metric = (MetricTable*)calloc(1, sizeof(MetricTable)); encoder = (MealyEncoder*)calloc(1, sizeof(MealyEncoder)); decoder = (ViterbiDecoder*)calloc(1, sizeof(ViterbiDecoder)); /* * Calculate all possible encoder state transitions. * Initialize decoding metrics and decoder. */ num_encode_stages = CONVOLUTION_TAIL_LEN+1; if (coder_type == 1) { /* convolution */ if (coding_ratio == 2) { wcdma_R1o2_k9_conv_init (encoder, num_encode_stages, gen_polys[0], gen_polys[1]); MetricTable_R1o2_Init(decoding_metric,metric_type, soft_prob_0, soft_prob_1, nSoftProbs); wcdma_dec_R1o2_k9_init( decoder, input_size, Decoding_depth, TailLength, encoder, decoding_metric); } else if (coding_ratio == 3) { wcdma_R1o3_k9_conv_init (encoder, num_encode_stages, gen_polys[0], gen_polys[1], gen_polys[2]); MetricTable_R1o3_Init(decoding_metric,metric_type, soft_prob_0, soft_prob_1, nSoftProbs); wcdma_dec_R1o3_k9_init( decoder, input_size, Decoding_depth, TailLength, encoder, decoding_metric); } } else if (coder_type == 2) { /* turbo */ printf("Turbo coding is not available.\n"); free(decoding_metric); free(encoder); free(decoder); return(-1); } /* * Allocated memory for temporary convolution decoder outputs. */ deintr_symbols = (int*)calloc(input_size, sizeof(int)); demod_conv_alphas = (int*)calloc(input_size/coding_ratio, sizeof(int)); intrl_soft_bits = (double*)calloc(input_size, sizeof(double)); demod_soft_metrics = (double*) calloc(input_size/coding_ratio, sizeof(double)); /* * Store data structures for this instance. */ decoding_metrics[instance] = decoding_metric; encoders[instance] = encoder; decoders[instance] = decoder; deintr_symbs[instance] = deintr_symbols; demod_alphas[instance] = demod_conv_alphas; intrl_sbits[instance] = intrl_soft_bits; demod_metrics[instance] = demod_soft_metrics; /* * Update instance allocation list. */ chdec_alloc_list[instance] = INSTANCE_IN_USE; decoder_instances++; return(instance); } else { return(-1); } /* if decoder_instances */}/* -------------------------------------------------------------------- *//* * Function: wcdma_chdecoding_dec * Desc.: Convolutional decoding block * * Note: * Tail vector is 8 bits long. */int wcdma_chdecoding_dec(int coder_type, /* 1=conv., 2=turbo */ int coding_ratio, /* 2 = 1/2 or 3 = 1/3 */ int demod_inputs[], /* IN: input bit vector */ int nInputs, /* IN: input vector size */ double soft_bits[], /* IN: output bit vector */ int decoded_bits[], /* OUT: output bit vector */ int tail[], /* IN: tail bits */ int instance) /* IN: instance number */{ int i, j, tail_length; char temp_byte; ViterbiDecoder *decoder; int *deintr_symbols, *demod_conv_alphas; double *intrl_soft_bits, *demod_soft_metrics; /* * Get data structures for the current instance. */ decoder = decoders[instance]; demod_conv_alphas = demod_alphas[instance]; deintr_symbols = deintr_symbs[instance]; intrl_soft_bits = intrl_sbits[instance]; demod_soft_metrics = demod_metrics[instance]; /* * De-interleave incoming symbol estimates. */#if 0 wcdma_block_deinterleaver(rows, cols, demod_inputs, deintr_symbols); wcdma_block_float_deinterleaver(rows, cols, soft_bits, intrl_soft_bits);#else for (i=0; i < nInputs; i++) deintr_symbols[i] = demod_inputs[i];#endif tail_length = CONVOLUTION_TAIL_LEN; /* * Combine individual bit estimates to 'coding_ratio'-bit symbol estimates. * Calculate soft metrics for a symbol as a mean of separate bit estimates. * De-convolute using Viterbi algorithm. */ if (coding_ratio == 2) { for (i=0, j=0; i < nInputs; i+=2) { temp_byte = 0; set_bit_in_byte((char)demod_inputs[i ], 1, &temp_byte); set_bit_in_byte((char)demod_inputs[i+1], 0, &temp_byte); demod_conv_alphas[j] = (int)temp_byte; demod_soft_metrics[j++] = (intrl_soft_bits[i] + intrl_soft_bits[i+1])/2; } /* for */ wcdma_dec_R1o2_k9_conv( decoder, demod_conv_alphas, demod_soft_metrics, nInputs/2, tail, tail_length, decoded_bits); } else if (coding_ratio == 3) { for (i=0, j=0; i < nInputs; i+=3) { temp_byte = 0; set_bit_in_byte((char)demod_inputs[i ], 2, &temp_byte); set_bit_in_byte((char)demod_inputs[i+1], 1, &temp_byte); set_bit_in_byte((char)demod_inputs[i+2], 0, &temp_byte); demod_conv_alphas[j] = (int)temp_byte; demod_soft_metrics[j++] = (intrl_soft_bits[i]+intrl_soft_bits[i+1]+intrl_soft_bits[i+2])/3; } /* for */ wcdma_dec_R1o3_k9_conv( decoder, demod_conv_alphas, demod_soft_metrics, nInputs/3, tail, tail_length, decoded_bits); } else { printf("wcdma_decoding_conv: Incorrect coding ratio %d\n.", coding_ratio); return(-1); } return(0);}/* -------------------------------------------------------------------- *//* * Function: wcdma_chdecoding_free * Desc.: Convolutional decoding and de-interleaving clean up. * * Note: */void wcdma_chdecoding_free(int instance){ free(decoding_metrics[instance]); free(encoders[instance]); free(decoders[instance]); free(deintr_symbs[instance]); free(demod_alphas[instance]); free(intrl_sbits[instance]); free(demod_metrics[instance]); /* * Update instance allocation list. */ chdec_alloc_list[instance] = FREE_INSTANCE; decoder_instances--; return;}/* -------------------------------------------------------------------- */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -