Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_ctcss_squelch_ff.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2006 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 2, 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 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gr_ctcss_squelch_ff.h>
28
29 static float ctcss_tones[] = {
30    67.0,  71.9,  74.4,  77.0,  79.7,  82.5,  85.4,  88.5,  91.5,  94.8, 
31    97.4, 100.0, 103.5, 107.2, 110.9, 114.8, 118.8, 123.0, 127.3, 131.8,
32   136.5, 141.3, 146.2, 151.4, 156.7, 162.2, 167.9, 173.8, 179.9, 186.2, 
33   192.8, 203.5, 210.7, 218.1, 225.7, 233.6, 241.8, 250.3
34 };
35
36 static int max_tone_index = 37;
37
38 gr_ctcss_squelch_ff_sptr
39 gr_make_ctcss_squelch_ff(int rate, float freq, float level, int len, int ramp, bool gate)
40 {
41   return gr_ctcss_squelch_ff_sptr(new gr_ctcss_squelch_ff(rate, freq, level, len, ramp, gate));
42 }
43
44 int gr_ctcss_squelch_ff::find_tone(float freq)
45 {
46   for (int i = 0; i <= max_tone_index; i++)
47     if (ctcss_tones[i] == freq) // FIXME: make almost equal
48       return i;
49
50   return -1;
51 }
52
53 gr_ctcss_squelch_ff::gr_ctcss_squelch_ff(int rate, float freq, float level, int len, int ramp, bool gate) : 
54     gr_squelch_base_ff("ctcss_squelch_ff", ramp, gate)
55 {
56   d_freq = freq;
57   d_level = level;
58
59   // Default is 100 ms detection time
60   if (len == 0)
61     d_len = (int)(rate/10.0);
62   else
63     d_len = len;
64
65   int i = find_tone(freq);
66
67   // Non-standard tones or edge tones get 2% guard band, otherwise
68   // guards are set at adjacent ctcss tone frequencies
69   float f_l, f_r;
70   if (i == -1 || i == 0)
71     f_l = freq*0.98;
72   else
73     f_l = ctcss_tones[i-1];
74     
75   if (i == -1 || i == max_tone_index)
76     f_r = freq*1.02;
77   else
78     f_r = ctcss_tones[i+1];
79
80   d_goertzel_l = gri_goertzel(rate, d_len, f_l);
81   d_goertzel_c = gri_goertzel(rate, d_len, freq);
82   d_goertzel_r = gri_goertzel(rate, d_len, f_r);
83
84   d_mute = true;
85 }
86
87 std::vector<float> gr_ctcss_squelch_ff::squelch_range() const
88 {
89   std::vector<float> r(3);
90   r[0] = 0.0;
91   r[1] = 1.0;
92   r[2] = (r[1]-r[0])/100; // step size
93
94   return r;
95 }
96
97 void gr_ctcss_squelch_ff::update_state(const float &in)
98 {
99   d_goertzel_l.input(in);
100   d_goertzel_c.input(in);
101   d_goertzel_r.input(in);
102
103   float d_out_l, d_out_c, d_out_r;
104   if (d_goertzel_c.ready()) {
105       d_out_l = abs(d_goertzel_l.output());
106       d_out_c = abs(d_goertzel_c.output());
107       d_out_r = abs(d_goertzel_r.output());
108   
109       //printf("d_out_l=%f d_out_c=%f d_out_r=%f\n", d_out_l, d_out_c, d_out_r);
110       d_mute = (d_out_c < d_level || d_out_c < d_out_l || d_out_c < d_out_r);
111   }
112 }