View Raw SPL
/*****************************************************************************
*                                                                            *
*   CUREXTRACT.SPL  Copyright (C) 2012 DSP Development Corporation           *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Extract series from a source window based on cursor indices *
*                                                                            *
*   Revisions:   20 Jul 2012  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/

#if @HELP_CUREXTRACT

    CUREXTRACT

    Purpose: Extract series from a source window based on cursor indices.

    Syntax:  CUREXTRACT(srcwin, deswin1, deswin2, ..., deswinN)

               srcwin  - A window. The source window to obtain the X indices
                         of the first and second cursors.

               deswinN - Optional, zero or more Windows. The target windows.
                         If not specified, defaults to the current window.

    Returns: A series if no target window is specified else the result of
             each extraction is placed into the target windows. 

    Example:
             W1: integ(gnorm(1000, 1/1000));curnput(100);curnput(200, 1, 2)
             W2: curextract(w1)

             W1 contains 1000 point random series. The first cursor
             position of W1 is set to index 100 and the second cursor
             position is set to index 200. W2 contains the 100 point
             extracted series in W1 from index 100 to index 200.

    Example:
             W1: integ(gnorm(1000, 1/1000));curnput(100);curnput(200, 1, 2)
             W2: integ(gnorm(1000, 1/1000))
             W3: integ(gnorm(1000, 1/1000))
        
             curextract(w1, w2, w3)

             W1 through W3 originally contain 1000 point random series. 
             The first cursor position of W1 is set to index 100 and
             the second cursor position is set to index 200. 100
             samples of the series in W2 and W3 are extracted from index
             100 to index 200.

    Remarks:
             If no target window is specified, the source window is
             extracted into the current window.

             The source window can also be a target window.

             See CURXEXTRACT to extract multiple windows based on the
             X locations of the source window cursors.

    See Also:
             Curxextract
             Extract
             Extractwin
             Xextract
             Xextractwin
#endif


/* extract based on cursor indices */
curextract(src, argv)
{
        local cnt, loc1, loc2, i, w, numpts, start;

        if ((cnt = argc) < 1)
        {
                error("curextract - source window required")
        }

        if (not(iswindow(src)))
        {
                error("curextract - source window required")
        }

        /* get cursor positions in terms of indices */
        loc1 = curpos(src, 1, 1);
        loc2 = curpos(src, 1, 2);

        /* number of points and starting index */
        numpts = (loc2 > 0) ? abs(loc2 - loc1) : -1;
        start  = (loc2 > 0) ? min(loc1, loc2)  : loc1;

        if (start > 0)
        {
                if (cnt == 1)
                {
                        /* no target, return extracted series */
                        return(extract(src, start, numpts));
                }

                loop (i = 1..(cnt - 1))
                {
                        w = getargv(i);

                        if (iswindow(w))
                        {
                                if (not(isempty(w)))
                                {
                                        /* extract based on indices */
                                        setwform(w, sprintf("extract(curr, %d, %d)", start, numpts));
                                }
                        }
                }
        }
}