3 * Copyright 2002 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
30 #include "quicksort_index.h"
31 #include "interleaver.h"
36 interleaver::interleaver()
43 interleaver::interleaver(const interleaver &INTERLEAVER)
46 d_INTER=INTERLEAVER.INTER();
47 d_DEINTER=INTERLEAVER.DEINTER();
50 interleaver::interleaver(int K, const std::vector<int> &INTER)
54 d_DEINTER.resize(d_K);
56 // generate DEINTER table
57 for(int i=0;i<d_K;i++) {
58 d_DEINTER[d_INTER[i]]=i;
62 //######################################################################
63 //# Read an INTERLEAVER specification from a file.
64 //# Format (hopefully will become more flexible in the future...):
67 //# list of space separated K integers from 0 to K-1 in appropriate order
69 //######################################################################
70 interleaver::interleaver(const char *name)
72 FILE *interleaverfile;
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");
78 fscanf(interleaverfile,"%d\n",&d_K);
80 d_DEINTER.resize(d_K);
82 for(int i=0;i<d_K;i++) fscanf(interleaverfile,"%d",&(d_INTER[i]));
84 // generate DEINTER table
85 for(int i=0;i<d_K;i++) {
86 d_DEINTER[d_INTER[i]]=i;
90 //######################################################################
91 //# Generate a random interleaver
92 //######################################################################
93 interleaver::interleaver(int K, int seed)
97 d_DEINTER.resize(d_K);
99 if(seed>=0) srand((unsigned int)seed);
100 std::vector<int> tmp(d_K);
101 for(int i=0;i<d_K;i++) {
105 quicksort_index <int> (tmp,d_INTER,0,d_K-1);
107 // generate DEINTER table
108 for(int i=0;i<d_K;i++) {
109 d_DEINTER[d_INTER[i]]=i;
117 //######################################################################
118 //# Write an INTERLEAVER specification from a file.
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)
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();