View Raw SPL
/*****************************************************************************
*                                                                            *
*   STATTEXT.SPL  Copyright (C) 2018 DSP Development Corporation             *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:       Randy Race                                                 *
*                                                                            *
*   Synopsis:     Adds text of summary statistics to the window margin       *
*                                                                            *
*   Revisions:    17 Apr 2018  RRR  Creation                                 *
*                                                                            *
*****************************************************************************/


#if @HELP_STATTEXT

    STATTEXT    

    Purpose: Adds text of summary statistics to the window margin.
                                                                        
    Syntax:  STATTEXT(opts, fmt)

                 opts - Optional. An integer series that specifies the
                        statistics to display. The series can contain one
                        or more of the following values:

                          1: Sample Size
                          2: Mean
                          3: Variance
                          4: Std Deviation
                          5: Std Error
                          6: Maximum
                          7: Minimum
                          8: Range
                          9: RMS

                        Defaults to 1..9, i.e. display all stats.

                  fmt - Optional. A format string in SPRINTF format.
                        Defaults to "" (empty string), use the automatic
                        format.
 
    Returns: Nothing, the summary stats text is displayed in the outside
             right margin of the window.

    Example:
             W1: integ(gnorm(10000, 1/1000))

             stattext

             The summary statistics are displayed in the right window
             of W1. As the series scrolls or moves, the summary statistics
             are updated.

    Example:
             W1: integ(gnorm(10000, 1/1000))

             stattext({6, 7, 2})

             The summary statistics are displayed in the right window
             of W1. The statistics display the Maximum, Minimum and
             Mean of the series. As the series scrolls or moves, the
             summary statistics are updated.

    Example:
             W1: integ(gnorm(10000, 1/1000))

             stattext({6, 7, 2}, "% .8g")

             Same as above except the right justified "g" format is used
             where the field width is 8 characters.

    Remarks:
             The summary statistics are displayed as text in the outside
             right margin of the window. The calculations are based on
             the displayed portion of the series.

             Use the OPTS series to control the type of statistics to
             display and the display order.

             Specify FMT to set the display format of the values. See
             SPRINTF for more details.

    See Also:
             Text
             Sprintf
             Stats
             Statsum
#endif


/* display summary statistics in right window margin */
stattext(opt, fmt)
{
        local t, t1, t2, t3, t4, t5, t6, t7, t8, t9, j, h;

        if (argc < 2)
        {
                if (argc < 1)
                {
                        opt = 1..9;
                }

                /* internal format */
                fmt = "";
        }

        if (isstring(opt))
        {
                fmt = opt;
                opt = 1..9;
        }

        /* insure series */
        opt = {opt};

        /* stat text based on displayed portion of series */
        if (strlen(fmt) == 0)
        {
                /* use internal formatting */
                t1 = "Sample Size:   \g{castreal(numel(cut(w0)))}";
                t2 = "Mean:          \g{mean(cut(w0))}";
                t3 = "Variance:      \g{stdev(cut(w0))^2}";
                t4 = "Std Deviation: \g{stdev(cut(w0))}";
                t5 = "Std Error:     \g{stdev(cut(w0))/sqrt(length(cut(w0)))}";
                t6 = "Maximum:       \g{max(cut(w0))}";
                t7 = "Minimum:       \g{min(cut(w0))}";
                t8 = "Range:         \g{max(cut(w0)) - min(cut(w0))}";
                t9 = "RMS:           \g{rms(cut(w0))}";
        }
        else
        {
                /* explicit format */
                t1 = sprintf('Sample Size:   {sprintf("%s", numel(cut(w0)))}',    fmt);
                t2 = sprintf('Mean:          {sprintf("%s", mean(cut(w0)))}',    fmt);
                t3 = sprintf('Variance:      {sprintf("%s", stdev(cut(w0))^2)}', fmt);
                t4 = sprintf('Std Deviation: {sprintf("%s", stdev(cut(w0)))}',   fmt);
                t5 = sprintf('Std Error:     {sprintf("%s", stdev(cut(w0))/sqrt(length(cut(w0))))}', fmt);
                t6 = sprintf('Maximum:       {sprintf("%s", max(cut(w0)))}',     fmt);
                t7 = sprintf('Minimum:       {sprintf("%s", min(cut(w0)))}',     fmt);
                t8 = sprintf('Range:         {sprintf("%s", max(cut(w0)) - min(cut(w0)))}', fmt);
                t9 = sprintf('RMS:           {sprintf("%s", rms(cut(w0)))}',     fmt);
        }

        t = "";

        /* build up display string */
        loop (j = opt)
        {
                if (j >= 1 && j <= 9)
                {
                        t += eval(sprintf("t%d", j)) + strescape("\n");
                }
        }

        /* extract trailing \n */
        t = strextract(t, 1, strlen(t) - 1);

        if (strlen(t) > 0)
        {
                /* disable plotting */
                pm = plotmode(w0, 0);

                /* find old stat text */
                h = findtext(w0, "tag", "stattext");

                if (length(h) > 0)
                {
                        /* delete it */
                        deletehandle(h);
                }

                /* add text to current window margin */
                h = text(0.0, 0.0, 2, t);

                /* text properties */
                h.location  = 1;
                h.margin    = 1;
                h.fontname  = "Consolas";
                h.pointsize = 10;
                h.bg        = SYS_INNER_COLOR;
                h.fg        = -1;
                h.box       = 1;
                h.boxcolor  = SYS_TEXT_COLOR;
                h.tag       = "stattext";

                /* show it */
                plotmode(w0, pm);
        }
}

/* default format */
stattext_deffmt()
{
        local fmt;

        fmt = sprintf("%% 16.%df", setprec());

        return(fmt);
}


/* decimal format */
stattext_dfmt(fmt)
{
        local dfmt, sep = ".";

        if (strlen(strfind(".", fmt)) == 0)
        {
                sep = "eEfFgG";
        }

        dfmt = strget(1, fmt, sep);
        dfmt += "d";

        return(dfmt);
}