2
\$\begingroup\$

I have the following circuit. I try to find a way to combine the all plots into a single one (like ltspice's .step card). Unfortunately I couldn't get the example from the ngspice's manual working.

I would be grateful if anyone could help me understand the logic behind what I am trying to achieve. I think I need to force my vectors to be stored in a single plot but from my research I found more than one way of doing that (I just cannot understand the logic of those examples so I couldn't adapt them for my circuit)

I use ngspice 35

.TITLE AC test

.INCLUDE ../../lib/lm393.sub

x1 in 0 1 0 out lm393

c1 in 0 100n
r1 1 in 100k
c2 out 0 100n
r2 1 out 4.7k

vsup 1 0 DC 5
vin in 0 AC 1

.CONTROL
*foreach x 1n 10n 50n 100n
*    alter c1 = $x
*    ac dec 100 1 100k
*end

alter c2 = 10n
ac dec 100 1 1Meg
alter c2 = 100n
ac dec 100 1 1Meg
alter c2 = 1u
ac dec 100 1 1Meg
alter c2 = 10u
ac dec 100 1 1Meg

plot db(ac1.out) db(ac2.out) db(ac3.out) db(ac4.out)
.ENDC
\$\endgroup\$
1

2 Answers 2

2
\$\begingroup\$

I found an way of solving my problem

set curplot = new                  *We create a new plot to store our vectors
set acplot = $curplot              *We "store" our plot in a variable for further use

foreach x 1n 10n 50n 100n          *Loop through our desired value
    alter c2 = $x
    ac dec 100 1 1Meg

    foreach y 1k 10k 50k 100k      *Loop through our desired value
        alter r2 = $y
        ac dec 100 1 1Meg
        set aux = $curplot         *Each analysis will create its own plot
                                   *We need to remember every analysis each time to retrieve our data
        set curplot = $acplot      *Now we set the plot we want to store data into
                                   *In our case the plot created previously
        let 'db out c2: $x r2: $y' *Using let we will create a new vector with a user defined name 
        + = db({$aux}.out)         *Now we use the use the plot "stored" in aux to get our data
                                   *The + sign is used the instruction continues on another line, not to append
                                   *The {} is used to "delimit" the variable to $aux, without {} it would be seen as $aux.out
    end
end

set curplot = $acplot              *Set current plot as the plot generated by us
plot all vs ac1.frequency          *User created plot are of type 'unknown' so it will not know to plot frequency as xaxis by default.
                                   *Furthermore we have not stored the frequency vector inside our plot so I used any ac analysis to get the data

I hope that this solution will help others and if anyone knows other solutions or have any improvement regarding mine I would like to know

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can run this script from ngspice? nice. Hard to find many advanced examples or literature about how to use ngspice and its limits. \$\endgroup\$
    – pat
    Commented Sep 21, 2021 at 6:35
  • \$\begingroup\$ You can accept your own answer (the green check mark). That will tell other people, in the future, searching for similar problems, that this one has an accepted solution. \$\endgroup\$ Commented Sep 21, 2021 at 7:18
1
\$\begingroup\$

If you're good at plotting within Octave or from python then you can do what I do, which is to save your data from ngspice and then read the data into Octave or python and plot it from there any way you want. There are a few libraries around but the ones I use for reading the data are both written by Werner Hoch, called spice_read.py for python and spice_readfile.m for Octave (or Matlab). A little googling should find the latest versions. You first run your simulation with:

ngspice -b -rmy_output_filename myspicefile.cir

and then you load the "my_output_filename" in your python or octave program. If you don't know much about python or octave then there will be a learning curve, but there are many rewards to be had from learning such a scientific programming environment. Beyond plotting you can filter, process, and automate your analysis.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.