Imported Upstream version 3.2.2
[debian/gnuradio] / gr-cvsd-vocoder / src / lib / cvsd_decode_bs.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007 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 3, 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 /*
24  * config.h is generated by configure.  It contains the results
25  * of probing for features, options etc.  It should be the first
26  * file included in your .cc file.
27  */
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <cvsd_decode_bs.h>
33 #include <gr_io_signature.h>
34 #include <limits.h>
35
36 /*
37  * Create a new instance of cvsd_decode_bs and return
38  * a boost shared_ptr.  This is effectively the public constructor.
39  */
40 cvsd_decode_bs_sptr 
41 cvsd_make_decode_bs (short min_step, short max_step, double step_decay,
42                      double accum_decay,  int K, int J,
43                      short pos_accum_max, short neg_accum_max)
44 {
45   return cvsd_decode_bs_sptr (new cvsd_decode_bs (min_step, max_step,
46                                                   step_decay, accum_decay, K, J,
47                                                   pos_accum_max, neg_accum_max));
48 }
49
50 cvsd_decode_bs::cvsd_decode_bs (short min_step, short max_step, double step_decay, 
51                                 double accum_decay, int K, int J,
52                                 short pos_accum_max, short neg_accum_max)
53   : gr_sync_interpolator ("cvsd_decode_bs",
54                           gr_make_io_signature (1, 1, sizeof (unsigned char)),
55                           gr_make_io_signature (1, 1, sizeof (short)),
56                           8),
57     d_min_step (min_step), d_max_step(max_step), d_step_decay(step_decay),
58     d_accum_decay(accum_decay), d_K(K), d_J(J), 
59     d_pos_accum_max(pos_accum_max), d_neg_accum_max(neg_accum_max),
60     d_accum(0), 
61     d_loop_counter(1), 
62     d_runner(0),
63     d_runner_mask(0),
64     d_stepsize(min_step)
65   
66 {
67   assert(d_K <= 32);
68   assert(d_J <= d_K);
69 }
70
71
72 cvsd_decode_bs::~cvsd_decode_bs ()
73 {
74   // nothing else required in this example
75 }
76
77 unsigned char cvsd_decode_bs::cvsd_bitwise_sum (unsigned int input)
78 {
79   unsigned int temp=input;
80   unsigned char bits=0;
81   
82   while(temp) {
83     temp=temp&(temp-1);
84     bits++;
85   }
86   return bits;
87 }
88
89 int cvsd_decode_bs::cvsd_round (double input)
90 {
91   double temp;
92   temp=input+0.5;
93   temp=floor(temp);
94   
95   return (int)temp;
96 }
97
98 unsigned int cvsd_decode_bs::cvsd_pow (short radix, short power)
99 {
100   double d_radix = (double) radix;
101   int i_power = (int) power;
102   double output;
103   
104   output=pow(d_radix,i_power);
105   return ( (unsigned int) cvsd_round(output));  
106 }
107
108
109 int 
110 cvsd_decode_bs::work (int noutput_items,
111                       gr_vector_const_void_star &input_items,
112                       gr_vector_void_star &output_items)
113 {
114  
115
116   const unsigned char *in = (const unsigned char *) input_items[0];
117   short *out = (short *) output_items[0];
118
119   int i=0;
120   short output_short=0;          // 2 bytes 0 .. 65,535
121   unsigned char bit_count=0;     // 1 byte, 0 .. 255
122   unsigned int mask=0;           // 4 bytes, 0 .. 4,294,967,295
123   unsigned char input_byte=0;    //  1 bytes
124   unsigned char input_bit=0;     // 1 byte, 0 .. 255
125   
126   // Loop through each input data point
127   for(i = 0; i < noutput_items/8.0; i++) {
128
129     input_byte = in[i];
130     // Initiliaze bit counter
131     bit_count=0;        
132     
133     while(bit_count<8) {
134       // Compute the Appropriate Mask
135       mask=cvsd_pow(2,7-bit_count);
136       
137       // Pull off the corresponding bit
138       input_bit = input_byte & mask;
139       
140       // Update the bit counter
141       bit_count++;
142       
143       // Update runner with the next input bit
144       // Runner is a shift-register; shift left, add on newest output bit
145       d_runner = (d_runner<<1) | ((unsigned int) input_bit);
146       
147       // Run this only if you have >= J bits in your shift register
148       if (d_loop_counter>=d_J) {
149         // Update Step Size
150         d_runner_mask=(cvsd_pow(2,d_J)-1);
151         if ((cvsd_bitwise_sum(d_runner & d_runner_mask)>=d_J)||(cvsd_bitwise_sum((~d_runner) & d_runner_mask)>=d_J)) {
152           // Runs of 1s and 0s
153           d_stepsize = std::min( (short) (d_stepsize + d_min_step), d_max_step);
154         }
155         else {
156           // No runs of 1s and 0s
157           d_stepsize = std::max( (short) cvsd_round(d_stepsize*d_step_decay), d_min_step);
158         }
159       }
160       
161       // Update Accum (i.e. the reference value)
162       if (input_bit) {
163         d_accum=d_accum+d_stepsize;
164       }
165       else {
166         d_accum=d_accum-d_stepsize;
167       }
168       
169       // Multiply by Accum_Decay
170       d_accum=(cvsd_round(d_accum*d_accum_decay));
171       
172       // Check for overflow
173       if (d_accum >=((int) d_pos_accum_max)) {
174         d_accum=(int)d_pos_accum_max;
175       }
176       else if (d_accum <=((int) d_neg_accum_max)) {
177         d_accum=(int)d_neg_accum_max;
178       }
179       
180       // Find the output short to write to the file
181       output_short=((short) d_accum);
182       
183       if (d_loop_counter <= d_K) {
184         d_loop_counter++;
185       }
186       
187       *(out++) = output_short;
188     } // while ()       
189     
190   } // for()
191   
192   return noutput_items;
193 }