DPCM编码MATLAB实现Word下载.docx
- 文档编号:18020945
- 上传时间:2022-12-12
- 格式:DOCX
- 页数:14
- 大小:18.15KB
DPCM编码MATLAB实现Word下载.docx
《DPCM编码MATLAB实现Word下载.docx》由会员分享,可在线阅读,更多相关《DPCM编码MATLAB实现Word下载.docx(14页珍藏版)》请在冰豆网上搜索。
m,1);
J1(1,1:
n)=I(1,1:
n);
m,n)=I(1:
m,n);
J1(m,1:
n)=I(m,1:
J2=ones(m,n);
J2(1:
J2(1,1:
J2(m,1:
J3=ones(m,n);
J3(1:
J3(1,1:
J3(m,1:
J4=ones(m,n);
J4(1:
J4(1,1:
J4(m,1:
%一阶DPCM编码
fork=2:
m-1
forl=2:
n-1
J1(k,l)=I(k,l)-I(k,l-1);
end
J1=round(J1);
cont1=fwrite(fid1,J1,'
int8'
cc1=fclose(fid1);
%二阶DPCM编码
J2(k,l)=I(k,l)-(I(k,l-1)/2+I(k-1,l)/2);
J2=round(J2);
cont2=fwrite(fid2,J2,'
cc2=fclose(fid2);
%三阶DPCM编码
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));
J3=round(J3);
cont3=fwrite(fid3,J3,'
cc3=fclose(fid3);
%四阶DPCM编码
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);
J4=round(J4);
cont4=fwrite(fid4,J4,'
cc4=fclose(fid4);
%=====================================================================
%以上是DPCM编码的编码过程,为了使程序具有连贯性,将编码和解码放在同一个M文件目录下
%以下是DPCM解码
r'
I11=fread(fid1,cont1,'
I12=fread(fid2,cont2,'
I13=fread(fid3,cont3,'
I14=fread(fid4,cont4,'
tt=1;
forl=1:
n
fork=1:
m
I1(k,l)=I11(tt);
tt=tt+1;
I2(k,l)=I12(tt);
I3(k,l)=I13(tt);
I4(k,l)=I14(tt);
I1=double(I1);
I2=double(I2);
I3=double(I3);
I4=double(I4);
m,1)=I1(1:
n)=I1(1,1:
m,n)=I1(1:
n)=I1(m,1:
m,1)=I2(1:
n)=I2(1,1:
m,n)=I2(1:
n)=I2(m,1:
m,1)=I3(1:
n)=I3(1,1:
m,n)=I3(1:
n)=I3(m,1:
m,1)=I4(1:
n)=I4(1,1:
m,n)=I4(1:
n)=I4(m,1:
%一阶解码
J1(k,l)=I1(k,l)+J1(k,l-1);
J1=uint8(J1);
%二阶解码
J2(k,l)=I2(k,l)+(J2(k,l-1)/2+J2(k-1,l)/2);
J2=uint8(J2);
%三阶解码
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));
J3=uint8(J3);
%四阶解码
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);
J4=uint8(J4);
%分区画图
figure
(1)
subplot(3,2,1);
imshow(I03);
%隐藏坐标轴和边框,以免坐标轴与标题重叠
axisoff
boxoff
title('
原始图像'
fontsize'
11,'
fontname'
隶体'
subplot(3,2,2);
imshow(I02);
灰度图像'
subplot(3,2,3);
imshow(J1);
一阶预测'
subplot(3,2,4);
imshow(J2);
二阶预测'
subplot(3,2,5);
imshow(J3);
三阶预测'
subplot(3,2,6);
imshow(J4);
四阶预测'
%学会DPCM编码必须彻底了解DPCM编码原理
%DPCM编码是数字图形处理的一项应用
%Removingallvariables,functions,andMEX-filesfrommemory,leavingthe%workspaceempty.
clearall
%Deletingallfigureswhosehandlesarenothidden.
closeall
%Deletingallfiguresincludingthosewithhiddenhandles.
closeallhidden
%ClearingallinputandoutputfromtheCommandWindowdisplaygivingusacleanscreen.clc
%Openingthefile'
TEOTH.mp3'
inthereadaccessmode.
fid=fopen('
%Generatingtheinputsignal'
m(t)'
byreadingthebinarydatain16bit
%integerformatfromthespecifiedfileandwritingitintoamatrix
%'
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'
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);
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;
%quant_in=diff_sig/step_size;
%Indicatingthesignoftheinputsignal'
andcalculatingthe%quantizedsignal'
quant_out'
signS=sign(m);
quant_out=quant_in;
fori=1:
S(i)=abs(quant_in(i))+0.5;
quant_out(i)=signS(i)*round(S(i))*step_size;
%Decodingthesignalusingthequantizeddifferencesignal.
s_out=quant_out;
s_out
(1)=quant_out
(1);
s_out(k)=quant_out(k)+alpha*s_out(k-1);
%Calculatingthequantizationnoise'
Nq'
Nq=(((step_size)^2)/12)*((Mp/Dp)^2)
%Calculatingsignaltonoiseratio'
SNR'
SNR=1.5*(levels^2)
SNR_db=10*log10(SNR)
%Plottingtheinputsignal'
%figure;
subplot(4,1,1);
plot(time,m(time),time,s_out(time),'
InputSpeechSignal'
xlabel('
Time'
ylabel('
gridon;
%Plottingthequantizedsignal'
quant_in(t)'
subplot(4,1,2);
stem(time,quant_in(time),'
QuantizedSpeechSignal'
Levels'
%PlottingtheDPCMsignal'
s_out(t)'
subplot(4,1,3);
plot(time,s_out(time));
DecodedDPCMSpeechSignal'
Dq(t)'
%Plottingtheerrorsignal'
error(t)'
subplot(4,1,4);
plot(time,error(time));
ErrorSignal'
Error(t)'
%Solicitatipodese馻lamuestrear
opcion=input('
Escriba1siquierecodificarse馻ldeaudioo2siquierecodificarotra:
'
)
%abrese馻lseleccionadaporelusuario
ifopcion==1;
t_input=input('
escribaeltiempoensegundosquedeseegrabarelaudio:
m=wavrecord(t_input*30000,30000,'
%fid=fopen('
sonido.wav'
%m=fread(fid,'
ini_cuenta=10;
ifopcion==2;
t=input('
escribaelvectordetiempoparalase馻l:
m=input('
escribalase馻lquedeseecodificarf(t):
ini_cuenta=2;
%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:
2*Mp];
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
residuo=rem(k,red_inc_muestreo);
ifresiduo==0
samp_in(k)=m(k);
elseifresiduo~=0
samp_in(k)=samp_in(k-1);
%Cuantizacion
quant=quantiz(samp_in,particion);
pcm_cad=dec2bin(quant)
ind_pcm=1;
%GeneracodigobinariodePCM
forh=ini_cuenta:
residuo=rem(h,red_inc_muestreo);
PCM(ind_pcm)=str2num(pcm_cad(h,:
));
ind_pcm=ind_pcm+1;
subplot(2,2,1);
plot(m);
title('
se馻lanal骻ica'
xlabel('
tiempo'
ylabel('
amplitud'
subplot(2,2,2);
stairs(samp_in);
se馻lmuestreada'
subplot(2,2,3);
plot(quant);
se馻lcuantizada'
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:
p(1:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- DPCM 编码 MATLAB 实现