summaryrefslogtreecommitdiff
path: root/gr-vocoder/lib/cvsd_decode_bs_impl.cc
blob: 3aa24f3f52840a78e7b21052b517e9821da57aec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* -*- c++ -*- */
/*
 * Copyright 2007,2010,2011,2013 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "cvsd_decode_bs_impl.h"
#include <gnuradio/io_signature.h>
#include <limits.h>

namespace gr {
namespace vocoder {

cvsd_decode_bs::sptr cvsd_decode_bs::make(short min_step,
                                          short max_step,
                                          double step_decay,
                                          double accum_decay,
                                          int K,
                                          int J,
                                          short pos_accum_max,
                                          short neg_accum_max)
{
    return gnuradio::get_initial_sptr(new cvsd_decode_bs_impl(
        min_step, max_step, step_decay, accum_decay, K, J, pos_accum_max, neg_accum_max));
}

cvsd_decode_bs_impl::cvsd_decode_bs_impl(short min_step,
                                         short max_step,
                                         double step_decay,
                                         double accum_decay,
                                         int K,
                                         int J,
                                         short pos_accum_max,
                                         short neg_accum_max)
    : sync_interpolator("vocoder_cvsd_decode_bs",
                        io_signature::make(1, 1, sizeof(unsigned char)),
                        io_signature::make(1, 1, sizeof(short)),
                        8),
      d_min_step(min_step),
      d_max_step(max_step),
      d_step_decay(step_decay),
      d_accum_decay(accum_decay),
      d_K(K),
      d_J(J),
      d_pos_accum_max(pos_accum_max),
      d_neg_accum_max(neg_accum_max),
      d_accum(0),
      d_loop_counter(1),
      d_runner(0),
      d_runner_mask(0),
      d_stepsize(min_step)
{
    if (d_K > 32)
        throw std::runtime_error("cvsd_decode_bs_impl: K must be <= 32");

    if (d_J > d_K)
        throw std::runtime_error("cvsd_decode_bs_impl: J must be <= K");
}

cvsd_decode_bs_impl::~cvsd_decode_bs_impl()
{
    // nothing else required in this example
}

unsigned char cvsd_decode_bs_impl::cvsd_bitwise_sum(unsigned int input)
{
    unsigned int temp = input;
    unsigned char bits = 0;

    while (temp) {
        temp = temp & (temp - 1);
        bits++;
    }
    return bits;
}

int cvsd_decode_bs_impl::cvsd_round(double input)
{
    double temp;
    temp = input + 0.5;
    temp = floor(temp);

    return (int)temp;
}

unsigned int cvsd_decode_bs_impl::cvsd_pow(short radix, short power)
{
    double d_radix = (double)radix;
    int i_power = (int)power;
    double output;

    output = pow(d_radix, i_power);
    return ((unsigned int)cvsd_round(output));
}

int cvsd_decode_bs_impl::work(int noutput_items,
                              gr_vector_const_void_star& input_items,
                              gr_vector_void_star& output_items)
{
    const unsigned char* in = (const unsigned char*)input_items[0];
    short* out = (short*)output_items[0];

    int i = 0;
    short output_short = 0;       // 2 bytes 0 .. 65,535
    unsigned char bit_count = 0;  // 1 byte, 0 .. 255
    unsigned int mask = 0;        // 4 bytes, 0 .. 4,294,967,295
    unsigned char input_byte = 0; //  1 bytes
    unsigned char input_bit = 0;  // 1 byte, 0 .. 255

    // Loop through each input data point
    for (i = 0; i < noutput_items / 8.0; i++) {
        input_byte = in[i];
        // Initialize bit counter
        bit_count = 0;

        while (bit_count < 8) {
            // Compute the Appropriate Mask
            mask = cvsd_pow(2, 7 - bit_count);

            // Pull off the corresponding bit
            input_bit = input_byte & mask;

            // Update the bit counter
            bit_count++;

            // Update runner with the next input bit
            // Runner is a shift-register; shift left, add on newest output bit
            d_runner = (d_runner << 1) | ((unsigned int)input_bit);

            // Run this only if you have >= J bits in your shift register
            if (d_loop_counter >= d_J) {
                // Update Step Size
                d_runner_mask = (cvsd_pow(2, d_J) - 1);
                if ((cvsd_bitwise_sum(d_runner & d_runner_mask) >= d_J) ||
                    (cvsd_bitwise_sum((~d_runner) & d_runner_mask) >= d_J)) {
                    // Runs of 1s and 0s
                    d_stepsize = std::min((short)(d_stepsize + d_min_step), d_max_step);
                } else {
                    // No runs of 1s and 0s
                    d_stepsize = std::max((short)cvsd_round(d_stepsize * d_step_decay),
                                          d_min_step);
                }
            }

            // Update Accum (i.e. the reference value)
            if (input_bit) {
                d_accum = d_accum + d_stepsize;
            } else {
                d_accum = d_accum - d_stepsize;
            }

            // Multiply by Accum_Decay
            d_accum = (cvsd_round(d_accum * d_accum_decay));

            // Check for overflow
            if (d_accum >= ((int)d_pos_accum_max)) {
                d_accum = (int)d_pos_accum_max;
            } else if (d_accum <= ((int)d_neg_accum_max)) {
                d_accum = (int)d_neg_accum_max;
            }

            // Find the output short to write to the file
            output_short = ((short)d_accum);

            if (d_loop_counter <= d_K) {
                d_loop_counter++;
            }

            *(out++) = output_short;
        } // while ()

    } // for()

    return noutput_items;
}

} /* namespace vocoder */
} /* namespace gr */