%% Extract REs from 3D and 4D grids
%% Setup sizes of the grids

M = 4;       % Subcarriers
N = 4;       % OFDM symbols
P = 2;       % Antenna ports
NRxAnts = 3; % Receive antennas

%
%% Create indices and show the locations within the grid addressed by the indices

ind = [6 22; 16 29];
txGrid = zeros(M,N,P);
txGrid(ind) = 1; % Addressed resource element locations contain 1

%
%% Visualize locations of indexed resource elements in the transmit grid

visualizeGrid = zeros(M+1,N+1,P);
visualizeGrid(1:M,1:N,:) = txGrid;
figure
subplot(321)
pcolor(visualizeGrid(:,:,1))
title('Port: 1')
xlabel('N')
ylabel('M')

subplot(323)
pcolor(visualizeGrid(:,:,2))
title('Port: 2')
xlabel('N')
ylabel('M')

%
%% Create 3D receive grid to extract from

rxGrid = zeros(M,N,NRxAnts);

%
%% Extract resource elements from received grid and show locations of extracted resource elements

[re, indOut] = lteExtractResources(ind,rxGrid);
rxGrid(indOut) = 1; % Addressed resource element locations contain 1

%
%% Visualize locations of extracted resource elements from in the receive grid

figure
visualizeGrid = zeros(M+1,N+1,NRxAnts);
visualizeGrid(1:M,1:N,:) = rxGrid; 

subplot(321)
pcolor(visualizeGrid(:,:,1))
title('Allplanes, RxAnt: 1')
xlabel('N')
ylabel('M')

subplot(323)
pcolor(visualizeGrid(:,:,2))
title('Allplanes, RxAnt: 2')
xlabel('N')
ylabel('M')

subplot(325)
pcolor(visualizeGrid(:,:,3))
title('Allplanes, RxAnt: 3')
xlabel('N')
ylabel('M')

%
%% Create 4D channel estimate grid to extract from

hEstGrid = zeros(M,N,NRxAnts,P);

%
%% Extract resource elements from channel estimate grid and show locations of extracted resource elements

[re, indOut] = lteExtractResources(ind,hEstGrid);
hEstGrid(indOut) = 1; % Addressed resource element locations contain 1

%
%% Visualize locations of the extracted resource elements from 3D receive grid in allplanes extraction mode

figure
visualizeGrid = zeros(M+1,N+1,NRxAnts,P);
visualizeGrid(1:M,1:N,:,:) = hEstGrid;

subplot(321)
pcolor(visualizeGrid(:,:,1,1))
title('Allplanes, RxAnt: 1, Port: 1')
xlabel('N')
ylabel('M')

subplot(323)
pcolor(visualizeGrid(:,:,2,1))
title('Allplanes, RxAnt: 2, Port: 1')
xlabel('N')
ylabel('M')

subplot(325)
pcolor(visualizeGrid(:,:,3,1))
title('Allplanes, RxAnt: 3, Port: 1')
xlabel('N')
ylabel('M')

subplot(322)
pcolor(visualizeGrid(:,:,1,2))
title('Allplanes, RxAnt: 1, Port: 2')
xlabel('N')
ylabel('M')

subplot(324)
pcolor(visualizeGrid(:,:,2,2))
title('Allplanes, RxAnt: 2, Port: 2')
xlabel('N')
ylabel('M')

subplot(326)
pcolor(visualizeGrid(:,:,3,2))
title('Allplanes, RxAnt: 3, Port: 2')
xlabel('N')
ylabel('M')

%
%% Create 4D channel estimate grid to extract from with the direct mode

hEstGridDirect = zeros(M,N,NRxAnts,P);

%
%% Extract 4D resource elements from channel estimate grid and show locations of extracted resource elements

[re, indOut] = lteExtractResources(ind,hEstGridDirect,'direct');   
hEstGridDirect(indOut) = 1;% Addressed resource element locations contain 1

%
%% Visualize locations of the extracted resource elements from hEstGridDirect in direct extraction mode

figure
visualizeGrid = zeros(M+1,N+1,NRxAnts,P);
visualizeGrid(1:M,1:N,:,:) = hEstGridDirect;

subplot(321)
pcolor(visualizeGrid(:,:,1,1))
title('Direct, RxAnt: 1, Port: 1')
xlabel('N')
ylabel('M')

subplot(323)
pcolor(visualizeGrid(:,:,2,1))
title('Direct, RxAnt: 1, Port: 1')
xlabel('N')
ylabel('M')

subplot(325)
pcolor(visualizeGrid(:,:,3,1))
title('Direct, RxAnt: 1, Port: 1')
xlabel('N')
ylabel('M')

subplot(322)
pcolor(visualizeGrid(:,:,1,2))
title('Direct, RxAnt: 1, Port: 1')
xlabel('N')
ylabel('M')

subplot(324)
pcolor(visualizeGrid(:,:,2,2))
title('Direct, RxAnt: 1, Port: 1')
xlabel('N')
ylabel('M')

subplot(326)
pcolor(visualizeGrid(:,:,3,2))
title('Direct, RxAnt: 1, Port: 1')
xlabel('N')
ylabel('M')

% Copyright 2012 The MathWorks, Inc.