calculate multiple sine sweeps one after another
Question: I'm writing a Python program to generate multiple sine sweeps one after
another with varying starting and ending frequencies and varying time
intervals.

One example would be:

  1. sweep from 0Hz to 170Hz in 1 millisecond

  2. sweep from 170Hz to 170Hz in 1 millisecond 

  3. sweep from 170Hz down to 10Hz in 1 millisecond

_So it is supposed to be a ramp up, ramp down waveform_.

The equation that I´ve used was inspired by [this
thread](http://stackoverflow.com/questions/11199509/sine-wave-that-slowly-
ramps-up-frequency-from-f1-to-f2-for-a-given-time)

    
    
    def LinearSineSweep(self, fStart, fEnd, samplingTime, samplesPerSecond):
        nValues = int(samplesPerSecond * samplingTime)
        for i in range(0, nValues):
            delta = float(i) / nValues
            t = samplingTime * delta
            phase = 2 * math.pi * t * (fStart + (fEnd - fStart) * delta / 2)
            return self._amplitude * math.sin(phase) + self._dcOffset
    
    LinearSineSweep(0, 170, 0.001, 44100)
    LinearSineSweep(170, 170, 0.001, 44100)
    LinearSineSweep(170, 10, 0.001, 44100)
    

But what I´m getting as an output is not correct:
[![170](http://i.stack.imgur.com/aYM3s.png)](http://i.stack.imgur.com/aYM3s.png)

Even with 10 times the frequency is still doesn`t come together as one
waveform

[![1700
HZ](http://i.stack.imgur.com/OLppW.png)](http://i.stack.imgur.com/OLppW.png)

_Is this a mathematical problem or a programming problem?_


Answer: As @jaket pointed out in a comment, you must make the phase vary continuously
from segment to segment (I'm paraphrasing a bit). Here's a variation of your
code that shows one way you could do this. I don't have all your other code,
so instead of `self`, the first argument of `LinearSineSweep` is a file to
which the samples are written as text. (I've also tweaked the code to
compensate for the fact that the requested interval will not, in general, be
an exact multiple of the sampling period.) `numpy` are `matplotlib` are used
to create the plot.

    
    
    from __future__ import print_function, division
    
    import math
    
    
    def LinearSineSweep(f, fStart, fEnd, samplingTime, samplesPerSecond,
                        t0=0, phi0=0):
        nValues = int(samplesPerSecond * samplingTime)
        actualSamplingTime = nValues / samplesPerSecond
        for i in range(0, nValues):
            delta = float(i) / nValues
            t = actualSamplingTime * delta
            phase = 2 * math.pi * t * (fStart + (fEnd - fStart) * delta / 2)
            value = math.sin(phase + phi0)
            # Write the time and sample value to the output...
            print(t0 + t, value, file=f)
        phase = 2 * math.pi * actualSamplingTime * (fStart + (fEnd - fStart) / 2)
        return t0 + actualSamplingTime, phi0 + phase
    
    
    if __name__ == "__main__":
        with open('out.csv', 'w') as f:
            t, phi = LinearSineSweep(f, 0, 1700, 0.001, 44100)
            t, phi = LinearSineSweep(f, 1700, 1700, 0.001, 44100, t, phi)
            t, phi = LinearSineSweep(f, 1700, 100, 0.001, 44100, t, phi)
    
        import numpy as np
        import matplotlib.pyplot as plt
    
        tvals, v = np.loadtxt('out.csv', unpack=True)
        plt.figure(figsize=(10, 4))
        plt.plot(tvals, v)
        plt.grid()
        plt.show()
    

Here's the plot:

[![plot](http://i.stack.imgur.com/VbPtR.png)](http://i.stack.imgur.com/VbPtR.png)

