主程序:
clc;
clear all;
close all;
ebn0=0:30;
% [qber,qreal]=qpsk(ebn0);
[qber_f,qreal_f]=qpsk_fading(ebn0);
figure(1)
semilogy(ebn0,qber_f,'-*b',ebn0,qreal_f,'-om','LineWidth',2);
xlabel('Eb/N0(dB)')
ylabel('BER')
title('Performance of QPSK')
legend('QPSK Rayleigh(Simulation)','QPSK Rayleigh(Theory)')
axis([0 30 1.00E-5 1.00E+0])
grid on;
子程序1:
% qpsk_fading.m
%
% Simulation program to realize QPSK transmission system
% (under one path fading)
%
% Programmed by H.Harada and T.Yamamura
%
%******************** Preparation part *************************************
function [ber_f,real_f]=qpsk_fading(ebn0)
sr=256000.0; % Symbol rate
ml=2; % ml:Number of modulation levels (BPSK:ml=1, QPSK:ml=2, 16QAM:ml=4)
br=sr .* ml; % Bit rate
nd = 100; % Number of symbols that simulates in each loop
lth=length(ebn0); % Eb/N0
IPOINT=8; % Number of oversamples
ber_f=zeros(1,lth);
real_f=(1-1./sqrt(1+1./10.^(ebn0/10)))/2;
%************************* Filter initialization ***************************
irfn=21; % Number of taps
alfs=0.5; % Rolloff factor
[xh] = hrollfcoef(irfn,IPOINT,sr,alfs,1); %Transmitter filter coefficients
[xh2] = hrollfcoef(irfn,IPOINT,sr,alfs,0); %Receiver filter coefficients
%******************* Fading initialization ********************
% If you use fading function "sefade", you can initialize all of parameters.
% Otherwise you can comment out the following initialization.
% The detailed explanation of all of valiables are mentioned in Program 2-8.
% Time resolution
tstp=1/sr/IPOINT; %采样周期
% Arrival time for each multipath normalized by tstp
% If you would like to simulate under one path fading model, you have only to set
% direct wave.
% itau = [0];
% itau = [0,2,3,4];
itau = [0,6,12,18];
% Mean power for each multipath normalized by direct wave.
% If you would like to simulate under one path fading model, you have only to set
% direct wave.
% dlvl = [0];
% dlvl = [0,10,20,25];
Ts=tstp;
A_dB=-20;
num_ch=10000;
A=10^(A_dB/10);
tau_d=30*(1e-9);%均方根时延扩展
% imax=ceil(-tau_d*log(A)/Ts);
imax=itau(end)+1;
p0=(1-exp(-Ts/tau_d))/(1-exp(-(imax+1)*Ts/tau_d));
l=0:imax;
pow_e=p0*exp(-l*Ts/tau_d);
for ia=1:length(pow_e)
H_e(:,ia)=((randn(1,num_ch)+j*randn(1,num_ch))/sqrt(2)).'*sqrt(pow_e(ia));
end
avg_pow_h_e=mean(H_e.*conj(H_e));
dlvl1 = avg_pow_h_e(itau+1);
for ib=1:length(dlvl1)
dlvl2(ib)=dlvl1(ib)/dlvl1(1);
end
dlvl=(log10(dlvl2)/(-0.05))/100;
% Number of waves to generate fading for each multipath.
% In normal case, more than six waves are needed to generate Rayleigh fading
% n0=[6];
n0=[6,7,6,7];
% Initial Phase of delayed wave
% In this simulation four-path Rayleigh fading are considered.
% th1=[0.0];
th1=[0.0,0.0,0.0,0.0];
% Number of fading counter to skip
itnd0=nd*IPOINT*100;
% Initial value of fading counter
% In this simulation one-path Rayleigh fading are considered.
% Therefore one fading counter are needed.
% itnd1=[1000];
itnd1=[1000,2000,3000,4000];
% Number of directwave + Number of delayed wave
% In this simulation one-path Rayleigh fading are considered
% now1=1;
now1=4;
% Maximum Doppler frequency [Hz]
% You can insert your favorite value
fd=160;
% You can decide two mode to simulate fading by changing the variable flat
% flat : flat fading or not
% (1->flat (only amplitude is fluctuated),0->nomal(phase and amplitude are fluctutated)
flat =1;
%******************** START CALCULATION *************************************
for d=1:lth
nloop=5000; % Number of simulation loops
noe = 0; % Number of error data
nod = 0; % Number of transmitted data
for iii=1:nloop
%*************************** Data generation ********************************
data1=rand(1,nd*ml)>0.5; % rand: built in function
%*************************** QPSK Modulation ********************************
[ich,qch]=qpskmod(data1,1,nd,ml);
[ich1,qch1]= compoversamp(ich,qch,length(ich),IPOINT);
[ich2,qch2]= compconv(ich1,qch1,xh);
%**************************** Attenuation Calculation ***********************
spow=sum(ich2.*ich2+qch2.*qch2)/nd; % sum: built in function
attn=0.5*spow*sr/br*10.^(-ebn0(d)/10);
attn=sqrt(attn); % sqrt: built in function
%********************** Fading channel **********************
% Generated data are fed into a fading simulator
% [ifade,qfade]=sefade(ich2,qch2,itau,dlvl,th1,n0,itnd1,now1,length(ich2),tstp,fd,flat);
[ifade,qfade]=AWB617(ich2,qch2,itau,dlvl,th1,n0,itnd1,now1,length(ich2),tstp,fd,flat);
% Updata fading counter
itnd1 = itnd1+ itnd0;
%********************* Add White Gaussian Noise (AWGN) **********************
[ich3,qch3]= comb(ifade,qfade,attn);% add white gaussian noise
[ich4,qch4]= compconv(ich3,qch3,xh2);
syncpoint=irfn*IPOINT+1;
ich5=ich4(syncpoint:IPOINT:length(ich4));
qch5=qch4(syncpoint:IPOINT:length(qch4));
%**************************** QPSK Demodulation *****************************
[demodata]=qpskdemod(ich5,qch5,1,nd,ml);
%************************** Bit Error Rate (BER) ****************************
noe2=sum(abs(data1-demodata)); % sum: built in function
nod2=length(data1); % length: built in function
noe=noe+noe2;
nod=nod+nod2;
%fprintf('%d\t%e\n',iii,noe2/nod2); % fprintf: built in function
end % for iii=1:nloop
%********************** Output result ***************************
ber_f(d) = noe/nod;
end
end
子程序2:
function[iout,qout,ramp]=AWB617(idata,qdata,itau,dlvl,th,n0,itn,n1,nsamp,tstp,fd,flat)
% [data6,data7]=sefade(data1,zeros(1,length(data1)),itau,dlvl,th1,n0,itnd1,now1,length(data1),tstp,fd,flat);
%****************** variables *************************
% idata input Ich data
% qdata input Qch data
% iout output Ich data
% qout output Qch data
% ramp : Amplitude contaminated by fading%衰落引起的幅度、正余弦污染
% rcos : Cosine value contaminated by fading
% rsin : Cosine value contaminated by fading
% itau : Delay time for each multipath fading%每路的延迟时间
% dlvl : Attenuation level for each multipath fading%多径衰落衰减程度
% th : Initialized phase for each multipath fading多径衰落初始相位
% n0 : Number of waves in order to generate each multipath fading
% itn : Fading counter for each multipath fading
% n1 : Number of summation 总和for direct and delayed waves
% nsamp : Total number of symbols
% tstp : Mininum time resolution
% fd : Maxmum doppler frequency
% flat flat fading or not
% (1->flat (only amplitude is fluctuated),0->nomal(phase and amplitude are fluctutated)
%******************************************************
iout = zeros(1,nsamp);
qout = zeros(1,nsamp);
total_attn = sum(10 .^( -1.0 .* dlvl ./ 10.0));%将功率衰减的分贝数转换为对应的增益(即输出功率与输入功率之间的比值)
%再求和,即得到的四路信号总的功率衰减增益
for k = 1 : n1 %分为4条路径
atts = 10.^( -0.05 .* dlvl(k));
if dlvl(k) >= 40.0
atts = 0.0;
end
theta = th(k) .* pi ./ 180.0; %类似于将90度转换为pi/2,th为延迟波的初相
[itmp,qtmp] = delay ( idata , qdata , nsamp , itau(k));%含为4条路路径,每条路径对应传输的100个数据的延迟不同
% [itmp3,qtmp3,ramp,rcos,rsin] = fade (itmp,qtmp,nsamp,tstp,fd,n0(k),itn(k),flat);
[h,t_state]=Jakes_model(fd,tstp,length(itmp),itn(k),1,0);
ramp=abs(h);
iout1=abs(h).*itmp;
qout1=abs(h).*qtmp;
iout = iout + atts .* iout1 ./ sqrt(total_attn);
qout= qout + atts .* qout1 ./ sqrt(total_attn);
% power=fft(iout);
% power(k)=sum(iout.^2)/(length(iout));
end
% ************************end of file***********************************
子程序3:
function [h,tf]=Jakes_model(fd,Ts,Ns,t0,E0,phi_N)
% inputs:
% fd: doppler frequency
% Ts: 采样周期
% Ns: 采样点数
% t0: 初始时间
% E0: 信道功率
% phi_N: 具有最大多普勒频率信号的初始相位
% output:
% h: 复合衰落信道
% t_state: 当前时刻
% check inputs
if nargin<6, phi_N=0; end
if nargin<5, E0=1; end
if nargin<4, t0=0; end
if nargin<3
error('More inputs are needed.');
end
N0=8; %
N=4*N0+2; % 多径数
wd=2*pi*fd;
% t=( itnd1 :nd*IPOINT-2+kk+itnd1)*tstp;
t=t0+[0:Ns-1]*Ts;
tf=t(end)+Ts; % the end time
% 尽量利用矩阵
% hI=2*sum(cos(phi_n)*coswt)+sqrt(2)*cos(phi_N)*cos(wd*t)
% hQ=2*sum(sin(phi_n)*coswt)+sqrt(2)*sin(phi_N)*cos(wd*t)
coswt=[sqrt(2)*cos(wd*t);2*cos(wd*cos(2*pi/N*[1:N0]')*t)];
h=E0/sqrt(2*N0+1)*exp(j*[phi_N pi/(N0+1)*[1:N0]])*coswt;
还有一些子程序,需要的可以联系qq:2718713662