
%code for optisystem
OutputPort1 = InputPort1;  
if(InputPort1.TypeSignal == 'Electrical')
   
cs = length(InputPort1.Sampled.Signal);


end
%%%%%%%%%%%%%

subcarrier = 512;
symbol =  512;
qam_size =4;
no_of_zero= 256;
SNR_dB=10;   %in case of noisy channel
p = 0;%length of cyclic prefix                           
bit = log2(qam_size);

effective_subcarrier= subcarrier-no_of_zero;

total_bits_needed = effective_subcarrier*bit*symbol;

x = randi([0 1],total_bits_needed,1);  % Random binary data stream 

    
k=series2parallel(x,effective_subcarrier*bit) ;     % series to parrallel converter 

hMod = modem.qammod(qam_size) ;            % Create a QAM modulator 
hMod.InputType = 'Bit';                    % Accept bits as inputs 
hMod.SymbolOrder = 'Gray';

tx = modulate(hMod,k)  ;                  % QAM Modulation



te=tx;

for col=1:1:symbol                        %zero padding
    for row=effective_subcarrier+1:1:effective_subcarrier+no_of_zero;
        te(row,col)=0;
    end
end

result_after_zero_padding=te;                %result after zero padding



mi=mirror(te);                          % creating complex conjugate 


c=ifft(mi);                                % performing IFFT and results in real valued OFDM signal 

w=cyclicpad(c,p);                          % adding cyclic prefix


p2s=parallel2series(w) ;          % parallel to serial converter

fs=2000000000;                          %performing PSD


df=fs/length(p2s);
f=-(fs/2) :df: (fs/2)-df;
psd=fftshift(abs(fft(p2s)).^2);

figure(2)
semilogy(f, psd, 'b')



title 'PSD in transmitter'
xlabel 'frequency'
ylabel 'power specrtral density in dB'



OutputPort1.Sampled.Signal = p2s;

length(p2s);

for nm = 1 : cs
              p2s2(1,nm) = p2s(nm,1);
end


OutputPort1.Sampled.Signal = p2s2;

