
%%   Initializing the Tx
% Giving input bits
dataBits = txDiagnostics.dataBits;
% Taking default value for 'txParam' if it is not provided
if(~exist('txParam','var'))
    % If 'txParam' doesn't exist
    txParam = struct('modOrder',4, 'codeRateIndex',0,'numFrames',5,'txDataBits',randi([0 1],(1882*5),1)); 
elseif(~isstruct(txParam))
    % If 'txParam' exists but is not a structure
    txParam = struct('modOrder',4, 'codeRateIndex',0,'numFrames',5,'txDataBits',randi([0 1],(1882*5),1));
end


% Filling default values for any missing or empty fields in 'txParam'
for ii = 1:length(txParam)
    strct = txParam(ii);
    if(~isfield(strct,'modOrder'))
        txParam(ii).modOrder = 4; 
    elseif(isempty(strct.modOrder))
        txParam(ii).modOrder = 4;
    end
    if(~isfield(strct,'codeRateIndex'))       
        txParam(ii).codeRateIndex = 0;
    elseif(isempty(strct.codeRateIndex))
        txParam(ii).codeRateIndex = 0;
    end
    if(~isfield(strct,'numFrames'))
        txParam(ii).numFrames = 5;
    elseif(isempty(strct.numFrames))
        txParam(ii).numFrames = 5;
    end
end

% Calculating total number of frames given through 'txParam' structure
nFrames = 0;
for ii = 1:length(txParam)
        nFrames = nFrames + txParam(ii).numFrames;
end

% 'packetValidIn' is formed based on 'packetValid' and is used in the model. 
if(~exist('packetValid','var'))
    % Taking default 'packetValidIn' if 'packetValid' is not provided
    packetValidIn = ones(nFrames,1);
else
    temp1 = find(packetValid);
    if(isempty(temp1))
        %If packetValid consists only zeros
        packetValidIn = 0;
    else
        if(length(temp1) > nFrames)
        % Taking packetValid only till 'nFrames' number of frames and 
        % discarding any extra packetValids
            temp2         = temp1(nFrames);
            packetValidIn = packetValid(1:temp2);
        else
            temp3         = temp1(end);
            packetValidIn = packetValid(1:temp3);
        end
    end
end


% Collecting modOrder and codeRateIndex from 'txParam' structure 
modOrder      = [];     % modulation order of data
codeRateIndex = [];     % code rate index of data
for ii = 1:length(txParam)                            
    modOrder      = [modOrder; repmat(txParam(ii).modOrder,txParam(ii).numFrames,1)]; 
    codeRateIndex = [codeRateIndex; repmat(txParam(ii).codeRateIndex,txParam(ii).numFrames,1)];  %#ok<*AGROW> 
end

txFs = 1.92e6; % Transmitter output sample rate

%% Initializing AWGN
whdlexamples.hdlawgn_init;

%% Initializing Rx
CFO=0;
subCarrierSpacing = 15e3;
maxFFT = 128;
Fs = maxFFT * subCarrierSpacing;
FrameDuration = 3e-3;
stopTime =  675/Fs + (1 + 1/36 + (txParam.numFrames+2)) * FrameDuration;

Config = whdlexamples.OFDMRxParameters();

impairmentControl = Config.impairmentControl;
headerConstellationBus = Config.headerConstellationBus;
dataConstellationBus = Config.dataConstellationBus;
constellationBus = Config.constellationBus;
diagBus = Config.diagBus;
samplecontrolbus

clear Config;
