GNU Radio's MESA Package
fir_filter_lfast.h
Go to the documentation of this file.
1 /*
2  * fir_filter_lfast.h
3  *
4  * Author: ghostop14
5  */
6 
7 #ifndef INCLUDE_LFAST_FIR_FILTER_LFAST_H_
8 #define INCLUDE_LFAST_FIR_FILTER_LFAST_H_
9 
10 #include <gnuradio/gr_complex.h>
11 
12 #include <boost/thread/thread.hpp>
13 #include <volk/volk.h>
14 using namespace std;
15 
16 namespace gr {
17 namespace lfast {
18 /*
19  * Base Filter
20  */
21 template <class io_type, class tap_type> class Filter {
22 protected:
23  tap_type *alignedTaps;
25  std::vector<tap_type> d_taps;
26  long numTaps;
27 
28 public:
29  Filter();
30  Filter(const std::vector<tap_type> &newTaps);
31  virtual ~Filter();
32 
33  virtual void setTaps(const std::vector<tap_type> &newTaps);
34  // For compatibility
35  inline virtual void set_taps(const std::vector<tap_type> &newTaps) {
36  setTaps(newTaps);
37  };
38  virtual std::vector<tap_type> getTaps() const;
39  inline virtual std::vector<tap_type> taps() const { return getTaps(); };
40  inline virtual long ntaps() { return numTaps; };
41 
42  // Returns number of samples consumed / produced
43  virtual long filter(io_type *outputBuffer, const io_type *inputBuffer,
44  long numSamples) {
45  return 0;
46  };
47 }; // end base filter template
48 
49 /*
50  * FIR Filter - Complex inputs, float taps
51  */
52 class FIRFilterCCF : public Filter<gr_complex, float> {
53 public:
54  FIRFilterCCF();
55  FIRFilterCCF(const std::vector<float> &newTaps);
56  virtual ~FIRFilterCCF();
57 
58  // NOTE: This routine is expecting numSamples to be an integer multiple of the
59  // number of taps
60  virtual long filterN(gr_complex *outputBuffer, const gr_complex *inputBuffer,
61  long numSamples);
62 
63  // OutputBuffer here should be at least numSamples/decimation in length
64  virtual long filterNdec(gr_complex *outputBuffer,
65  const gr_complex *inputBuffer, long numSamples,
66  int decimation);
67 
68  virtual gr_complex filter(const gr_complex *inputBuffer);
69 
70  // This is for testing comparison. This function does all calculations in 1
71  // CPU-based thread.
72  virtual long filterCPU(gr_complex *outputBuffer,
73  const gr_complex *inputBuffer, long numSamples);
74 };
75 
76 /*
77  * FIR Filter - float inputs, float taps
78  */
79 class FIRFilterFFF : public Filter<float, float> {
80 public:
81  FIRFilterFFF();
82  FIRFilterFFF(const std::vector<float> &newTaps);
83  virtual ~FIRFilterFFF();
84 
85  // NOTE: This routine is expecting numSamples to be an integer multiple of the
86  // number of taps
87  virtual long filterN(float *outputBuffer, const float *inputBuffer,
88  long numSamples);
89 
90  // OutputBuffer here should be at least numSamples/decimation in length
91  virtual long filterNdec(float *outputBuffer, const float *inputBuffer,
92  long numSamples, int decimation);
93 
94  virtual gr_complex filter(const float *inputBuffer);
95 
96  // This is for testing comparison. This function does all calculations in 1
97  // CPU-based thread.
98  virtual long filterCPU(float *outputBuffer, const float *inputBuffer,
99  long numSamples);
100 };
101 
102 /*
103  * FIR Filter - complex inputs, complex taps
104  */
105 class FIRFilterCCC : public Filter<gr_complex, gr_complex> {
106 public:
107  FIRFilterCCC();
108  FIRFilterCCC(const std::vector<gr_complex> &newTaps);
109  virtual ~FIRFilterCCC();
110 
111  // NOTE: This routine is expecting numSamples to be an integer multiple of the
112  // number of taps
113  virtual long filterN(gr_complex *outputBuffer, const gr_complex *inputBuffer,
114  long numSamples);
115 
116  // OutputBuffer here should be at least numSamples/decimation in length
117  virtual long filterNdec(gr_complex *outputBuffer,
118  const gr_complex *inputBuffer, long numSamples,
119  int decimation);
120 
121  virtual gr_complex filter(const gr_complex *inputBuffer);
122 
123  // This is for testing comparison. This function does all calculations in 1
124  // CPU-based thread.
125  virtual long filterCPU(gr_complex *outputBuffer,
126  const gr_complex *inputBuffer, long numSamples);
127 };
128 
129 // -----------------------------------------------------------------
130 // ------ Multi-threaded filters ----------------------------------
131 // -----------------------------------------------------------------
132 
133 /*
134  * Multi-threaded base template.
135  * Provides threading and buffers
136  *
137  */
138 template <class io_type> class MTBase {
139 protected:
140  const io_type *pInputBuffer;
141  io_type *pOutputBuffer;
142 
143  // Supports up to 16 threads
144  boost::thread *threads[16];
145  bool threadRunning[16];
149 
150 public:
151  MTBase(int nthreads = 4);
152  virtual ~MTBase();
153 
154  int numThreads() { return d_nthreads; };
155 
156  inline virtual void setDecimation(int newDecimation) {
157  decimation = newDecimation;
158  };
159  inline virtual int getDecimation() { return decimation; };
160  inline virtual bool decimating() {
161  if (decimation > 1)
162  return true;
163  else
164  return false;
165  };
166 
167  virtual long calcDecimationBlockSize(long numSamples);
168 
169  // NOTE: calcDecimationIndex assumes the index is an even multiple of a
170  // decimation block size In other words, calcDecimationBlockSize was called to
171  // get an appropriate block size, then blockStartIndex is an integer multiple
172  // of that result.
173  virtual long calcDecimationIndex(long blockStartIndex);
174 
175  // NOTE: This method IS NOT thread-safe. Make sure to use a scoped_lock or be
176  // sure no threads are running before changing the number of threads.
177  virtual void setThreads(int nthreads);
178 
179  virtual bool anyThreadRunning();
180 };
181 
182 // --------------------------------------------------
183 // Multi-threaded filter, complex data, float taps
184 // --------------------------------------------------
185 class FIRFilterCCF_MT : public MTBase<gr_complex>, public FIRFilterCCF {
186 protected:
187  virtual void runThread1(long startIndex, long numSamples);
188  virtual void runThread2(long startIndex, long numSamples);
189  virtual void runThread3(long startIndex, long numSamples);
190  virtual void runThread4(long startIndex, long numSamples);
191  virtual void runThread5(long startIndex, long numSamples);
192  virtual void runThread6(long startIndex, long numSamples);
193  virtual void runThread7(long startIndex, long numSamples);
194  virtual void runThread8(long startIndex, long numSamples);
195  virtual void runThread9(long startIndex, long numSamples);
196  virtual void runThread10(long startIndex, long numSamples);
197  virtual void runThread11(long startIndex, long numSamples);
198  virtual void runThread12(long startIndex, long numSamples);
199  virtual void runThread13(long startIndex, long numSamples);
200  virtual void runThread14(long startIndex, long numSamples);
201  virtual void runThread15(long startIndex, long numSamples);
202  virtual void runThread16(long startIndex, long numSamples);
203 
204 public:
205  FIRFilterCCF_MT(int nthreads);
206  FIRFilterCCF_MT(const std::vector<float> &newTaps, int nthreads);
207  virtual ~FIRFilterCCF_MT();
208 
209  // NOTE: This routine is expecting numSamples to be an integer multiple of the
210  // number of taps
211  virtual long filterN(gr_complex *outputBuffer, const gr_complex *inputBuffer,
212  long numSamples);
213 
214  // OutputBuffer here should be at least numSamples/decimation in length
215  virtual long filterNdec(gr_complex *outputBuffer,
216  const gr_complex *inputBuffer, long numSamples,
217  int decimation);
218 };
219 
220 // --------------------------------------------------
221 // Multi-threaded filter, complex data, float taps
222 // --------------------------------------------------
223 class FIRFilterFFF_MT : public MTBase<float>, public FIRFilterFFF {
224 protected:
225  virtual void runThread1(long startIndex, long numSamples);
226  virtual void runThread2(long startIndex, long numSamples);
227  virtual void runThread3(long startIndex, long numSamples);
228  virtual void runThread4(long startIndex, long numSamples);
229  virtual void runThread5(long startIndex, long numSamples);
230  virtual void runThread6(long startIndex, long numSamples);
231  virtual void runThread7(long startIndex, long numSamples);
232  virtual void runThread8(long startIndex, long numSamples);
233  virtual void runThread9(long startIndex, long numSamples);
234  virtual void runThread10(long startIndex, long numSamples);
235  virtual void runThread11(long startIndex, long numSamples);
236  virtual void runThread12(long startIndex, long numSamples);
237  virtual void runThread13(long startIndex, long numSamples);
238  virtual void runThread14(long startIndex, long numSamples);
239  virtual void runThread15(long startIndex, long numSamples);
240  virtual void runThread16(long startIndex, long numSamples);
241 
242 public:
243  FIRFilterFFF_MT(int nthreads);
244  FIRFilterFFF_MT(const std::vector<float> &newTaps, int nthreads);
245  virtual ~FIRFilterFFF_MT();
246 
247  // NOTE: This routine is expecting numSamples to be an integer multiple of the
248  // number of taps
249  virtual long filterN(float *outputBuffer, const float *inputBuffer,
250  long numSamples);
251 
252  // OutputBuffer here should be at least numSamples/decimation in length
253  virtual long filterNdec(float *outputBuffer, const float *inputBuffer,
254  long numSamples, int decimation);
255 };
256 
257 // --------------------------------------------------
258 // Multi-threaded filter, complex data, complex taps
259 // --------------------------------------------------
260 class FIRFilterCCC_MT : public MTBase<gr_complex>, public FIRFilterCCC {
261 protected:
262  virtual void runThread1(long startIndex, long numSamples);
263  virtual void runThread2(long startIndex, long numSamples);
264  virtual void runThread3(long startIndex, long numSamples);
265  virtual void runThread4(long startIndex, long numSamples);
266  virtual void runThread5(long startIndex, long numSamples);
267  virtual void runThread6(long startIndex, long numSamples);
268  virtual void runThread7(long startIndex, long numSamples);
269  virtual void runThread8(long startIndex, long numSamples);
270  virtual void runThread9(long startIndex, long numSamples);
271  virtual void runThread10(long startIndex, long numSamples);
272  virtual void runThread11(long startIndex, long numSamples);
273  virtual void runThread12(long startIndex, long numSamples);
274  virtual void runThread13(long startIndex, long numSamples);
275  virtual void runThread14(long startIndex, long numSamples);
276  virtual void runThread15(long startIndex, long numSamples);
277  virtual void runThread16(long startIndex, long numSamples);
278 
279 public:
280  FIRFilterCCC_MT(int nthreads);
281  FIRFilterCCC_MT(const std::vector<gr_complex> &newTaps, int nthreads);
282  virtual ~FIRFilterCCC_MT();
283 
284  // NOTE: This routine is expecting numSamples to be an integer multiple of the
285  // number of taps
286  virtual long filterN(gr_complex *outputBuffer, const gr_complex *inputBuffer,
287  long numSamples);
288 
289  // OutputBuffer here should be at least numSamples/decimation in length
290  virtual long filterNdec(gr_complex *outputBuffer,
291  const gr_complex *inputBuffer, long numSamples,
292  int decimation);
293 };
294 } // namespace lfast
295 } // namespace gr
296 
297 #endif /* INCLUDE_LFAST_FIR_FILTER_LFAST_H_ */
Definition: fir_filter_lfast.h:260
const io_type * pInputBuffer
Definition: fir_filter_lfast.h:140
int decimation
Definition: fir_filter_lfast.h:148
virtual long filter(io_type *outputBuffer, const io_type *inputBuffer, long numSamples)
Definition: fir_filter_lfast.h:43
STL namespace.
int d_nthreads
Definition: fir_filter_lfast.h:147
Definition: fir_filter_lfast.h:21
int numThreads()
Definition: fir_filter_lfast.h:154
Definition: fir_filter_lfast.h:138
Definition: fir_filter_lfast.h:185
virtual long ntaps()
Definition: fir_filter_lfast.h:40
std::vector< tap_type > d_taps
Definition: fir_filter_lfast.h:25
Definition: AutoDopplerCorrect.h:27
Definition: fir_filter_lfast.h:79
bool threadReady
Definition: fir_filter_lfast.h:146
long numTaps
Definition: fir_filter_lfast.h:26
Definition: fir_filter_lfast.h:105
io_type * pOutputBuffer
Definition: fir_filter_lfast.h:141
virtual void setDecimation(int newDecimation)
Definition: fir_filter_lfast.h:156
Definition: fir_filter_lfast.h:52
io_type * singlePointBuffer
Definition: fir_filter_lfast.h:24
Definition: fir_filter_lfast.h:223
tap_type * alignedTaps
Definition: fir_filter_lfast.h:23
virtual std::vector< tap_type > taps() const
Definition: fir_filter_lfast.h:39
virtual void set_taps(const std::vector< tap_type > &newTaps)
Definition: fir_filter_lfast.h:35
virtual bool decimating()
Definition: fir_filter_lfast.h:160
virtual int getDecimation()
Definition: fir_filter_lfast.h:159