File: rx_filter.cpp

package info (click to toggle)
gqrx-sdr 2.3.1-2~bpo70%2B1
  • links: PTS
  • area: main
  • in suites: wheezy-backports
  • size: 2,628 kB
  • sloc: cpp: 12,005; ansic: 558; makefile: 2
file content (166 lines) | stat: -rw-r--r-- 5,313 bytes parent folder | download | duplicates (2)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* -*- c++ -*- */
/*
 * Gqrx SDR: Software defined radio receiver powered by GNU Radio and Qt
 *           http://gqrx.dk/
 *
 * Copyright 2011 Alexandru Csete OZ9AEC.
 *
 * Gqrx is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * Gqrx is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Gqrx; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */
#include <cmath>
#include <gnuradio/io_signature.h>
#include <gnuradio/filter/firdes.h>
#include <iostream>
#include "dsp/rx_filter.h"

static const int MIN_IN = 1;  /* Mininum number of input streams. */
static const int MAX_IN = 1;  /* Maximum number of input streams. */
static const int MIN_OUT = 1; /* Minimum number of output streams. */
static const int MAX_OUT = 1; /* Maximum number of output streams. */


/*
 * Create a new instance of rx_filter and return
 * a boost shared_ptr. This is effectively the public constructor.
 */
rx_filter_sptr make_rx_filter(double sample_rate, double low, double high, double trans_width)
{
    return gnuradio::get_initial_sptr(new rx_filter(sample_rate, low, high, trans_width));
}

rx_filter::rx_filter(double sample_rate, double low, double high, double trans_width)
    : gr::hier_block2 ("rx_filter",
                      gr::io_signature::make (MIN_IN, MAX_IN, sizeof (gr_complex)),
                      gr::io_signature::make (MIN_OUT, MAX_OUT, sizeof (gr_complex))),
      d_sample_rate(sample_rate),
      d_low(low),
      d_high(high),
      d_trans_width(trans_width)
{
    if (low < -0.95*sample_rate/2.0)
        d_low = -0.95*sample_rate/2.0;
    if (high > 0.95*sample_rate/2.0)
        d_high = 0.95*sample_rate/2.0;

    /* generate taps */
    d_taps = gr::filter::firdes::complex_band_pass(1.0, d_sample_rate, d_low, d_high, d_trans_width);

    /* create band pass filter */
    d_bpf = gr::filter::fir_filter_ccc::make(1, d_taps);

    /* connect filter */
    connect(self(), 0, d_bpf, 0);
    connect(d_bpf, 0, self(), 0);
}

rx_filter::~rx_filter ()
{

}

void rx_filter::set_param(double low, double high, double trans_width)
{
    d_trans_width = trans_width;
    d_low         = low;
    d_high        = high;

    if (d_low < -0.95*d_sample_rate/2.0)
        d_low = -0.95*d_sample_rate/2.0;
    if (d_high > 0.95*d_sample_rate/2.0)
        d_high = 0.95*d_sample_rate/2.0;

    /* generate new taps */
    d_taps = gr::filter::firdes::complex_band_pass(1.0, d_sample_rate, d_low, d_high, d_trans_width);

#ifndef QT_NO_DEBUG_OUTPUT
    std::cout << "Genrating taps for new filter LO:" << d_low <<
                 " HI:" << d_high << " TW:" << d_trans_width << std::endl;
    std::cout << "Required number of taps: " << d_taps.size() << std::endl;
#endif

    d_bpf->set_taps(d_taps);
}


/** Frequency translating filter **/

/*
 * Create a new instance of rx_xlating_filter and return
 * a boost shared_ptr. This is effectively the public constructor.
 */
rx_xlating_filter_sptr make_rx_xlating_filter(double sample_rate, double center, double low, double high, double trans_width)
{
    return gnuradio::get_initial_sptr(new rx_xlating_filter(sample_rate, center, low, high, trans_width));
}

rx_xlating_filter::rx_xlating_filter(double sample_rate, double center, double low, double high, double trans_width)
    : gr::hier_block2 ("rx_xlating_filter",
                      gr::io_signature::make (MIN_IN, MAX_IN, sizeof (gr_complex)),
                      gr::io_signature::make (MIN_OUT, MAX_OUT, sizeof (gr_complex))),
      d_sample_rate(sample_rate),
      d_center(center),
      d_low(low),
      d_high(high),
      d_trans_width(trans_width)
{
    /* generate taps */
    d_taps = gr::filter::firdes::complex_band_pass(1.0, d_sample_rate, -d_high, -d_low, d_trans_width);

    /* create band pass filter */
    d_bpf = gr::filter::freq_xlating_fir_filter_ccc::make(1, d_taps, d_center, d_sample_rate);

    /* connect filter */
    connect(self(), 0, d_bpf, 0);
    connect(d_bpf, 0, self(), 0);
}


rx_xlating_filter::~rx_xlating_filter()
{

}


void rx_xlating_filter::set_offset(double center)
{
    /* we have to change sign because the set_center_freq() actually
       shifts the passband with the specified amount, which has
       opposite sign of selecting a center frequency.
    */
    d_center = -center;
    d_bpf->set_center_freq(d_center);
}


void rx_xlating_filter::set_param(double low, double high, double trans_width)
{
    d_trans_width = trans_width;
    d_low         = low;
    d_high        = high;

    /* generate new taps */
    d_taps = gr::filter::firdes::complex_band_pass(1.0, d_sample_rate, -d_high, -d_low, d_trans_width);

    d_bpf->set_taps(d_taps);
}


void rx_xlating_filter::set_param(double center, double low, double high, double trans_width)
{
    set_offset(center);
    set_param(low, high, trans_width);
}