⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dpcmcodec.c

📁 chipcon公司的无线射频射频芯片cc1100应用的公共程序
💻 C
字号:
/*****************************************************************************
 *                                                                           *
 *        **********                                                         *
 *       ************                                                        *
 *      ***        ***                                                       *
 *      ***   +++   ***                                                      *
 *      ***   + +   ***                                                      *
 *      ***   +                CHIPCON CC1010 Wireless audio project         *
 *      ***   + +   ***                                                      *
 *      ***   +++   ***                      DpcmCodec                       *
 *      ***       ***                                                        *
 *       ***********                                                         *
 *        *********                                                          *
 *                                                                           *
 *****************************************************************************
 * This source file is part of a software project for Full Duplex,           *
 * single-chip, wireless intercom, written for the CC1010 chip               *
 * (RF-transceiver chip with integrated 8051 micro-controller).              *
 *****************************************************************************
 * Author:              OAE                                                  *
 *****************************************************************************
 * Revision history:                                                         *
 *                                                                           *
 * $Log: DpcmCodec.c,v $
 * Revision 1.1  2003/08/04 12:35:07  tos
 * Initial version in CVS.
 *
 *                                                                           *
 *                                                                           *
 ****************************************************************************/

#include <stdio.h>
#include <chipcon/reg1010.h>
#include <chipcon/cc1010eb.h>
#include <chipcon/hal.h>

// Quantizer step size lookup table
char StepSize[16] =
{
    0,-1,-2,-4,-8,-16,-32,-64,
    0, 1, 2, 4, 8, 16, 32, 64
};

// Variables
byte dpcmcodeout;// The encoded difference
short diff;// Difference between actual sample and predicted sample
byte encoderpredsample = 127;
byte decoderpredsample;


/**************************************************************************
* DpcmEncoder() - DPCM encoder routine                                    *
***************************************************************************
* Description:                                                            *
*   Performs DPCM speech compression (2:1).                               *
***************************************************************************
* Input arguments:                                                        *
*   byte sample: Unsigned speech sample (0-255).                          *
*   bit returncode: Function returns an encoded difference (4 bit) if set,*
*       or a predicted sample (8 bit) if not set.                         *
* Return value:                                                           *
*   byte: An encoded 4-bit DPCM code or a predicted sample.               *
**************************************************************************/
byte DpcmEncoder (byte sample, bit returncode)
{
    // Compute the differense between the actual- and predicted sample
    diff = sample - encoderpredsample;

    // Quantize the difference
    if (diff >= 0)
        dpcmcodeout = 8;// Set the first bit
    else // (diff < 0)
    {
        dpcmcodeout = 0;
        diff = -diff;// Use the absolute value of the difference

    }// End else

    if (diff >= 8)
    {
        dpcmcodeout |= 4;// Set the second bit
        if (diff >= 32)
        {
            dpcmcodeout |= 2;// Set the third bit
            if (diff >= 64)
                dpcmcodeout |= 1;// Set the fourth bit (LSB)

        }// End if
        else// (diff < 32)
        {
            if (diff >= 16)
                dpcmcodeout |= 1;// Set the fourth bit (LSB)

        }// End else

    }// End if
    else// (diff < 8)
    {
        if (diff >= 2)
        {
            dpcmcodeout |= 2;// Set the third bit
            if (diff >= 4)
                dpcmcodeout |= 1;// Set the fourth bit(LSB)

        }// End if
        else // (diff < 2)
        {
            if (diff >= 1)
            dpcmcodeout |= 1;// Set the fourth bit(LSB)

        }// End else

    }// End else

    /* Compute new predicted sample by adding the
    old predicted sample to the quantzed difference*/
    encoderpredsample += StepSize[dpcmcodeout];

    // Return the new DPCM code or the new predicted sample
    if (returncode)
        return (dpcmcodeout);
    else
        return (encoderpredsample);

}// End function


/**************************************************************************
* DpcmDecoder() - DPCM decoder routine                                    *
***************************************************************************
* Description:                                                            *
*   Performs DPCM speech decompression (1:2).                             *
***************************************************************************
* Input arguments:                                                        *
*   byte dpcmcodein: Contains one or two encoded differences.             *
*   bit msbits: The 4 msb of dpcmcodein are decoded if set, the 4 lsb are *
*       decoded if not set.                                               *
* Return value:                                                           *
*   byte: A decoded predicted sample (8 bit).                             *
**************************************************************************/
byte DpcmDecoder (byte dpcmcodein, bit msbits)
{
    // Decide if the four MSB or LSB should be decoded
    if (msbits)
        // Shift dpcmcodein four bits to the right
        dpcmcodein >>= 4;

    /* Compute new predicted sample by adding the
    old predicted sample to the quantzed difference*/
    decoderpredsample += StepSize[dpcmcodein & 0x0f];

    // Return the decoded sample
    return (decoderpredsample);

} // End function

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -