function [x, tout] = fmmod_jvk1(m, tin, fc, deltaf)
%fmmod_jvk1: Function to FM modulate a message.
%
%   Inputs: m = vector containing message samples.
%           tin = vector of time samples corresponding to m.
%           fc = desired center frequency of FM output
%           deltaf = peak frequency deviation
%   Outputs:    x = vector containing FM modulated wave.
%               tout = vector of time samples corresponding to x
%
%   The function also performs an increase in the sampling rate between
%   the message and the modulated wave. The upsampling factor is chosen
%   based on the ratio of the desired carrier frequency and the bandwidth
%   of the baseband message.


gamma = deltaf/max(abs(m));         %compute desired freq sensitivity
                                    %of the modulator based on allowed
                                    %peak frequency deviation

y = zeros(size(m));                 %y is the integral of the message
Tin = tin(2) - tin(1);              %Tin is the sampling interval of the
                                    %baseband message

%Integrator
y(1) = (Tin/2)*m(1);

for i = 2:length(m)
    y(i) = y(i-1) + (Tin/2)*(m(i)+m(i-1));
end

%CarsonBW = 2*(deltaf + 1/(2*Tin));
%Nup = 8*ceil((fc + 0.5*CarsonBW)*2*Tin);
Nup = 8*ceil(fc*2*Tin);             %Upsampling factor

yup = interp(y,Nup);                %Interpolate
tout = (0:(length(yup) - 1))*(Tin/Nup); %Create output time vector

%FM modulation
theta = 2*pi*(fc*tout + gamma*yup);
x = cos(theta);

end

