1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| clc clear all fs = 100000; t = 0:1/fs:10; ts = 0:0.3:10;
xt = 0.1.*cos(0.15.*t)+1.5.*sin(2.5.*t)+0.5.*cos(4.*t); xny = 0.1.*cos(0.15.*ts)+1.5.*sin(2.5.*ts)+0.5.*cos(4.*ts); bm = [];
for i=1:length(xny) if xny(i)>0 B1=1; else B1=0; end xn(i)=abs(xny(i)); C=[0 16 32 64 128 256 512 1024 2048]; for j=1:length(C); if xn(i)>=C(j)&&xn(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(xn(i),a,b); [B6,a2,b2]=judge(xn(i),a1,b1); [B7,a3,b3]=judge(xn(i),a2,b2); [B8,a4,b4]=judge(xn(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; bm = [bm,B1,B2,B3,B4,B5,B6,B7,B8]; end
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:length(xny) 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:length(xny) position=floor((xn(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
figure(1) subplot(4,1,1) plot(t,xt) title('原始输入信号') ylabel('x(t)'); xlabel('t'); axis([0 10 -3 3]); hold on subplot(4,1,2) stem(1:length(xny),xny) title('抽样后信号') ylabel('x(n)'); xlabel('n'); axis([0 length(xny) -3 3]); hold on subplot(4,1,3) stem(1:length(result2),result2) title('量化输出信号') ylabel('x^(n)'); xlabel('n'); axis([0 length(result2) -3 3]); hold on subplot(4,1,4) stem(1:length(bm),bm) title('编码结果') axis([0 length(bm) -0.1 1.1]); hold on
|