欢迎来到代码驿站!

C代码

当前位置:首页 > 软件编程 > C代码

利用c语言实现卷积码编码器示例

时间:2021-04-22 09:08:29|栏目:C代码|点击:

实现(2, 1, 7)卷积码编码
信息序列1001 1010 1111 1100
生成序列g1 = 1011011;g2 = 1111001
初始状态全0.
以上参数可自行在main中修改。

复制代码 代码如下:

/***This is an simple example program of convolutional encoder.
   *The information sequence, the register initial states and the generation sequence
   *    can all be 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 at 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;
 }
 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 at 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");
}

上一篇:C++中函数指针详解及代码分享

栏    目:C代码

下一篇:C++实现各种排序算法类汇总

本文标题:利用c语言实现卷积码编码器示例

本文地址:http://www.codeinn.net/misctech/106243.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有