Imported Upstream version 3.2.2
[debian/gnuradio] / gr-cvsd-vocoder / src / lib / cvsd_encode_sb.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_encode_sb.h>
33 #include <gr_io_signature.h>
34 #include <limits.h>
35
36 /*
37  * Create a new instance of cvsd_encode_sb and return
38  * a boost shared_ptr.  This is effectively the public constructor.
39  */
40 cvsd_encode_sb_sptr 
41 cvsd_make_encode_sb (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_encode_sb_sptr (new cvsd_encode_sb (min_step, max_step,
46                                                   step_decay, accum_decay, K, J,
47                                                   pos_accum_max, neg_accum_max));
48 }
49
50 cvsd_encode_sb::cvsd_encode_sb (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_decimator ("cvsd_encode_sb",
54                        gr_make_io_signature (1, 1, sizeof (short)),
55                        gr_make_io_signature (1, 1, sizeof (unsigned char)),
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_stepsize(min_step)
64
65 {
66   assert(d_K <= 32);
67   assert(d_J <= d_K);
68 }
69
70
71 cvsd_encode_sb::~cvsd_encode_sb ()
72 {
73   // nothing else required in this example
74 }
75
76 unsigned char cvsd_encode_sb::cvsd_bitwise_sum (unsigned int input)
77 {
78   unsigned int temp=input;
79   unsigned char bits=0;
80   
81   while(temp) {
82     temp=temp&(temp-1);
83     bits++;
84   }
85   return bits;
86 }
87
88 int cvsd_encode_sb::cvsd_round (double input)
89 {
90   double temp;
91   temp=input+0.5;
92   temp=floor(temp);
93   
94   return (int)temp;
95 }
96
97 unsigned int cvsd_encode_sb::cvsd_pow (short radix, short power)
98 {
99   double d_radix = (double) radix;
100   int i_power = (int) power;
101   double output;
102
103   output=pow(d_radix,i_power);
104   return ( (unsigned int) cvsd_round(output));  
105 }
106
107
108
109 int 
110 cvsd_encode_sb::work (int noutput_items,
111                              gr_vector_const_void_star &input_items,
112                              gr_vector_void_star &output_items)
113 {
114   const short *in = (const short *) input_items[0];
115   unsigned char *out = (unsigned char *) output_items[0];
116
117   unsigned short i=0;            // 2 bytes, 0 .. 65,535
118   unsigned char output_bit=0;    // 1 byte, 0 .. 255
119   unsigned char output_byte=0;   // 1 bytes 0.255
120   unsigned char bit_count=0;             // 1 byte, 0 .. 255
121   unsigned int mask=0;           // 4 bytes, 0 .. 4,294,967,295
122   
123   // Loop through each input data point
124   for(i = 0; i < noutput_items*8; i++) {
125     if((int)in[i] >= d_accum) {    // Note:  sign((data(n)-accum))
126       output_bit=1;
127     }
128     else {
129       output_bit=0;
130     }
131     
132     // Update Accum (i.e. the reference value)
133     if (output_bit) {
134       d_accum=d_accum+d_stepsize;
135       //printf("Addding %d to the accum; the result is: %d.\n", d_stepsize, d_accum);
136     }
137     else {
138       d_accum=d_accum-d_stepsize;
139       //printf("Subtracting %d to the accum; the result is: %d.\n", d_stepsize, d_accum);
140     }
141
142     // Multiply by Accum_Decay
143     d_accum=(cvsd_round(d_accum*d_accum_decay));
144
145     // Check for overflow
146     if (d_accum >= ((int)d_pos_accum_max)) {
147       d_accum = (int)d_pos_accum_max;
148     }
149     else if(d_accum <= ((int) d_neg_accum_max)) {
150       d_accum = (int) d_neg_accum_max;
151     }
152                  
153     // Update runner with the last output bit
154     // Update Step Size
155     if (d_loop_counter >= d_J) { // Run this only if you have >= J bits in your shift register
156       mask=(cvsd_pow(2, d_J) - 1);
157       if ((cvsd_bitwise_sum(d_runner & mask) >= d_J) || (cvsd_bitwise_sum((~d_runner) & mask) >= d_J)) {
158         // Runs of 1s and 0s
159         d_stepsize = std::min( (short)(d_stepsize + d_min_step), d_max_step);
160       }
161       else {
162         // No runs of 1s and 0s
163         d_stepsize = std::max( (short)cvsd_round(d_stepsize*d_step_decay), d_min_step);     
164       }
165     }
166              
167     // Runner is a shift-register; shift left, add on newest output bit
168     d_runner = (d_runner<<1) | ((unsigned int) output_bit);
169
170     // Update the ouput type; shift left, add on newest output bit
171     // If you have put in 8 bits, output it as a byte
172     output_byte = (output_byte<<1) | output_bit;
173     bit_count++;
174     
175     if (d_loop_counter <= d_K) {
176       d_loop_counter++;
177     }
178
179     // If you have put 8 bits, output and clear.
180     if (bit_count==8) {
181       // Read in short from the file
182       *(out++) = output_byte;
183       
184       // Reset the bit_count
185       bit_count=0;
186       output_byte=0;
187     }
188   } // While
189
190   return noutput_items;
191 }