2
\$\begingroup\$

I am trying to change a parameter in LT Spice with if statements and make it change on whether a different value is even or odd.

Mathematically, this would be easy with a remainder or modulous function because XR2 or X%2= 0 means it's even and XR2 or X%2= 1 means it's odd. However, I've looked through the LTSpice .param documentation and couldn't find any support for this type of function. Does anyone know another way to perform this operation or if this function goes by a different name?

\$\endgroup\$
1
  • \$\begingroup\$ I am changing the voltage used to bias a group of FETs in a complex amplifier. I wanted to run a full factorial of possible choices to find a good output. So I used a .step function to step through 1-16 and using the param operation and depending on whether or not its divisible by 8, 4, 2, or 1 and if its even or odd set the voltage accordingly. \$\endgroup\$ Mar 5 '13 at 18:24
0
\$\begingroup\$

LTspice does support Modulus function :"Mod", for example MOD(2,2)=0, MOD(3,2)=1.

\$\endgroup\$
2
  • \$\begingroup\$ Can you provide a reference? I found that there is the % operator for modulo, here \$\endgroup\$
    – clabacchio
    Mar 12 '13 at 14:39
  • \$\begingroup\$ How did this answer get slected? There is no mod() function in LTspice. There is an idtmod(), there can be a custom .func defined, but there is no mod(x,y) function in the default LTspice installation, be it IV or XVII. \$\endgroup\$ Apr 13 at 15:02
1
\$\begingroup\$

As a general comment: most models in SPICE are careful with discontinuities, especially in conductance. This is because the Newton-Raphson algorithm may have problems with the discontinuity and lose convergence.

You don't say what type of device you are trying to change or if you are dynamically changing the device (during sim) or at .IC so I can only give you a general guide line.

I don't run LTSpice so my suggestion may not be applicable.

I would use a multiple input controlled source and define the parameters to derive a contorl voltage that then modulates either a voltage controlled voltage source, current source etc.

The multiple input controlled source takes the parameters of \$ k_0 +k_1V_1 +k_2V_2 + \$... and cross terms. So you are building a macro model that emulates the function needed. .IC can be defined on a run by run basis if that is what you are doing.

the definition is E +node -node POLY(value) etc. etc.

\$\endgroup\$
1
\$\begingroup\$

This isn't the cleanest solution, but it works. I was able to calculate the remainder myself. This can be extended to any modulous function and I will use Y as my modulous variable.

Assuming that X is an integer, you can take X/Y, round down to the nearest integer, multiply this by Y, and subtract it from the original value. In all, it looks something like this.

.param A1 {X - floor(X/Y)*Y}

Since my code already uses a ceil() and division for calculating X, it is already wasteful on top of this extensive calculation. If anyone can find a more efficient way of doing this I would appreciate it.

\$\endgroup\$
1
  • \$\begingroup\$ This should have been the answer. It works slightly faster with int(). \$\endgroup\$ Apr 13 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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