Imported Upstream version 3.2.2
[debian/gnuradio] / gr-pager / src / pager_slicer_fb.cc
1 /*
2  * Copyright 2004,2006,2007 Free Software Foundation, Inc.
3  * 
4  * This file is part of GNU Radio
5  * 
6  * GNU Radio is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3, or (at your option)
9  * any later version.
10  * 
11  * GNU Radio is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License
17  * along with GNU Radio; see the file COPYING.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <pager_slicer_fb.h>
27 #include <gr_io_signature.h>
28
29 pager_slicer_fb_sptr pager_make_slicer_fb(float alpha)
30 {
31     return pager_slicer_fb_sptr(new pager_slicer_fb(alpha));
32 }
33
34 pager_slicer_fb::pager_slicer_fb(float alpha) :
35     gr_sync_block ("slicer_fb",
36                    gr_make_io_signature (1, 1, sizeof(float)),
37                    gr_make_io_signature (1, 1, sizeof(unsigned char)))
38 {
39     d_alpha = alpha;
40     d_beta = 1.0-alpha;
41     d_avg = 0.0;
42 }
43
44 // Tracks average, minimum, and peak, then converts input into one of:
45 //
46 // [0, 1, 2, 3]
47 unsigned char pager_slicer_fb::slice(float sample)
48 {
49     unsigned char decision;
50
51     // Update DC level and remove
52     d_avg = d_avg*d_beta+sample*d_alpha;
53     sample -= d_avg;
54
55     if (sample > 0) {
56         if (sample > 2.0)          
57             decision = 3;
58         else
59             decision = 2;
60     }
61     else {
62         if (sample < -2.0)
63             decision = 0;
64         else
65             decision = 1;
66     }
67
68     return decision;
69 }
70
71 int pager_slicer_fb::work(int noutput_items,
72                           gr_vector_const_void_star &input_items,
73                                   gr_vector_void_star &output_items)
74 {
75     float *iptr = (float *) input_items[0];
76     unsigned char *optr = (unsigned char *) output_items[0];
77
78     int size = noutput_items;
79
80     for (int i = 0; i < size; i++)
81         *optr++ = slice(*iptr++);
82
83     return noutput_items;
84 }