Forum
Aug 3rd, 2024, 5:29am
 Pages: 1
 cookacounty New Member Offline Posts: 3 Clamping Function Feb 21st, 2023, 7:25am   Is there a mathematical function anyone would recommend to clamp a value between two limits? Using an "if" statement is obviously a bad idea.For example you have a voltage "Vlim" that you want to clamp between -2 and +2. The function would do essentially nothing until the limits. Back to top IP Logged
 cookacounty New Member Offline Posts: 3 Re: Clamping Function Reply #1 - Feb 21st, 2023, 11:48am   Seems like a "soft limiter is the solution. I notice it still has if statements but is continuous around them so maybe that's okay Code:```https://simplis.com/documentation/simetrix/verilog_a_reference/topics/writingverilog_acode_asoftlimiter.htm ``` Code:```'include "disciplines.vams" module soft_limiter(in, out) ; electrical in, out ; parameter real vlow=-1.0, vhigh=1.0, soft=0.1 from (0:1.0) ; localparam real band = (vhigh-vlow)*soft, vlow_1 = vlow+band, vhigh_1 = vhigh-band ; real vin ; analog begin @(initial_step) if (vhighvhigh_1) V(out) <+ vhigh_1+band*(1.0-exp(-(vin-vhigh_1)/band)); else if (vin
 cookacounty New Member Offline Posts: 3 Re: Clamping Function Reply #2 - Feb 21st, 2023, 12:18pm   In a function format for use with "real" datatypes: Code:```analog function real soft_limit; input in; real in; input lim_pos,lim_neg,soft; real lim_pos,lim_neg,soft; // Soft should be from 0 to 0.5 real band = (lim_pos-lim_neg)*soft, vlow_1 = lim_neg+band, vhigh_1 = lim_pos-band; begin if (in>vhigh_1) soft_limit = vhigh_1+band*(1.0-exp(-(in-vhigh_1)/band)); else if (in
 Pages: 1