From 7a609ccd9c2379ddbe1df31babd95ba5cb06752c Mon Sep 17 00:00:00 2001 From: h_vogt Date: Sat, 29 Apr 2017 19:44:49 +0200 Subject: [PATCH] Add a new command 'clip vectorname xmin xmax' This will create a new vector cl_vectorname in the current plot, with all data values outside of xmin, xmax set to NaN. Example: clip v(9) 1.5u 2.5u plot cl_v(9) --- src/frontend/com_plot.c | 16 +++++++++++++ src/frontend/com_plot.h | 1 + src/frontend/commands.c | 8 +++++++ src/frontend/vectors.c | 46 ++++++++++++++++++++++++++++++++++++ src/include/ngspice/fteext.h | 1 + 5 files changed, 72 insertions(+) diff --git a/src/frontend/com_plot.c b/src/frontend/com_plot.c index 6758dbd04..73acebf7d 100644 --- a/src/frontend/com_plot.c +++ b/src/frontend/com_plot.c @@ -2,6 +2,7 @@ #include "ngspice/bool.h" #include "ngspice/wordlist.h" +#include "ngspice/fteext.h" #include "plotting/plotit.h" @@ -24,3 +25,18 @@ com_bltplot(wordlist *wl) #endif +void +com_clip(wordlist *wl) +{ + int err; + double xmin, xmax; + char *vecname = wl->wl_word; + wl = wl->wl_next; + char *mword = wl->wl_word; + xmin = INPevaluate(&mword, &err, TRUE); + wl = wl->wl_next; + mword = wl->wl_word; + xmax = INPevaluate(&mword, &err, TRUE); + vec_clip(vecname, xmin, xmax); +} + diff --git a/src/frontend/com_plot.h b/src/frontend/com_plot.h index e8ef7c22a..d241ed130 100644 --- a/src/frontend/com_plot.h +++ b/src/frontend/com_plot.h @@ -5,4 +5,5 @@ void com_plot(wordlist *wl); #ifdef TCL_MODULE void com_bltplot(wordlist *wl); #endif +void com_clip(wordlist *wl); #endif diff --git a/src/frontend/commands.c b/src/frontend/commands.c index e3d5761e0..9b993c504 100644 --- a/src/frontend/commands.c +++ b/src/frontend/commands.c @@ -602,6 +602,10 @@ struct comm spcp_coms[] = { { 0, 0, 0, 0 }, E_DEFHMASK, 0, 0, NULL, ": Print circuit inventory" }, + { "clip", com_clip, TRUE, FALSE, + { 0, 0, 0, 0 }, E_DEFHMASK, 3, 3, + NULL, + ": Clip the named vector between xmin and xmax" }, #ifdef HAVE_TSEARCH { "check_ifparm", com_check_ifparm, TRUE, FALSE, { 0, 0, 0, 0 }, E_DEFHMASK, 0, 0, @@ -1006,6 +1010,10 @@ struct comm nutcp_coms[] = { { 0, 0, 0, 0 }, E_DEFHMASK, 0, 0, NULL, ": Print circuit inventory" } , + { "clip", com_clip, TRUE, FALSE, + { 0, 0, 0, 0 }, E_DEFHMASK, 3, 3, + NULL, + ": Clip the named vector between xmin and xmax" }, { NULL, NULL, FALSE, FALSE, { 0, 0, 0, 0 }, E_DEFHMASK, 0, LOTS, NULL, diff --git a/src/frontend/vectors.c b/src/frontend/vectors.c index f06b1b362..e7c6b30c1 100644 --- a/src/frontend/vectors.c +++ b/src/frontend/vectors.c @@ -1151,3 +1151,49 @@ plot_prefix(char *pre, char *str) else return (TRUE); } + +/* clip a vector between hte two scale values xmin and xmax +add a copy of the vector to the current plot +Set all data outside xmin and xmax will be set to NAN */ +bool +vec_clip(char* vecname, double xmin, double xmax) +{ + /* check for xmin and xmax */ + if (xmax < xmin) { + double tmp = xmin; + xmin = xmax; + xmax = tmp; + } + else if (xmax == xmin) { + fprintf(stderr, "Warnig: Cannot clip vector %s\n", vecname); + return FALSE; + } + /* Create new vector as copy within current plot */ + struct dvec *oldvec = vec_fromplot(vecname, plot_cur); + struct dvec *newvec = vec_copy(oldvec); + char *oldname = oldvec->v_name; + char *newname = tprintf("cl_%s", oldname); +// char * newname = copy("new1"); + tfree(newvec->v_name); + newvec->v_name = newname; + vec_new(newvec); + newvec->v_flags = oldvec->v_flags; + /* Compare newvec->v_scale to xmin, xmax */ + int length = newvec->v_length; + int i; + for (i = 0; i < length; i++) { + if ((plot_cur->pl_scale->v_realdata[i] < xmin) || (plot_cur->pl_scale->v_realdata[i] > xmax)) + if (isreal(newvec)) { + newvec->v_realdata[i] = NAN; + } + else { + newvec->v_compdata[i].cx_real = NAN; + newvec->v_compdata[i].cx_imag = NAN; + } + else + continue; + } + vec_rebuild_lookup_table(plot_cur); + return TRUE; +} + diff --git a/src/include/ngspice/fteext.h b/src/include/ngspice/fteext.h index db7816584..7fd6bcb04 100644 --- a/src/include/ngspice/fteext.h +++ b/src/include/ngspice/fteext.h @@ -366,6 +366,7 @@ extern void plot_new(struct plot *pl); extern char *vec_basename(struct dvec *v); extern bool plot_prefix(char *pre, char *str); extern void vec_transpose(struct dvec *v); +extern bool vec_clip(char* vecname, double xmin, double xmax); /* main.c */ extern bool ft_intrpt;