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?


1 Answer 1


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)

  • $\begingroup$ So, if I understand correctly, for a 5 Hz modulation I cannot have a duration shorter than 0.4 seconds? $\endgroup$
    – YanivA
    Commented Dec 12, 2015 at 9:44
  • $\begingroup$ My bad, with the solution with the plateau you can go down to 0.2 seconds, but with fewer intermediate frequencies (thus providing a transition that is not as smooth) as you get closer to 0.2 (but never less than 0.2 seconds which would be jumping straight to 305Hz then back after 0.2 seconds when the signals are back in phase). $\endgroup$
    – SleuthEye
    Commented Dec 12, 2015 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.