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