/***this is a simple example program of convolutional encoder.
*the information sequence, the register initial states and the generation sequence
* Can all is modified in the main function.
*/
#include <stdio.h>
#define LEN (array, len) {len=sizeof (array)/sizeof (array[0]); Size of Array
int encoder (int **gen, int n, int L, int reg[], int m, int inf[], int inf_len, int output[])
/*encoder (int **gen, int n, int L, int reg[], int m, int inf[], int inf_len, int output[])
*this function is a convolutional encoder.
*gen is the generation sequence, which is a two-dimension array,
And it is a two-dimension pointer,
*n is the number of bits out the encoder in each clock cycle,
*l is for the constraight length,
*reg is for the shift registers,
*m is for the number of registers,
*inf is for the information sequence,
*inf_len is for the INF length,
*output is for the output code.
*/
{
int Inf_ex[inf_len + M];
int I,j;//index
for (I=0;i < Inf_len + m;i++)//extend The information sequence to include the last M bits
{
if (i < Inf_len)
inf_ex[i] = inf[i];
else
inf_ex[i] = 0;
&NBSP}
for (I=0;i < Inf_len + m;i++)//foreach bit in extend information
{
for (j=0; J < n;j++)//output n bits at each clock cycle
{
int out_tem=0;//temp Number
if (* (gen + l*j) = 1)//judge Whether the next information bit should paticipate in the Mod op
Out_tem + + inf_ex[ I];
int k;
For (K=0;k < m;k++)//foreach registers
{
if (* (Gen + l*j + k + 1) = = 1)
Out_tem = Reg[k];//mod OP According to the generation sequence
}
Out_tem%= 2;//mod 2
Output[i*n + j] = Out_tem;
}
for (J=m-1;j > 0;j--)//register shift
{
REG[J] = reg[j-1];
}
REG[0] = Inf_ex[i];//input information bits into register
}
return 1;
}
Main ()
{
int inf[]={1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0};//information sequence
int inf_len;//information Length
LEN (INF, Inf_len);
int gen[2][7]={{1, 0, 1, 1, 0, 1, 1}, {1, 1, 1, 1, 0, 0, 1}};//generation sequence
int n;//the number of bits out the encoder in each clock cycle
int l;//constraight Length
LEN (gen, N);
LEN (Gen[0], L);
int m=l-1;//the number of shift registers
int init_s[]={0, 0, 0, 0, 0, 0}; Initial states are all zero
int Reg[m];//register
int I;//index
For (I=0;i < m;i++)
{
Reg[i] = init_s[i];
}
int output_len= (Inf_len + m) *n;//output length, every bit of input can generate n bits of output sequence
int output[(Inf_len + m) *n];//output sequence
Encoder (gen, N, L, Reg, M, INF, Inf_len, output);//encoder
For (I=0;i < output_len;i++)
{
printf ("%d", output[i]);
}
System ("pause");
}