//Program:
//Simple OFDM system (send 8 bits)
//Fire Wada Lab
//
//Usage:
//Execute-> Load into Scilab (or Ctrl L)
//In Scilab Window type: 
//qpsk_ofdm

function qpsk_ofdm;
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
rand("uniform");
data = floor(rand(n_symbol,num_symbol)*M);
//data = [0 1 2 3];


//2. mapping into I-Q constellation
data_1 = matrix(modqpsk(1+data),n_symbol,num_symbol);

subplot(2,2,1);
a = get("current_axes");
a.data_bounds = [-3,-3;3,3];
xtitle("I-Q constellation","I", "Q");
plot(real(data_1),imag(data_1),'r.');


//serial to parallel
data_2 = data_1;


//3. IFFT 
data_3 = ifft(data_2);
subplot(2,2,2);
a = get("current_axes");
a.data_bounds = [0,0;7,2];
xtitle("IFFT");
plot((abs(data_3)),'ob-');


//4. GI add
data_4 = [data_3($,:);data_3] ;

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

subplot(2,2,3);
a = get("current_axes");
a.data_bounds = [0,0;7,2];
xtitle("GI add");
plot(abs(data_4),'*cya--');


//5. GI remove
data_5 = data_4(2:$,:);


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


//Parallel to serial
data_7 = data_6;

subplot(2,2,4);
a = get("current_axes");
xtitle("The result after FFT");
a.data_bounds = [-3,-3;3,3];
plot(real(data_7),imag(data_7),'r.');

endfunction
