matlab实现A律13折线的编码和译码以及量化误差的计算

%judge函数
function [B,m1,n1]=judge(I,m,n)
if I>=m&&I<(m+n)/2
    B=0;
    m1=m;
    n1=(m+n)/2;
else
    B=1;
    m1=(m+n)/2;
    n1=n;
end
%主函数如下
clear all;
clc;
figure(7)
fc=2000;
t=0:1/2000:1;
m=2000*sin(100*pi*t);
subplot(311);
plot(t,m);%抽样序列
axis([0 0.02 -2000 2000]);
title('抽样序列');
xlabel('t(s)');
for i=1:length(m)
    if m(i)>0 %确定极性码
        B1=1;
    else
        B1=0;
    end
    m(i)=abs(m(i));
    C=[0 16 32 64 128 256 512 1024 2048];%A律13折现非均匀区间
    for j=1:length(C);
        if m(i)>=C(j)&&m(i)<=C(j+1)
            L=j-1;
            L1=dec2bin(L,3);
            B2=L1(1);
            B2=str2num(B2);
            B3=L1(2);
            B3=str2num(B3);
            B4=L1(3);
            B4=str2num(B4);
        end
    end
    a=C(L+1);%确定段内码
    b=C(L+2);
    [B5,a1,b1]=judge(m(i),a,b);
    [B6,a2,b2]=judge(m(i),a1,b1);
    [B7,a3,b3]=judge(m(i),a2,b2);
    [B8,a4,b4]=judge(m(i),a3,b3);
    result(i,1)=B1;
    result(i,2)=B2;
    result(i,3)=B3;
    result(i,4)=B4;
    result(i,5)=B5;
    result(i,6)=B6;
    result(i,7)=B7;
    result(i,8)=B8;
end
%以下为A律13折线译码
C2=[0,16;16,32;32,64;64,128;128,256;256,512;512,1024;1024,2048];
step=[1,1,2,4,8,16,32,64];
for i=1:2000
    temp(i,1)=result(i,1);
    temp(i,2)=bin2dec(num2str(result(i,2:4)));
    temp(i,3)=bin2dec(num2str(result(i,5:8)));
end
for i=1:2000
    position=floor((m(i)-C2(temp(i,2)+1,1))/step(temp(i,2)+1));
    result2(i)=C2(temp(i,2)+1,1)+position*step(temp(i,2)+1)+0.5*step(temp(i,2)+1);
    if temp(i,1)==0
        result2(i)=-result2(i);
    end
end
subplot(312);
t2=0:1/2000:1999/2000;
plot(t2,result2);
axis([0 0.02 -2000 2000]);
title('PCM译码信号(即量化信号)');
xlabel('t(s)');
m=2000*sin(100*pi*t);
for i=1:2000
    error(i)=result2(i)-m(i);
end
subplot(313);
plot(t2,error);
axis([0 0.02 min(error)-1 max(error)+1]);
title('量化误差');
xlabel('t(s)');

被量化信号改动主函数的那一行即可,或许也可以写个input()

运行结果
在这里插入图片描述

  • 7
    点赞
  • 78
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值