GNU Radio Manual and C++ API Reference  3.8.1.0
The Free & Open Software Radio Ecosystem
fft_shift.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2019 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 INCLUDED_FFT_FFT_SHIFT_H
24 #define INCLUDED_FFT_FFT_SHIFT_H
25 
26 #include <algorithm>
27 #include <cassert>
28 #include <vector>
29 
30 namespace gr {
31 namespace fft {
32 
33 /*! \brief reorder FFT results which are ordered from 0 to 1 in normalized frequency
34  * to -0.5 to 0.5 by cyclic shift
35  */
36 template <typename T>
37 class fft_shift
38 {
39 public:
40  fft_shift(size_t fft_length)
41  : d_fftlen(fft_length),
42  d_lenpos(fft_length / 2 + (fft_length % 2)),
43  d_lenneg(fft_length / 2),
44  d_buf(fft_length)
45  {
46  }
47 
48  /*! performs the cyclic shift on a vector v
49  */
50  void shift(std::vector<T>& v) { shift(&v.front(), v.size()); }
51 
52  /*! performs the cyclic shift on an array
53  */
54  void shift(T* data, size_t fft_len)
55  {
56  resize(fft_len);
57  std::copy_n(data, d_lenpos, d_buf.begin());
58  std::copy_n(data + d_lenpos, d_lenneg, data);
59  std::copy_n(d_buf.begin(), d_lenpos, data + d_lenneg);
60  }
61 
62  /*! if needed adjusts the buffer size to a new fft length
63  */
64  void resize(size_t fft_length)
65  {
66  if (d_fftlen == fft_length)
67  return;
68  d_fftlen = fft_length;
69  d_lenpos = d_fftlen / 2 + (d_fftlen % 2);
70  d_lenneg = d_fftlen / 2;
71  assert(d_lenpos + d_lenneg == d_fftlen);
72  d_buf.resize(d_lenpos);
73  }
74 
75 protected:
76 private:
77  size_t d_fftlen; // FFT length
78  size_t d_lenpos; // number of FFT bins with positive frequencies
79  size_t d_lenneg; // number of FFT bins with negative frequencies
80  std::vector<T> d_buf; // buffer used for cyclic shift
81 };
82 
83 } // namespace fft
84 } // namespace gr
85 #endif // INCLUDED_FFT_FFT_SHIFT_H
void resize(size_t fft_length)
Definition: fft_shift.h:64
fft_shift(size_t fft_length)
Definition: fft_shift.h:40
Definition: cc_common.h:45
GNU Radio logging wrapper for log4cpp library (C++ port of log4j)
Definition: basic_block.h:43
void shift(std::vector< T > &v)
Definition: fft_shift.h:50
reorder FFT results which are ordered from 0 to 1 in normalized frequency to -0.5 to 0...
Definition: fft_shift.h:37
void shift(T *data, size_t fft_len)
Definition: fft_shift.h:54