在线咨询
eetop公众号 创芯大讲堂 创芯人才网
切换到宽版

EETOP 创芯网论坛 (原名:电子顶级开发网)

手机号码,快捷登录

手机号码,快捷登录

找回密码

  登录   注册  

快捷导航
搜帖子
查看: 2318|回复: 5

QPSK仿真代码

[复制链接]
发表于 2016-8-24 09:37:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

x
% MATLAB script for System Simulation Home

% Carrier frequency for modulation and demodulation
   Fc =5;
   N_sample=8;                                              %设置采样频率的倍数为5倍载波频率
   dt0=1/N_sample/Fc;
   Ts=1/Fc/5;
   dt=Ts;
% QPSK transmitter
%
data=500;                                              % 输入数据长度500
t=0:dt0:data*dt0-dt0;                                   %
%%  产生数字基带信号
rand_data =randn(1,data);
for i=1:data
if rand_data(i)>=0.5
input(i)=1;
else
input(i)=0;
end
end
%Series to Parallel
for i=1:data
if rem(i,2)==1                                          %如果i是奇数,执行这一部分
   if input(i)==1
    I(i)=1;
    I(i+1)=1;
else
    I(i)=-1;
    I(i+1)=-1;
end
else                                                          %i是偶数,执行这一部分
if input(i)==1
Q(i-1)=1;
Q(i)=1;
else
Q(i-1)=-1;
Q(i)=-1;
end
end
end                                                            %构造i q路信号

%绘图
figure(1)
t1=0:dtdata-1)*dt;
[f1,inputf]=T2F(t1,input);
plot(f1,(20*log(inputf)))
%axis([0 data -40 100])
grid
title('Spectrum of Input binary data')

%%   Zero insertion                                         零插值
N_sample=5;                                                      % Sampling rate is 25
for i=1:N_sample*data                                      %数据变成了500*5
if rem(i,N_sample)==1                                      %这个循环表明 原来的I,Q数据每两个数之间插入了4个0
Izero(i)=I(fix((i-1)/N_sample)+1);                      %fix 向零靠拢取整
Qzero(i)=Q(fix((i-1)/N_sample)+1);
else
Izero(i)=0;
Qzero(i)=0;
end
end

% 绘图
figure(2)
subplot(221)
[f1,If]=T2F(t1,I);
plot(f1,20*log(If))
axis([-13 13 -100 50])
grid
title('Spectrum of I-channel data')

subplot(222)
[f1,Qf]=T2F(t1,Q);
plot(f1,20*log(Qf))
% plot(fftshift(20*log(abs(fft(Q)))))
axis([-13 13 -100 50])
grid
title('Spectrum of Q-channel data')


subplot(223)
t2=0:dtN_sample*data-1)*dt;
[f2,izerof]=T2F(t2,Izero);
plot(f2,20*log(izerof))
% plot(fftshift(20*log(abs(fft(Izero)))))
axis([-13 13 -100 50])
grid
title('Spectrum of I-channel data after zero insertion')

subplot(224)
[f2,qzerof]=T2F(t2,Qzero);
plot(f2,20*log(qzerof))
% plot(fftshift(20*log(abs(fft(Qzero)))))
axis([-13 13 -100 50])
grid
title('Spectrum of Q-channel data after zero insertion')
%%  滤波
%Pulse shaping filter                                脉冲整形滤波器,此处是升余弦滤波
%接着,将进行低通滤波,因为 随着传输速率的增大,基带脉冲的频谱将变宽
%如果不(如升余弦滤波)进行低通滤波,后面加载频的时候可能会出现困难。
NT =50;                                                   %用来控制滤波器长度的参数,此值越大抽头越多,也就越精确,滤波器的阶数等于2*N_T+1
N=2*N_sample*NT;                               %滤波器抽头数=信号点数=形成的滤波器点数
Fs=25;                                                    %采样频率
rf=0.1;                                                      %滚降系数。
psf=rcosfir(rf,NT,N_sample,Fs,'sqrt');            %滤波器产生函数

Ipulse= conv(Izero,psf);                           %零差值后的数字信号与滤波器卷积,形成滤波后的IQ路信号
Qpulse= conv(Qzero,psf);

%绘图
figure(3)
subplot(221)
plot(psf)
axis([200 300 -0.2 0.6])
grid
title('Time domain response of pulse shaping filter')

subplot(222)
t3=0:dtN-1)*dt;
[f3,psff]=T2F(t3,psf);
plot(f3,(psff));
%plot(f3,20*log(psff));
%axis([-13 13 -400 0])
grid
title('Transfer function of pulse shaping filter')

subplot(223)
t32=0:dt:(N_sample*data+N-1)*dt;
[f32,iplusef]=T2F(t32,Ipulse);
plot(f32,20*log(iplusef))
%axis([-13 13 -350 100])
grid
title('Spectrum of I-channel after pulse shaping filter')

subplot(224)
[f32,qplusef]=T2F(t32,Qpulse);
plot(f32,20*log(qplusef))
%axis([-13 13 -350 100])
grid
title('Spectrum of Q-channel after pulse shaping filter')
%% Modulation                                               QPSK调制
for i=1:(N_sample*data+N)                                                  
t(i)=(i-1)/(Fc*N_sample);                                                           %采样间隔
Imod(i)=Ipulse(i).*sqrt(2)*cos(2*pi*Fc*t(i));                   %IQ路信号分别同子载波相乘,子载波相互正交
Qmod(i)=Qpulse(i).*(-sqrt(2)*sin(2*pi*Fc*t(i)));
end

sum=Imod+Qmod;                                                           %合成一路发射信号

%绘图
figure(4)
subplot(311)
[f4,imodf]=T2F(t,Imod);
plot(f4,20*log(imodf))
%plot(f4,fftshift(20*log(abs(fft(Imod)))))
%axis([-13 13 -500 50])
grid
title('Spectrum of I-channel after modulation')

subplot(312)
[f4,qmodf]=T2F(t,Qmod);
plot(f4,20*log(qmodf))
% plot(fftshift(20*log(abs(fft(Qmod)))))
%axis([-13 13 -500 50])
grid
title('Spectrum of Q-channel after modulation')

subplot(313)
[f4,sumf]=T2F(t,sum);
plot(f4,20*log(sumf))
% plot(fftshift(20*log(abs(fft(Qmod)))))
axis([-13 13 -500 50])
grid
title('Spectrum of qpsk after modulation')
%%  QPSK Receiver
% 接收机(没有上下变频的过程)

% Demodulation                                                                  相干解调
for i=1:N_sample*data+N
Idem(i)=sum(i).*sqrt(2)*cos(2*pi*Fc*t(i));
Qdem(i)=sum(i).*(-sqrt(2)*sin(2*pi*Fc*t(i)));
end

% Matched filter                                                                   匹配滤波
mtf= rcosfir(rf,NT, N_sample,Fs,'sqrt');                                      %平方根升余弦滤波器,低通滤波
Imat = conv(Idem,mtf);
Qmat = conv(Qdem,mtf);

% Data selection 数据选择
for i=1:N_sample*data           
Isel(i)=Imat(i+N);
Qsel(i)=Qmat(i+N);
end

%绘图
figure(5)
subplot(221)
t5=0:dt:(N_sample*data+N-1)*dt;
[f5,idemf]=T2F(t5,Idem);
plot(f5,20*log(idemf))
axis([-13 13 -200 50])
grid
title('Spectrum of I-channel after demodulation')

subplot(222)
[f5,qdemf]=T2F(t5,Qdem);
plot(f5,20*log(qdemf))
axis([-13 13 -200 50])
grid
title('Spectrum of Q-channel after demodulation')

subplot(223)
t52=0:dt:(data*N_sample+N+N-1)*dt;
[f52,imatf]=T2F(t52,Imat);
plot(f52,20*log(imatf))
axis([-13 13 -400 100])
grid
title('Spectrum of I-channel after matched filter')

subplot(224)
[f52,qmatf]=T2F(t52,Qmat);
plot(f52,20*log(qmatf))
axis([-13 13 -400 100])
grid
title('Spectrum of Q-channel after matched filter')

%%  Sampler
for i = 1:data                  %去掉插值的零
Isam(i)= Isel((i-1)*N_sample+1);
Qsam(i)= Qsel((i-1)*N_sample+1);
end

% Decision threshold       抽样判决得到数字序列 (未加噪声所以抽样判决前后结果几乎没有变化)
threshold = 0.2;
for i = 1:data
if Isam(i)>= threshold
Ifinal(i)= 1;
else
Ifinal(i)= -1;
end
if Qsam(i) >= threshold
Qfinal(i) = 1;
else
Qfinal(i)= -1;
end
end

% Parallel to Series           将双极性码恢复成单极性码
for i = 1:data
if rem(i, 2)== 1
if Ifinal(i)== 1
final(i)=1;
else
final(i)= 0;
end
else
if Qfinal(i) == 1
final(i)= 1;
else
final(i)= 0;
end
end
end

figure(6)
subplot(221)
t6=0:dt:(data-1)*dt;
[f6,isamf]=T2F(t6,Isam);
plot(f6,20*log(isamf))
axis([-13 13 -100 50])
grid
title('Spectrum of I-channel after sampler')

subplot(222)
[f6,qsamf]=T2F(t6,Qsam);
plot(f6,20*log(qsamf))
axis([-13 13 -100 50])
grid
title('Spectrum of Q-channel after sampler')

subplot(223)
[f6,ifinalf]=T2F(t6,Ifinal);
plot(f6,20*log(ifinalf))
axis([-13 13 -100 50])
grid
title('Spectrum of I-channel after Decision threshold')

subplot(224)
[f6,qfinalf]=T2F(t6,Qfinal);
plot(f6,20*log(qfinalf))
axis([-13 13 -100 50])
grid
title('Spectrum of Q-channel after Decision threshold')


figure(7)
plot(Isam, Qsam,'*','linewidth', 2)     %去掉插入的零后
axis([-1.2 1.2 -1.2 1.2])
grid
title('Constellation of sampler')  %星座图

figure(8)
t8=t6;
[f8,finalf]=T2F(t8,final);
plot(f8,20*log(finalf))
axis([-13 13 -100 50])
grid
title('Spectrum of final received binary data')
发表于 2016-8-28 23:39:17 | 显示全部楼层
学习一下
发表于 2016-9-1 13:31:36 | 显示全部楼层
学习一下
发表于 2016-9-2 09:15:28 | 显示全部楼层
謝謝分享!
发表于 2016-9-6 18:39:00 | 显示全部楼层
学习了,谢谢楼主
发表于 2016-9-16 07:59:27 | 显示全部楼层
学习了,谢谢楼主
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐 上一条 /2 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-4-29 01:41 , Processed in 0.025130 second(s), 8 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
快速回复 返回顶部 返回列表