From 0e0b276a3e658e324b3eb0d52e8df2ab0e514ec2 Mon Sep 17 00:00:00 2001 From: Holger Vogt Date: Thu, 23 Aug 2018 10:56:50 +0200 Subject: [PATCH] ISRC: update to trnoise source, same as in VSRC --- src/spicelib/devices/isrc/isrcacct.c | 2 +- src/spicelib/devices/isrc/isrcdel.c | 3 +++ src/spicelib/devices/isrc/isrcload.c | 24 ++++++++++++++++-------- src/spicelib/devices/isrc/isrcpar.c | 7 ++++++- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/spicelib/devices/isrc/isrcacct.c b/src/spicelib/devices/isrc/isrcacct.c index 5c86dc954..425d50ce5 100644 --- a/src/spicelib/devices/isrc/isrcacct.c +++ b/src/spicelib/devices/isrc/isrcacct.c @@ -201,7 +201,7 @@ ISRCaccept(CKTcircuit *ckt, GENmodel *inModel) /* FIXME, dont' want this here, over to aof_get or somesuch */ if (ckt->CKTtime == 0.0) { if (ft_ngdebug) - printf("VSRC: free fft tables\n"); + printf("ISRC: free fft tables\n"); fftFree(); } #endif diff --git a/src/spicelib/devices/isrc/isrcdel.c b/src/spicelib/devices/isrc/isrcdel.c index c2b6f6c33..be1d13f4e 100644 --- a/src/spicelib/devices/isrc/isrcdel.c +++ b/src/spicelib/devices/isrc/isrcdel.c @@ -7,6 +7,7 @@ Author: 1985 Thomas L. Quarles #include "isrcdefs.h" #include "ngspice/sperror.h" #include "ngspice/suffix.h" +#include "ngspice/1-f-code.h" int @@ -15,6 +16,8 @@ ISRCdelete(GENinstance *gen_inst) ISRCinstance *inst = (ISRCinstance *) gen_inst; FREE(inst->ISRCcoeffs); + trnoise_state_free(inst->ISRCtrnoise_state); + FREE(inst->ISRCtrrandom_state); return OK; } diff --git a/src/spicelib/devices/isrc/isrcload.c b/src/spicelib/devices/isrc/isrcload.c index d5deca331..71a0b72f8 100644 --- a/src/spicelib/devices/isrc/isrcload.c +++ b/src/spicelib/devices/isrc/isrcload.c @@ -334,19 +334,27 @@ INoi1 1 0 DC 0 TRNOISE(0n 0.5n 1 10n) : generate 1/f noise double TS = state -> TS; double RTSAM = state->RTSAM; - /* reset top (hack for repeated tran commands) */ - if (time == 0) - state->top = 0; + /* reset top (hack for repeated tran commands) + when there is the jump from time=0 to time>0 */ + if (time == 0.0) + state->timezero = TRUE; + else + if (state->timezero) { + state->top = 0; + state->timezero = FALSE; + } - /* no noise */ - if(TS == 0.0) { + /* no noise or time == 0 */ + if (TS == 0.0 || time == 0.0) { value = 0.0; - } else { + } + else { + /* 1/f and white noise */ - size_t n1 = (size_t) floor(time / TS); + size_t n1 = (size_t)floor(time / TS); double V1 = trnoise_state_get(state, ckt, n1); - double V2 = trnoise_state_get(state, ckt, n1+1); + double V2 = trnoise_state_get(state, ckt, n1 + 1); value = V1 + (V2 - V1) * (time / TS - (double)n1); } diff --git a/src/spicelib/devices/isrc/isrcpar.c b/src/spicelib/devices/isrc/isrcpar.c index 46a220f8e..cecf2e199 100644 --- a/src/spicelib/devices/isrc/isrcpar.c +++ b/src/spicelib/devices/isrc/isrcpar.c @@ -206,7 +206,9 @@ ISRCparam(int param, IFvalue *value, GENinstance *inst, IFvalue *select) if (here->ISRCfunctionOrder > 6 && RTSAM != 0.0) RTSEMT = here->ISRCcoeffs[6]; // RTS trap emission time - + /* after an 'alter' command to the TRNOISE voltage source the state gets re-written + with the new parameters. So free the old state first. */ + trnoise_state_free(here->ISRCtrnoise_state); here->ISRCtrnoise_state = trnoise_state_init(NA, TS, NALPHA, NAMP, RTSAM, RTSCAPT, RTSEMT); } @@ -233,6 +235,9 @@ ISRCparam(int param, IFvalue *value, GENinstance *inst, IFvalue *select) if (here->ISRCfunctionOrder > 4) PARAM2 = here->ISRCcoeffs[4]; // second parameter + /* after an 'alter' command to the TRRANDOM voltage source the state gets re-written + with the new parameters. So free the old state first. */ + tfree(here->ISRCtrrandom_state); here->ISRCtrrandom_state = trrandom_state_init(rndtype, TS, TD, PARAM1, PARAM2); }