a(i*8-7:i*8)=out(1:8); end
max=512;
/SK调制部分
fs=2000; %采样频率 dt=1/fs; f1=20;
f2=120; %两个信号的频率 g1=a;
g2=~a; %信号反转,和g1反向
g11=(ones(1,200))'*g1; %抽样 --首先生成1行200列的全1矩阵,然后竖过来.然后与g1进行矩阵乘法
g1a=g11(:)'; %将无数行,无数列的一个矩阵g11,转置成为1行N列 g21=(ones(1,200))'*g2; g2a=g21(:)'; t=0:dt:51.2-dt; t1=length(t); subplot(2,1,2); plot(t,g1a);
title('PCM编码后的信号(局部)') ylabel('幅度') grid on;
axis([49.8 50.3 -0.2 1.2]); fsk1=g1a.*cos(2*pi*f1.*t); fsk2=g2a.*cos(2*pi*f2.*t);
fsk=fsk1+fsk2; %产生的信号 figure(2)
subplot(2,1,1); plot(t,fsk);
title('FSK信号(局部)') ylabel('幅度')
axis([49.8 50.3 -1.2 1.2]);
sn=awgn(fsk,10); %通过高斯信道 figure(2)
subplot(2,1,2);
plot(t,sn); %噪声波形
title('通过高斯以后的FSK信号(局部)') ylabel('幅度')
axis([49.8 50.3 -1 1.2]); %FSK解调
b1=fir1(101,[10/800 20/800]);
b2=fir1(101,[90/800 110/800]); %设置带通参数 H1=filter(b1,1,sn);
H2=filter(b2,1,sn); %经过带通滤波器后的信号
6
figure(3)
subplot(2,1,1); plot(t,H1);
title('经过带通滤波器f1后的波形(局部)') axis([49.8 50.3 -1 1.2]); ylabel('幅度') xlabel('t')
subplot(2,1,2); plot(t,H2);
title('经过带通滤波器f2后的波形(局部)') axis([49.8 50.3 -1 1.2]); ylabel('幅度') xlabel('t') sw1=H1.*H1;
sw2=H2.*H2; %经过相乘器 figure(4) subplot(211); plot(t,sw1);
title('经过相乘器h1后的波形') axis([49.8 50.3 -1 1.2]); ylabel('幅度') subplot(212); plot(t,sw2);
title('经过相乘器h2后的波形') ylabel('幅度')
axis([49.8 50.3 -1 1.2]); xlabel('t')
bn=fir1(101,15/800); %经过低通滤波器 figure(5)
st1=filter(bn,1,sw1); st2=filter(bn,1,sw2); subplot(211); plot(t,st1);
title('经过低通滤波器sw1后的波形') axis([49.8 50.3 -1 1.2]); ylabel('幅度') subplot(212); plot(t,st2);
title('经过低通滤波器sw2后的波形') ylabel('幅度')
axis([49.8 50.3 -1 1.2]); xlabel('t') %判决
for i=1:length(t)
7
if(st1(i)>=st2(i)) st(i)=1; else st(i)=0; end end
figure(6) subplot(211); plot(t,st);
axis([0 10 -1.5 1.5]);
title('经过抽样判决器后的波形') axis([49.8 50.3 -1 1.2]); grid on;
ylabel('幅度')
%对接收到的信号进行反PCM编码 %%抽取其中的PCM编码序列 for i=1:512
pcm(i)=st(i*200); end
%循环将PCM编码变成量化电平数 for i=1:64;
%初始化计算参数 dlm=0; dp=0;
%将2-4位段落码换算成起始电平 if pcm(i*8-6)==1; dlm=dlm+4; end
if pcm(i*8-5)==1; dlm=dlm+2; end
if pcm(i*8-4)==1; dlm=dlm+1; end
%得到由段落码换算来的起始电平 dp=2^(dlm+4)
%先加上最小量化电平的一半 dp=dp+2^(dlm)/2;
%将5-8位段内码结合段落码一起转换成电平数 %第5位
if pcm(i*8-3)==1;
dp=dp+2^(dlm+3); end %第6位
if pcm(i*8-2)==1;
8
dp=dp+2^(dlm+2); end %第7位
if pcm(i*8-1)==1;
dp=dp+2^(dlm+1); end %第8位
if pcm(i*8)==1;
dp=dp+2^(dlm); end
%最后判断第1位码,决定正负 if pcm(i*8-7)==0; dp=-dp; end
jiedp(i)=dp; end
%将量化电平数变成电压 for i=1:64;
dianya(i)=jiedp(i)/2048*6; end N=64;
fs=2800; %抽样频率 t=(0:N-1)/fs;%采样时间s f=100;%模拟信号的频率Hz
figure(6);subplot(2,1,2);plot(t,dianya);grid on
axis([0 1/f*2 -5 5]);title('PCM解码后的模拟信号')
ASK调制方法的源代码: close all clear all; clc;
%生成模拟信号
fudu=5; %模拟信号的振幅 fs=2800; %抽样频率
f=100;%模拟信号的频率Hz N=64; %抽样个数
t=(0:N-1)/fs;%采样时间s
moni=fudu*sin(2*pi*f*t);%生成了模拟信号 figure(1);
%将第1个窗口分成2行2列,取第1个位置 subplot(2,1,1); plot(t,moni); grid on;
axis([0 1/f*2 -fudu fudu]); %输出2个周期的信号
9
title('输入模拟信号'); for i=1:64;
%设计输入范围是-6~6V,对模拟信号抽取64个样本 %循环对每个样本进行归一化并计算出量化单位x %设量化器最大分层电平是2048个量化单位. x(i)=moni(i)/6*2048; end
%pcm编码部分--对每个样本进行编码 for i=1:64; if x(i)>0
out(1)=1;%抽样值大于0,极性码为1 else
out(1)=0;%抽样值小于0,极性码为0 end
%确定段落码
if abs(x(i))>=0 & abs(x(i))<32
out(2)=0;out(3)=0;out(4)=0;step=1;st=0;%x的绝对值在0到32之间,则落在第一段内,段落码为001,段落起始电平st为16,量化间隔step为1 elseif 32<=abs(x(i)) & abs(x(i))<64
out(2)=0;out(3)=0;out(4)=1;step=2;st=32;%x的绝对值在32到64之间,则落在第二段内,段落码为010,段落起始电平st为32,量化间隔step为2 elseif 64<=abs(x(i)) & abs(x(i))<128
out(2)=0;out(3)=1;out(4)=0;step=4;st=64;%x的绝对值在64到128之间,则落在第三段内,段落码为011,段落起始电平st为64,量化间隔step为4 elseif 128<=abs(x(i)) & abs(x(i))<256
out(2)=0;out(3)=1;out(4)=1;step=8;st=128;%x的绝对值在128到256之间,则落在第四段内,段落码为100,段落起始电平st为128,量化间隔step为8 elseif 256<=abs(x(i)) & abs(x(i))<512
out(2)=1;out(3)=0;out(4)=0;step=16;st=256;%x的绝对值在256到512之间,则落在第五段内,段落码为101,段落起始电平st为256,量化间隔step为16 elseif 512<=abs(x(i)) & abs(x(i))<1024
out(2)=1;out(3)=0;out(4)=1;step=32;st=512;%x的绝对值在512到1024之间,则落在第六段内,段落码为110,段落起始电平st为512,量化间隔step为32 elseif 1024<=abs(x(i)) & abs(x(i))<2048
out(2)=1;out(3)=1;out(4)=0;step=64;st=1024;%x的绝对值在1024到2048之间,则落在第七段内,段落码为111,段落起始电平st为1024,量化间隔step为64 else
out(2)=1;out(3)=1;out(4)=1;step=64;st=1024;%x的绝对值超出了2048,则段落码为111,段落起始电平st为1024,量化间隔step为64 end
%确定段内码
if (abs(x(i))>=2048)
out(2:8)=[ 1 1 1 1 1 1 1];%x的绝对值超出了2048,段落码和段内码均为1111111 else
10