%% Recover VHT-SIG-B in 2x2 MIMO Channel
% Recover VHT-SIG-B in a 2x2 MIMO channel for an SNR=10 dB and a receiver that has a 9 dB noise figure. Confirm that the information bits are recovered correctly.
% Set the channel bandwidth and the corresponding sample rate.

cbw = 'CBW20';
fs = 20e6;

%
% Create a VHT configuration object with 20 MHz bandwidth and two transmission paths. Generate the L-LTF and VHT-SIG-B waveforms.

vht = wlanVHTConfig('ChannelBandwidth',cbw,'NumTransmitAntennas',2, ...
    'NumSpaceTimeStreams',2);

txVHTLTF = wlanVHTLTF(vht);
[txVHTSIGB,txVHTSIGBBits] = wlanVHTSIGB(vht);

%
% Pass the VHT-LTF and VHT-SIG-B waveforms through a 2x2 TGac channel.

tgacChan = wlanTGacChannel('NumTransmitAntennas',2, ...
    'NumReceiveAntennas',2, 'ChannelBandwidth',cbw,'SampleRate',fs);
rxVHTLTF = tgacChan(txVHTLTF);
rxVHTSIGB = tgacChan(txVHTSIGB);

%
% Add white noise for an SNR = 10dB.

chNoise = comm.AWGNChannel('NoiseMethod','Signal to noise ratio (SNR)',...
    'SNR',10);

rxVHTLTF = chNoise(rxVHTLTF);
rxVHTSIGB = chNoise(rxVHTSIGB);

%
% Add additional white noise corresponding to a receiver with a 9 dB noise figure. The noise variance is equal to k*T*B*F, where k is Boltzmann's constant, T is the ambient temperature, B is the channel bandwidth (sample rate), and F is the receiver noise figure.

nVar = 10^((-228.6+10*log10(290)+10*log10(fs)+9)/10);
rxNoise = comm.AWGNChannel('NoiseMethod','Variance','Variance',nVar);

rxVHTLTF = rxNoise(rxVHTLTF);
rxVHTSIGB = rxNoise(rxVHTSIGB);

%
% Demodulate the VHT-LTF signal and use it to generate a channel estimate.

demodVHTLTF = wlanVHTLTFDemodulate(rxVHTLTF,vht);
chEst = wlanVHTLTFChannelEstimate(demodVHTLTF,vht);

%
% Recover the VHT-SIG-B information bits. Display the scatter plot of the equalized symbols.

[recVHTSIGBBits,eqSym,cpe] = wlanVHTSIGBRecover(rxVHTSIGB,chEst,nVar,cbw);
scatterplot(eqSym)

%
% Display the common phase error.

cpe

%
% Determine the number of errors between the transmitted and received VHT-SIG-B information bits.

numErr = biterr(txVHTSIGBBits,recVHTSIGBBits)

% Copyright 2012 The MathWorks, Inc.