function rxWaveform = vhtSigRecGenerateWaveform(cfgVHTTx,numRx, ...
    delayProfile,noisePower,cfo,numTxPkt,idleTime)
%vhtSigRecGenerateWaveform Featured example helper function
%
%   Generates an impaired multi-packet VHT waveform

%   Copyright 2015-2019 The MathWorks, Inc.

rngState = rng(120); % Set random state

% Create the MAC frame configuration object
macCfg = wlanMACFrameConfig('FrameType','QoS Data','FrameFormat','VHT');

% Calculate the MSDU lengths required to generate an A-MPDU of given
% length.
msduLengths = wlanMSDULengths(cfgVHTTx.APEPLength,macCfg,cfgVHTTx);

% MSDUs (payload) required to generate the A-MPDU
msdu = cell(1,numel(msduLengths));
for i = 1:numel(msdu)
    msdu{i} = randi([0 255],msduLengths(i),1);
end

% Generate a VHT format MAC frame with the given APEP length
[psdu,cfgVHTTx.APEPLength] = wlanMACFrame(msdu,macCfg,cfgVHTTx,'OutputFormat','bits');

% Generate multi-packet waveform with PSDU repeatedly applied. With each
% packet containing the same PSDU, this enables us to perform packet error
% checking after recovering the packets.
txWave = wlanWaveformGenerator(psdu,cfgVHTTx, ...
    'NumPackets',numTxPkt,'IdleTime',idleTime);

% Pass through a TGac fading channel with leading zeros
sr = wlanSampleRate(cfgVHTTx);
tgnChannel = wlanTGacChannel( ...
    'SampleRate',             sr, ...
    'DelayProfile',           delayProfile, ...             
    'ChannelBandwidth',       cfgVHTTx.ChannelBandwidth, ...
    'NumTransmitAntennas',    cfgVHTTx.NumTransmitAntennas, ...
    'NumReceiveAntennas',     numRx, ...
    'LargeScaleFadingEffect', 'None'); % No path loss or shadowing
chanOut = tgnChannel( ...
    [zeros(round(idleTime*sr),cfgVHTTx.NumTransmitAntennas); txWave]);

% Add carrier frequency offset
chanOut = helperFrequencyOffset(chanOut,sr,cfo);

% Add noise
awgnChannel = comm.AWGNChannel( ...
    'NoiseMethod', 'Variance', ...
    'Variance',    10^(noisePower/10));
rxWaveform = awgnChannel(chanOut);
rng(rngState); % Restore random state

end

