Several enhancements to gr-trellis and gnuradio-examples/python/channel-coding:
[debian/gnuradio] / gr-trellis / src / lib / interleaver.cc
1 /* -*- c++ -*- */\r
2 /*\r
3  * Copyright 2002 Free Software Foundation, Inc.\r
4  *\r
5  * This file is part of GNU Radio\r
6  *\r
7  * GNU Radio is free software; you can redistribute it and/or modify\r
8  * it under the terms of the GNU General Public License as published by\r
9  * the Free Software Foundation; either version 2, or (at your option)\r
10  * any later version.\r
11  *\r
12  * GNU Radio is distributed in the hope that it will be useful,\r
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15  * GNU General Public License for more details.\r
16  *\r
17  * You should have received a copy of the GNU General Public License\r
18  * along with GNU Radio; see the file COPYING.  If not, write to\r
19  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,\r
20  * Boston, MA 02111-1307, USA.\r
21  */\r
22 \r
23 #include <cstdlib> \r
24 #include <cstdio>\r
25 #include <iostream>\r
26 #include <stdexcept>\r
27 #include <cmath>\r
28 #include "quicksort_index.h"\r
29 #include "interleaver.h"\r
30 \r
31 interleaver::interleaver()\r
32 {\r
33   d_K=0;\r
34   d_INTER.resize(0);\r
35   d_DEINTER.resize(0);\r
36 }\r
37 \r
38 interleaver::interleaver(const interleaver &INTERLEAVER)\r
39 {\r
40   d_K=INTERLEAVER.K();\r
41   d_INTER=INTERLEAVER.INTER();\r
42   d_DEINTER=INTERLEAVER.DEINTER();\r
43 }\r
44 \r
45 interleaver::interleaver(int K, const std::vector<int> &INTER)\r
46 {\r
47   d_K=K;\r
48   d_INTER=INTER;\r
49   d_DEINTER.resize(d_K);\r
50   \r
51   // generate DEINTER table\r
52   for(int i=0;i<d_K;i++) {\r
53     d_DEINTER[d_INTER[i]]=i;\r
54   }\r
55 }\r
56 \r
57 //######################################################################\r
58 //# Read an INTERLEAVER specification from a file.\r
59 //# Format (hopefully will become more flexible in the future...):\r
60 //# K\r
61 //# blank line\r
62 //# list of space separated K integers from 0 to K-1 in appropriate order\r
63 //# optional comments\r
64 //######################################################################\r
65 interleaver::interleaver(const char *name) \r
66 {\r
67   FILE *interleaverfile;\r
68 \r
69   if((interleaverfile=fopen(name,"r"))==NULL) \r
70     throw std::runtime_error ("file open error in interleaver()");\r
71     //printf("file open error in interleaver()\n");\r
72   \r
73   fscanf(interleaverfile,"%d\n",&d_K);\r
74   d_INTER.resize(d_K);\r
75   d_DEINTER.resize(d_K);\r
76 \r
77   for(int i=0;i<d_K;i++) fscanf(interleaverfile,"%d",&(d_INTER[i]));\r
78   \r
79   // generate DEINTER table\r
80   for(int i=0;i<d_K;i++) {\r
81     d_DEINTER[d_INTER[i]]=i;\r
82   }\r
83 }\r
84 \r
85 //######################################################################\r
86 //# Generate a random interleaver\r
87 //######################################################################\r
88 interleaver::interleaver(int K, unsigned int seed)\r
89 {\r
90   d_K=K;\r
91   d_INTER.resize(d_K);\r
92   d_DEINTER.resize(d_K);\r
93 \r
94   srand(seed); \r
95   std::vector<int> tmp(d_K);\r
96   for(int i=0;i<d_K;i++) {\r
97     d_INTER[i]=i;\r
98     tmp[i] = rand(); \r
99   }\r
100   //quicksort_index <int> (tmp,d_INTER,0,d_K-1); got to resolve this...\r
101   quicksort_index1 (tmp,d_INTER,0,d_K-1);\r
102 \r
103   // generate DEINTER table\r
104   for(int i=0;i<d_K;i++) {\r
105     d_DEINTER[d_INTER[i]]=i;\r
106   }\r
107 }\r
108 \r