%% Recover Bandwidth Signaling from Initial Scrambler State
% Configure and generate a non-HT Data signal with a channel bandwidth of 160 MHz and dynamic bandwidth operation.

bandwidth = 'CBW160';
cfg = wlanNonHTConfig('ChannelBandwidth',bandwidth,'PSDULength',1, ...
    'SignalChannelBandwidth',true,'BandwidthOperation','Dynamic');
bits = randi([0 1],8*cfg.PSDULength,1,'int8');
[range,~] = scramblerRange(cfg);
scramInit = randi(range);
y = wlanNonHTData(bits,cfg,scramInit);

% Transmit the waveform through an AWGN channel with an SNR of 50.

snr = 50;
noiseVarEst = 10^(-snr/10);
rx = awgn(y,snr);

% Recover the frequency-domain signal by OFDM demodulating the non-HT Data signal, specifying an OFDM symbol sampling offset.

field = 'NonHT-Data';
symOffset = 0.5;
sym = wlanNonHTOFDMDemodulate(rx,field,bandwidth,'OFDMSymbolOffset',symOffset);

% Extract the data subcarriers.

info = wlanNonHTOFDMInfo(field,bandwidth);
sym = sym(info.DataIndices,:);

% Recover the first 20 MHz subchannel of the PSDU, enhancing the demapping of the OFDM subcarriers by specifying channel state information. Confirm that the received and transmitted PSDUs match.

csi = ones(48,1);
[psdu,scramInit] = wlanNonHTDataBitRecover(sym(1:48,:),noiseVarEst,csi,cfg);
isequal(bits,psdu)

% Recover and display bandwidth signaling by interpreting the scrambler state.

[bandwidth,dyn] = wlanInterpretScramblerState(scramInit)

% Copyright 2020 The MathWorks, Inc.