test06-02 PCM模拟调制
2013-11-28 23:18
155 查看
clear all;
close all;
dt=0.01;
t=0:dt:10;
xt=sin(4*pi*t);
fs=10;
sdt=1/fs;
t1=0:sdt:10;
st=sin(4*pi*t1);
[out]=pcm_encode(st);
[out1]=pcm_decode(out,1);
figure(1);
subplot(411);
plot(t,xt);
title('原始信号');
subplot(412);
plot(t1,st);
title('抽样信号');
subplot(413);
plot(out);
title('编码信号');
subplot(414);
plot(out1);
title('译码恢复信号');
//////////////////////////////
function [out]=pcm_decode(in,v)
n=length(in);
in=reshape(in',8,n/8)';
slot(1)=0;
slot(2)=16;
slot(3)=32;
slot(4)=64;
slot(5)=128;
slot(6)=256;
slot(7)=512;
slot(8)=1024;
step(1)=1;
step(2)=1;
step(3)=2;
step(4)=4;
step(5)=8;
step(6)=16;
step(7)=32;
step(8)=64;
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)/2048*v;
end
///////////////////////
function[out]=pcm_encode(st)
x=st*2048;
n=length(x);
for i=1:n;
if x(i)>0
out(i,1)=1;
else
out(i,1)=0;
end
if 0<=abs(x(i))&abs(x(i))<16
out(i,2)=0;out(i,3)=0;out(i,4)=0;
step=1;
%起始电平
st=0;
elseif 16<=abs(x(i))&abs(x(i))<32
out(i,2)=0;out(i,3)=0;out(i,4)=1;step=1;st=16;
elseif 32<=abs(x(i))&abs(x(i))<64
out(i,2)=0;out(i,3)=1;out(i,4)=0;step=2;st=32;
elseif 64<=abs(x(i))&abs(x(i))<128
out(i,2)=0;out(i,3)=1;out(i,4)=1;step=4;st=64;
elseif 128<=abs(x(i))&abs(x(i))<256
out(i,2)=1;out(i,3)=0;out(i,4)=0;step=8;st=128;
elseif 256<=abs(x(i))&abs(x(i))<512
out(i,2)=1;out(i,3)=0;out(i,4)=1;step=16;st=256;
elseif 512<=abs(x(i))&abs(x(i))<1024
out(i,2)=1;out(i,3)=1;out(i,4)=0;step=32;st=512;
else
out(i,2)=1;out(i,3)=1;out(i,4)=1;step=64;st=1024;
end
if (abs(x(i))>=2048)
%处理过载现象
out(i,2:8)=[1 1 1 1 1 1 1];
else
%计算段内码
tmp=floor((abs(x(i))-st)/step);
%十进制转二进制数,%如果不减48,最后4位是ASCII数
% 49 48 48 48
t=dec2bin(tmp,4)-48;
out(i,5:8)=t(1:4);
end
end
out=reshape(out',1,8*n);
close all;
dt=0.01;
t=0:dt:10;
xt=sin(4*pi*t);
fs=10;
sdt=1/fs;
t1=0:sdt:10;
st=sin(4*pi*t1);
[out]=pcm_encode(st);
[out1]=pcm_decode(out,1);
figure(1);
subplot(411);
plot(t,xt);
title('原始信号');
subplot(412);
plot(t1,st);
title('抽样信号');
subplot(413);
plot(out);
title('编码信号');
subplot(414);
plot(out1);
title('译码恢复信号');
//////////////////////////////
function [out]=pcm_decode(in,v)
n=length(in);
in=reshape(in',8,n/8)';
slot(1)=0;
slot(2)=16;
slot(3)=32;
slot(4)=64;
slot(5)=128;
slot(6)=256;
slot(7)=512;
slot(8)=1024;
step(1)=1;
step(2)=1;
step(3)=2;
step(4)=4;
step(5)=8;
step(6)=16;
step(7)=32;
step(8)=64;
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)/2048*v;
end
///////////////////////
function[out]=pcm_encode(st)
x=st*2048;
n=length(x);
for i=1:n;
if x(i)>0
out(i,1)=1;
else
out(i,1)=0;
end
if 0<=abs(x(i))&abs(x(i))<16
out(i,2)=0;out(i,3)=0;out(i,4)=0;
step=1;
%起始电平
st=0;
elseif 16<=abs(x(i))&abs(x(i))<32
out(i,2)=0;out(i,3)=0;out(i,4)=1;step=1;st=16;
elseif 32<=abs(x(i))&abs(x(i))<64
out(i,2)=0;out(i,3)=1;out(i,4)=0;step=2;st=32;
elseif 64<=abs(x(i))&abs(x(i))<128
out(i,2)=0;out(i,3)=1;out(i,4)=1;step=4;st=64;
elseif 128<=abs(x(i))&abs(x(i))<256
out(i,2)=1;out(i,3)=0;out(i,4)=0;step=8;st=128;
elseif 256<=abs(x(i))&abs(x(i))<512
out(i,2)=1;out(i,3)=0;out(i,4)=1;step=16;st=256;
elseif 512<=abs(x(i))&abs(x(i))<1024
out(i,2)=1;out(i,3)=1;out(i,4)=0;step=32;st=512;
else
out(i,2)=1;out(i,3)=1;out(i,4)=1;step=64;st=1024;
end
if (abs(x(i))>=2048)
%处理过载现象
out(i,2:8)=[1 1 1 1 1 1 1];
else
%计算段内码
tmp=floor((abs(x(i))-st)/step);
%十进制转二进制数,%如果不减48,最后4位是ASCII数
% 49 48 48 48
t=dec2bin(tmp,4)-48;
out(i,5:8)=t(1:4);
end
end
out=reshape(out',1,8*n);
相关文章推荐
- DM365 dvsdk_4_02_00_06开发环境搭建
- 06/04/02
- 02-06 控制AutoCAD环境(六) 设置和返回系统变量
- Test 02
- 麻省理工学院公开课:信号与系统:模拟与数字信号处理> 连续时间调制
- 03-02自底向上风格_模拟游戏
- (原创)c#学习笔记08--面向对象编程简介02--OOP技术06--事件
- FNOI_TEST_1 a(模拟)
- day5 test06
- Bootstrap 3之美06-Page Header、Breadcrumbs、Dropdowns、Button Dropdowns、用Button和Dropdowns模拟Select、Input Groups、Thumbnails、Panels、Wells
- Test 06
- 模拟share_ptr的实现-02-基于模板的实现
- [模拟电路] 1、模拟调制、解调电路原理
- android基础知识12:android自动化测试06—Instrumentation 02 单元测试
- 使KVM autotest支持qemu 模拟
- 02-06加密与解密_设计程序
- 【Test 2016-10-1】模拟+差分序列+spfa最短路+贪心
- 常用接口介绍06——视频篇02——SDI
- JAVA高级视频02_IO输入与输出日记3(02-06到10)
- php学习06-02