Menu

Min.w.example for s_xfer, and strange results

sdaau
2010-08-31
2017-10-30
  • sdaau

    sdaau - 2010-08-31

    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:

    * Schematics Netlist *
    .param SRCFREQ= 5KHz
    vcm         IN 0    dc 0 ac 1 sin(0 2.5 {SRCFREQ}) 
    R_Rtest     OUT 0   1K
    AU1         IN OUT  AU1SXFER
    .MODEL AU1SXFER s_xfer (  num_coeff=[1] den_coeff=[1 0.937 1.689 0.974 0.581
    + 0.123] int_ic=[0 0 0 0 0 0] denormalized_freq=18849.5559)
    .control
    ac dec 50 10Hz 100kHz
    hardcopy ac.ps IN OUT
    .endc
    

    This I call in batch mode, and get ac.ps generated:

    $ ngspice -b test.net 
    Circuit: * schematics netlist *
    Doing analysis at TEMP = 27.000000 and TNOM = 27.000000
    No. of Data Rows : 201
    The file "ac.ps" may be printed on a postscript printer.
    Note: No ".plot", ".print", or ".fourier" lines; no simulations run
    

    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:

     
  • sdaau

    sdaau - 2010-08-31

    Bloody stupid sourceforge not allowing edits of posts and not having previews !! :( :( :(

    Here is the wrongly parsed diagram from before:

     
  • sdaau

    sdaau - 2010-08-31

    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:

    * Schematics Netlist *
    .param SRCFREQ= 5KHz
    vcm         IN 0    dc 0 ac 1 sin(0 2.5 {SRCFREQ}) 
    R_Rtest     OUT 0   1K
    AU1         IN OUT  AU1SXFER
    .MODEL AU1SXFER s_xfer (  num_coeff=[1] den_coeff=[1 0.937 1.689 0.974 0.581
    + 0.123] int_ic=[0 0 0 0 0 0] denormalized_freq=18849.5559)
    .control
    ac dec 50 10Hz 100kHz
    * show the OUT vector - it is complex
    print OUT
    * plot the diagram 
    hardcopy ac.ps abs(IN) abs(OUT)
    .endc
    

    and the result of running it is:

    $ ngspice -b cheb3.net 
    Circuit: * schematics netlist *
    Doing analysis at TEMP = 27.000000 and TNOM = 27.000000
    No. of Data Rows : 201
                                 * schematics netlist *
                                 AC Analysis  Tue Aug 31 06:37:38  2010
    --------------------------------------------------------------------------------
    Index   frequency       out                             
    --------------------------------------------------------------------------------
    0   1.000000e+01    8.128781e+00,   -1.27997e-01    
    1   1.047129e+01    8.128656e+00,   -1.34028e-01    
    2   1.096478e+01    8.128518e+00,   -1.40343e-01    
    ...
    199 9.549926e+04    9.017648e-10,   -3.06164e-08    
    200 1.000000e+05    6.839718e-10,   -2.43177e-08    
    The file "ac.ps" may be printed on a postscript printer.
    Note: No ".plot", ".print", or ".fourier" lines; no simulations run
    

    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!!!

     
  • sdaau

    sdaau - 2010-08-31

    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:

    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 other one is "eCircuit Center: Noise Analysis - Resistor Example":

    http://www.ecircuitcenter.com/Circuits/Noise/Noise_Analysis/res_noise.htm
    SPICE NOISE ANALYSIS

    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:

    * Schematics Netlist *
    .param SRCFREQ= 5KHz
    vcm         IN 0    dc 0 ac 1 sin(0 2.5 {SRCFREQ}) 
    R_Rtest     OUT 0   1K
    AU1         IN OUT  AU1SXFER
    .MODEL AU1SXFER s_xfer (  num_coeff=[1] den_coeff=[1 0.937 1.689 0.974 0.581
    + 0.123] int_ic=[0 0 0 0 0 0] denormalized_freq=18849.5559)
    .control
    * set color for postscript output
    set hcopypscolor=1
    * the 'ac' command creates (and setplot's) plot ac1, which contains vector OUT, which is complex
    ac dec 50 10Hz 1Meg
    *~ * show the OUT vector - it is complex
    *~ print OUT
    *~ * note that 'tran' analysis will *also* have a vector called OUT; 
    *~ * therefore, if instead of ac we use:
    *~ tran 10us 1000us
    *~ * we will *still* have a workable vector for noise; 
    *~ * although its meaning will be different!! 
    * the 'noise' command creates (and setplot's) plots noise1 (contains vectors inoise_spectrum, onoise_spectrum), noise2(contains inoise_total  onoise_total),
    * v(real(OUT)) ; v(imag(OUT)) will also generate the same graph; 
    * however, v(OUT) will generate no graph whatsoever !! 
    noise v(abs(OUT)) vcm dec 50 10Hz 1Meg
    * choose the right plot first
    setplot noise1
    * plot the diagram 
    * no need for abs(inoise_spectrum) abs(onoise_spectrum) here, real values
    * note onoise_spectrum here stays flat! 
    * use ac1.OUT syntax to refer to a vector in another plot than the current one
    * the default grid, or just ylog are not very clear - set loglog for both axes
    hardcopy noise.ps inoise_spectrum onoise_spectrum abs(ac1.OUT) loglog xlimit 10 1Meg 
    .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!

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.