Generating Swept Sine waves

user3453339 picture user3453339 · Mar 23, 2014 · Viewed 8.1k times · Source

How can I formulate an equation for generating a swept sine wave. I am new to signal processing and cannot find much about the topic of generating swept sine waves online. Please point me to some sources that I can use to generate an equation and use in a code. Thank you.

Answer

Paul R picture Paul R · Mar 23, 2014

The simplest way to do this is to use a phase accumulator - this is a simple method and it ensures phase continuity as the frequency changes.

To generate a fixed frequency sine wave you might do this (pseudo code):

//
// A = sine wave amplitude
// fs = sample rate (Hz)
// f = sine wave frequency (Hz)
//
phi = 0;                      // phase accumulator
delta = 2 * pi * f / Fs;      // phase increment per sample
for each sample
    output = A * sin(phi);    // output sample value for current sample
    phi += delta;             // increment phase accumulator

For a swept sine wave you would ramp up the frequency linearly, i.e. ramp up delta linearly.

//
// A = sine wave amplitude
// fs = sample rate (Hz)
// f0 = initial frequency (Hz)
// f1 = final frequency (Hz)
// T_sweep = duration of sweep (s)
//
phi = 0;                      // phase accumulator
f = f0;                       // initial frequency
delta = 2 * pi * f / Fs;      // phase increment per sample
f_delta = (f1 - f0) / (Fs * T_sweep);
                              // instantaneous frequency increment per sample
for each sample
    output = A * sin(phi);    // output sample value for current sample
    phi += delta;             // increment phase accumulator
    f += f_delta;             // increment instantaneous frequency
    delta = 2 * pi * f / Fs;  // re-calculate phase increment