View Raw SPL
/*****************************************************************************
*                                                                            *
*   GSAWTOOTH.SPL Copyright (C) 2015 DSP Development Corporation             *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Generates a sawtooth waveform                               *
*                                                                            *
*   Revisions:   21 Mar 2015  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/

#if @HELP_GSAWTOOTH

    GSAWTOOTH

    Purpose: Generates a sawtooth wave.

    Syntax:  GSAWTOOTH(len, dx, f, p, width, duty)

                 len - An integer, number of samples

                  dx - A real, sample interval

                   f - An optional  real, squarewave frequency in Hertz,
                       defaults to 1.0

                   p - An optional real, phase in radians, defaults to 0.0

               width - An optional real between 0 and 1. The fraction of the
                       sawtooth period where the maximum value occurs.
                       Defaults to 1.0.

                duty - An optional real between 0 and 100. The duty cycle,
                       the percentage of a period the sawtooth is "on".
                       Defaults to 100.0

    Returns: A series

    Example:
             gsawtooth(1000, 1/1000, 3)

             Generates 1000 points of a 3 Hz sawtooth with a sample rate
             of 1000 Hz. The peak value occurs at the end of each period.

    Example:
             W1: gsawtooth(1000, 1/1000, 4)
             W2: gtriwave(1000, 1/1000, 4);overp(w1, lred)

             W1 contains 1000 points of an 4 Hz sawtooth with a sample
             rate of 1000 Hz. W2 contains 1000 points of an 4 Hz
             triangle wave with a sample rate of 1000 Hz. The overplot
             provides a graphical comparison.

    Example:
             W1: gsawtooth(1000, 1/1000, 1, 0, 0.8)
             W2: gsawtooth(1000, 1/1000, 1, 0, 0.5)
             W3: gsawtooth(1000, 1/1000, 1, 0, 0.2)
             W4: gsawtooth(1000, 1/1000, 1);overp(w1, w2, w3)

             legend(W4, 0.05, 1.1, "Width:1.0", "Width:0.8", "Width:0.5", "Width:0.2")

             W1 through W4 contain 1000 points of an 1 Hz sawtooth
             with a sample rate of 1000 Hz. The WIDTH parameter for
             each sawtooth is set to 0.8, 0.5, 0.2 and 1.0
             respectively. The overplot in W4 provides a graphical
             comparison of the effect of the differing sawtooth
             widths.

    Remarks:
             The sawtooth is normalized with an amplitude between 0.0 and
             1.0. The leading edge of the sawtooth rises monotonically.

             See GTRIWAVE to generate a triangle wave.

    See Also:
             Gsqrwave
             Gtriwave

#endif


/* generate a sawtooth waveform */
gsawtooth(len, dx, freq, phase, width, duty)
{
        local s;

        if (argc < 6)
        {
                if (argc < 5)
                {
                        if (argc < 4)
                        {
                                if (argc < 3)
                                {
                                        if (argc < 2) error("gsawtooth - length and deltax required");

                                        freq = 1.0;
                                }

                                phase = 0.0;
                        }

                        width = 1.0;
                }

                duty = 100;
        }

        /* just use gtriwave */
        s = gtriwave(len, dx, freq, phase, width, duty);

        return(s);
}