GNU Radio 3.6.1 C++ API
vocoder_cvsd_encode_sb.h
Go to the documentation of this file.
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 #ifndef INCLUDED_VOCODER_CVSD_ENCODER_SB_H
23 #define INCLUDED_VOCODER_CVSD_ENCODER_SB_H
24 
25 #include <vocoder_api.h>
26 #include <gr_sync_decimator.h>
27 
29 
31 
32  /*!
33  * \brief Constructor parameters to initialize the CVSD encoder. The
34  * default values are modeled after the Bluetooth standard and should
35  * not be changed except by an advanced user
36  *
37  * \ingroup vocoder_blk
38  *
39  * \param min_step Minimum step size used to update the internal reference. Default: "10"
40  * \param max_step Maximum step size used to update the internal reference. Default: "1280"
41  * \param step_decay Decay factor applied to step size when there is not a run of J output 1s or 0s. Default: "0.9990234375" (i.e. 1-1/1024)
42  * \param accum_decay Decay factor applied to the internal reference during every interation of the codec. Default: "0.96875" (i.e. 1-1/32)
43  * \param K; Size of shift register; the number of output bits remembered by codec (must be less or equal to 32). Default: "32"
44  * \param J; Number of bits in the shift register that are equal; i.e. the size of a run of 1s, 0s. Default: "4"
45  * \param pos_accum_max Maximum integer value allowed for the internal reference. Default: "32767" (2^15 - 1 or MAXSHORT)
46  * \param neg_accum_max Minimum integer value allowed for the internal reference. Default: "-32767" (-2^15 + 1 or MINSHORT+1)
47  *
48  */
49 
51  short max_step=1280,
52  double step_decay=0.9990234375,
53  double accum_decay= 0.96875,
54  int K=32,
55  int J=4,
56  short pos_accum_max=32767,
57  short neg_accum_max=-32767);
58 
59 /*!
60  * \brief This block performs CVSD audio encoding. Its design and implementation
61  * is modeled after the CVSD encoder/decoder specifications defined in the
62  * Bluetooth standard.
63  *
64  * \ingroup vocoder_blk
65  *
66  * CVSD is a method for encoding speech that seeks to reduce the
67  * bandwidth required for digital voice transmission. CVSD takes
68  * advantage of strong correlation between samples, quantizing the
69  * difference in amplitude between two consecutive samples. This
70  * difference requires fewer quantization levels as compared to other
71  * methods that quantize the actual amplitude level, reducing the
72  * bandwidth. CVSD employs a two level quantizer (one bit) and an
73  * adaptive algorithm that allows for continuous step size adjustment.
74  *
75  * The coder can represent low amplitude signals with accuracy without
76  * sacrificing performance on large amplitude signals, a trade off that
77  * occurs in some non-adaptive modulations.
78  *
79  * The CVSD encoder effectively provides 8-to-1 compression. More
80  * specifically, each incoming audio sample is compared to an internal
81  * reference value. If the input is greater or equal to the reference,
82  * the encoder outputs a "1" bit. If the input is less than the reference,
83  * the encoder outputs a "0" bit. The reference value is then updated
84  * accordingly based on the frequency of outputted "1" or "0" bits. By
85  * grouping 8 outputs bits together, the encoder essentially produce one
86  * output byte for every 8 input audio samples.
87  *
88  * This encoder requires that input audio samples are 2-byte short signed
89  * integers. The result bandwidth conversion, therefore, is 16 input bytes
90  * of raw audio data to 1 output byte of encoded audio data.
91  *
92  * The CVSD encoder module must be prefixed by an up-converter to over-sample
93  * the audio data prior to encoding. The Bluetooth standard specifically
94  * calls for a 1-to-8 interpolating up-converter. While this reduces the
95  * overall compression of the codec, this is required so that the encoder
96  * can accurately compute the slope between adjacent audio samples and
97  * correctly update its internal reference value.
98  *
99  * References:
100  *
101  * 1. Continuously Variable Slope Delta Modulation (CVSD) A Tutorial,
102  * Available: http://www.eetkorea.com/ARTICLES/2003AUG/A/2003AUG29_NTEK_RFD_AN02.PDF.
103  *
104  * 2. Specification of The Bluetooth System
105  * Available: http://grouper.ieee.org/groups/802/15/Bluetooth/core_10_b.pdf.
106  *
107  * 3. McGarrity, S., Bluetooth Full Duplex Voice and Data Transmission. 2002.
108  * Bluetooth Voice Simulink® Model, Available:
109  * http://www.mathworks.com/company/newsletters/digest/nov01/bluetooth.html
110  *
111  */
112 
114 {
115 private:
117  short max_step,
118  double step_decay,
119  double accum_decay,
120  int K,
121  int J,
122  short pos_accum_max,
123  short neg_accum_max);
124 
125  vocoder_cvsd_encode_sb(short min_step, short max_step, double step_decay,
126  double accum_decay, int K, int J,
127  short pos_accum_max, short neg_accum_max);
128 
129  //! Member functions required by the encoder/decoder
130  //! \brief Rounding function specific to CVSD
131  //! \return the input value rounded to the nearest integer
132  int cvsd_round(double input);
133 
134  //! \brief A power function specific to CVSD data formats
135  //! \return (radix)^power, where radix and power are short integers
136  unsigned int cvsd_pow (short radix, short power);
137 
138  //! \brief Sums number of 1's in the input
139  //! \return the number of 1s in the four bytes of an input unsigned integer
140  unsigned char cvsd_bitwise_sum (unsigned int input);
141 
142  // Members variables related to the CVSD encoder use to update interal reference value
143  short d_min_step;
144  short d_max_step;
145  double d_step_decay;
146  double d_accum_decay;
147 
148  int d_K; //!< \brief Size of shift register; the number of output bits remembered in shift register
149  int d_J; //!< \brief Number of bits in the shift register that are equal; size of run of 1s, 0s
150 
151  short d_pos_accum_max;
152  short d_neg_accum_max;
153 
154  int d_accum; //!< \brief Current value of internal reference
155  int d_loop_counter; //!< \brief Current value of the loop counter
156  unsigned int d_runner; //!< \brief Current value of the shift register
157  short d_stepsize; //!< \brief Current value of the step sizer
158 
159  public:
160  ~vocoder_cvsd_encode_sb (); // public destructor
161 
162  short min_step() { return d_min_step; }
163  short max_step() { return d_max_step; }
164  double step_decay() { return d_step_decay; }
165  double accum_decay() { return d_accum_decay; }
166  int K() { return d_K; }
167  int J() { return d_J; }
168  short pos_accum_max() { return d_pos_accum_max; }
169  short neg_accum_max() { return d_neg_accum_max; }
170 
171  int work (int noutput_items,
172  gr_vector_const_void_star &input_items,
173  gr_vector_void_star &output_items);
174 };
175 
176 #endif /* INCLUDED_VOCODER_CVSD_ENCODE_SB_H */