function [payload,packetDuration,cfg] = helperBRModulationTestPacketConfig(txTestID,packetType,sps)
%helperBRModulationTestPacketConfig Returns packet parameters
%   [PAYLOAD,PACKETDURATION,CFG] = helperBRModulationTestPacketConfig(...
%   TXTESTID,PACKETTYPE,SPS) returns the payload, PAYLOAD, packet duration,
%   PACKETDURATION, and waveform configuration, CFG.
%
%   PAYLOAD represents the test sequence(s) based on the transmitter test
%   case ID and the length of payload depends on the test and packet type.
%
%   PACKETDURATION denotes the duration of a Bluetooth packet in
%   microseconds, which corresponding to the number of packet symbols.
%
%   CFG is a format configuration object of type <a
%   href="matlab:help('bluetoothWaveformConfig')">bluetoothWaveformConfig</a>
%   used to configure the Bluetooth waveform generator.
% 
%   TXTESTID is a character vector or string scalar specifying the
%   transmitter test ID. It must be one of these {'RF/TRM/CA/BV-07-C',
%   'RF/TRM/CA/BV-08-C', 'RF/TRM/CA/BV-09-C'}.
%
%   PACKETTYPE denotes the packet type used for transmitter tests. It must
%   be one of these {'DH1','DH3','DH5','DM1',DM3','DM5'}.
%
%   SPS denotes the number of samples per symbol.

%   Copyright 2020 The MathWorks, Inc.

% Consider maximum payload length for the selected packet type as per
% Bluetooth RF-PHY Test Specifications.
if strcmp(packetType,'DH1')
    payloadLength = 27;
elseif strcmp(packetType,'DH3')
    payloadLength = 183;
elseif strcmp(packetType,'DH5')
    payloadLength = 339;
elseif strcmp(packetType,'DM1')
    payloadLength = 17;
elseif strcmp(packetType,'DM3')
    payloadLength = 121;
else
    payloadLength = 224;
end

% Create and configure Bluetooth waveform configuration object
cfg = bluetoothWaveformConfig('Mode','BR','PacketType',packetType,...
                             'PayloadLength',payloadLength,'WhitenStatus',...
                             'Off','SamplesPerSymbol',sps);

% Generate payload based on the transmitter test
if strcmp(txTestID,'RF/TRM/CA/BV-07-C')
    payloadPattern1 = [1;1;1;1;0;0;0;0];
    payload1 = repmat(payloadPattern1,cfg.PayloadLength,1);
    payloadPattern2 = [1;0;1;0;1;0;1;0];
    payload2 = repmat(payloadPattern2,cfg.PayloadLength,1);
    payload = [payload1 payload2];
elseif strcmp(txTestID,'RF/TRM/CA/BV-08-C')
    polynomial = 'z^9+z5+1';
    initialConditions = ones(1,9);
    pnSeq = comm.PNSequence('Polynomial',polynomial,...
        'InitialConditions',initialConditions,'SamplesPerFrame',cfg.PayloadLength*8);
    payload = pnSeq();
else %RF/TRM/CA/BV-09-C
    payloadPattern = [1;0;1;0;1;0;1;0];
    payload = repmat(payloadPattern,cfg.PayloadLength,1);
end

% Derive Bluetooth packet duration corresponding to the generated Bluetooth
% symbols.
packetDuration = helperBluetoothPacketDuration(packetType,'BR',payloadLength);
end