fig = figure;
grid on;
hold on;
ax = fig.CurrentAxes;
ax.YScale = 'log';
xlim([EbNo(1), EbNo(end)]);
ylim([1e-4 1]);
xlabel('Eb/No (dB)');
ylabel('BER');
fig.NumberTitle = 'off';
fig.Renderer = 'zbuffer';
fig.Name = 'Transmit vs. Receive Diversity';
title('Transmit vs. Receive Diversity');
set(fig,'DefaultLegendAutoUpdate','off');
% Loop over several EbNo points
for idx = 1:length(EbNo)
reset(errorCalc1);
reset(errorCalc2);
reset(errorCalc3);
% Set the EbNo property of the AWGNChannel System objects
awgn1Rx.EbNo = EbNo(idx);
awgn2Rx.EbNo = EbNo(idx);
% Loop over the number of packets
for packetIdx = 1:numPackets
% Generate data vector per frame
data = randi([0 P-1], frmLen, 1);
% Modulate data
modData = bpskMod(data);
% Alamouti Space-Time Block Encoder
encData = ostbcEnc(modData);
% Create the Rayleigh distributed channel response matrix
% for two transmit and two receive antennas
H(1:N:end, :, :) = (randn(frmLen/2, N, M) + ...
1i*randn(frmLen/2, N, M))/sqrt(2);
% assume held constant for 2 symbol periods
H(2:N:end, :, :) = H(1:N:end, :, :);
% Extract part of H to represent the 1x1, 2x1 and 1x2 channels
H11 = H(:,1,1);
H21 = H(:,:,1)/sqrt(2);
H12 = squeeze(H(:,1,:));
% Pass through the channels
chanOut11 = H11 .* modData;
chanOut21 = sum(H21.* encData, 2);
chanOut12 = H12 .* repmat(modData, 1, 2);
% Add AWGN
rxSig11 = awgn1Rx(chanOut11);
rxSig21 = awgn1Rx(chanOut21);
rxSig12 = awgn2Rx(chanOut12);
% Alamouti Space-Time Block Combiner
decData = ostbcComb(rxSig21, H21);
% ML Detector (minimum Euclidean distance)
demod11 = bpskDemod(rxSig11.*conj(H11));
demod21 = bpskDemod(decData);
demod12 = bpskDemod(sum(rxSig12.*conj(H12), 2));
% Calculate and update BER for current EbNo value
% for uncoded 1x1 system
ber_noDiver(:,idx) = errorCalc1(data, demod11);
% for Alamouti coded 2x1 system
ber_Alamouti(:,idx) = errorCalc2(data, demod21);
% for Maximal-ratio combined 1x2 system
ber_MaxRatio(:,idx) = errorCalc3(data, demod12);
end % end of FOR loop for numPackets
% Calculate theoretical second-order diversity BER for current EbNo
ber_thy2(idx) = berfading(EbNo(idx), 'psk', 2, 2);
% Plot results
semilogy(EbNo(1:idx), ber_noDiver(1,1:idx), 'r*', ...
EbNo(1:idx), ber_Alamouti(1,1:idx), 'go', ...
EbNo(1:idx), ber_MaxRatio(1,1:idx), 'bs', ...
EbNo(1:idx), ber_thy2(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, ber_noDiver(1,:));
fitBER21 = berfit(EbNo, ber_Alamouti(1,:));
fitBER12 = berfit(EbNo, ber_MaxRatio(1,:));
semilogy(EbNo, fitBER11, 'r', EbNo, fitBER21, 'g', EbNo, fitBER12, 'b');
hold off;
% Restore default stream
RandStream.setGlobalStream(prevStream);