| | 36 | //----------------------------------------------------------------------------- |
| | 37 | // |
| | 38 | // Convolutional coding definitions |
| | 39 | // |
| | 40 | //----------------------------------------------------------------------------- |
| | 41 | |
| | 42 | /** \brief Defines the basic entry for building the trellis at the decoder |
| | 43 | */ |
| | 44 | class TrellisEntry{ |
| | 45 | public: |
| | 46 | unsigned int previousState; ///< The state prior to the current state, used for tracing back the trellis |
| | 47 | unsigned short int symbolNo; ///< The symbol that caused the change of state to the currrent state |
| | 48 | signed int distance; ///< The total distance of the current state |
| | 49 | }; |
| | 50 | |
| | 51 | /** \brief Includes all the necessary information for encoding and decoding the symbols |
| | 52 | * |
| | 53 | * The contructor has to be ALWAYS be used. The generator polynomials for each code rate have to be specified in DECIMAL, not OCTAL format |
| | 54 | * |
| | 55 | * For more information for the meaning of the symbols used and for generator polynomials look at |
| | 56 | * Proakis Digital Communications book 4th Edition, pages 471 & 492 respectively. |
| | 57 | */ |
| | 58 | class trellisTable{ |
| | 59 | public: |
| | 60 | /// The contructor has to be always used |
| | 61 | trellisTable(unsigned int * generatorPolynomials, unsigned short int k, unsigned short int n, unsigned short K); |
| | 62 | |
| | 63 | /// =The destructor |
| | 64 | ~trellisTable(); |
| | 65 | |
| | 66 | unsigned short int k;///<the input bits at a time |
| | 67 | unsigned short int n;///< the output bits at a time |
| | 68 | unsigned short int K;///<the contraint length |
| | 69 | unsigned short int numberOfInputStates;///< Based on k, how many different input states exist |
| | 70 | unsigned int numberOfTrellisStates;///<Number of different trellis states, defined by k & K |
| | 71 | |
| | 72 | unsigned int **output; ///< The rows are the trellis states and the columns the symbols, gives the output given the current state and symbol. |
| | 73 | unsigned int **nextState;///< The sane as output, gives the the next state given the current state and synbol |
| | 74 | |
| | 75 | protected: |
| | 76 | ///Generates the trellis table given the generator polynomials in DECIMAL not OCTAL |
| | 77 | void GenerateTrellisTable(unsigned int * generatorPolynomials); |
| | 78 | |
| | 79 | ///Peforms modulo 2 addition to the bits of the input number (symbol) |
| | 80 | unsigned short int Modulo2BitWiseAdd(unsigned short int inputNumber); |
| | 81 | }; |
| | 82 | |
| | 83 | /** \brief Defines the basic functionality for the convolution encoding/decoding |
| | 84 | */ |
| | 85 | |
| | 86 | class fec_conv{ |
| | 87 | public: |
| | 88 | /// It accepts the pointer to the trellis Table |
| | 89 | void SetTrellisTable(trellisTable *theTrellisTableIn); |
| | 90 | |
| | 91 | protected: |
| | 92 | /// It converts a decimal numnber (symbol) into a vector of bits |
| | 93 | void Dec2Bin(unsigned int decNumber,unsigned short int * outputData,unsigned short int numberOfBits); |
| | 94 | |
| | 95 | trellisTable * theTrellisTable;///< It holds the trellis table used for encoding/decoding |
| | 96 | }; |
| | 97 | |
| | 98 | /** \brief It defines the encoding functionality |
| | 99 | * |
| | 100 | * To use this class, first pass the trellis table pointer |
| | 101 | * |
| | 102 | * To encode: reset the state, feed data & get encoded data, repeat for the next packet. |
| | 103 | * |
| | 104 | * To make the encoder to return at the zero state during encoding, feed K 0 symbols after the encoding of the data is done. |
| | 105 | */ |
| | 106 | class fec_conv_encoder : public fec_conv{ |
| | 107 | public: |
| | 108 | ///Default constructor |
| | 109 | fec_conv_encoder(); |
| | 110 | |
| | 111 | ///Default destructor |
| | 112 | ~fec_conv_encoder(); |
| | 113 | |
| | 114 | /// Resets the state of the encoder to the zero state |
| | 115 | void ResetState(); |
| | 116 | |
| | 117 | ///It gets the current state of the encoder |
| | 118 | unsigned int GetState(); |
| | 119 | |
| | 120 | ///It accepts a vector of k bits and returns a vector of n bits |
| | 121 | void Encode(unsigned short int * inputData,unsigned short int * outputData); |
| | 122 | |
| | 123 | protected: |
| | 124 | unsigned int currentState;///<The current state of the encoder |
| | 125 | }; |
| | 126 | |
| | 127 | /** \brief It defines the decoding functionality |
| | 128 | * |
| | 129 | * The following cycle should be followed to use this class: |
| | 130 | * |
| | 131 | * 1. Set the trellis table |
| | 132 | * |
| | 133 | * 2. Set the traceback length |
| | 134 | * |
| | 135 | * 3. Set mode |
| | 136 | * |
| | 137 | * 4. Feed symbols into the decoder |
| | 138 | * |
| | 139 | * 5. Trace back trellis |
| | 140 | * |
| | 141 | * 6. Get the decoded symbols |
| | 142 | * |
| | 143 | * 7. Reset and repeat from 4 for the next packet. If the decoding parameters change, might need to repeat from 1. |
| | 144 | */ |
| | 145 | class fec_conv_decoder:public fec_conv { |
| | 146 | public: |
| | 147 | ///Default contructor |
| | 148 | fec_conv_decoder(); |
| | 149 | |
| | 150 | ///Default destructor |
| | 151 | ~fec_conv_decoder(); |
| | 152 | |
| | 153 | /// Set the numbers of the input symbols to be decoded, this equals to the total bits on the output of the encoder divided by k |
| | 154 | void SetNoOfSymbols2TraceBack(unsigned int tracebackLength); |
| | 155 | |
| | 156 | /// Feeds a symbol (in a vector of n bits) at time for decoding |
| | 157 | void Symbol2Decode(unsigned short int * inputData); |
| | 158 | |
| | 159 | /// After all symbols were fed in the decoder, it will tracesback the trellis to the begining. |
| | 160 | void TraceBackTrellis(); |
| | 161 | |
| | 162 | ///After the trellis was traced back, it returns a decoded symbol, starting from the beggining each time is called. |
| | 163 | void GetDecodedSymbol(unsigned short int * outputData); |
| | 164 | |
| | 165 | ///It resets the state of the decoder in order to be able to start a new decoding session |
| | 166 | void Reset(); |
| | 167 | |
| | 168 | ///Sets the mode of the decoder 0: the encoded data start from zero state 1: the encoded data start from the the zero state and end at the zero state. |
| | 169 | void SetMode(unsigned short int mode); |
| | 170 | |
| | 171 | protected: |
| | 172 | ///Calculates the distance between the input bits (symbol) to the current trellis symbol |
| | 173 | signed int CalculateDistance(unsigned short int inBits, unsigned short int symbol); |
| | 174 | |
| | 175 | unsigned int currentTrellisIndex;///<The current trellis stage used when building and tracing back the trellis. |
| | 176 | unsigned int decodedSymbolIndex;///<The next symbol to be returned when the GetDecodedSymbol is called. |
| | 177 | unsigned int noOfSymbols2TraceBack;///<Number of symbols to trace back, should be equal to the symbols used in the encoding process. |
| | 178 | unsigned int mode;///<The mode of the encoder |
| | 179 | |
| | 180 | unsigned int *tracedBackSymbols;///<A vector array with the symbols traced back |
| | 181 | TrellisEntry **theTrellis; ///<A two dimensional array making the trellis, states are rows, and columns are each trellis stage. |
| | 182 | }; |