GNU Radio 3.4.2 C++ API
cvsd_encode_sb.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2007 Free Software Foundation, Inc.
00004  * 
00005  * This file is part of GNU Radio
00006  * 
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  * 
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 #ifndef INCLUDED_CVSD_ENCODER_SB_H
00023 #define INCLUDED_CVSD_ENCODER_SB_H
00024 
00025 #include <gr_sync_decimator.h>
00026 
00027 class cvsd_encode_sb;
00028 
00029 typedef boost::shared_ptr<cvsd_encode_sb> cvsd_encode_sb_sptr;
00030 
00031  /*!
00032   * \brief Constructor parameters to initialize the CVSD encoder.  The default
00033   * values are modeled after the Bluetooth standard and should not be changed
00034   * except by an advanced user
00035   *
00036   * \param min_step       Minimum step size used to update the internal reference.  Default: "10"
00037   * \param max_step       Maximum step size used to update the internal reference.  Default: "1280"
00038   * \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)
00039   * \param accum_decay    Decay factor applied to the internal reference during every interation of the codec.  Default: "0.96875"  (i.e. 1-1/32)
00040   * \param K;             Size of shift register; the number of output bits remembered by codec (must be less or equal to 32).  Default: "32"
00041   * \param J;             Number of bits in the shift register that are equal; i.e. the size of a run of 1s, 0s. Default: "4"
00042   * \param pos_accum_max  Maximum integer value allowed for the internal reference.  Default: "32767" (2^15 - 1 or MAXSHORT)
00043   * \param neg_accum_max  Minimum integer value allowed for the internal reference.  Default: "-32767" (-2^15 + 1 or MINSHORT+1)
00044   *
00045   */
00046 
00047 cvsd_encode_sb_sptr cvsd_make_encode_sb (short min_step=10,
00048                                          short max_step=1280,
00049                                          double step_decay=0.9990234375,
00050                                          double accum_decay= 0.96875,
00051                                          int K=32,
00052                                          int J=4,
00053                                          short pos_accum_max=32767,
00054                                          short neg_accum_max=-32767);
00055 
00056 /*!
00057  * \brief This block performs CVSD audio encoding.  Its design and implementation
00058  * is modeled after the CVSD encoder/decoder specifications defined in the
00059  * Bluetooth standard.
00060  *
00061  * \ingroup vocoder_blk
00062  *
00063  * CVSD is a method for encoding speech that seeks to reduce the
00064  * bandwidth required for digital voice transmission.  CVSD takes
00065  * advantage of strong correlation between samples, quantizing the
00066  * difference in amplitude between two consecutive samples.  This
00067  * difference requires fewer quantization levels as compared to other
00068  * methods that quantize the actual amplitude level, reducing the
00069  * bandwidth.  CVSD employs a two level quantizer (one bit) and an
00070  * adaptive algorithm that allows for continuous step size adjustment.
00071  *
00072  * The coder can represent low amplitude signals with accuracy without
00073  * sacrificing performance on large amplitude signals, a trade off that
00074  * occurs in some non-adaptive modulations.
00075  *
00076  * The CVSD encoder effectively provides 8-to-1 compression.  More
00077  * specifically, each incoming audio sample is compared to an internal
00078  * reference value.  If the input is greater or equal to the reference,
00079  * the encoder outputs a "1" bit.  If the input is less than the reference,
00080  * the encoder outputs a "0" bit.  The reference value is then updated
00081  * accordingly based on the frequency of outputted "1" or "0" bits.  By
00082  * grouping 8 outputs bits together, the encoder essentially produce one
00083  * output byte for every 8 input audio samples.
00084  *
00085  * This encoder requires that input audio samples are 2-byte short signed
00086  * integers.  The result bandwidth conversion, therefore, is 16 input bytes
00087  * of raw audio data to 1 output byte of encoded audio data.
00088  *
00089  * The CVSD encoder module must be prefixed by an up-converter to over-sample
00090  * the audio data prior to encoding.  The Bluetooth standard specifically
00091  * calls for a 1-to-8 interpolating up-converter.  While this reduces the
00092  * overall compression of the codec, this is required so that the encoder
00093  * can accurately compute the slope between adjacent audio samples and
00094  * correctly update its internal reference value.
00095  *
00096  * References:
00097  *
00098  * 1.  Continuously Variable Slope Delta Modulation (CVSD) A Tutorial,
00099  *     Available: http://www.eetkorea.com/ARTICLES/2003AUG/A/2003AUG29_NTEK_RFD_AN02.PDF.
00100  *
00101  * 2.  Specification of The Bluetooth System
00102  *     Available: http://grouper.ieee.org/groups/802/15/Bluetooth/core_10_b.pdf.
00103  *
00104  * 3.  McGarrity, S., Bluetooth Full Duplex Voice and Data Transmission. 2002.
00105  *     Bluetooth Voice Simulink® Model, Available:
00106  *     http://www.mathworks.com/company/newsletters/digest/nov01/bluetooth.html
00107  *
00108  */
00109 
00110 class cvsd_encode_sb : public gr_sync_decimator
00111 {
00112 private:
00113   friend cvsd_encode_sb_sptr cvsd_make_encode_sb (short min_step,
00114                                                   short max_step,
00115                                                   double step_decay,
00116                                                   double accum_decay,
00117                                                   int K,
00118                                                   int J,
00119                                                   short pos_accum_max,
00120                                                   short neg_accum_max);
00121 
00122   cvsd_encode_sb (short min_step, short max_step, double step_decay,
00123                   double accum_decay, int K, int J,
00124                   short pos_accum_max, short neg_accum_max);
00125   
00126   //! Member functions required by the encoder/decoder
00127   //! \brief Rounding function specific to CVSD
00128   //! \return the input value rounded to the nearest integer
00129   int cvsd_round(double input);
00130   
00131   //! \brief A power function specific to CVSD data formats
00132   //! \return (radix)^power, where radix and power are short integers
00133   unsigned int cvsd_pow (short radix, short power);
00134  
00135   //! \brief Sums number of 1's in the input
00136   //! \return the number of 1s in the four bytes of an input unsigned integer
00137   unsigned char cvsd_bitwise_sum (unsigned int input);
00138 
00139   // Members variables related to the CVSD encoder use to update interal reference value
00140   short d_min_step;
00141   short d_max_step;
00142   double d_step_decay;
00143   double d_accum_decay;
00144 
00145   int d_K; //!< \brief Size of shift register; the number of output bits remembered in shift register
00146   int d_J; //!< \brief Number of bits in the shift register that are equal; size of run of 1s, 0s
00147   
00148   short d_pos_accum_max;
00149   short d_neg_accum_max;
00150 
00151   int d_accum;            //!< \brief Current value of internal reference
00152   int d_loop_counter;     //!< \brief Current value of the loop counter
00153   unsigned int d_runner;  //!< \brief Current value of the shift register
00154   short d_stepsize;       //!< \brief Current value of the step sizer
00155 
00156  public:
00157   ~cvsd_encode_sb ();   // public destructor
00158 
00159   short min_step() { return d_min_step; }
00160   short max_step() { return d_max_step; }
00161   double step_decay() { return d_step_decay; }
00162   double accum_decay() { return d_accum_decay; }
00163   int K() { return d_K; }
00164   int J() { return d_J; }
00165   short pos_accum_max() { return d_pos_accum_max; }
00166   short neg_accum_max() { return d_neg_accum_max; }
00167 
00168   int work (int noutput_items,
00169             gr_vector_const_void_star &input_items,
00170             gr_vector_void_star &output_items);
00171 };
00172 
00173 #endif /* INCLUDED_CVSD_ENCODE_SB_H */