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 3, 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 if(fscanf(interleaverfile,"%d\n",&d_K) == EOF) {
79 if(ferror(interleaverfile) != 0)
80 throw std::runtime_error ("interleaver::interleaver(const char *name): file read error\n");
84 d_DEINTER.resize(d_K);
86 for(int i=0;i<d_K;i++) {
87 if(fscanf(interleaverfile,"%d",&(d_INTER[i])) == EOF) {
88 if(ferror(interleaverfile) != 0)
89 throw std::runtime_error ("interleaver::interleaver(const char *name): file read error\n");
93 // generate DEINTER table
94 for(int i=0;i<d_K;i++) {
95 d_DEINTER[d_INTER[i]]=i;
99 //######################################################################
100 //# Generate a random interleaver
101 //######################################################################
102 interleaver::interleaver(int K, int seed)
106 d_DEINTER.resize(d_K);
108 if(seed>=0) srand((unsigned int)seed);
109 std::vector<int> tmp(d_K);
110 for(int i=0;i<d_K;i++) {
114 quicksort_index <int> (tmp,d_INTER,0,d_K-1);
116 // generate DEINTER table
117 for(int i=0;i<d_K;i++) {
118 d_DEINTER[d_INTER[i]]=i;
126 //######################################################################
127 //# Write an INTERLEAVER specification from a file.
131 //# list of space separated K integers from 0 to K-1 in appropriate order
132 //# optional comments
133 //######################################################################
134 void interleaver::write_interleaver_txt(std::string filename)
136 std::ofstream interleaver_fname (filename.c_str());
137 if (!interleaver_fname) {std::cout << "file not found " << std::endl ; exit(-1);}
138 interleaver_fname << d_K << std::endl;
139 interleaver_fname << std::endl;
140 for(int i=0;i<d_K;i++)
141 interleaver_fname << d_INTER[i] << ' ';
142 interleaver_fname << std::endl;
143 interleaver_fname.close();