Imported Upstream version 3.0
[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 2, 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 <stdexcept>
27 #include <cmath>
28 #include "quicksort_index.h"
29 #include "interleaver.h"
30
31 interleaver::interleaver()
32 {
33   d_K=0;
34   d_INTER.resize(0);
35   d_DEINTER.resize(0);
36 }
37
38 interleaver::interleaver(const interleaver &INTERLEAVER)
39 {
40   d_K=INTERLEAVER.K();
41   d_INTER=INTERLEAVER.INTER();
42   d_DEINTER=INTERLEAVER.DEINTER();
43 }
44
45 interleaver::interleaver(int K, const std::vector<int> &INTER)
46 {
47   d_K=K;
48   d_INTER=INTER;
49   d_DEINTER.resize(d_K);
50   
51   // generate DEINTER table
52   for(int i=0;i<d_K;i++) {
53     d_DEINTER[d_INTER[i]]=i;
54   }
55 }
56
57 //######################################################################
58 //# Read an INTERLEAVER specification from a file.
59 //# Format (hopefully will become more flexible in the future...):
60 //# K
61 //# blank line
62 //# list of space separated K integers from 0 to K-1 in appropriate order
63 //# optional comments
64 //######################################################################
65 interleaver::interleaver(const char *name) 
66 {
67   FILE *interleaverfile;
68
69   if((interleaverfile=fopen(name,"r"))==NULL) 
70     throw std::runtime_error ("file open error in interleaver()");
71     //printf("file open error in interleaver()\n");
72   
73   fscanf(interleaverfile,"%d\n",&d_K);
74   d_INTER.resize(d_K);
75   d_DEINTER.resize(d_K);
76
77   for(int i=0;i<d_K;i++) fscanf(interleaverfile,"%d",&(d_INTER[i]));
78   
79   // generate DEINTER table
80   for(int i=0;i<d_K;i++) {
81     d_DEINTER[d_INTER[i]]=i;
82   }
83 }
84
85 //######################################################################
86 //# Generate a random interleaver
87 //######################################################################
88 interleaver::interleaver(int K, unsigned int seed)
89 {
90   d_K=K;
91   d_INTER.resize(d_K);
92   d_DEINTER.resize(d_K);
93
94   srand(seed); 
95   std::vector<int> tmp(d_K);
96   for(int i=0;i<d_K;i++) {
97     d_INTER[i]=i;
98     tmp[i] = rand(); 
99   }
100   //quicksort_index <int> (tmp,d_INTER,0,d_K-1); //got to resolve this...
101   quicksort_index1 (tmp,d_INTER,0,d_K-1);
102
103   // generate DEINTER table
104   for(int i=0;i<d_K;i++) {
105     d_DEINTER[d_INTER[i]]=i;
106   }
107 }
108