[1407] | 1 | /*! |
---|
| 2 | * \file |
---|
| 3 | * \brief Definition of frequency domain filter class |
---|
| 4 | * \author Simon Wood |
---|
| 5 | * |
---|
| 6 | * ------------------------------------------------------------------------- |
---|
| 7 | * |
---|
| 8 | * Copyright (C) 1995-2010 (see AUTHORS file for a list of contributors) |
---|
| 9 | * |
---|
| 10 | * This file is part of IT++ - a C++ library of mathematical, signal |
---|
| 11 | * processing, speech processing, and communications classes and functions. |
---|
| 12 | * |
---|
| 13 | * IT++ is free software: you can redistribute it and/or modify it under the |
---|
| 14 | * terms of the GNU General Public License as published by the Free Software |
---|
| 15 | * Foundation, either version 3 of the License, or (at your option) any |
---|
| 16 | * later version. |
---|
| 17 | * |
---|
| 18 | * IT++ is distributed in the hope that it will be useful, but WITHOUT ANY |
---|
| 19 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
| 20 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 21 | * details. |
---|
| 22 | * |
---|
| 23 | * You should have received a copy of the GNU General Public License along |
---|
| 24 | * with IT++. If not, see <http://www.gnu.org/licenses/>. |
---|
| 25 | * |
---|
| 26 | * ------------------------------------------------------------------------- |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #ifndef FREQ_FILT_H |
---|
| 30 | #define FREQ_FILT_H |
---|
| 31 | |
---|
| 32 | #include <itpp/base/vec.h> |
---|
| 33 | #include <itpp/base/converters.h> |
---|
| 34 | #include <itpp/base/math/elem_math.h> |
---|
| 35 | #include <itpp/base/matfunc.h> |
---|
| 36 | #include <itpp/base/specmat.h> |
---|
| 37 | #include <itpp/base/math/min_max.h> |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | namespace itpp |
---|
| 41 | { |
---|
| 42 | |
---|
| 43 | /*! |
---|
| 44 | \brief Freq_Filt Frequency domain filtering using the overlap-add technique |
---|
| 45 | \ingroup filters |
---|
| 46 | |
---|
| 47 | The Freq_Filt class implements an FFT based filter using the overlap-add |
---|
| 48 | technique. The data is filtered by first transforming the input sequence |
---|
| 49 | into the frequency domain with an efficient FFT implementation (i.e. FFTW) |
---|
| 50 | and then multiplied with a Fourier transformed version of the impulse |
---|
| 51 | response. The resulting data is then inversed Fourier transformed to return |
---|
| 52 | a filtered time domain signal. |
---|
| 53 | |
---|
| 54 | Freq_Filt is a templated class. The template paramter \c Num_T defines the |
---|
| 55 | data type for the impulse response \c b, input data \c x and output data |
---|
| 56 | \c y. |
---|
| 57 | |
---|
| 58 | The class constructor chooses an optimal FFT length and data block |
---|
| 59 | size that minimizes execution time. |
---|
| 60 | |
---|
| 61 | For example, |
---|
| 62 | \code |
---|
| 63 | vec b = "1 2 3 4"; |
---|
| 64 | Freq_Filt<double> FF(b,8000); |
---|
| 65 | \endcode |
---|
| 66 | |
---|
| 67 | where 8000 corresponds to the expected vector length of the data |
---|
| 68 | to be filtered. The actual number of elements does not have to be |
---|
| 69 | exact, but it should be close. |
---|
| 70 | |
---|
| 71 | Here is a complete example: |
---|
| 72 | \code |
---|
| 73 | vec b = "1 2 3 4"; |
---|
| 74 | vec x(20); |
---|
| 75 | x(0) = 1; |
---|
| 76 | |
---|
| 77 | // Define a filter object for doubles |
---|
| 78 | Freq_Filt<double> FF(b,x.length()); |
---|
| 79 | |
---|
| 80 | // Filter the data |
---|
| 81 | vec y = FF.filter(x); |
---|
| 82 | |
---|
| 83 | // Check the FFT and block sizes that were used |
---|
| 84 | int fftsize = FF.getFFTSize(); |
---|
| 85 | int blk = FF.getBlkSize(); |
---|
| 86 | \endcode |
---|
| 87 | |
---|
| 88 | To facilitate large data sets the Freq_Filt class has a streaming |
---|
| 89 | option. In this mode of operation data history is internally |
---|
| 90 | stored. This allows the class to be used for large data sets that |
---|
| 91 | are read from disk or streamed in real-time. |
---|
| 92 | |
---|
| 93 | \code |
---|
| 94 | bool stream = true; |
---|
| 95 | vec b = "1 2 3 4"; |
---|
| 96 | Freq_Filt<double> FF(b,1000); |
---|
| 97 | |
---|
| 98 | vec x,y; |
---|
| 99 | while(!EOF) { |
---|
| 100 | x = "read buffer of data"; |
---|
| 101 | y = FF.filter(x,stream); |
---|
| 102 | cout << << endl; |
---|
| 103 | } |
---|
| 104 | \endcode |
---|
| 105 | */ |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | template<class Num_T> |
---|
| 109 | class Freq_Filt |
---|
| 110 | { |
---|
| 111 | public: |
---|
| 112 | /*! |
---|
| 113 | \brief Constructor |
---|
| 114 | |
---|
| 115 | The empty constructor makes it possible to have other container objects |
---|
| 116 | of the Freq_Filt class |
---|
| 117 | */ |
---|
| 118 | Freq_Filt() {} |
---|
| 119 | |
---|
| 120 | /*! |
---|
| 121 | \brief Constructor with initialization of the impulse response \b b. |
---|
| 122 | |
---|
| 123 | Create a filter object with impulse response \b b. The FFT size and |
---|
| 124 | data block size are also initialized. |
---|
| 125 | \code |
---|
| 126 | vec b = "1 2 3 4"; |
---|
| 127 | vec x(20); |
---|
| 128 | Freq_Filt FF(b,x.length()); |
---|
| 129 | \endcode |
---|
| 130 | */ |
---|
| 131 | Freq_Filt(const Vec<Num_T> &b, const int xlength) {init(b, xlength);} |
---|
| 132 | |
---|
| 133 | /*! |
---|
| 134 | \brief Filter data in the input vector \b x |
---|
| 135 | |
---|
| 136 | Filters data in batch mode or streaming mode |
---|
| 137 | \code |
---|
| 138 | FF.filter(x); // Filters data in batch mode |
---|
| 139 | FF.filter(x,1); // Filters data in streaming mode |
---|
| 140 | \endcode |
---|
| 141 | */ |
---|
| 142 | Vec<Num_T> filter(const Vec<Num_T> &x, const int strm = 0); |
---|
| 143 | |
---|
| 144 | //! Return FFT size |
---|
| 145 | int get_fft_size() { return fftsize; } |
---|
| 146 | |
---|
| 147 | //! Return the data block size |
---|
| 148 | int get_blk_size() { return blksize; } |
---|
| 149 | |
---|
| 150 | //! Destructor |
---|
| 151 | ~Freq_Filt() {} |
---|
| 152 | |
---|
| 153 | private: |
---|
| 154 | int fftsize, blksize; |
---|
| 155 | cvec B; // FFT of impulse vector |
---|
| 156 | Vec<Num_T> impulse; |
---|
| 157 | Vec<Num_T> old_data; |
---|
| 158 | cvec zfinal; |
---|
| 159 | |
---|
| 160 | void init(const Vec<Num_T> &b, const int xlength); |
---|
| 161 | vec overlap_add(const vec &x); |
---|
| 162 | svec overlap_add(const svec &x); |
---|
| 163 | ivec overlap_add(const ivec &x); |
---|
| 164 | cvec overlap_add(const cvec &x); |
---|
| 165 | void overlap_add(const cvec &x, cvec &y); |
---|
| 166 | }; |
---|
| 167 | |
---|
| 168 | |
---|
| 169 | // Initialize impulse rsponse, FFT size and block size |
---|
| 170 | template <class Num_T> |
---|
| 171 | void Freq_Filt<Num_T>::init(const Vec<Num_T> &b, const int xlength) |
---|
| 172 | { |
---|
| 173 | // Set the impulse response |
---|
| 174 | impulse = b; |
---|
| 175 | |
---|
| 176 | // Get the length of the impulse response |
---|
| 177 | int num_elements = impulse.length(); |
---|
| 178 | |
---|
| 179 | // Initizlize old data |
---|
| 180 | old_data.set_size(0, false); |
---|
| 181 | |
---|
| 182 | // Initialize the final state |
---|
| 183 | zfinal.set_size(num_elements - 1, false); |
---|
| 184 | zfinal.zeros(); |
---|
| 185 | |
---|
| 186 | vec Lvec; |
---|
| 187 | |
---|
| 188 | /* |
---|
| 189 | * Compute the FFT size and the data block size to use. |
---|
| 190 | * The FFT size is N = L + M -1, where L corresponds to |
---|
| 191 | * the block size (to be determined) and M corresponds |
---|
| 192 | * to the impulse length. The method used here is designed |
---|
| 193 | * to minimize the (number of blocks) * (number of flops per FFT) |
---|
| 194 | * Use the FFTW flop computation of 5*N*log2(N). |
---|
| 195 | */ |
---|
| 196 | vec n = linspace(1, 20, 20); |
---|
| 197 | n = pow(2.0, n); |
---|
| 198 | ivec fftflops = to_ivec(elem_mult(5.0 * n, log2(n))); |
---|
| 199 | |
---|
| 200 | // Find where the FFT sizes are > (num_elements-1) |
---|
| 201 | //ivec index = find(n,">", static_cast<double>(num_elements-1)); |
---|
| 202 | ivec index(n.length()); |
---|
| 203 | int cnt = 0; |
---|
| 204 | for (int ii = 0; ii < n.length(); ii++) { |
---|
| 205 | if (n(ii) > (num_elements - 1)) { |
---|
| 206 | index(cnt) = ii; |
---|
| 207 | cnt += 1; |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | index.set_size(cnt, true); |
---|
| 211 | |
---|
| 212 | fftflops = fftflops(index); |
---|
| 213 | n = n(index); |
---|
| 214 | |
---|
| 215 | // Minimize number of blocks * number of flops per FFT |
---|
| 216 | Lvec = n - (double)(num_elements - 1); |
---|
| 217 | int min_ind = min_index(elem_mult(ceil((double)xlength / Lvec), to_vec(fftflops))); |
---|
| 218 | fftsize = static_cast<int>(n(min_ind)); |
---|
| 219 | blksize = static_cast<int>(Lvec(min_ind)); |
---|
| 220 | |
---|
| 221 | // Finally, compute the FFT of the impulse response |
---|
| 222 | B = fft(to_cvec(impulse), fftsize); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | // Filter data |
---|
| 226 | template <class Num_T> |
---|
| 227 | Vec<Num_T> Freq_Filt<Num_T>::filter(const Vec<Num_T> &input, const int strm) |
---|
| 228 | { |
---|
| 229 | Vec<Num_T> x, tempv; |
---|
| 230 | Vec<Num_T> output; |
---|
| 231 | |
---|
| 232 | /* |
---|
| 233 | * If we are not in streaming mode we want to process all of the data. |
---|
| 234 | * However, if we are in streaming mode we want to process the data in |
---|
| 235 | * blocks that are commensurate with the designed 'blksize' parameter. |
---|
| 236 | * So, we do a little book keeping to divide the iinput vector into the |
---|
| 237 | * correct size. |
---|
| 238 | */ |
---|
| 239 | if (!strm) { |
---|
| 240 | x = input; |
---|
| 241 | zfinal.zeros(); |
---|
| 242 | old_data.set_size(0, false); |
---|
| 243 | } |
---|
| 244 | else { // we aare in streaming mode |
---|
| 245 | tempv = concat(old_data, input); |
---|
| 246 | if (tempv.length() <= blksize) { |
---|
| 247 | x = tempv; |
---|
| 248 | old_data.set_size(0, false); |
---|
| 249 | } |
---|
| 250 | else { |
---|
| 251 | int end = tempv.length(); |
---|
| 252 | int numblks = end / blksize; |
---|
| 253 | if ((end % blksize)) { |
---|
| 254 | x = tempv(0, blksize * numblks - 1); |
---|
| 255 | old_data = tempv(blksize * numblks, end - 1); |
---|
| 256 | } |
---|
| 257 | else { |
---|
| 258 | x = tempv(0, blksize * numblks - 1); |
---|
| 259 | old_data.set_size(0, false); |
---|
| 260 | } |
---|
| 261 | } |
---|
| 262 | } |
---|
| 263 | output = overlap_add(x); |
---|
| 264 | |
---|
| 265 | return output; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | } // namespace itpp |
---|
| 269 | |
---|
| 270 | #endif // #ifndef FREQ_FILT_H |
---|