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

📄 student.cpp

📁 基于PCM和CVSD技术的语音编码仿真:在VC6.0环境下
💻 CPP
字号:
//#ifdef __cplusplus
//extern "C" {
//#endif
/*==================================================================================================
Module Name: Student.cpp
General Description: Student write these functions
====================================================================================================
Semit 
(c) Copyright SEMIT 2002, All Rights Reserved
Revision History:
Modification Tracking
------------------------- ------------ ---------- -------------------------------------------
Author         Date       Number      Description of Changes
------------------------- ------------ ----------------------------------------
Semit        08/5/2002    Unknown      A-Law PCM  and CVSD encode and decode functions
====================================================================================================
INCLUDE FILES
==================================================================================================*/
#include <stdafx.h>
#include <math.h>
#include "Student.h"

/*==================================================================================================
LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
==================================================================================================*/

/*==================================================================================================
LOCAL VARIABLES
==================================================================================================*/
static STUDENT_CVSD Student_CVSD;
/*==================================================================================================
 *                     GLOBAL FUNCTIONS                                                             
/*==================================================================================================
FUNCTION: PCM_StudentAlawEncode
DESCRIPTION: 
    This function is A-Law PCM encode function
ARGUMENTS PASSED:
    InputValue:  The inputed sample value , the range is -2047 ~ +2047
RETURN VALUE:
    The 8-bits encoded value
PRE-CONDITIONS:
    None
POST-CONDITIONS:
    None
IMPORTANT NOTES:  
==================================================================================================*/
extern unsigned char PCM_StudentAlawEncode(int InputValue) 
{
	unsigned char OutputValue = 0x0;
	int temp=InputValue,i=0;
    int a[2]={0},k,j;
	if(temp>32&&temp<=64)             //用13折线法将采样值编码
	{
		temp=32+(temp-32)/2;
	}
	if(temp>64&&temp<=128)
	{
		temp=48+(temp-64)/4;
	}
	if(temp>128&&temp<=256)
	{
		temp=64+(temp-128)/8;
	}
	if(temp>256&&temp<=512)
	{
		temp=80+(temp-256)/16;
	}
    if(temp>512&&temp<=1024)
	{
		temp=96+(temp-512)/32;
	}
	if(temp>1024&&temp<2048)
	{
		temp=112+(temp-1024)/64;
	}
	while(temp>=16)                     //将编码后的值转换成16进制
	{                                   //存储在数组a[]中
		a[i]=temp%16;
		temp=temp/16;
		i++;
	}
    a[i]=temp;
	for(j=0;j<2;j++)                //将16进制数用相应字母表示
	{
        if(a[j]==10)
		{
			a[j]=0xa;
			continue;
		}
		if(a[j]==11)
		{
			a[j]=0xb;
			continue;
		}
		if(a[j]==12)
		{
			a[j]=0xc;
			continue;
		}
        if(a[j]==13)
		{
			a[j]=0xd;
			continue;
		}
		if(a[j]==14)
		{
			a[j]=0xe;
			continue;
		}
		if(a[j]==15)
		{
			a[j]=0xf;
			continue;
		}
	}
    k=a[1]*0x10+a[0]+0x80;           //整合16进制数的个位和十位,将值赋给k
	OutputValue = k;
	/*Add your code here*/

   return OutputValue;               //将16进制数以2进制的形式返回
}
/*==================================================================================================
FUNCTION: PCM_StudentAlawDecode
DESCRIPTION: 
    This function is A-Law PCM dencode function
ARGUMENTS PASSED:
    CodeValue:  The 8-bits encoded value
RETURN VALUE:
    The decoded sample value 
PRE-CONDITIONS:
    None
POST-CONDITIONS:
    None
IMPORTANT NOTES:  
==================================================================================================*/
extern int PCM_StudentAlawDecode(unsigned char CodeValue)
{
	int DecodeValue = 0;
    int kt=CodeValue-0x80;
    if(kt>=112)                     //用反变换的方式将编码值还原
	{
		kt=1024+(kt-112)*64;
	}
    if(kt>=96&&kt<112)
	{
		kt=512+(kt-96)*32;
	}
    if(kt>=80&&kt<96)
	{
		kt=256+(kt-80)*16;
	}
	if(kt>=64&&kt<80)
	{
		kt=128+(kt-64)*8;
	}
    if(kt>=48&&kt<64)
	{
		kt=64+(kt-48)*4;
	}
	if(kt>=32&&kt<48)
	{
		kt=32+(kt-32)*2;
	}
	DecodeValue=kt;
	/*Add your code here*/

	return DecodeValue;         //返回解码后的值,即源码
}
/*==================================================================================================
FUNCTION: CVSD_StudentEncode
DESCRIPTION: 
    This function is CVSD encode function
ARGUMENTS PASSED:
    Amplitude:   The sin wave amplitude , the range is 0 - 32767
	Frequency:   The sin wave frequency , the range is 4*1024 - 16* 1024
	SampleTimes: The times of sample , the range is 10 - 30

The sample value is computed as follows:
    Sample[i]  = Amplitude * sin(2 * PI * Frequency * (double)i /(double)(64*1024));

RETURN VALUE:
    The pointer to struct Student_CVSD, the member 'Encode' of the struct is the cvsd code.
PRE-CONDITIONS:
    None
POST-CONDITIONS:
    None
IMPORTANT NOTES:  
==================================================================================================*/

extern STUDENT_CVSD* CVSD_StudentEncode(int Amplitude, int SampleTimes, int Frequency)
{

	int i;
    double Sample[31];
	for(i = 0; i <= SampleTimes; i ++){        /*64khz is the sample frequency*/
        Sample[i]  = Amplitude * sin(2 * PI * Frequency * (double)i /(double)(64*1024));
 	}
    
	double h=0.96875,bt=0.9990234375;
	int dmin=10,dmax=1280,b[30]={0};
    double* x;
	double xe[30]={0},yReal=0,y[30]={0},d[30]={0};
	long double ymin=-1,ymax=1;
	for(i=0;i<15;i++)                   //求y的上限和下限
	{
        ymin=ymin*2;
		ymax=ymax*2;
	}
    ymin=ymin+1;
	ymax=ymax-1;
    x=Sample;                              //将正弦波采样值赋给

	for(i=0;i<SampleTimes;i++)
	{

		b[i]=(x[i]-xe[i])>=0 ? 1:0;        //编码值b[i]

    	if(i>2&&b[i]==b[i-1]&&b[i]==b[i-2]&&b[i]==b[i-3])        //确定量阶d[i]
		{
            d[i]=min(d[i-1]+dmin,dmax);
		}
		else
		{
			d[i]=max(bt*d[i-1],dmin);
		}
        

        if(b[i])                     //确定累加器参数的估计值y[i]
		{
			y[i]=xe[i]+d[i];
		}
		else
		{
			y[i]=xe[i]-d[i];
		}

		if(y[i]>=0)                  //确定累加器的值yReal       
		{
			yReal=min(y[i],ymax);
		}
		else
		{
			yReal=max(y[i],ymin);
		}

		if(i>0)
		{
			xe[i+1]=h*yReal;                //确定当前估计值xe[i]
		}

		Student_CVSD.Encode[i]=b[i];
	}
    
 	/*Add your code here*/

   return &Student_CVSD;
}
/*==================================================================================================
FUNCTION: CVSD_StudentDecode
DESCRIPTION: 
    This function is CVSD decode function
ARGUMENTS PASSED:
    SampleTimes:  The times of sample
	Use the member 'Encode'of struct Student_CVSD
RETURN VALUE:
    The pointer to struct Student_CVSD, the member 'Decode' of the struct is the decoded value.
PRE-CONDITIONS:
    None
POST-CONDITIONS:
    None
IMPORTANT NOTES:  
==================================================================================================*/
extern STUDENT_CVSD* CVSD_StudentDecode(int SampleTimes)
{
	int i;
	int* b;
    double h=0.96875,bt=0.9990234375;
    int dmin=10,dmax=1280;
    double xe[32]={0},yReal=0,y[30]={0},d[30]={0};
	long double ymin=-1,ymax=1;
	for(i=0;i<15;i++)                        //过程与编码过程大致相同
	{                                        //除编码值b[]为已知
        ymin=ymin*2;
		ymax=ymax*2;
	}
    ymin=ymin+1;
	ymax=ymax-1;
    b=Student_CVSD.Encode;

    for(i=0;i<SampleTimes;i++)
	{

    	if(i>3&&b[i]==b[i-1]&&b[i]==b[i-2]&&b[i]==b[i-3])        //确定量阶d[i]
		{
            d[i]=min(d[i-1]+dmin,dmax);
		}
		else
		{
			d[i]=max(bt*d[i-1],dmin);
		}
        

        if(b[i])                     //确定累加器参数的估计值y[i]
		{
			y[i]=xe[i]+d[i];
		}
		else
		{
			y[i]=xe[i]-d[i];
		}

		if(y[i]>=0)                  //确定累加器的值yReal       
		{
			yReal=min(y[i],ymax);
		}
		else
		{
			yReal=max(y[i],ymin);
		}

		if(i>0)
		{
			xe[i+1]=h*yReal;                //确定当前估计值xe[i]
		}
		
        Student_CVSD.Decode[i]=xe[i];
	}

    
	/*Add your code here*/

    return &Student_CVSD;
}
/*===========================================================================================*/
//#ifdef __cplusplus
//}
//#endif

⌨️ 快捷键说明

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