# Implementing triangular modulation in Matlab

I am trying to implement a triangular modulation in Matlab, however I am having issues implementing the resulting frequency ramps.

I have the following input signal (in reality, this signal is the input for a voltage controlled oscillator):

Also, I have generated a matrix "z" which assigns a frequency value to each amplitude value of the input signal:

1st row: VCO output frequency [Hz]

2nd row: input signal amplitude

I am looking for tips to implement the code for the output of the VCO, which should be starting at 1 Hz at 0s, 50 Hz at 0.05s and then falling to 1 Hz again.

Code looks like this so far:

fs = 1000;                              %Sampling frequency [Hz]
t = 0:1/fs:1e2/fs;                      %Signal duration [s]
f = 10;                                 %Base frequency [Hz]
f_mod = [1:1:50 51:-1:1];               %Frequency vector [Hz]

x = 0.5 * sawtooth(2*pi*f*t, 0.5)+0.5;  %Input signal generation for VCO

z = [f_mod; x];                         %matrix assigns ampl of x with freq of VCO

y =                                     %output signal of VCO

subplot(2, 1, 1)
plot(t,x)
xlabel('Time in seconds [t]')
ylabel('Amplitude')
subplot(2, 1, 2)
plot(t,y)
xlabel('time in seconds [t]')
ylabel('Amplitude')


Given a signal with frequency $$f(t)$$, the phase of the signal is $$\phi(t) = \int f(t) dt$$, assuming constant frequency for each sampling interval you have $$\phi(t + T_s) = \phi(t) + T_s f(t)$$, you can easily compute this using the cumsum function (cummulative sum).
y = sin(cumsum(f_mod / fs))*x\$

This will have the envelope of $$x$$ and the frequencies of given by $$f$$, but this only makes sense with slowly varying $$x$$, (you may also be interested in the Hartley transform).