Merge commit 'v3.3.0' into upstream
[debian/gnuradio] / gr-atsc / src / lib / convolutional_interleaver.h
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 #ifndef _CONVOLUTIONAL_INTERLEAVER_H_
24 #define _CONVOLUTIONAL_INTERLEAVER_H_
25
26 #include <vector>
27 #include <interleaver_fifo.h>
28 #include <assert.h>
29
30 /*!
31  * \brief template class for generic convolutional interleaver
32  */
33
34 template<class symbol_type>
35 class convolutional_interleaver {
36  public:
37
38   convolutional_interleaver (bool interleave_p, int nbanks, int fifo_size_incr);
39   virtual ~convolutional_interleaver ();
40
41   //! reset interleaver (flushes contents and resets commutator)
42   void reset ();
43   
44   //! sync interleaver (resets commutator, but doesn't flush fifos)
45   void sync () { m_commutator = 0; }
46
47   //! return end to end delay in symbols (delay through concatenated interleaver / deinterleaver)
48   int end_to_end_delay ();
49
50   //! transform a single symbol
51   symbol_type transform (symbol_type input){
52     symbol_type retval = m_fifo[m_commutator]->stuff (input);
53     m_commutator++;
54     if (m_commutator >= m_nbanks)
55       m_commutator = 0;
56     return retval;
57   }
58
59   //! transform a bunch of symbols
60   void transform (symbol_type *out, const symbol_type *in, int nsymbols);
61
62 protected:
63   int   m_commutator;
64   int   m_nbanks;
65   int   m_fifo_size_incr;
66   std::vector<interleaver_fifo<symbol_type> *> m_fifo;
67 };
68
69 template<class symbol_type>
70 convolutional_interleaver<symbol_type>::convolutional_interleaver (
71                                            bool interleave_p,
72                                            int nbanks,
73                                            int fifo_size_incr)
74 {
75   assert (nbanks >= 1);
76   assert (fifo_size_incr >= 1);
77
78   m_nbanks = nbanks;
79   m_fifo_size_incr = fifo_size_incr;
80
81   m_fifo.resize (nbanks);
82   
83   if (interleave_p){    // configure as interleaver
84     for (int i = 0; i < nbanks; i++)
85       m_fifo[i] = new interleaver_fifo<symbol_type>(i * fifo_size_incr);
86   }
87   else {                // configure as de-interleaver
88     for (int i = 0; i < nbanks; i++)
89       m_fifo[nbanks - 1 - i] = new interleaver_fifo<symbol_type>(i * fifo_size_incr);
90   }
91   sync ();
92 }
93
94 template<class symbol_type>
95 convolutional_interleaver<symbol_type>::~convolutional_interleaver ()
96 {
97   for (int i = 0; i < m_nbanks; i++)
98     delete m_fifo[i];
99 }
100
101 template<class symbol_type> void
102 convolutional_interleaver<symbol_type>::reset ()
103 {
104   sync ();
105   for (int i = 0; i < m_nbanks; i++)
106     m_fifo[i]->reset ();
107 }
108
109 template<class symbol_type> int
110 convolutional_interleaver<symbol_type>::end_to_end_delay ()
111 {
112   int m = m_nbanks * m_fifo_size_incr;
113   return m * (m_nbanks - 1);
114 }
115
116 template<class symbol_type> void
117 convolutional_interleaver<symbol_type>::transform (symbol_type *out,
118                                                    const symbol_type *in,
119                                                    int nsymbols)
120 {
121   // we may want to unroll this a couple of times...
122   for (int i = 0; i < nsymbols; i++)
123     out[i] = transform (in[i]);
124 }
125
126 #endif /* _CONVOLUTIONAL_INTERLEAVER_H_ */