一、實驗原理
DPCM編碼,是差分預測編碼調製的縮寫,是典型的預測編碼系統。這種方式是用已經過去的抽樣值來預測當前的抽樣值,對它們的差值進行編碼。之所以不用原始樣本做預測是因爲在解碼端無法得到原始樣本數據,只能得到存在誤差的樣本。差值編碼可以提高編碼頻率,這種技術已應用於模擬信號的數字通信之中。
二、實驗步驟
三、關鍵代碼分析
1)dpcm函數
void dpcm(unsigned char *srcBuffer,int width,int height,unsigned char *rebuidBuffer,unsigned char *MSEBuffer)
{
int i,j;
char temp1;
unsigned char temp2;
for(i=0;i<height;i++)
{
temp1=srcBuffer[i*width]-128;//對第一列用128做預測,預測範圍爲-128到128
temp2=(temp1+128)/2;//加128提升到0-256再量化
MSEBuffer[i*width]=temp2*2;//反量化
rebuidBuffer[i*width]=MSEBuffer[i*width];//量化誤差加上預測值再減128得到預測樣本
for(j=1;j<width;j++)
{
temp1=srcBuffer[i*width+j]-rebuidBuffer[i*width+j-1];//用它右側的誤差樣本來做預測
temp2=(temp1+128)/2;
MSEBuffer[i*width+j]=temp2*2;
rebuidBuffer[i*width+j]=rebuidBuffer[i*width+j-1]+MSEBuffer[i*width+j]-128;
}
}
}
2)將bmp數據做dpcm編碼
Readrgb(input,file_h,info_h,rgbDataOut);//從bmp文件中提取rgb數據
//read y
unsigned char *srcBuffer=(unsigned char*)malloc(info_h.biWidth*info_h.biHeight);
unsigned char *rebuidBuffer=(unsigned char*)malloc(info_h.biWidth*info_h.biHeight);
unsigned char *MSEBuffer=(unsigned char*)malloc(info_h.biWidth*info_h.biHeight);
if(!(srcBuffer && rebuidBuffer && MSEBuffer))
{
printf("malloc ybuffer error!\n");
}
RGB2YUV (info_h.biWidth,info_h.biHeight,rgbDataOut,0,srcBuffer);
/*將rgb數據轉化爲yuv*/ dpcm(srcBuffer,info_h.biWidth,info_h.biHeight,rebuidBuffer,MSEBuffer);
fwrite(MSEBuffer,sizeof(unsigned char),info_h.biWidth*info_h.biHeight,output);//將誤差數據寫入輸出文件
四、實驗結果
1)dpcm輸出的重建圖像
2)原始圖像
3)預測誤差圖像和原始圖像huffman編碼比較
4)預測誤差圖像和原始圖像的符號概率分佈
五、結論
在dpcm編碼之後,信源符號的分佈更加集中,huffman編碼的效率提高了。