View Raw SPL
/*****************************************************************************
*                                                                            *
*   GRTSQR.SPL   Copyright (C) 1998 DSP Development Corporation              *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Generates a squarewave with a specified rise time           *
*                                                                            *
*   Revisions:   12 Feb 1998  RRR  Creation - from SPOLE.MAC                 *
*                                                                            *
*****************************************************************************/

#if @HELP_GRTSQR

    GRTSQR

    Purpose: Generates a squarewave with a specified rise time.

    Syntax:  GRTSQR(len, dx, f, p, rt)

               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.

                rt - An optional real, rise time in seconds, defaults
                     to 0.1.

              duty - An optional real, the duty cycle in percentage of
                     time "on". A value from 0 to 100%, defaults
                     to 50.

    Returns: A series

    Example:
             grtsqr(1000, 0.001, 3, 0, 0.02)

             Generates 1000 points of a 3 Hz squarewave with a rise
             time of 0.02 seconds.

    Example:
             grtsqr(1000, 0.001, 3, 0, 0.02, 20)

             Same as above except the duty cycle is set to 20%.

    Remarks:
             Low pass filters an ideal squarewave with a single pole
             analog filter to produce a squarewave with a specified
             rise time. The rise time is defined as the time
             required for a step to go from 10% to 90% of final
             value. The desired rise time specifies the low pass
             cutoff frequency of the filter as follows:

                    fc = 2.2 / (2 * pi * rt)

             The squarewave is normalized with an amplitude of 0.0 to
             1.0 Volts.

             The single pole analog lowpass filter is implemented in
             the digital domain using the impulse invariance
             technique.

    See Also:
             Iir
             Gsqrwave
             Slp

    References:

    [1] J. K. Roberge
        Operational Amplifiers: Theory and Practice
        John Wiley & Sons Inc., 1975
        pp. 92-97

#endif


/* generate a squarewave with a specified rise time */
grtsqr(len, dx, f, p, rt, duty)
{
        local s;

        if (argc < 6)
        {
                if (argc < 5)
                {
                        if (argc < 4)
                        {
                                if (argc < 3)
                                {
                                        if (argc < 2)
                                        {
                                                help("Grtsqr");
                                                return;
                                        }
                                
                                        f = 1.0;
                                }
                        
                                p = 0.0;
                        }
                
                        rt = 0.1;
                }
                
                duty = -1;
        }

        /* create an ideal squarewave from 0 to 1 */
        if (duty < 0)
        {
                s = gsqrwave(len, dx, f, p);
        }
        else
        {
                s = gsqrwave(len, dx, f, p, duty);
        }

        /* single pole low pass filter to achieve rise time */
        s = slp(s, 1.1 / (rt * pi));

        return(s);
}