DPCM编码MATLAB实现

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

DPCM编码MATLAB实现%本文是数字图像处理的一个源程序%实现的功能是DPCM编码%DPCM编码,简称差值编码,是对模拟信号幅度抽样的差值进行量化编码的调制方式%本程序实现一阶/二阶/三阶/四阶DPCM数字信号预测%一阶/二阶/三阶/四阶预测的区别不仅在于信号的清晰度,而更重要在于%阶数越高,图像越光滑.clcclearcloseall;%从D盘导入图片,以学校风光图片为例实现DPCMI03=imread('d:\shuxuejianmo.bmp');%把RGB图像转化为灰度图像I02=rgb2gray(I03);I=double(I02);fid1=fopen('mydata1.dat','w');fid2=fopen('mydata2.dat','w');fid3=fopen('mydata3.dat','w');fid4=fopen('mydata4.dat','w');[m,n]=size(I);%对预测信号将边缘锁定,防止程序运行时抓不到数据J1=ones(m,n);J1(1:m,1)=I(1:m,1);J1(1,1:n)=I(1,1:n);J1(1:m,n)=I(1:m,n);J1(m,1:n)=I(m,1:n);J2=ones(m,n);J2(1:m,1)=I(1:m,1);J2(1,1:n)=I(1,1:n);J2(1:m,n)=I(1:m,n);J2(m,1:n)=I(m,1:n);J3=ones(m,n);J3(1:m,1)=I(1:m,1);J3(1,1:n)=I(1,1:n);J3(1:m,n)=I(1:m,n);J3(m,1:n)=I(m,1:n);J4=ones(m,n);J4(1:m,1)=I(1:m,1);J4(1,1:n)=I(1,1:n);J4(1:m,n)=I(1:m,n);J4(m,1:n)=I(m,1:n);%一阶DPCM编码fork=2:m-1forl=2:n-1J1(k,l)=I(k,l)-I(k,l-1);endendJ1=round(J1);cont1=fwrite(fid1,J1,'int8');cc1=fclose(fid1);%二阶DPCM编码fork=2:m-1forl=2:n-1J2(k,l)=I(k,l)-(I(k,l-1)/2+I(k-1,l)/2);endendJ2=round(J2);cont2=fwrite(fid2,J2,'int8');cc2=fclose(fid2);%三阶DPCM编码fork=2:m-1forl=2:n-1J3(k,l)=I(k,l)-(I(k,l-1)*(4/7)+I(k-1,l)*(2/7)+I(k-1,l-1)*(1/7));endendJ3=round(J3);cont3=fwrite(fid3,J3,'int8');cc3=fclose(fid3);%四阶DPCM编码fork=2:m-1forl=2:n-1J4(k,l)=I(k,l)-(I(k,l-1)/2+I(k-1,l)/4+I(k-1,l-1)/8+I(k-1,l+1)/8);endendJ4=round(J4);cont4=fwrite(fid4,J4,'int8');cc4=fclose(fid4);%=====================================================================%以上是DPCM编码的编码过程,为了使程序具有连贯性,将编码和解码放在同一个M文件目录下%=====================================================================%以下是DPCM解码fid1=fopen('mydata1.dat','r');fid2=fopen('mydata2.dat','r');fid3=fopen('mydata3.dat','r');fid4=fopen('mydata4.dat','r');I11=fread(fid1,cont1,'int8');I12=fread(fid2,cont2,'int8');I13=fread(fid3,cont3,'int8');I14=fread(fid4,cont4,'int8');tt=1;forl=1:nfork=1:mI1(k,l)=I11(tt);tt=tt+1;endendtt=1;forl=1:nfork=1:mI2(k,l)=I12(tt);tt=tt+1;endendtt=1;forl=1:nfork=1:mI3(k,l)=I13(tt);tt=tt+1;endendtt=1;forl=1:nfork=1:mI4(k,l)=I14(tt);tt=tt+1;endendI1=double(I1);I2=double(I2);I3=double(I3);I4=double(I4);J1=ones(m,n);J1(1:m,1)=I1(1:m,1);J1(1,1:n)=I1(1,1:n);J1(1:m,n)=I1(1:m,n);J1(m,1:n)=I1(m,1:n);J2=ones(m,n);J2(1:m,1)=I2(1:m,1);J2(1,1:n)=I2(1,1:n);J2(1:m,n)=I2(1:m,n);J2(m,1:n)=I2(m,1:n);J3=ones(m,n);J3(1:m,1)=I3(1:m,1);J3(1,1:n)=I3(1,1:n);J3(1:m,n)=I3(1:m,n);J3(m,1:n)=I3(m,1:n);J4=ones(m,n);J4(1:m,1)=I4(1:m,1);J4(1,1:n)=I4(1,1:n);J4(1:m,n)=I4(1:m,n);J4(m,1:n)=I4(m,1:n);%一阶解码fork=2:m-1forl=2:n-1J1(k,l)=I1(k,l)+J1(k,l-1);endendcc1=fclose(fid1);J1=uint8(J1);%二阶解码fork=2:m-1forl=2:n-1J2(k,l)=I2(k,l)+(J2(k,l-1)/2+J2(k-1,l)/2);endendcc2=fclose(fid2);J2=uint8(J2);%三阶解码fork=2:m-1forl=2:n-1J3(k,l)=I3(k,l)+(J3(k,l-1)*(4/7)+J3(k-1,l)*(2/7)+J3(k-1,l-1)*(1/7));endendcc3=fclose(fid3);J3=uint8(J3);%四阶解码fork=2:m-1forl=2:n-1J4(k,l)=I4(k,l)+(J4(k,l-1)/2+J4(k-1,l)/4+J4(k-1,l-1)/8+J4(k-1,l+1)/8);endendcc4=fclose(fid4);J4=uint8(J4);%分区画图figure(1)subplot(3,2,1);imshow(I03);%隐藏坐标轴和边框,以免坐标轴与标题重叠axisoffboxofftitle('原始图像','fontsize',11,'fontname','隶体');subplot(3,2,2);imshow(I02);axisoffboxofftitle('灰度图像','fontsize',11,'fontname','隶体');subplot(3,2,3);imshow(J1);axisoffboxofftitle('一阶预测','fontsize',11,'fontname','隶体');subplot(3,2,4);imshow(J2);axisoffboxofftitle('二阶预测','fontsize',11,'fontname','隶体');subplot(3,2,5);imshow(J3);axisoffboxofftitle('三阶预测','fontsize',11,'fontname','隶体');subplot(3,2,6);imshow(J4);axisoffboxofftitle('四阶预测','fontsize',11,'fontname','隶体');%学会DPCM编码必须彻底了解DPCM编码原理%DPCM编码是数字图形处理的一项应用%Removingallvariables,functions,andMEX-filesfrommemory,leavingthe%workspaceempty.clearall%Deletingallfigureswhosehandlesarenothidden.closeall%Deletingallfiguresincludingthosewithhiddenhandles.closeallhidden%ClearingallinputandoutputfromtheCommandWindowdisplaygivingusacleanscreen.clc%Openingthefile'TEOTH.mp3'inthereadaccessmode.fid=fopen('TEOTH.mp3','r');%Generatingtheinputsignal'm(t)'byreadingthebinarydatain16bit%integerformatfromthespecifiedfileandwritingitintoamatrix%'m(t)'andthenumberofelementssuccessfullyreadisreturnedintoan%outputargument'count'.[m,count]=fread(fid,'int16');%Redefiningthecountforefficiency.count=8500;%Settingthesamplingfrequency.%becausetheaudiosignalhasamaximumfrequencyof4Kandaccordingto%Nyquistcriteria,wegetthefollowingsamplingfrequency.Fs=8000;%Settingthesamplinginstant.Ts=1;%Settingthenumberofsamplestobeused.No_Samples=(2*Fs)+Ts;%Definethetimevectorforthecalculations.time=[1:Fs/64];%Calculatingmaximumvalueoftheinputsignal'm(t)'.Mp=max(m)%Settingnumberofbitsinasymbol.bits=5;%Numberoflevelsofuniformquantization.levels=2^bits;%Calculatingthebitrate.bit_rate=8000*bits;%SincetheDPCMisimplementedbythelinearpredictor(transversal%predictor)Hencesettingupthepredictioncoefficient'alpha'.alpha=0.45;%Transmittingthedifference.%Sincethereisnoestimatedvaluebeforethefirstsamplesowegetdiff_sig(1)=m(1);%Calculatingtherestofthevaluesofthedifferencesignalwiththehelp%ofcoefficient.fork=2:count,diff_sig(k)=m(k)-alpha*m(k-1);end%Calculatingmaximumvalueoftheinputsignal'diff_sig(t)',i.e,tobe%quantized.Dp=max(diff_sig)%Calculat

1 / 13
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功