N = 8; % period of the DFT
s = randn(1,8);
h = randn(1,3);
stem(s,'r'); title('signal');
stem(h); title('channel')

lin_s_h = conv(h,s) %linear convolution of h and s
cir_s_h = cconv(h,s,N) %circular convolution of h and s with period N

stem(lin_s_h); title('Linear convolution')

stem(cir_s_h); title('Circular convolution')

Ncp = 2; %number of symbols to copy and paste for CP
s_cp = [s(end-Ncp+1:end) s]; % copy last Ncp syms from s, add as prefix
stem(s,'r'); title('Signal')
stem(s_cp); title('CP + Signal')

lin_scp_h=conv(h,s_cp) % linear conv. of CP-OFDM symbol s_cp and CIR h
stem(lin_scp_h); title('Linear convolution')

r = lin_scp_h(Ncp+1:N+Ncp) % cut from index Ncp+1 to N+Ncp
stem(r,'g'); title('Received signal')

% Verifying DFT property
R = fft(r,N); %frequency response of received signal
H = fft(h,N); %frequency response of CIR
S = fft(s,N); %frequency response of OFDM signal (non CP)

r1 = ifft(S.*H); %IFFT of product of individual DFTs

stem(r1);

display(['IFFT(DFT(H)*DFT(S)) : ',num2str(r1)])
display(['cconv(s,h) : ', num2str(r)])