% Simple OFDM Dem
%
% shows OFDM modulation and demodulation with multi-path and cyclic prefix

h = [1j, 1, -1];
Lcp = 2;

%% Modulation
% Two OFDM blocks with 4 BPSK symbols each
disp('Before IFFT:')
B= [ 1,  1, -1, -1;
     1, -1,  1, -1]
 
disp('after IFFT:')
b = ifft(B, 4, 2)

disp('after cyclic prefix:')
b = [b(:, end-Lcp+1:end), b]   % copy last Lcp symbols to front

disp('after P/S conversion (transmitted signal):')
tx = reshape(b.', 1, [])

%% multi-path channel - no noise
rx = conv(tx, h);

disp('received signal:')
rx = rx(1:length(tx))         % discard convolution transient at end

%% Demodulation
disp('after S/P conversion')
r = reshape(rx, [], 2).'

disp('after removing cyclic prefix:')
r(:,1:Lcp) = []

disp('after FFT:')
R = fft(r, 4, 2)

% Frequency response of channel for equalization
disp('Frequency response of channel:')
H = fft(h, 4)

disp('after frequency domain equalization:')
E = R./(ones(2,1)*H)
