function [FMSig,PhiFinal] = VCO(fc, Av, kv, Sig, fs, Phase, PhiInit)
% VCO - simulate a voltage controlled oscillator
%
% The generated FM signal depends on the input signal as follows
%
%                                       t
% FMSig = Av*cos( 2*pi*fc*t + 2*pi*kv* INT Sig(tau) dtau + Phase)
%                                     -infty
%
% This function can be invoked either in vector form, useful for simulating
% an FM transmitter, or on a smaple by sample basis, useful when VCO is 
% used in a feedback system (PLL). In the latter case, PhiInit and PhiFinal
% are needed.
%
% Syntax:
%
% [FMSig,PhiFinal] = VCO(fc, Av, kv, Sig, fs, Phase, PhiInit)
%
% inputs Phase and PhiInit are optional and default to zero.

% Author: BPP

% deal with optional inputs
if ( nargin < 6 )
    Phase = 0;
end

if ( nargin < 7 )
    PhiInit = 0;
end

% the instantaneous phase phi(t) is 2*pi*(fc*t + INT m(tau) dtau +Phase).
% This can be computed (Rieman approximation) via an IIR filter with
% coefficients a=[1 -1] and b=[1/fs].
[ Phi, PhiFinal ] = ...
    filter( [ 1/fs ], [ 1 -1 ], 2*pi*(fc+kv*Sig), PhiInit );
% reduce phase modulo 2*pi
Phi = mod( Phi + Phase, 2*pi );
PhiFinal = mod( PhiFinal, 2*pi );

% output signal
FMSig = Av*cos( Phi );


