12144bdc0e2105b202a5f4db9784a1e92ce34cda
[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 <cstdio>\r
24 #include <stdexcept>\r
25 #include <cmath>\r
26 #include "interleaver.h"\r
27 \r
28 interleaver::interleaver()\r
29 {\r
30   d_K=0;\r
31   d_INTER.resize(0);\r
32   d_DEINTER.resize(0);\r
33 }\r
34 \r
35 interleaver::interleaver(const interleaver &INTERLEAVER)\r
36 {\r
37   d_K=INTERLEAVER.K();\r
38   d_INTER=INTERLEAVER.INTER();\r
39   d_DEINTER=INTERLEAVER.DEINTER();\r
40 }\r
41 \r
42 interleaver::interleaver(const int K, const std::vector<int> &INTER)\r
43 {\r
44   d_K=K;\r
45   d_INTER=INTER;\r
46   d_DEINTER.resize(d_K);\r
47   \r
48   // generate DEINTER table\r
49   for(int i=0;i<d_K;i++) {\r
50     d_DEINTER[d_INTER[i]]=i;\r
51   }\r
52 }\r
53 \r
54 //######################################################################\r
55 //# Read an INTERLEAVER specification from a file.\r
56 //# Format (hopefully will become more flexible in the future...):\r
57 //# K\r
58 //# blank line\r
59 //# list of space separated K integers from 0 to K-1 in appropriate order\r
60 //# optional comments\r
61 //######################################################################\r
62 interleaver::interleaver(const char *name) \r
63 {\r
64   FILE *interleaverfile;\r
65 \r
66   if((interleaverfile=fopen(name,"r"))==NULL) \r
67     throw std::runtime_error ("file open error in interleaver()");\r
68     //printf("file open error in interleaver()\n");\r
69   \r
70   fscanf(interleaverfile,"%d\n",&d_K);\r
71   d_INTER.resize(d_K);\r
72   d_DEINTER.resize(d_K);\r
73 \r
74   for(int i=0;i<d_K;i++) fscanf(interleaverfile,"%d",&(d_INTER[i]));\r
75   \r
76   // generate DEINTER table\r
77   for(int i=0;i<d_K;i++) {\r
78     d_DEINTER[d_INTER[i]]=i;\r
79   }\r
80 }\r