Added gr-pager component to trunk by merging from r3474:r3537 in
[debian/gnuradio] / gr-pager / src / pager_slicer_fb.cc
1 /*
2  * Copyright 2004,2006 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 2, 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, float beta)
30 {
31     return pager_slicer_fb_sptr(new pager_slicer_fb(alpha, beta));
32 }
33
34 pager_slicer_fb::pager_slicer_fb(float alpha, float beta) :
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 = beta;
41     d_max = 0.0;
42     d_hi = 0.0;
43     d_avg = 0.0;
44     d_lo = 0.0;
45     d_min = 0.0;
46 }
47
48 // Tracks average, minimum, and peak, then converts input into one of:
49 //
50 // [0, 1, 2, 3]
51 unsigned char pager_slicer_fb::slice(float sample)
52 {
53     unsigned char decision;
54
55     // Update DC level and remove
56     d_avg = d_avg*(1.0-d_alpha)+sample*d_alpha;
57     sample -= d_avg;
58
59     if (sample > 0) {
60         if (sample > d_hi) {                // In max region
61             d_max = d_max*(1.0-d_alpha) + sample*d_alpha;
62             decision = 3;
63         }
64         else {
65             d_max -= (d_max-d_avg)*d_beta;  // decay otherwise
66             decision = 2;
67         }
68     }
69     else {
70         if (sample < d_lo) {                // In min region
71             d_min = d_min*(1.0-d_alpha) + sample*d_alpha;
72             decision = 0;
73         }
74         else {
75             d_min -= (d_min-d_avg)*d_beta;  // decay otherwise
76             decision = 1;
77         }
78     }
79
80     d_hi = d_max*2.0/3.0;
81     d_lo = d_min*2.0/3.0;
82
83     //fprintf(stderr, "%f %d\n", sample, decision);
84     return decision;
85 }
86
87 int pager_slicer_fb::work(int noutput_items,
88                           gr_vector_const_void_star &input_items,
89                                   gr_vector_void_star &output_items)
90 {
91     float *iptr = (float *) input_items[0];
92     unsigned char *optr = (unsigned char *) output_items[0];
93
94     int size = noutput_items;
95
96     for (int i = 0; i < size; i++)
97         *optr++ = slice(*iptr++);
98
99     return noutput_items;
100 }