# How to create a sine wave that transitions through frequencies and returns to phase

For an auditory psychological experiment, I need to create a sine wave, such that at a given point the frequency of the wave will rise smoothly, and descend back to base frequency. I need it to return in phase with a second sine wave which is a continuous sine wave of the base frequency:

ie: one ear will get a continuous 300 Hz sine wave, the other will get a 300 Hz sine wave, that rises to 305 for a short while, and descends back to 300 Hz in phase with the second ear.

I generated the frequency modulation with a complex phasor recursion, as suggested here: https://dsp.stackexchange.com/a/1087/18658

ie, each point in the sine wave is given by

z(n+1) = z(n) * exp(j* 2pi* frequency).


The parameter I can play with is the length of modulation. Any ideas about how to find the nearest length that gives me the in-phase return?

For the first continuous $f_0=300\mbox{Hz}$ signal, after $2N$ samples (for a duration of $2N/f_s$ at a sampling rate $f_s$) the phase will have changed by: \begin{align} \Delta \phi_1 &= 2\pi \frac{f_0}{f_s} (2N-1) \end{align}
On the other hand, as the second signal ramps up linearly (in small quantized steps as per your phasor implementation) from $f_0=300\mbox{Hz}$ to $f_1=305\mbox{Hz}$ then down from 305Hz to 300Hz over the same $2N$ samples, the phase will have changed by: \begin{align} \Delta \phi_2 &= \sum_{n=1}^{N} 2\pi \left(\frac{f_0}{f_s} + \left(\frac{f_1}{f_s}-\frac{f_0}{f_s}\right)\frac{n}{N}\right) + \sum_{n=1}^{N-1} 2\pi \left(\frac{f_1}{f_s} + \left(\frac{f_0}{f_s}-\frac{f_1}{f_s}\right)\frac{n}{N}\right) \\ % &= 2\pi \frac{f_1}{f_s} % + 2\pi \sum_{n=1}^{N-1} \left(\frac{f_0}{f_s} + \left(\frac{f_1}{f_s}-\frac{f_0}{f_s}\right)\frac{n}{N} + \frac{f_1}{f_s} + \left(\frac{f_0}{f_s}-\frac{f_1}{f_s}\right)\frac{n}{N}\right) \\ % &= 2\pi \frac{f_1}{f_s} % + 2\pi \sum_{n=1}^{N-1} \left(\frac{f_0}{f_s} + \frac{f_1}{f_s}\right) \\ &= 2\pi \frac{f_1}{f_s} + 2\pi \left(\frac{f_0}{f_s}+\frac{f_1}{f_s}\right) \left(N - 1\right) \end{align}
For those two signals to be in phase after $2N$ samples, their phase difference must be a multiple of $2\pi$. Thus the constraint is that \begin{align} \Delta \phi_2 &= \Delta \phi_1 + 2\pi k \\ 2\pi \frac{f_1}{f_s} + 2\pi \left(\frac{f_0}{f_s}+\frac{f_1}{f_s}\right) \left(N - 1\right) &= 2\pi \frac{f_0}{f_s} \left(2N-1\right) + 2\pi k \end{align} for some arbitrary integer $k$, or equivalently: \begin{align} N &= k \frac{f_s}{f_1-f_0} \end{align} Which, provided that $kf_s/(f_1-f_0)$ is an integer greater than 0, yields a signal duration of \begin{align} \frac{2k}{f_1-f_0} \end{align}
In your specific case for a frequency excursion of $5\mbox{Hz}$, the signals will return in phase for modulation durations of 0.4, 0.8, 1.2, ... seconds.
Note that if you need to sustain the signal for some $M$ samples at $f_1$ (instead of immediately going back down after the rise), then the constraint becomes: \begin{align} N+M &= k \frac{f_s}{f_1-f_0} \end{align} In other words the duration of the rise and plateau together would, in your case, need to be a multiple of 0.2 seconds. So for example if you want to have a plateau of 1 second at 305Hz, you might choose a rise + plateau of 1.2 seconds, giving 0.2 seconds for the ramp up and another 0.2 seconds for the ramp down (for a total signal duration of 1.4 seconds)