matlab仿真程序编写,基于MATLAB的PCM脉冲编码调制仿真实现的程序编写

:L 基于MATLAB的PCM脉冲编码调制仿真实现

一道MATLAB题运行不出来.帮看看为什么.(有的人说好象开头没有定义变量,请帮忙给改好了,万分感谢的)

设输入信号为 x(t)=Acsin2Лt,对x(t)信号进行抽样、量化和A律PCM编码,经过传输后,接收端进行PCM译码。

画出经过PCM编码、译码后的波形与未编码波形;

设信道没有误码,画出不同Ac情况下,PCM译码后的量化信噪比。

解:仿真的系统框图如图所示。

图为A律PCM量化器的动态范围仿真框图

%show the pcm encode and decode

clear all;

close all;

t = 0:0.01:10;

vm1 = -70:1:0; %输入的正弦信号幅度不同

vm = 10.^(vm1/20);

figure(1)

for k = 1:length(vm)

for m = 1:2

x = vm (k)*sin(2*pi*t+2*pi*rand(1));

v = 1;

xx = x/v;%normalize

sxx = floor(xx*4096);

y = pcm_encode(sxx);

yy = pcm_decode(y,v);

nq(m) = sum((x-yy).*(x-yy))/length(x);

sq(m) = mean(yy.^2);

snr(m) = (sq(m)/nq(m));

drawnow

subplot(211)

plot(t,x);

title('sample sequence');

subplot(212)

plot(t,yy)

title('pcm decode sequence');

end

snrq(k)=10*log10(mean(snr));

end

figure(2)

plot(vm1,snrq);

grid;

function [out]=pcm_encode(x)

%x encode to pcm code

n=length(x);

%-4096

for i = 1:n

if x(i)>0

out(i,1)=1;

else

out(i,1)=0;

end

if abs (x(i))> = 0 & abs(x(i))<32

out(i,2)=0;out(i,3)=0;out(i,4)=0;step=2;st=0;

elseif32<=abs(x(i))&abs(x(i))<64

out(i,2)=0;out(i,3)=0;out(i,4)=1;step=2;st=32;

elseif64<=abs(x(i))&abs(x(i))<128

out(i,2)=0;out(i,3)=1;out(i,4)=0;step=4;st=64;

elseif128<=abs(x(i))&abs(x(i))<256

out(i,2)=0;out(i,3)=1;out(i,4)=1;step=8;st=128

elseif256<=abs(x(i))&abs(x(i))<512

out(i,2)=1;out(i,3)=0;out(i,4)=0;step=16;st=256;

elseif512<=abs(x(i))&abs(x(i))<1024

out(i,2)=1;out(i,3)=0;out(i,4)=1;step=32;st=512;

elseif1024<=abs(x(i))&abs(x(i))<2048

out(i,2)=1;out(i,3)=1;out(i,4)=0;step=64;st=1024;

elseif2048<=abs(x(i))&abs(x(i))<4096

out(i,2)=1;out(i,3)=1;out(i,4)=1;step=128;st=2048;

else

out(i,2)=1;out(i,3)=1;out(i,4)=1;step=128;st=2048;

end

if(abs(x(i))&abs(x(i)))<4096

out(I,2:8)=[1 1 1 1 1 1 1];

else

tmp=floor((abs(x(i))-st)/step);

t=dec2bin(tmp,4)-48;%函数dec2bin输出的是ASCII字符串,48对应0

out(i,5:8)=t(1:4);

end

end

out=reshape(out',1,8*n);

function[out]= pcm_decode(in,v)

%decode the input pcm code

%in : input the pcm code 8 bits sample

%v:quantized level

n=length(in);

in=reshape(in',8,n/8)';

slot(1)=0;

slot(2)=32;

slot(3)=64;

slot(4)=128;

slot(5)=256;

slot(6)=512;

slot(7)=1024;

slot(8)=2048;

step(1)=2;

step(2)=2;

step(3)=4;

step(4)=8;

step(5)=16;

step(6)=32;

step(7)=64;

step(8)=128;

for i=1:n/8

ss=2*in(i,1)-1;

tmp = in(i,2)*4+in(i,3)*2+in(i,4)+1;

st = slot(tmp);

dt = (in(i,5)*8+in(i,6)*4+in(i,7)*2+in(i,8))*step(tmp)+0.5*step(tmp);

out(i)=ss*(st+dt)/4096*v;

end

[本帖最后由 mooni 于 2009-4-29 12:16 编辑]

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值