%% Extract Resources From 3D Receive Grid and 4D Channel Estimate Grid
% Extract resources from a 3D receive grid and 4D channel estimate grid. Show the location of the indices within the grid.
% Setup sizes of the grids: [M N P] and [M N NRxAnts P], where M is the number of subcarriers, N is the number of OFDM symbols, NRxAnts is the number of rx antennas, and P is the number of tx antennas.

M = 4;
N = 4;
P = 2;
NRxAnts = 3;

%
% Create indices and show the locations within the transmit grid addressed by these indices. As you will notice, different resource elements are addressed on each antenna port. Addressed resource element locations contain 1.

ind = [6 22; 16 29];
txGrid = zeros(M,N,P);
txGrid(ind) = 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 a 3D received grid to extract resource elements. Extract resource elements from the received grid. Show the locations of these extracted resource elements. Addressed resource element locations contain 1.

rxGrid = zeros(M,N,NRxAnts);

[re, indOut] = lteExtractResources(ind,rxGrid);
rxGrid(indOut) = 1;

%
% Visualize locations of indexed resource elements 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 a 4D channel estimate grid to extract resource elements. Extract resource elements from the channel estimate grid. Show the locations of these extracted resource elements. Addressed resource element locations contain 1.

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

[re, indOut] = lteExtractResources(ind,hEstGrid);
hEstGrid(indOut) = 1;

%
% Visualize locations of the resource elements extracted using 'allplanes' mode from 3D receive grid.

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 a 4D channel estimate grid to extract resource elements. Extract resource elements from the channel estimate grid using 'direct' extraction mode. Show the locations of these extracted resource elements. Addressed resource element locations contain 1.

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

[re, indOut] = lteExtractResources(ind,hEstGridDirect,'direct');
hEstGridDirect(indOut) = 1;

%
% Visualize locations of the resource elements extracted using 'direct' mode from 4D channel estimate grid.

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.