I finally managed to come up with a MWE (minimal working example) for a Laplace s_xfer transfer characteristic analysis in ngspice; and this is, strangely enough, thanks to an "Altium Protel" (?) page, "S-Domain Transfer Function (Single-Ended I/O)".
I have simply copypasted their s_xfer definition, and added other commands (like ac analysis, since that is what they mention using) to reconstruct their test case:
Obviously, if the characteristic goes negative, and it isn't supposed to - the first thing one may think of, is to actually get the absolute value of it… And that is, indeed, the solution.
In fact, if one uses the 'print' command, one can see that the OUT vector is complex (has both real and imaginary entries) - as expected from theory, where the transfer characteristic/frequency response is in principle complex; and in theory, one also applies absolute value to the transfer characteristic, so as to obtain a 'gain characteristic' (in terms of http://en.wikipedia.org/wiki/Transfer_function).
So, the only change in the code is using 'abs(IN) abs(OUT)' when plotting, instead of 'IN OUT' directly - so the code becomes:
Well, while we're at this example, a short review of noise analysis may be in order. In respect to noise analysis, I had much trouble understanding how it works, until I found these two external resources, which may or may not apply to ngspice. The first is "kxcad.net Home - Electronic Index - Altium(Protel) Index - SimModelsRef - Noise Analysis", which notes:
http://www.kxcad.net/Altium_Protel/SimModelsRef/simulation_analyses/noise_analysis.htm
Noise analysis lets you measure the noise contributions of resistors and semiconductor devices by plotting the Noise Spectral Density, which is the noise measured in Volts squared per Hertz (V2/Hz). Capacitors, inductors and controlled sources are treated as noise free.
The Noise Analysis essentially does the work in the above example for you. SPICE performs Noise Analysis together with an AC Analysis statement. For example, the statement
.NOISE V(4) V1 5
asks SPICE to calculate the equivalent noise at both output V(4) and V1 at every 5th frequency point in the AC analysis.
So, the second quote, even if not describing the same syntax as 'ngspice' - it points out correctly that first AC analysis needs to be performed, in order to carry out noise analysis; or in other words, in the above ".noise v(4)" the "4" must be a vector which was calculated in a previous simulation run! (in fact, you could even run 'tran' analysis first, and supply a vector from its results to 'noise'; it will run, but will produce strange results!)
The first quote, then, states that only effects in respect to noise should be expected from semiconductors and resistors; since the s_xfer doesn't have a specific parameter related to noise, it probably shouldn't influence it. On the other hand, s_xfer does have a frequency transfer characteristic - and we should expect to see its signature in the noise analysis.
Well, finally this is the code (test.net) that performs noise analysis:
*SchematicsNetlist*.paramSRCFREQ=5KHzvcmIN0dc0ac1sin(02.5{SRCFREQ})R_RtestOUT01KAU1INOUTAU1SXFER.MODELAU1SXFERs_xfer(num_coeff=[1]den_coeff=[10.9371.6890.9740.581+0.123]int_ic=[000000]denormalized_freq=18849.5559).control*setcolorforpostscriptoutputsethcopypscolor=1*the'ac'commandcreates(andsetplot's)plotac1,whichcontainsvectorOUT,whichiscomplexacdec5010Hz1Meg*~*showtheOUTvector-itiscomplex*~printOUT*~*notethat'tran'analysiswill*also*haveavectorcalledOUT; *~*therefore,ifinsteadofacweuse:*~tran10us1000us*~*wewill*still*haveaworkablevectorfornoise; *~*althoughitsmeaningwillbedifferent!!*the'noise'commandcreates(andsetplot's)plotsnoise1(containsvectorsinoise_spectrum,onoise_spectrum),noise2(containsinoise_totalonoise_total),*v(real(OUT)); v(imag(OUT)) will also generate the same graph; *however,v(OUT)willgeneratenographwhatsoever!!noisev(abs(OUT))vcmdec5010Hz1Meg*choosetherightplotfirstsetplotnoise1*plotthediagram*noneedforabs(inoise_spectrum)abs(onoise_spectrum)here,realvalues*noteonoise_spectrumherestaysflat!*useac1.OUTsyntaxtorefertoavectorinanotherplotthanthecurrentone*thedefaultgrid,orjustylogarenotveryclear-setloglogforbothaxeshardcopynoise.psinoise_spectrumonoise_spectrumabs(ac1.OUT)loglogxlimit101Meg.endc
Then, I simply call batch mode "ngspice -b test.net", and I get 'noise.ps' generated, which is shown below:
Note that:
The graph shows that the 'noise' analysis keeps the output (onoise_spectrum) constant, and changes the input (inoise_spectrum) correspondingly - so input noise levels have to be increased with rising freqency to obtain constant output (which matches the low-pass nature of the modeled s_xfer filter)
the filter signature in the noise gets obvious in the diagram, only if loglog is specified (for both x and y axes)
If you want to draw vectors from 'plot' (sets) other than the currently set one, you should use the syntax plot.vector
I just don't get two things:
Why does inoise_spectrum get constant again above, if I read correctly, ~17 KHz, when the filter keeps going down above that frequency?
Can I somehow change the x-axis grid spacing in logarithmic view (or should I use 'gnuplot' for that)?
Well, hope someone may find this useful,
Cheers!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi all,
I finally managed to come up with a MWE (minimal working example) for a Laplace s_xfer transfer characteristic analysis in ngspice; and this is, strangely enough, thanks to an "Altium Protel" (?) page, "S-Domain Transfer Function (Single-Ended I/O)".
I have simply copypasted their s_xfer definition, and added other commands (like ac analysis, since that is what they mention using) to reconstruct their test case:
so the code is:
This I call in batch mode, and get ac.ps generated:
Now, the strange thing is this - the output I get for ac.ps is in fact this image:
However, the "Altium Protel" in fact shows this diagram:
Bloody stupid sourceforge not allowing edits of posts and not having previews !! :( :( :(
Here is the wrongly parsed diagram from before:

YES! Well, I think I got it :)
Obviously, if the characteristic goes negative, and it isn't supposed to - the first thing one may think of, is to actually get the absolute value of it… And that is, indeed, the solution.
In fact, if one uses the 'print' command, one can see that the OUT vector is complex (has both real and imaginary entries) - as expected from theory, where the transfer characteristic/frequency response is in principle complex; and in theory, one also applies absolute value to the transfer characteristic, so as to obtain a 'gain characteristic' (in terms of http://en.wikipedia.org/wiki/Transfer_function).
So, the only change in the code is using 'abs(IN) abs(OUT)' when plotting, instead of 'IN OUT' directly - so the code becomes:
and the result of running it is:
while the diagram obtained is:
that is, it seems to be exactly the same as in the original Altium page :)
Well… nice to have this covered, finally … :D
Cheers!!!
Hi all,
Well, while we're at this example, a short review of noise analysis may be in order. In respect to noise analysis, I had much trouble understanding how it works, until I found these two external resources, which may or may not apply to ngspice. The first is "kxcad.net Home - Electronic Index - Altium(Protel) Index - SimModelsRef - Noise Analysis", which notes:
The other one is "eCircuit Center: Noise Analysis - Resistor Example":
The Noise Analysis essentially does the work in the above example for you. SPICE performs Noise Analysis together with an AC Analysis statement. For example, the statement
.NOISE V(4) V1 5
asks SPICE to calculate the equivalent noise at both output V(4) and V1 at every 5th frequency point in the AC analysis.
So, the second quote, even if not describing the same syntax as 'ngspice' - it points out correctly that first AC analysis needs to be performed, in order to carry out noise analysis; or in other words, in the above ".noise v(4)" the "4" must be a vector which was calculated in a previous simulation run! (in fact, you could even run 'tran' analysis first, and supply a vector from its results to 'noise'; it will run, but will produce strange results!)
The first quote, then, states that only effects in respect to noise should be expected from semiconductors and resistors; since the s_xfer doesn't have a specific parameter related to noise, it probably shouldn't influence it. On the other hand, s_xfer does have a frequency transfer characteristic - and we should expect to see its signature in the noise analysis.
Well, finally this is the code (test.net) that performs noise analysis:
Then, I simply call batch mode "ngspice -b test.net", and I get 'noise.ps' generated, which is shown below:
Note that:
The graph shows that the 'noise' analysis keeps the output (onoise_spectrum) constant, and changes the input (inoise_spectrum) correspondingly - so input noise levels have to be increased with rising freqency to obtain constant output (which matches the low-pass nature of the modeled s_xfer filter)
the filter signature in the noise gets obvious in the diagram, only if loglog is specified (for both x and y axes)
If you want to draw vectors from 'plot' (sets) other than the currently set one, you should use the syntax plot.vector
I just don't get two things:
Why does inoise_spectrum get constant again above, if I read correctly, ~17 KHz, when the filter keeps going down above that frequency?
Can I somehow change the x-axis grid spacing in logarithmic view (or should I use 'gnuplot' for that)?
Well, hope someone may find this useful,
Cheers!