📄 student.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;
/*****************PCM Encode by S.C.H.*********************************************/
unsigned char s,k,seg,high=0,low=0;
int sv;
if(InputValue>0)s=128;
else s=0;
InputValue=abs(InputValue);
if(InputValue>=1024)high=112,sv=1024,k=64; //判断高三位
else if(InputValue>=512)high=96,sv=512,k=32;
else if(InputValue>=256)high=80,sv=256,k=16;
else if(InputValue>=128)high=64,sv=128,k=8;
else if(InputValue>=64)high=48,sv=64,k=4;
else if(InputValue>=32)high=32,sv=32,k=2;
else if(InputValue>=16)high=16,sv=16,k=1;
else if(InputValue>=0)high=0,sv=0,k=1;
if(InputValue>=sv+8*k)low=8;
else if(InputValue>=sv+4*k)low=4;
else if(InputValue>=sv+2*k)low=2;
else if(InputValue>=sv+k)low=1;
seg=low+1;
switch(low) { //判断低四位
case 8:if(InputValue>=sv+seg*k){low++;seg++;}else break;
if(InputValue>=sv+seg*k){low++;seg++;}else break;
if(InputValue>=sv+seg*k){low++;seg++;}else break;
if(InputValue>=sv+seg*k){low++;seg++;}else break;
case 4:if(InputValue>=sv+seg*k){low++;seg++;}else break;
if(InputValue>=sv+seg*k){low++;seg++;}else break;
case 2:if(InputValue>=sv+seg*k){low++;}else break;
case 1:break;
default:break;
}
OutputValue=s+high+low;
/*****************PCM Encode by S.C.H.*********************************************/
return OutputValue;
}
/*==================================================================================================
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;
/*****************PCM Decode by S.C.H.*********************************************/
char k,s=-1;
int sv;
if(CodeValue>=128){s=1;CodeValue-=128;}
if(CodeValue>=112)sv=1024,k=64; //确定段起始电平及量化间隔
else if(CodeValue>=96)sv=512,k=32;
else if(CodeValue>=80)sv=256,k=16;
else if(CodeValue>=64)sv=128,k=8;
else if(CodeValue>=48)sv=64,k=4;
else if(CodeValue>=32)sv=32,k=2;
else if(CodeValue>=16)sv=16,k=1;
else if(CodeValue>=0)sv=0,k=1;
CodeValue=CodeValue&0x0f;
DecodeValue=s*(sv+CodeValue*k);
/*****************PCM Decode by S.C.H.*********************************************/
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)
{
double Sample[30];
int i;
for(i = 0; i < SampleTimes; i ++){ /*64khz is the sample frequency*/
Sample[i] = Amplitude * sin(2 * PI * Frequency * (double)i /(double)(64*1024));
}
/*****************CVSD Encode by S.C.H.*********************************************/
int y[30];
double old1=0,old2=0,old3=0,dec[30];
double g[30],k=0.96875,k0=0,dk=10,a=0.9990234375;
g[0]=0,dec[0]=0;
for(i=0;i<SampleTimes;i++)
{
if(i)
{
if(Sample[i]-g[i]>=0)y[i]=1;
else y[i]=0;
if((y[i]==old1) & (y[i]==old2) & (y[i]==old3))
{
if(y[i])
{k0+=dk;}
}
else if((a*k0)<=10)k0=10;else k0*=a;
if(y[i])dec[i]=g[i]+k0;
else dec[i]=g[i]-k0;
g[i+1]=dec[i]*k;
old3=old2;
old2=old1;
old1=y[i];
}
else
{
if(Sample[i]-g[i]>=0){y[i]=1;dec[i]=g[i]+k0;}else {y[i]=0;dec[i]=g[i]-k0;}
g[i+1]=dec[i]*k;
}
Student_CVSD.Encode[i]=y[i];
}
/*****************CVSD Encode by S.C.H.*********************************************/
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)
{
/*****************CVSD Decode by S.C.H.*********************************************/
int i;
int old1=0,old2=0,old3=0;
double g[30],decode[30],k=0.96875,k0=0,dk=10,a=0.9990234375;
g[0]=0;decode[0]=0;
for(i=0;i<30;i++)
{
if(i)
{
if((Student_CVSD.Encode[i]==old1) & (Student_CVSD.Encode[i]==old2) & (Student_CVSD.Encode[i]==old3))
{
if(Student_CVSD.Encode[i])
{k0+=dk;}
}
else if((a*k0)<=10)k0=10;else k0*=a;
if(Student_CVSD.Encode[i])g[i]=decode[i]+k0;
else g[i]=decode[i]-k0;
old3=old2;
old2=old1;
old1=Student_CVSD.Encode[i];
decode[i+1]=g[i]*k;
}
else {if(Student_CVSD.Encode[i])g[i+1]=decode[i]+k0;else g[i+1]=decode[i]-k0;decode[i+1]=g[i]*k;}
Student_CVSD.Decode[i]=decode[i];
}
/*****************CVSD Decode by S.C.H.*********************************************/
return &Student_CVSD;
}
/*===========================================================================================*/
//#ifdef __cplusplus
//}
//#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -