0% found this document useful (0 votes)
293 views

1.transmit Diversity vs. Receive Diversity: Matlab Code With Output

This MATLAB code simulates and compares different diversity techniques for wireless communications. It performs the following: 1. Compares transmit diversity (Alamouti coding) vs receive diversity (maximal ratio combining) for a 2x1 and 1x2 MIMO system. It plots the BER vs SNR and fits curves to the results. 2. Simulates orthogonal space-time block coding (OSTBC) for a 4x4 MIMO system and compares it to other techniques like Alamouti coding. It again plots BER vs SNR. 3. Simulates an OFDM system and plots the BER vs SNR, comparing it to theoretical results. It also shows scatter plots and

Uploaded by

Tolu Ayana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
293 views

1.transmit Diversity vs. Receive Diversity: Matlab Code With Output

This MATLAB code simulates and compares different diversity techniques for wireless communications. It performs the following: 1. Compares transmit diversity (Alamouti coding) vs receive diversity (maximal ratio combining) for a 2x1 and 1x2 MIMO system. It plots the BER vs SNR and fits curves to the results. 2. Simulates orthogonal space-time block coding (OSTBC) for a 4x4 MIMO system and compares it to other techniques like Alamouti coding. It again plots BER vs SNR. 3. Simulates an OFDM system and plots the BER vs SNR, comparing it to theoretical results. It also shows scatter plots and

Uploaded by

Tolu Ayana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

MATLAB CODE WITH OUTPUT

1.Transmit Diversity vs. Receive Diversity


frmLen = 100; % frame length
numPackets = 1000; % number of packets
EbNo = 0:2:20; % Eb/No varying to 20 dB
N = 2; % maximum number of Tx antennas
M = 2; % maximum number of Rx antennas
%and set up the simulation.
% Create a local random stream to be used by random number generators for
% repeatability.
hStr = RandStream('mt19937ar', 'Seed', 55408);
% Create BPSK mod-demod objects
P = 2; % modulation order
bpskmod = modem.pskmod('M', P, 'SymbolOrder', 'Gray');
bpskdemod = modem.pskdemod(bpskmod);
% Pre-allocate variables for speed
tx2 = zeros(frmLen, N); H = zeros(frmLen, N, M);
r21 = zeros(frmLen, 1); r12 = zeros(frmLen, 2);
z21 = zeros(frmLen, 1); z21_1 = zeros(frmLen/N, 1); z21_2 = z21_1;
z12 = zeros(frmLen, M);
error11 = zeros(1, numPackets); BER11 = zeros(1, length(EbNo));
error21 = error11; BER21 = BER11; error12 = error11; BER12 = BER11; BERthy2 = BER11;
% Set up a figure for visualizing BER results
h = gcf; grid on;
hold on;
set(gca, 'yscale', 'log', 'xlim', [EbNo(1), EbNo(end)], 'ylim', [1e-4 1]);
xlabel(' Eb/No(dB)'); ylabel('BER'); set(h,'NumberTitle','off');
set(h, 'renderer', 'zbuffer'); set(h,'Name','Transmit vs. Receive Diversity');
title('Transmit vs. Receive Diversity');
% Loop over several EbNo points
for idx = 1:length(EbNo)
% Loop over the number of packets
for packetIdx = 1:numPackets
data = randi(hStr, [0 P-1], frmLen, 1); % data vector per user
% per channel
tx = modulate(bpskmod, data); % BPSK modulation
% Alamouti Space-Time Block Encoder, G2, full rate
% G2 = [s1 s2; -s2* s1*]
s1 = tx(1:N:end); s2 = tx(2:N:end);
tx2(1:N:end, :) = [s1 s2];
tx2(2:N:end, :) = [-conj(s2) conj(s1)];
% Create the Rayleigh distributed channel response matrix
% for two transmit and two receive antennas
H(1:N:end, :, :) = (randn(hStr, frmLen/2, N, M) + ...
1i*randn(hStr, frmLen/2, N, M))/sqrt(2);
% assume held constant for 2 symbol periods
H(2:N:end, :, :) = H(1:N:end, :, :);
% Received signals
% for uncoded 1x1 system
r11 = awgn(H(:, 1, 1).*tx, EbNo(idx), 0, hStr);
% for G2-coded 2x1 system - with normalized Tx power, i.e., the
% total transmitted power is assumed constant
r21 = awgn(sum(H(:, :, 1).*tx2, 2)/sqrt(N), EbNo(idx), 0, hStr);
% for Maximal-ratio combined 1x2 system
for i = 1:M
r12(:, i) = awgn(H(:, 1, i).*tx, EbNo(idx), 0, hStr);
end
% Front-end Combiners - assume channel response known at Rx
% for G2-coded 2x1 system
hidx = 1:N:length(H);
z21_1 = r21(1:N:end).* conj(H(hidx, 1, 1)) + ...
conj(r21(2:N:end)).* H(hidx, 2, 1);
z21_2 = r21(1:N:end).* conj(H(hidx, 2, 1)) - ...
conj(r21(2:N:end)).* H(hidx, 1, 1);
z21(1:N:end) = z21_1; z21(2:N:end) = z21_2;
% for Maximal-ratio combined 1x2 system
for i = 1:M
z12(:, i) = r12(:, i).* conj(H(:, 1, i));
end
% ML Detector (minimum Euclidean distance)
demod11 = demodulate(bpskdemod, r11.*conj(H(:, 1, 1)));
demod21 = demodulate(bpskdemod, z21);
demod12 = demodulate(bpskdemod, sum(z12, 2));
% Determine errors
error11(packetIdx) = biterr(demod11, data);
error21(packetIdx) = biterr(demod21, data);
error12(packetIdx) = biterr(demod12, data);
end % end of FOR loop for numPackets
% Calculate BER for current idx
% for uncoded 1x1 system
BER11(idx) = sum(error11)/(numPackets*frmLen);
% for G2 coded 2x1 system
BER21(idx) = sum(error21)/(numPackets*frmLen);
% for Maximal-ratio combined 1x2 system
BER12(idx) = sum(error12)/(numPackets*frmLen);
% for theoretical performance of second-order diversity
BERthy2(idx) = berfading(EbNo(idx), 'psk', 2, 2);
% Plot results
semilogy(EbNo(1:idx), BER11(1:idx), 'r*', ...
EbNo(1:idx), BER21(1:idx), 'go', ...
EbNo(1:idx), BER12(1:idx), 'bs', ...
EbNo(1:idx), BERthy2(1:idx), 'm');
legend('No Diversity (1Tx, 1Rx)', 'Alamouti (2Tx, 1Rx)',...
'Maximal-Ratio Combining (1Tx, 2Rx)', ...
'Theoretical 2nd-Order Diversity');
drawnow;
end % end of for loop for EbNo
% Perform curve fitting and replot the results
fitBER11 = berfit(EbNo, BER11);
fitBER21 = berfit(EbNo, BER21);
fitBER12 = berfit(EbNo, BER12);
semilogy(EbNo, fitBER11, 'r', EbNo, fitBER21, 'g', EbNo, fitBER12, 'b');
hold off;
2. ORTHOGONAL Space-Time Block Coding

load ostbcRes.mat;
% Set up a figure for visualizing BER results
%clf(h); grid on; hold on; set(h, 'renderer', 'zbuffer');
set(gca, 'yscale', 'log', 'xlim', [EbNo(1), EbNo(end)], 'ylim', [1e-5 1]);
xlabel('Eb/No (dB)');
ylabel('BER');
%set(h,'Name','Orthogonal Space-Time Block Coding(2)');
title('MIMO 4x4 System and Other Comparisons');
hold on; grid on;
% Theoretical performance of fourth-order diversity for QPSK
BERthy4 = berfading(EbNo, 'psk', 4, 4);
% Plot results
semilogy(EbNo, ber11, 'r*', EbNo, ber41, 'ms', EbNo, ber22, 'c^', ...
EbNo, ber14, 'ko', EbNo, BERthy4, 'g');
legend('No Diversity (4Tx, 4Rx), QAM', 'OSTBC (4Tx, 4Rx), QAM', ...
'Alamouti (4Tx, 4Rx), QAM', 'Maximal-Ratio Combining (4Tx, 4Rx), QAM', ...
'Theoretical 4th-Order Diversity, QAM');
% Perform curve fitting
fitBER11 = berfit(EbNo, ber11);
fitBER41 = berfit(EbNo(1:9), ber41(1:9));
fitBER22 = berfit(EbNo(1:8), ber22(1:8));
fitBER14 = berfit(EbNo(1:7), ber14(1:7));
semilogy(EbNo, fitBER11, 'r', EbNo(1:9), fitBER41, 'm', ...
EbNo(1:8), fitBER22, 'c', EbNo(1:7), fitBER14, 'k'); hold off;
3 . COMPARISON OF BER vs. SNR (4-QAM) FOR AN OFDM

%OFDM
% close all
% clear all
% clc
tt = tic();
%%
% Initialisations
Single_frame_size = 96;
Total_no_of_Frames = 100;
scatter_plot_frame = 48;
x=1; %Frame distinction xx=1; %Scatter plot distinction
si=1; %for BER rows
pilt=3+3j; % Pilot complex insertion data
no_of_Carriers = 64; % Data to be transmitted without CP/CE
initial_test_snr = -2;
snr_step_size = 2;
final_test_snr = 50;
BER = zeros( Total_no_of_Frames ,floor( (final_test_snr+snr_step_size)/snr_step_size )); % To store
final Bit error rate results; here it is 100*26
no_of_pilots = 4;
no_of_data_without_carriers = (Single_frame_size*2/no_of_pilots) + no_of_pilots; % 52 in this case ;
derived from line "x"
cyclic_extension = 16;
choice_snr = 14; % choose a valid snr value to plot the result ---- see below
%%
% Generating data
t_data = floor(2*rand(1,Single_frame_size*Total_no_of_Frames));
t_rxed_data=zeros(2,Single_frame_size*Total_no_of_Frames);
% Time averaging for optimum results
ber = zeros(1,26);
BER = BER';
for col=1:26; %%%change if SNR loop Changed
ber(1,col)=ber(1,col)+sum(BER(col,:));
end
%ber=ber/(Single_frame_size*Total_no_of_Frames*log2(M)); % Normalisation
%%% RESULTS
%%
% Scatter Plot
% Create scatter plot of noisy signal and transmitted
% signal on the same axes. h = scatterplot(scatter_plot_requirement(2,:),1,0,'g.');
hold on;
%scatterplot(scatter_plot_requirement(1,:),1,0,'k*',h);
choice_snr = num2str(choice_snr);
title_print = strcat('received Signal of SNR = ',choice_snr);
title(title_print);
legend('Noisy received Signal','Signal Constellation');
axis([-5 5 -5 5 ]); % Set axis ranges.
hold on;%%
% Plot for comparison between original signal wrt processed rxed signal
figure;
space = 10;
t = 1:space:Single_frame_size*Total_no_of_Frames;
subplot(211);
y=[t_rxed_data(1,1:space:Single_frame_size*Total_no_of_Frames);t_data(1:space:Single_frame_size*To
tal_no_of_Frames)]';
h = stem(t,y);
set(h(1),'MarkerFaceColor','blue') ;
set(h(2),'MarkerFaceColor','G','Marker','square');
title('Blue --> Processed received Signal at SNR = 50 & Red --> Original generated data');
subplot(212);
y=[t_rxed_data(2,1:space:Single_frame_size*Total_no_of_Frames);t_data(1:space:Single_frame_size*To
tal_no_of_Frames)]';
h = stem(t,y);
set(h(1),'MarkerFaceColor','blue') ;
set(h(2),'MarkerFaceColor','red','Marker','square') ;
title_print = strcat('Blue --> Processed received Signal of SNR = ',choice_snr,' & Red --> Original
generated data');
title(title_print);%%
% BER vs SNR graph
figure;
i=0:2:16;
theoryBer = (1/4)*3/2*erfc(sqrt(4*0.1*(10.^(i/10))));
semilogy(i,theoryBer,'bs-','LineWidth',2);
hold on;
%semilogy(i,ber(1:9),'mx-','LineWidth',2);
title('BER vs SNR');
ylabel('Normalised BER');
xlabel('SNR (dB)');
grid on;
legend('OFDM technique', '-');
%%
toc(tt); % Processing time on my laptop was ~2 seconds
4. BER and SNR (4-QAM) for the proposed technique for MIMO-OFDM system

clear
N = 10^6; % number of bits or symbols
Eb_N0_dB = (0:25); % multiple Eb/N0 values
nTx = 2;
nRx = 2;
for ii = 1:length(Eb_N0_dB)
% Transmitter
ip = rand(1,N)>0.5; % generating 0,1 with equal probability
s = 2*ip-1; % BPSK modulation 0 -> -1; 1 -> 0
sMod = kron(s,ones(nRx,1)); %
sMod = reshape(sMod,[nRx,nTx,N/nTx]); % grouping in [nRx,nTx,N/NTx ] matrix
h = 1/sqrt(2)*(randn(nRx,nTx,N/nTx) + 1i*randn(nRx,nTx,N/nTx)); % Rayleigh channel
n = 1/sqrt(2)*(randn(nRx,N/nTx) + 1i*randn(nRx,N/nTx)); % white gaussian noise, 0dB variance
% Channel and noise Noise addition
y = squeeze(sum(h.*sMod,2)) + 10^(-Eb_N0_dB(ii)/20)*n;
% Receiver
% Forming the MMSE equalization matrix W = inv(H^H*H+sigma^2*I)*H^H
% H^H*H is of dimension [nTx x nTx]. In this case [2 x 2]
% Inverse of a [2x2] matrix [a b; c d] = 1/(ad-bc)[d -b;-c a]
hCof = zeros(2,2,N/nTx) ;
hCof(1,1,:) = sum(h(:,2,:).*conj(h(:,2,:)),1) + 10^(-Eb_N0_dB(ii)/10); % d term
hCof(2,2,:) = sum(h(:,1,:).*conj(h(:,1,:)),1) + 10^(-Eb_N0_dB(ii)/10); % a term
hCof(2,1,:) = -sum(h(:,2,:).*conj(h(:,1,:)),1); % c term
hCof(1,2,:) = -sum(h(:,1,:).*conj(h(:,2,:)),1); % b term
hDen = ((hCof(1,1,:).*hCof(2,2,:)) - (hCof(1,2,:).*hCof(2,1,:))); % ad-bc term
hDen = reshape(kron(reshape(hDen,1,N/nTx),ones(2,2)),2,2,N/nTx); % formatting for division
hInv = hCof./hDen; % inv(H^H*H)
hMod = reshape(conj(h),nRx,N); % H^H operation
yMod = kron(y,ones(1,2)); % formatting the received symbol for equalization
yMod = sum(hMod.*yMod,1); % H^H * y
yMod = kron(reshape(yMod,2,N/nTx),ones(1,2)); % formatting
yHat = sum(reshape(hInv,2,N).*yMod,1); % inv(H^H*H)*H^H*y
% receiver - hard decision decoding
ipHat = real(yHat)>0;
% counting the errors
% nErr(ii) = size(find(ip- ipHat),2);
end
%simBer = nErr/N; % simulated ber
EbN0Lin = 10.^(Eb_N0_dB/10);
theoryBer_nRx1 = 0.5.*(1-1*(1+1./EbN0Lin).^(-0.5));
p = 1/2 - 1/2*(1+1./EbN0Lin).^(-1/2);
theoryBerMRC_nRx2 = p.^2.*(1+2*(1-p));
close all
figure
semilogy(Eb_N0_dB,theoryBer_nRx1,'bp-','LineWidth',2);
hold on
semilogy(Eb_N0_dB,theoryBerMRC_nRx2,'kd-','LineWidth',2);
%semilogy(Eb_N0_dB,simBer,'mo-','LineWidth',2);
axis([0 25 10^-5 0.5])
grid on
legend(' 4-QAM system with model', ' effect of noise On 4-QAM system', 'sim (nTx=2, nRx=2, MMSE)');
xlabel('SNR,dB');
ylabel('Bit Error Rate');
title('BER vs SNR for QAM modulation with 2x2 MIMO-OFDM equalizer (Rayleigh channel)');
5. Linear Equalization for Frequency-selective Fading

simName = 'Linear equalization for frequency-selective fading';


%chan.PathDelays = [0 0.9 1.5]*Tsym; % Path delay vector (s)
%chan.AvgPathGaindB = [0 -3 -6]; % Average path gain vector (dB)
% Adaptive equalizer
nWeights = 8;
forgetFactor = 0.99; % RLS algorithm forgetting factor
alg = rls(forgetFactor); % RLS algorithm object
eqObj = lineareq(nWeights, alg, pskmodObj.Constellation); % Equalizer object
eqObj.RefTap = 3; % Reference tap
% Link simulation. Store BER values.
BERvect = zeros(1,nBlocks); % Preallocate storage
for block = 1:nBlocks, commadapteqloop; BERvect(block) = BER; end
avgBER2 = mean(BERvect); % Average BER over transmission blocks
6. Linear Equalization for Frequency-flat Fading

simName = 'Linear equalization for frequency-flat fading';


fd = 30; % Maximum Doppler shift (Hz)
%chan = rayleighchan(Tsamp, fd); % Create channel object.
chan.ResetBeforeFiltering = 0; % Allow state retention across blocks.
% Adaptive equalizer
nWeights = 1; % Single weight
stepSize = 0.1; % Step size for LMS algorithm
alg = lms(stepSize); % Adaptive algorithm object
% Link simulation
nBlocks = 50; % Number of transmission blocks in simulation
for block = 1:nBlocks, commadapteqloop; end % Run link simulation in
a loop

You might also like