%% Search EPDCCH for DCI Messages
% Encode a DCI message and modulate it on the EPDCCH. Perform EPDCCH decoding and then EPDCCH blind-search to recover the DCI message. For DCI messages sent on the EPDCCH, set the ControlChannelType to 'EPDCCH'.
% Initialize cell-wide settings structure and EPDCCH transmission channel structure.

enb = lteRMCDL('R.43');
chs.RNTI = 42;
chs.ControlChannelType = 'EPDCCH';
chs.EPDCCHType = 'Localized';
chs.EPDCCHPRBSet = [2 3];
chs.EPDCCHNID = 0;
chs.EPDCCHFormat = 1;
chs.DCIFormat = 'Format1A';

%
% Create a DCI message. Generate EPDCCH candidates.

[dci,dciBits] = lteDCI(enb,chs,struct('DCIFormat',chs.DCIFormat));

candidates = lteEPDCCHSpace(enb,chs);
chs.EPDCCHECCE = candidates(1,:);

%
% Generate RE grid indices and EPDCCH info structure. Encode the DCI message into a codeword for transmission. Generate EPDCCH symbols and populate resource grid.

[ind,info] = lteEPDCCHIndices(enb,chs);
cw = lteDCIEncode(chs,dciBits,info.EPDCCHG);

sym = lteEPDCCH(enb,chs,cw);
grid = lteDLResourceGrid(enb,4);
grid(ind) = sym;

%
% Decode the EPDCCH transmission. Recover and view DCI message.

rxsoftbits = lteEPDCCHDecode(enb,chs,grid);

rxdci = lteEPDCCHSearch(enb,chs,rxsoftbits);
rxdci{1}

% Search for multiple EPDCCH sets. The first EPDCCH set is as configured above and the second is of Distributed type with 8 PRBs.
%
% Transmit the EPDCCH DM-RS for channel estimation.

grid(lteEPDCCHDMRSIndices(enb,chs)) = lteEPDCCHDMRS(enb,chs);

%
% Configure the channel estimation.

cec.PilotAverage = 'TestEVM';
cec.Reference = 'EPDCCHDMRS';

%
% Configure two EPDCCH sets.

chs.EPDCCHTypeList = {'Localized' 'Distributed'};
chs.EPDCCHPRBSetList = {[2; 3] (8:15).'};

%
% Perform the EPDCCH search for each set.

for p = 1:numel(chs.EPDCCHTypeList)
    chs.EPDCCHType = chs.EPDCCHTypeList{p};
    chs.EPDCCHPRBSet = chs.EPDCCHPRBSetList{p};
    [hestgrid,noiseest] = lteDLChannelEstimate(enb,chs,cec,grid);
    rxsoftbits = lteEPDCCHDecode(enb,chs,grid,hestgrid,noiseest);
    rxdci = lteEPDCCHSearch(enb,chs,rxsoftbits);
    X = ['EPDCCH set ' num2str(p)];
    disp([X ', DCI messages found: ' num2str(numel(rxdci))])
    if (~isempty(rxdci))
        rxdci{1}
    end
end

% A DCI message is found in EPDCCH set 1 but not in EPDCCH set 2.
% Copyright 2012 The MathWorks, Inc.