optimize custom data plots and expression evaluation, added avg()

This commit is contained in:
Stefan Frederik 2022-02-12 13:20:24 +01:00
parent c0d2951883
commit c6a7d8f9bb
8 changed files with 220 additions and 142 deletions

View File

@ -654,7 +654,7 @@ static int waves_callback(int event, int mx, int my, KeySym key, int button, int
j = -1;
if(!bus_msb && strstr(ntok, " ")) {
j = xctx->graph_nvars;
plot_raw_custom_data(sweep_idx, ntok);
plot_raw_custom_data(sweep_idx, 0, xctx->graph_allpoints, ntok);
} else {
j = get_raw_index(ntok);
}

View File

@ -2424,7 +2424,6 @@ void draw_graph(int i, const int flags, Graph_ctx *gr)
idx = -1;
if(xctx->graph_values && !bus_msb && strstr(ntok, " ")) {
idx = xctx->graph_nvars;
plot_raw_custom_data(sweep_idx, ntok);
}
/* quickly find index number of ntok variable to be plotted */
if( idx == xctx->graph_nvars || (idx = get_raw_index(bus_msb ? bus_msb : ntok)) != -1 ) {
@ -2476,6 +2475,7 @@ void draw_graph(int i, const int flags, Graph_ctx *gr)
sweep_idx, wcnt, n_nodes, gr);
}
} else {
if(idx == xctx->graph_nvars) plot_raw_custom_data(sweep_idx, first, last, ntok);
draw_graph_points(idx, first, last, point, wave_color, wcnt, n_nodes, gr);
}
}
@ -2516,6 +2516,7 @@ void draw_graph(int i, const int flags, Graph_ctx *gr)
sweep_idx, wcnt, n_nodes, gr);
}
} else {
if(idx == xctx->graph_nvars) plot_raw_custom_data(sweep_idx, first, last, ntok);
draw_graph_points(idx, first, last, point, wave_color, wcnt, n_nodes, gr);
}
}

View File

@ -519,18 +519,20 @@ int get_raw_index(const char *node)
#define ABS -9
#define SGN -10
#define INTEG -11
#define DERIV -12
#define AVG -12
#define DERIV -13
#define NUMBER -60
typedef struct {
int i;
double d;
double prev;
double prevy;
double prevx;
} Stack1;
void plot_raw_custom_data(int sweep_idx, const char *expr)
void plot_raw_custom_data(int sweep_idx, int first, int last, const char *expr)
{
int i, p, idx, ofs = 0;
int dset;
int i, p, idx;
const char *n;
char *endptr, *ntok_copy = NULL, *ntok_save, *ntok_ptr;
Stack1 stack1[200];
@ -554,6 +556,7 @@ void plot_raw_custom_data(int sweep_idx, const char *expr)
else if(!strcmp(n, "abs()")) stack1[stackptr1++].i = ABS;
else if(!strcmp(n, "sgn()")) stack1[stackptr1++].i = SGN;
else if(!strcmp(n, "integ()")) stack1[stackptr1++].i = INTEG;
else if(!strcmp(n, "avg()")) stack1[stackptr1++].i = AVG;
else if(!strcmp(n, "deriv()")) stack1[stackptr1++].i = DERIV;
else if( (v = strtod(n, &endptr)), !*endptr) {
stack1[stackptr1].i = NUMBER;
@ -571,80 +574,96 @@ void plot_raw_custom_data(int sweep_idx, const char *expr)
dbg(1, " plot_raw_custom_data(): stack1= %d\n", stack1[stackptr1 - 1].i);
}
my_free(575, &ntok_copy);
for(dset = 0 ; dset < xctx->graph_datasets; dset++) {
for(p = ofs ; p < ofs + xctx->graph_npoints[dset]; p++) {
stackptr2 = 0;
for(i = 0; i < stackptr1; i++) { /* number */
if(stack1[i].i == NUMBER) {
stack2[stackptr2++] = stack1[i].d;
for(p = first ; p <= last; p++) {
stackptr2 = 0;
for(i = 0; i < stackptr1; i++) { /* number */
if(stack1[i].i == NUMBER) {
stack2[stackptr2++] = stack1[i].d;
}
else if(stack1[i].i >=0 && stack1[i].i < xctx->graph_allpoints) { /* spice node */
stack2[stackptr2++] = xctx->graph_values[stack1[i].i][p];
}
if(stackptr2 > 1) { /* 2 argument operators */
if(stack1[i].i == PLUS) {
stack2[stackptr2 - 2] = stack2[stackptr2 - 2] + stack2[stackptr2 - 1];
stackptr2--;
}
else if(stack1[i].i >=0 && stack1[i].i < xctx->graph_allpoints) { /* spice node */
stack2[stackptr2++] = xctx->graph_values[stack1[i].i][p];
else if(stack1[i].i == MINUS) {
stack2[stackptr2 - 2] = stack2[stackptr2 - 2] - stack2[stackptr2 - 1];
stackptr2--;
}
else if(stack1[i].i == MULT) {
stack2[stackptr2 - 2] = stack2[stackptr2 - 2] * stack2[stackptr2 - 1];
stackptr2--;
}
else if(stack1[i].i == DIVIS) {
stack2[stackptr2 - 2] = stack2[stackptr2 - 2] / stack2[stackptr2 - 1];
stackptr2--;
}
else if(stack1[i].i == POW) {
stack2[stackptr2 - 2] = pow(stack2[stackptr2 - 2], stack2[stackptr2 - 1]);
stackptr2--;
}
}
if(stackptr2 > 0) { /* 1 argument operators */
if(stack1[i].i == SIN) {
stack2[stackptr2 - 1] = sin(stack2[stackptr2 - 1]);
}
else if(stack1[i].i == COS) {
stack2[stackptr2 - 1] = cos(stack2[stackptr2 - 1]);
}
else if(stack1[i].i == ABS) {
stack2[stackptr2 - 1] = fabs(stack2[stackptr2 - 1]);
}
else if(stack1[i].i == SGN) {
stack2[stackptr2 - 1] = stack2[stackptr2 - 1] > 0.0 ? 1 :
stack2[stackptr2 - 1] < 0.0 ? -1 : 0;
}
else if(stack1[i].i == INTEG) {
double integ = 0;
if( p == first ) {
integ = 0;
stack1[i].prev = 0;
stack1[i].prevy = stack2[stackptr2 - 1];
} else {
integ = stack1[i].prev + (x[p] - x[p - 1]) * (stack1[i].prevy + stack2[stackptr2 - 1]) * 0.5;
stack1[i].prev = integ;
stack1[i].prevy = stack2[stackptr2 - 1];
}
stack2[stackptr2 - 1] = integ;
}
else if(stack1[i].i == AVG) {
double avg = 0;
if( p == first ) {
avg = stack2[stackptr2 - 1];
stack1[i].prev = stack2[stackptr2 - 1];
stack1[i].prevy = stack2[stackptr2 - 1];
stack1[i].prevx = x[p];
} else {
avg = stack1[i].prev * (x[p - 1] - stack1[i].prevx) +
(x[p] - x[p - 1]) * (stack1[i].prevy + stack2[stackptr2 - 1]) * 0.5;
avg /= (x[p] - stack1[i].prevx);
stack1[i].prev = avg;
stack1[i].prevy = stack2[stackptr2 - 1];
}
stack2[stackptr2 - 1] = avg;
}
if(stackptr2 > 1) { /* 2 argument operators */
if(stack1[i].i == PLUS) {
stack2[stackptr2 - 2] = stack2[stackptr2 - 2] + stack2[stackptr2 - 1];
stackptr2--;
}
else if(stack1[i].i == MINUS) {
stack2[stackptr2 - 2] = stack2[stackptr2 - 2] - stack2[stackptr2 - 1];
stackptr2--;
}
else if(stack1[i].i == MULT) {
stack2[stackptr2 - 2] = stack2[stackptr2 - 2] * stack2[stackptr2 - 1];
stackptr2--;
}
else if(stack1[i].i == DIVIS) {
stack2[stackptr2 - 2] = stack2[stackptr2 - 2] / stack2[stackptr2 - 1];
stackptr2--;
}
else if(stack1[i].i == POW) {
stack2[stackptr2 - 2] = pow(stack2[stackptr2 - 2], stack2[stackptr2 - 1]);
stackptr2--;
else if(stack1[i].i == DERIV) {
double deriv = 0;
if( p == first ) {
deriv = 0;
stack1[i].prev = stack2[stackptr2 - 1];
} else {
deriv = (stack2[stackptr2 - 1] - stack1[i].prev) / (x[p] - x[p - 1]);
stack1[i].prev = stack2[stackptr2 - 1] ;
}
stack2[stackptr2 - 1] = deriv;
}
if(stackptr2 > 0) { /* 1 argument operators */
if(stack1[i].i == SIN) {
stack2[stackptr2 - 1] = sin(stack2[stackptr2 - 1]);
}
else if(stack1[i].i == COS) {
stack2[stackptr2 - 1] = cos(stack2[stackptr2 - 1]);
}
else if(stack1[i].i == ABS) {
stack2[stackptr2 - 1] = fabs(stack2[stackptr2 - 1]);
}
else if(stack1[i].i == SGN) {
stack2[stackptr2 - 1] = stack2[stackptr2 - 1] > 0.0 ? 1 :
stack2[stackptr2 - 1] < 0.0 ? -1 : 0;
}
else if(stack1[i].i == INTEG) {
double integ = 0;
if( p == ofs ) {
integ = 0;
stack1[i].prev = 0;
} else {
integ = stack1[i].prev + (x[p] - x[p - 1]) * stack2[stackptr2 - 1];
stack1[i].prev = integ;
}
stack2[stackptr2 - 1] = integ;
}
else if(stack1[i].i == DERIV) {
double deriv = 0;
if( p == ofs ) {
deriv = 0;
stack1[i].prev = stack2[stackptr2 - 1];
} else {
deriv = (stack2[stackptr2 - 1] - stack1[i].prev) / (x[p] - x[p - 1]);
stack1[i].prev = stack2[stackptr2 - 1] ;
}
stack2[stackptr2 - 1] = deriv;
}
} /* else if(stackptr2 > 0) */
} /* for(i = 0; i < stackptr1; i++) */
y[p] = stack2[0];
}
ofs += xctx->graph_npoints[dset];
} /* else if(stackptr2 > 0) */
} /* for(i = 0; i < stackptr1; i++) */
y[p] = stack2[0];
}
}

View File

@ -1028,7 +1028,7 @@ extern int get_raw_index(const char *node);
extern void free_rawfile(int dr);
extern int read_rawfile(const char *f);
extern double get_raw_value(int dataset, int idx, int point);
extern void plot_raw_custom_data(int sweep_idx, const char *ntok);
extern void plot_raw_custom_data(int sweep_idx, int first, int last, const char *ntok);
extern int schematic_waves_loaded(void);
extern int edit_wave_attributes(int what, int i, Graph_ctx *gr);
extern void draw_graph(int i, int flags, Graph_ctx *gr);

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -38,36 +38,53 @@ L 8 790 -530 790 -510 {}
L 8 770 -610 790 -610 {}
L 8 790 -610 807.5 -620 {}
L 8 810 -610 830 -610 {}
B 2 1110 -950 1530 -800 {flags=graph
y1 = 0
B 2 1270 -780 1690 -630 {flags=graph
y1 = -0.038
y2 = 20
divy = 5
subdivy=1
x1=9.15065e-06
x2=0.000197012
x1=-9.66825e-10
x2=0.0006
divx=9
node="v(led)
v(sw)"
color="11 18" unitx=m subdivx=4}
B 2 1110 -790 1530 -660 {flags=graph
y1 = 0
B 2 1270 -630 1690 -500 {flags=graph
y1 = 4.9e-06
y2 = 20
divy = 6
subdivy=1
x1=9.15065e-06
x2=0.000197012
x1=-9.66825e-10
x2=0.0006
divx=8
node="v(panel)" unitx=m
color=13}
B 2 1110 -650 1530 -520 {flags=graph
y1 = 0
y2 = 4
node="panel
led" unitx=m
color="7 4"}
B 2 1270 -500 1690 -370 {flags=graph
y1 = 4e-13
y2 = 3.8
divy = 4
subdivy=1
x1=9.15065e-06
x2=0.000197012
x1=-9.66825e-10
x2=0.0006
divx=8
node="i(vsw)" color=12 unitx=m}
unitx=m
color="7 8"
node="i(vled)
i(vpanel)"}
B 2 1270 -930 1690 -780 {flags=graph
y1 = -6.4e-08
y2 = 40
divy = 5
subdivy=1
x1=-9.66825e-10
x2=0.0006
divx=9
unitx=m subdivx=4
color="7 4"
node="\\"i(vled) v(led) *\\"
\\"i(vpanel) v(panel) *\\""}
B 18 45 -850 300 -665 {}
A 5 300 -850 5.590169943749475 243.434948822922 360 {fill=true}
P 7 6 375 -665 320 -821.25 315 -835 302.5 -850 290 -855 45 -865 {}
@ -94,7 +111,7 @@ T {2x10 1W white LED} 1230 -340 0 0 0.4 0.4 {layer=8}
T {IDEAL Diode} 690 -470 0 0 0.4 0.4 {layer=8}
T {2xseries 1W white LEDs} 1250 -230 0 0 0.4 0.4 {}
T {Select one or more graphs (and no other objects)
and use arrow keys to zoom / pan waveforms} 1120 -1010 0 0 0.3 0.3 {}
and use arrow keys to zoom / pan waveforms} 1190 -1010 0 0 0.3 0.3 {}
N 80 -450 80 -430 {lab=SRC}
N 1050 -250 1140 -250 {lab=0}
N 1140 -290 1140 -250 {lab=0}
@ -125,7 +142,7 @@ N 310 -450 345 -450 {lab=PANEL}
C {title.sym} 160 -40 0 0 {name=l1 author="Stefan Schippers" net_name=true}
C {code_shown.sym} 245 -245 0 0 {name=CONTROL value=".control
save v(panel) v(sw) v(led) i(vpanel)
tran 1u 200u uic
tran 1u 600u uic
write solar_panel.raw
.endc
* .dc VP 0 21 0.01
@ -165,7 +182,7 @@ footprint=1206
device=resistor
m=1
net_name=true}
C {vsource.sym} 610 -780 0 0 {name=Vset1 value="pulse 0 1 0 1n 1n 1.9u 5u" net_name=true}
C {vsource.sym} 610 -780 0 0 {name=Vset1 value="pulse 0 1 0 1n 1n 1.7u 5u" net_name=true}
C {lab_pin.sym} 610 -750 0 0 {name=l13 sig_type=std_logic lab=0 net_name=true}
C {res.sym} 800 -650 3 0 {name=R3
value="r='V(CTRL1) > 0.5 ? 0.01 : 1e7'"
@ -219,7 +236,7 @@ C {spice_probe.sym} 1160 -480 0 0 {name=p1 analysis=tran}
C {spice_probe.sym} 360 -450 0 0 {name=p2 analysis=tran}
C {spice_probe.sym} 860 -550 0 1 {name=p3 analysis=tran}
C {spice_probe.sym} 100 -450 0 1 {name=p4 analysis=tran}
C {launcher.sym} 1125 -1045 0 0 {name=h3
C {launcher.sym} 1195 -1045 0 0 {name=h3
descr="Select arrow and
Ctrl-Left-Click to load/unload waveforms"
tclcommand="

View File

@ -32,8 +32,8 @@ y1 = -0.048929
y2 = 0.999755
divy = 3
subdivy=1
x1=1.28713e-07
x2=2.26934e-07 divx=10
x1=1.35174e-07
x2=2.76612e-07 divx=10
node="ldbl[0]
ldbl[16]
ldbl[32]
@ -51,8 +51,8 @@ y1 = 0
y2 = 1.60
subdivy=1
divy = 4
x1=1.28713e-07
x2=2.26934e-07
x1=1.35174e-07
x2=2.76612e-07
divx=10
subdivx=4
node="ldcp
@ -70,8 +70,8 @@ y2 = 1.6
divy = 3
subdivy=0
subdivx = 1
x1=1.28713e-07
x2=2.26934e-07 divx=10
x1=1.35174e-07
x2=2.76612e-07 divx=10
node="ldwl[0]
ldwl[1]
ldwl[2]
@ -93,8 +93,8 @@ y2 = 1.6
ypos1=0.0990096
ypos2=2.81842
divy = 1
x1=1.28713e-07
x2=2.26934e-07
x1=1.35174e-07
x2=2.76612e-07
divx=12
subdivx=4
node="---In/Out---
@ -122,8 +122,8 @@ B 2 1840 -1300 2890 -1160 {flags=graph
y1 = 0
y2 = 1.60
divy = 4
x1=1.28713e-07
x2=2.26934e-07
x1=1.35174e-07
x2=2.76612e-07
divx=8
unitx=n
node="xsa[0].ldqib
@ -133,16 +133,17 @@ xctrl.ldq_b"
color="4 12 7 10"
}
B 2 1840 -240 2890 0 {flags=graph
y1 = -0.0559946
y2 = 0.0205097
y1 = -0.0286575
y2 = 0.00267868
divy = 5
x1=1.28713e-07
x2=2.26934e-07
x1=1.35174e-07
x2=2.76612e-07
unity=m
divx=10
subdivx=1
node=i(vvcc)
color=3
node="i(vvcc)
\\"i(vvcc) avg()\\""
color="3 7"
unitx=n
subdivy=4
}