View Raw SPL

static u_d_m_v = "";

/* plot a discrete frequency response in two Windows */
freqzplot(h, w, Fs, winmag, winphase)
{
        local m, p, wmag, wphase, wmnum, wpnum, win1, win2;

        /* parse args */
        if (argc < 5)
        {
                if (argc < 4)
                {
                        if (argc < 3)
                        {
                                if (argc < 2)
                                {
                                        if (argc < 1) error(sprintf("%s - complex frequency response required", __FUNC__));
                                        w = xvals(h);
                                }
                                
                                Fs = {}
                        }

                        winmag = refwin(W0);
                }

                winphase = {};

                if (iswindow(winmag))
                {
                        wpnum = getwnum(winmag) + 1;

                        if (wpnum > 0 && wpnum <= numwindows())
                        {
                                winphase = castwindow(getwnum(winmag) + 1);
                        }
                }
        }

        if (iswindow(winmag))
        {
                /* for casting */
                wmnum = getwnum(winmag);

                /* window for mag response */
                wmag = castwindow(wmnum);

                /* to handle log(0) */
                u_d_m_v = getconf("use_default_math_value");
                setconf("use_default_math_value", "0");

                /* log-magnitude */
                m = 20*log10(mag(h));

                setconf("use_default_math_value", u_d_m_v);
                u_d_m_v = "";

                /* clear old formula */
                protect(wmag, "Magnitude Response");
                clear(wmag);

                /* plot as lines */
                setplotstyle(wmag, 0);

                /* linear axes */
                setxlog(wmag, 0);
                setylog(wmag, 0);

                /* label axes */
                scales(wmag, 2);

                /* grids */
                setgridstyle(wmag, 1, 3);
                setgridstyle(wmag, 2, 3);

                /* assign to window */
                win1 = castwindow(wmnum);
                win1 = xy(w, m);

                /* xy labels */
                label(wmag, "Magnitude Response");
                setylabel(wmag, "Magnitude (dB)");

                if (isempty(Fs))
                {
                        setxlabel(wmag, sprintf("Normalized Frequency (%s)", getvunits(w)));
                }
                else
                {
                        setxlabel(wmag, sprintf("Frequency (%s)", getvunits(w)));
                }
        }

        if (iswindow(winphase))
        {
                /* for casting */
                wpnum = getwnum(winphase);

                /* window for phase response */
                wphase = castwindow(wpnum);

                /* phase in degrees */
                p = unwrap(phase(h)) * 180 / pi;
                setvunits(p, "Degrees");

                /* clear old formula */
                protect(wphase, "Phase Response");
                clear(wphase);

                /* plot as lines */
                setplotstyle(wphase, 0);

                /* linear axes */
                setylog(wphase, 0);
                setxlog(wphase, 0);

                /* label axes */
                scales(wphase, 2);

                /* grids */
                setgridstyle(wphase, 1, 3);
                setgridstyle(wphase, 2, 3);

                /* assign to window */ 
                win2 = castwindow(wpnum);
                win2 =  xy(w, p);

                /* xy labels */
                label(wphase, "Phase Response");
                setylabel(wphase, "Phase (degrees)");

                if (isempty(Fs))
                {
                        setxlabel(wphase, sprintf("Normalized Frequency (%s)", getvunits(w)));
                }
                else
                {
                        setxlabel(wphase, sprintf("Frequency (%s)", getvunits(w)));
                }
        }
}


/* error handler */
freqzplot_error(errnum, errmes)
{
        if (strlen(u_d_m_v) > 0)
        {
                setconf("use_default_math_value", u_d_m_v);
                u_d_m_v = "";
        }

        error(errmes);
}