% Program:
% Simple OFDM system (send 8 bits)
% Fire Wada Lab
clear all;
num_symbol = 100;   % number of symbols
n_symbol = 4;       % points in symbol 
M = 4;              %  size of signal constellation
modqpsk= [1+i, -1+i, 1-i, -1-i];


%% 1 . create random data
data = floor(rand(n_symbol,num_symbol)*M);


%% 2. mapping into I-Q constellation
data_1 = modqpsk(1+data);

subplot(2,2,1);
plot(data_1,'r.');
axis([-3 3 -3 3])
title('data constellation')

data_2 = data_1;

%% 3. IFFT 
data_3 = ifft(data_2);
subplot(2,2,2);
plot((real(data_3)),'-');
title('IFFT');

%%4. GI add
data_4 = [data_3(n_symbol,:);data_3];

%%4.1 Add Noise
sigpower=mean(mean(abs(data_4).^2));
sn= 20; %% 10dB
awgn = (randn(n_symbol+1,num_symbol)+i*randn(n_symbol+1,num_symbol));
awgnpower=mean(mean(abs(awgn).^2));
awgn = awgn/sqrt(awgnpower)*10^(-sn/20)*sqrt(sigpower);
data_4=data_4+awgn;

subplot(2,2,3);
plot(real(data_4),'-');
title('GI add');

%% 5. GI remove
data_5 = data_4(2:n_symbol+1,:);


%% 6. FFT
data_6 = fft(data_5);


subplot(2,2,4);
plot(data_6,'b.');
axis([-3 3 -3 3])
title('receive data constellation')
