DPCM编码MATLAB实现.docx

上传人:b****1 文档编号:14167819 上传时间:2023-06-21 格式:DOCX 页数:17 大小:18.09KB
下载 相关 举报
DPCM编码MATLAB实现.docx_第1页
第1页 / 共17页
DPCM编码MATLAB实现.docx_第2页
第2页 / 共17页
DPCM编码MATLAB实现.docx_第3页
第3页 / 共17页
DPCM编码MATLAB实现.docx_第4页
第4页 / 共17页
DPCM编码MATLAB实现.docx_第5页
第5页 / 共17页
DPCM编码MATLAB实现.docx_第6页
第6页 / 共17页
DPCM编码MATLAB实现.docx_第7页
第7页 / 共17页
DPCM编码MATLAB实现.docx_第8页
第8页 / 共17页
DPCM编码MATLAB实现.docx_第9页
第9页 / 共17页
DPCM编码MATLAB实现.docx_第10页
第10页 / 共17页
DPCM编码MATLAB实现.docx_第11页
第11页 / 共17页
DPCM编码MATLAB实现.docx_第12页
第12页 / 共17页
DPCM编码MATLAB实现.docx_第13页
第13页 / 共17页
DPCM编码MATLAB实现.docx_第14页
第14页 / 共17页
DPCM编码MATLAB实现.docx_第15页
第15页 / 共17页
DPCM编码MATLAB实现.docx_第16页
第16页 / 共17页
DPCM编码MATLAB实现.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

DPCM编码MATLAB实现.docx

《DPCM编码MATLAB实现.docx》由会员分享,可在线阅读,更多相关《DPCM编码MATLAB实现.docx(17页珍藏版)》请在冰点文库上搜索。

DPCM编码MATLAB实现.docx

DPCM编码MATLAB实现

DPCM编码MATLAB实现

%本文是数字图像处理的一个源程序

%实现的功能是DPCM编码

%DPCM编码,简称差值编码,是对模拟信号幅度抽样的差值进行量化编码的调制方式

%本程序实现一阶/二阶/三阶/四阶DPCM数字信号预测

%一阶/二阶/三阶/四阶预测的区别不仅在于信号的清晰度,而更重要在于

%阶数越高,图像越光滑.

clc

clear

closeall;

%从D盘导入图片,以学校风光图片为例实现DPCM

I03=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-1

forl=2:

n-1

J1(k,l)=I(k,l)-I(k,l-1);

end

end

J1=round(J1);

cont1=fwrite(fid1,J1,'int8');

cc1=fclose(fid1);

%二阶DPCM编码

fork=2:

m-1

forl=2:

n-1

J2(k,l)=I(k,l)-(I(k,l-1)/2+I(k-1,l)/2);

end

end

J2=round(J2);

cont2=fwrite(fid2,J2,'int8');

cc2=fclose(fid2);

%三阶DPCM编码

fork=2:

m-1

forl=2:

n-1

J3(k,l)=I(k,l)-(I(k,l-1)*(4/7)+I(k-1,l)*(2/7)+I(k-1,l-1)*(1/7));

end

end

J3=round(J3);

cont3=fwrite(fid3,J3,'int8');

cc3=fclose(fid3);

%四阶DPCM编码

fork=2:

m-1

forl=2:

n-1

J4(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);

end

end

J4=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:

n

fork=1:

m

I1(k,l)=I11(tt);

tt=tt+1;

end

end

tt=1;

forl=1:

n

fork=1:

m

I2(k,l)=I12(tt);

tt=tt+1;

end

end

tt=1;

forl=1:

n

fork=1:

m

I3(k,l)=I13(tt);

tt=tt+1;

end

end

tt=1;

forl=1:

n

fork=1:

m

I4(k,l)=I14(tt);

tt=tt+1;

end

end

I1=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-1

forl=2:

n-1

J1(k,l)=I1(k,l)+J1(k,l-1);

end

end

cc1=fclose(fid1);

J1=uint8(J1);

%二阶解码

fork=2:

m-1

forl=2:

n-1

J2(k,l)=I2(k,l)+(J2(k,l-1)/2+J2(k-1,l)/2);

end

end

cc2=fclose(fid2);

J2=uint8(J2);

%三阶解码

fork=2:

m-1

forl=2:

n-1

J3(k,l)=I3(k,l)+(J3(k,l-1)*(4/7)+J3(k-1,l)*(2/7)+J3(k-1,l-1)*(1/7));

end

end

cc3=fclose(fid3);

J3=uint8(J3);

%四阶解码

fork=2:

m-1

forl=2:

n-1

J4(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);

end

end

cc4=fclose(fid4);

J4=uint8(J4);

%分区画图

figure

(1)

subplot(3,2,1);

imshow(I03);

%隐藏坐标轴和边框,以免坐标轴与标题重叠

axisoff

boxoff

title('原始图像','fontsize',11,'fontname','隶体');

subplot(3,2,2);

imshow(I02);

axisoff

boxoff

title('灰度图像','fontsize',11,'fontname','隶体');

subplot(3,2,3);

imshow(J1);

axisoff

boxoff

title('一阶预测','fontsize',11,'fontname','隶体');

subplot(3,2,4);

imshow(J2);

axisoff

boxoff

title('二阶预测','fontsize',11,'fontname','隶体');

subplot(3,2,5);

imshow(J3);

axisoff

boxoff

title('三阶预测','fontsize',11,'fontname','隶体');

subplot(3,2,6);

imshow(J4);

axisoff

boxoff

title('四阶预测','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)

%Calculatingthestepsizeofthequantization.

step_size=(2*Mp)/levels

%Quantizingthedifferencesignal.

fork=1:

No_Samples,

samp_in(k)=m(k*Ts);

quant_in(k)=samp_in(k)/step_size;

error(k)=(samp_in(k)-quant_in(k))/No_Samples;

end

%quant_in=diff_sig/step_size;

%Indicatingthesignoftheinputsignal'm(t)'andcalculatingthe%quantizedsignal'quant_out'.

signS=sign(m);

quant_out=quant_in;

fori=1:

count,

S(i)=abs(quant_in(i))+0.5;

quant_out(i)=signS(i)*round(S(i))*step_size;

end

%Decodingthesignalusingthequantizeddifferencesignal.

s_out=quant_out;

s_out

(1)=quant_out

(1);

fork=2:

count,

s_out(k)=quant_out(k)+alpha*s_out(k-1);

end

%Calculatingthequantizationnoise'Nq'.

Nq=(((step_size)^2)/12)*((Mp/Dp)^2)

%Calculatingsignaltonoiseratio'SNR'.

SNR=1.5*(levels^2)

SNR_db=10*log10(SNR)

%Plottingtheinputsignal'm(t)'.

%figure;

subplot(4,1,1);

plot(time,m(time),time,s_out(time),'r');

title('InputSpeechSignal');

xlabel('Time');

ylabel('m(t)');

gridon;

%Plottingthequantizedsignal'quant_in(t)'.

%figure;

subplot(4,1,2);

stem(time,quant_in(time),'r');

title('QuantizedSpeechSignal');

xlabel('Time');

ylabel('Levels');

gridon;

%PlottingtheDPCMsignal's_out(t)'.

%figure;

subplot(4,1,3);

plot(time,s_out(time));

title('DecodedDPCMSpeechSignal');

xlabel('Time');

ylabel('Dq(t)');

gridon;

%Plottingtheerrorsignal'error(t)'.

subplot(4,1,4);

plot(time,error(time));

title('ErrorSignal');

xlabel('Time');

ylabel('Error(t)');

gridon;

%Removingallvariables,functions,andMEX-filesfrommemory,leavingthe%workspaceempty.

clearall

clearall

closeall

%Solicitatipodese馻lamuestrear

opcion=input('Escriba1siquierecodificarse馻ldeaudioo2siquierecodificarotra:

')

%abrese馻lseleccionadaporelusuario

ifopcion==1;

t_input=input('escribaeltiempoensegundosquedeseegrabarelaudio:

');

m=wavrecord(t_input*30000,30000,'int16');

%fid=fopen('sonido.wav','r');

%m=fread(fid,'int16');

ini_cuenta=10;

end

ifopcion==2;

t=input('escribaelvectordetiempoparalase馻l:

');

m=input('escribalase馻lquedeseecodificarf(t):

');

ini_cuenta=2;

end

%Solicitafrecuenciademuestreoynivelesdecuantizacion

Fs=input('escribalafrecuenciademuestreo:

');

levels=input('escribalosnivelesdecuantizacion:

');

Mp=max(m)%Calculaelnivelm醲imodelase馻l

step_size=(Mp*2)/levels%Incrementoentrecadaniveldecuant

particion=[-Mp:

step_size:

Mp];%vectordeparticion(cuant)

%particion=[0:

step_size:

2*Mp];

Ts=1;

longitud_m=length(m);

inc_muestreo=longitud_m/Fs;

red_inc_muestreo=floor(inc_muestreo);

No_samples=(red_inc_muestreo*Fs)+1;%Numerodemuestras,

%Muestreo

fork=ini_cuenta:

No_samples

ifk==ini_cuenta

samp_in(k-1)=0;

ind_pcm=1

end

residuo=rem(k,red_inc_muestreo);

ifresiduo==0

samp_in(k)=m(k);

elseifresiduo~=0

samp_in(k)=samp_in(k-1);

end

end

%Cuantizacion

quant=quantiz(samp_in,particion);

pcm_cad=dec2bin(quant)

ind_pcm=1;

%GeneracodigobinariodePCM

forh=ini_cuenta:

No_samples

residuo=rem(h,red_inc_muestreo);

ifresiduo==0

PCM(ind_pcm)=str2num(pcm_cad(h,:

));

ind_pcm=ind_pcm+1;

end

end

subplot(2,2,1);plot(m);title('se馻lanal骻ica');xlabel('tiempo');ylabel('amplitud');subplot(2,2,2);stairs(samp_in);title('se馻lmuestreada');xlabel('tiempo');ylabel('amplitud');subplot(2,2,3);plot(quant);title('se馻lcuantizada');xlabel('tiempo');ylabel('nivelesdecuantizacion');

DPCM预测编码的MATLAB原代码:

收藏

DPCM预测编码原代码:

i1=imread('3.jpg');

i1=rgb2gray(i1);

i1=imcrop(i1,[2020160160]);

i=double(i1);

[m,n]=size(i);

p=zeros(m,n);

y=zeros(m,n);

y(1:

m,1)=i(1:

m,1);

p(1:

m,1)=i(1:

m,1);

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2