1.
%Eb/N0 Vs SER for BPSK over Rayleigh flat-fading with receive diversity
clearvars;clc;
%---------Input Fields------------------------
nSym = 10e5; %Number of symbols to transmit
N = [1,2,20]; %number of diversity branches
EbN0dBs = -20:2:36; %Eb/N0 range in dB for simulation
M = 2; %M-ary PSK modulation
k=log2(M);
EsN0dBs = 10*log10(k)+EbN0dBs; %EsN0dB calculation
figure;
for nRx = N %simulate for each # of received branchs
ser_MRC = zeros(1,numel(EsN0dBs));%simulated symbol error rates
ser_EGC = zeros(1,numel(EsN0dBs));
ser_SC = zeros(1,numel(EsN0dBs));
q=1;
%---------- Transmitter -----------
d = randi([0 M-1],1,nSym);
s = pskmod(d,M);%Modulation
s_diversity = kron(ones(nRx,1),s);%nRx signal branches
for EsN0dB = EsN0dBs
h = sqrt(1/2)*(randn(nRx,nSym)+1j*randn(nRx,nSym));%Rayleigh flat-fading
signal = h.*s_diversity; %effect of channel on the modulated signal
gamma = 10.^(EsN0dB/10);%for AWGN noise addition
P = sum(abs(signal).^2,2)./nSym; %calculate power in each branch of signal
N0 = P/gamma; %required noise spectral density for each branch
%Scale each row of noise with the calculated noise spectral density
noise = (randn(size(signal))+1j*randn(size(signal))).*sqrt(N0/2);
r = signal+noise;%received signal branches
%MRC processing assuming perfect channel estimates
s_MRC = sum(conj(h).*r,1)./sum(abs(h).^2,1); %detector decisions
d_MRC = pskdemod(s_MRC,M); %demodulation decisions
%EGC processing assuming perfect channel estimates
h_phases = exp(-1j*angle(h));%estimated channel phases
s_EGC = sum(h_phases.*r,1)./sum(abs(h),1); %detector decisions
d_EGC = pskdemod(s_EGC,M);%demodulation decisions
%SC processing assuming perfect channel estimates
[h_max,idx] = max(abs(h),[],1); %max |h| values along all branches
h_best = h(sub2ind(size(h),idx,1:size(h,2)));%best path's h estimate
y = r(sub2ind(size(r), idx, 1:size(r,2)));%selected output
s_SC = y.*conj(h_best)./abs(h_best).^2; %detector decisionss
d_SC = pskdemod(s_SC,M); %demodulate(MOD_TYPE,M,s_SC); %demodulation decisions
ser_MRC(q) = sum(d_MRC ~= d)/nSym;%Error rates computation
ser_EGC(q) = sum(d_EGC ~= d)/nSym;
ser_SC(q) = sum(d_SC ~= d)/nSym;
q=q+1;
end
semilogy(EbN0dBs,ser_MRC,'b-','lineWidth',1);hold on;grid on;
semilogy(EbN0dBs,ser_EGC,'r--','lineWidth',1);
semilogy(EbN0dBs,ser_SC,'g-.','lineWidth',1);
drawnow;
end
xlim([-20,36]);ylim([0.00001,1.1]);
xlabel('Eb/N0(dB)');ylabel('Symbol Error Rate (P_s)')
title('Receive diversity schemes in Rayleigh flat-fading')
2.%Eb/N0 Vs SER for BPSK over Rayleigh flat-fading with transmit diversity
clearvars;clc;
%---------Input Fields------------------------
nSym = 10e3; %Number of symbols to transmit
EbN0dBs = -20:2:36; %Eb/N0 range in dB for simulation
M = 2; %M-ary PSK modulation
%MOD_TYPE = 'PSK'; %Modulation type
k=log2(M);EsN0dBs = 10*log10(k)+EbN0dBs; %EsN0dB calculation
ser_sim = zeros(1,numel(EsN0dBs));q=1; %simulated symbol error rates
for EsN0dB = EsN0dBs %simulate for each # of received branches
%---------- Transmitter -----------
%d = ceil(M*rand(nSym,1));%uniform random symbols 1:M
d = randi([0:M-1],1,nSym);
s = pskmod(d,M);%Modulation
ss = kron(reshape(s,2,[]),ones(1,2));%shape as 2xnSym vector
h = sqrt(1/2)*(randn(2,nSym/2)+1j*randn(2,nSym/2));%channel coeffs
H = kron(h,ones(1,2)); %shape as 2xnSym vector
H(:,2:2:end) = conj(flipud(H(:,2:2:end)));
H(2,2:2:end) = -1*H(2,2:2:end);%Alamouti coded channel coeffs
signal = sum(H.*ss,1);%effect of channel on the modulated signal
gamma = 10.^(EsN0dB/10);%for AWGN noise addition
P = sum(abs(signal).^2,2)./nSym; %calculate power in each branch of signal
N0 = P/gamma; %required noise spectral density for each branch
%Scale each row of noise with the calculated noise spectral density
noise = (randn(size(signal))+1j*randn(size(signal))).*sqrt(N0/2);
r = signal+noise; %received signal
%Receiver processing
rVec = kron(reshape(r,2,[]),ones(1,2)); %2xnSym format
Hest = H; %perfect channel estimation
Hest(1,1:2:end) = conj(Hest(1,1:2:end));
Hest(2,2:2:end) = conj(Hest(2,2:2:end));%Hermitian transposed Hest matrix
y = sum(Hest.*rVec,1); %combiner output
sCap = y./sum(conj(Hest).*Hest,1);%decision vector for demod
dCap = pskdemod(sCap,M);%demodulation
ser_sim(q) = sum(dCap ~= d)/nSym;
q=q+1;%error rate calculation
end
figure;
semilogy(EbN0dBs,ser_sim,'r','lineWidth',1.5);hold on;%plot simulated error rates
title('2x1 Transmit diversity - Alamouti coding');grid on;
xlabel('EbN0 (dB)');ylabel('Symbol error rate (Ps)');
3.
%Eb/N0 Vs SER for BPSK over Rayleigh flat-fading with receive diversity
clearvars;clc;
%---------Input Fields------------------------
nSym = 10e5; %Number of symbols to transmit
N = [1,2]; %number of diversity branches
EbN0dBs = -20:2:36; %Eb/N0 range in dB for simulation
M = 2; %M-ary PSK modulation
%MOD_TYPE = 'PSK'; %Modulation type
k=log2(M);EsN0dBs = 10*log10(k)+EbN0dBs; %EsN0dB calculation
figure;
%nRx = N %simulate for each # of received branchs
ser_MRC = zeros(1,numel(EsN0dBs));%simulated symbol error rates
ser_EGC = zeros(1,numel(EsN0dBs));
ser_SC = zeros(1,numel(EsN0dBs));
ber_thy2 = zeros(1,length(EsN0dBs));
q1=1;
%---------- Transmitter -----------
%d = ceil(M*rand(1,nSym));%uniform random symbols 1:M
d = randi([0:M-1],1,nSym);
s = pskmod(d,M);%Modulation
s_diversity = kron(ones(1,1),s);%nRx signal branches
for EsN0dB = EsN0dBs
h = sqrt(1/2)*(randn(1,nSym)+1j*randn(1,nSym));%Rayleigh flat-fading
signal = h.*s_diversity; %effect of channel on the modulated signal
gamma = 10.^(EsN0dB/10);%for AWGN noise addition
P = sum(abs(signal).^2,2)./nSym; %calculate power in each branch of signal
N0 = P/gamma; %required noise spectral density for each branch
%Scale each row of noise with the calculated noise spectral density
noise = (randn(size(signal))+1j*randn(size(signal))).*sqrt(N0/2);
r = signal+noise;%received signal branches
%MRC processing assuming perfect channel estimates
s_MRC = sum(conj(h).*r,1)./sum(abs(h).^2,1); %detector decisions
d_MRC = pskdemod(s_MRC,M); %demodulation decisions
% %EGC processing assuming perfect channel estimates
% h_phases = exp(-1j*angle(h));%estimated channel phases
% s_EGC = sum(h_phases.*r,1)./sum(abs(h),1); %detector decisions
% d_EGC = pskdemod(s_EGC,M);%demodulation decisions
% %SC processing assuming perfect channel estimates
% [h_max,idx] = max(abs(h),[],1); %max |h| values along all branches
% h_best = h(sub2ind(size(h),idx,1:size(h,2)));%best path's h estimate
% y = r(sub2ind(size(r), idx, 1:size(r,2)));%selected output
% s_SC = y.*conj(h_best)./abs(h_best).^2; %detector decisions
% d_SC = pskdemod(s_SC,M); %demodulate(MOD_TYPE,M,s_SC); %demodulation decisions
ser_MRC(q1) = sum(d_MRC ~= d)/nSym;%Error rates computation
% ser_EGC(q) = sum(d_EGC ~= d)/nSym;
% ser_SC(q) = sum(d_SC ~= d)/nSym;
semilogy(EbN0dBs,ser_MRC,'b-','lineWidth',1.5);hold on;grid on;
drawnow;
q1=q1+1;
end
%semilogy(EbN0dBs,ser_MRC,'b-','lineWidth',1.5);hold on;
s_diversity = kron(ones(2,1),s);%nRx signal branches
ser_MRC2 = zeros(1,numel(EsN0dBs));%simulated symbol error rates
q2=1;
for EsN0dB = EsN0dBs
h = sqrt(1/2)*(randn(2,nSym)+1j*randn(2,nSym));%Rayleigh flat-fading
signal = h.*s_diversity; %effect of channel on the modulated signal
gamma = 10.^(EsN0dB/10);%for AWGN noise addition
P = sum(abs(signal).^2,2)./nSym; %calculate power in each branch of signal
N0 = P/gamma; %required noise spectral density for each branch
%Scale each row of noise with the calculated noise spectral density
noise = (randn(size(signal))+1j*randn(size(signal))).*sqrt(N0/2);
r = signal+noise;%received signal branches
%MRC processing assuming perfect channel estimates
s_MRC2 = sum(conj(h).*r,1)./sum(abs(h).^2,1); %detector decisions
d_MRC2 = pskdemod(s_MRC2,M); %demodulation decisions
% %EGC processing assuming perfect channel estimates
% h_phases = exp(-1j*angle(h));%estimated channel phases
% s_EGC = sum(h_phases.*r,1)./sum(abs(h),1); %detector decisions
% d_EGC = pskdemod(s_EGC,M);%demodulation decisions
% %SC processing assuming perfect channel estimates
% [h_max,idx] = max(abs(h),[],1); %max |h| values along all branches
% h_best = h(sub2ind(size(h),idx,1:size(h,2)));%best path's h estimate
% y = r(sub2ind(size(r), idx, 1:size(r,2)));%selected output
% s_SC = y.*conj(h_best)./abs(h_best).^2; %detector decisions
% d_SC = pskdemod(s_SC,M); %demodulate(MOD_TYPE,M,s_SC); %demodulation decisions
ser_MRC2(q2) = sum(d_MRC2 ~= d)/nSym;%Error rates computation
% ser_EGC(q) = sum(d_EGC ~= d)/nSym;
% ser_SC(q) = sum(d_SC ~= d)/nSym;
semilogy(EbN0dBs,ser_MRC2,'r-','lineWidth',1.5);hold on;
drawnow;
q2=q2+1;
end
% semilogy(EbN0dBs,ser_MRC2,'r-','lineWidth',1.5);hold on;
% semilogy(EbN0dBs,ser_EGC,'r--','lineWidth',1.5);
% semilogy(EbN0dBs,ser_SC,'g-.','lineWidth',1.5);
%Eb/N0 Vs SER for BPSK over Rayleigh flat-fading with transmit diversity
ser_sim = zeros(1,numel(EsN0dBs));q3=1; %simulated symbol error rates
for EsN0dB = EsN0dBs %simulate for each # of received branches
%---------- Transmitter -----------
%d = ceil(M*rand(nSym,1));%uniform random symbols 1:M
d = randi([0:M-1],1,nSym);
s = pskmod(d,M);%Modulation
ss = kron(reshape(s,2,[]),ones(1,2));%shape as 2xnSym vector
h = sqrt(1/2)*(randn(2,nSym/2)+1j*randn(2,nSym/2));%channel coeffs
H = kron(h,ones(1,2)); %shape as 2xnSym vector
H(:,2:2:end) = conj(flipud(H(:,2:2:end)));
H(2,2:2:end) = -1*H(2,2:2:end);%Alamouti coded channel coeffs
signal = sum(H.*ss,1);%effect of channel on the modulated signal
gamma = 10.^(EsN0dB/10);%for AWGN noise addition
P = sum(abs(signal).^2,2)./nSym; %calculate power in each branch of signal
N0 = P/gamma; %required noise spectral density for each branch
%Scale each row of noise with the calculated noise spectral density
noise = (randn(size(signal))+1j*randn(size(signal))).*sqrt(N0/2);
r = signal+noise; %received signal
%Receiver processing
rVec = kron(reshape(r,2,[]),ones(1,2)); %2xnSym format
Hest = H; %perfect channel estimation
Hest(1,1:2:end) = conj(Hest(1,1:2:end));
Hest(2,2:2:end) = conj(Hest(2,2:2:end));%Hermitian transposed Hest matrix
y = sum(Hest.*rVec,1); %combiner output
sCap = y./sum(conj(Hest).*Hest,1);%decision vector for demod
dCap = pskdemod(sCap,M);%demodulation
ser_sim(q3) = sum(dCap ~= d)/nSym;
semilogy(EbN0dBs,ser_sim,'g--','lineWidth',1.5);hold on;%plot simulated error rates
drawnow;
q3=q3+1;%error rate calculation
end
% semilogy(EbN0dBs,ser_sim,'g--','lineWidth',1.5);hold on;%plot simulated error rates
for q4 = 1:EsN0dBs
ber_thy2(q4) = berfading(EsN0dB(q4), 'psk', 2, 1);
semilogy(EbN0dB,ber_thy2,'c--','lineWidth',1.5);
q4 = q4 + 1;
end
xlim([-20,36]);ylim([0.00001,1.1]);
xlabel('Eb/N0(dB)');ylabel('Symbol Error Rate (P_s)')
title('Receive diversity schemes and Transmit diversity in Rayleigh flat-fading');
grid on;
xlabel('EbN0 (dB)');ylabel('Symbol error rate (Ps)');
legend('No Diversity (1Tx, 1Rx)', 'Receive diversity Maximal-Ratio Combining (1Tx, 2Rx)',...
'Alamouti_Transmit diversity (2Tx, 1Rx)')
运行结果: