summaryrefslogtreecommitdiff
path: root/gr-filter/lib/filterbank.cc
blob: 20bdf29ca7ccf032eb7b15a22d75dbf753e4c81d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* -*- c++ -*- */
/*
 * Copyright 2012,2014 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gnuradio/filter/filterbank.h>
#include <cstdio>
#include <iostream>
#include <stdexcept>

namespace gr {
namespace filter {
namespace kernel {

filterbank::filterbank(const std::vector<std::vector<float>>& taps)
    : d_taps(taps), d_nfilts(d_taps.size())
{
    if (d_nfilts == 0) {
        throw std::invalid_argument("The taps vector may not be empty.");
    }
    d_active.resize(d_nfilts);
    // Create an FIR filter for each channel and zero out the taps
    std::vector<float> vtaps(1, 0.0f);
    d_fir_filters.reserve(d_nfilts);
    for (unsigned int i = 0; i < d_nfilts; i++) {
        d_fir_filters.emplace_back(1, vtaps);
    }
    // Now, actually set the filters' taps
    set_taps(d_taps);
}

void filterbank::set_taps(const std::vector<std::vector<float>>& taps)
{
    // Check that the number of filters is correct.
    if (d_nfilts != taps.size()) {
        throw std::runtime_error("The number of filters is incorrect.");
    }
    d_taps = taps;
    // Check that taps contains vectors of taps, where each vector
    // is the same length.
    d_ntaps = d_taps[0].size();
    for (unsigned int i = 1; i < d_nfilts; i++) {
        if (d_taps[i].size() != d_ntaps) {
            throw std::runtime_error("All sets of taps must be of the same length.");
        }
    }
    for (unsigned int i = 0; i < d_nfilts; i++) {
        // If filter taps are all zeros don't bother to crunch the numbers.
        d_active[i] = false;
        for (unsigned int j = 0; j < d_ntaps; j++) {
            if (d_taps[i][j] != 0) {
                d_active[i] = true;
                break;
            }
        }

        d_fir_filters[i].set_taps(d_taps[i]);
    }
}

void filterbank::print_taps()
{
    unsigned int i, j;
    for (i = 0; i < d_nfilts; i++) {
        printf("filter[%d]: [", i);
        for (j = 0; j < d_taps_per_filter; j++) {
            printf(" %.4e", d_taps[i][j]);
        }
        printf("]\n\n");
    }
}

std::vector<std::vector<float>> filterbank::taps() const { return d_taps; }

} /* namespace kernel */
} /* namespace filter */
} /* namespace gr */