function [ s_n,simbolos,PAPR ] = transmisor(SYS)

%=========================================================================
% Simula un transmisor OFDMA con esquemas de modulación
% M-QAM o Q-PSK.
%=========================================================================


Bits = randi([0 1],SYS.Nbits,1);                            % Fuente de datos binaria

% Seleccionar entre modulador M-QAM o QPSK %

if (strcmp(SYS.modulacion,'QAM'))
    modulador = modem.qammod('M',SYS.M,'SymbolOrder','Gray','InputType','Bit');
else
    modulador = modem.pskmod('M',SYS.M,'SymbolOrder','Gray','InputType','Bit');
end

simbolos = modulate(modulador,Bits) ;                       % Modulador M-QAM o QPSK

skl = reshape(simbolos,SYS.N,SYS.Nsimbolos_OFDM);           % S/P

aux = zeros(SYS.Nifft,SYS.Nsimbolos_OFDM);                  % Mapeador OFDMA
for j=1:SYS.Nsimbolos_OFDM
    aux(:,j)=[zeros(ceil((SYS.L-SYS.N)/2),1);skl(:,j);zeros(floor((SYS.L-SYS.N)/2),1)];
end
for l=1:SYS.Nsimbolos_OFDM                                  
    aux(:,l) = fftshift(aux(:,l));
end

ifft_signal = ifft(aux,SYS.Nifft) ;                          % IFFT
ifft_signal = sqrt(SYS.Nifft)*ifft_signal ;                  % Escalado de amplitud

ifft_signal_CP = zeros(SYS.Ls,SYS.Nsimbolos_OFDM);           % Añadir CP
ifft_signal_CP(1:SYS.Lg,:) = ifft_signal(SYS.L-SYS.Lg+1:SYS.L,:);
for it=1:SYS.L
    ifft_signal_CP(it+SYS.Lg,:)=ifft_signal(it,:);
end

s_n = reshape(ifft_signal_CP,SYS.Nsimbolos_OFDM*SYS.Ls,1);   % Señal OFDMA digital en banda base
s_t = resample(s_n,4,1);                                     % Sobremuestreo
PAPR = 20*log10(max(abs(s_t))/mean(abs(s_t)));               % Peak to Average Power Ratio de la señal OFDMA en banda base sobremuestreada (dB)


end

