The Designer's Guide Community Forum
https://designers-guide.org/forum/YaBB.pl
Modeling >> Behavioral Models >> Clamping Function
https://designers-guide.org/forum/YaBB.pl?num=1676993139

Message started by cookacounty on Feb 21st, 2023, 7:25am

Title: Clamping Function
Post by cookacounty on 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.

Title: Re: Clamping Function
Post by cookacounty on 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 (vhigh<vlow)
begin
$strobe("Lower limit must be less than higher limit") ;
$finish ;
end
vin = V(in) ;
if (vin>vhigh_1)
V(out) <+ vhigh_1+band*(1.0-exp(-(vin-vhigh_1)/band));
else if (vin<vlow_1)
V(out) <+ vlow_1-band*(1.0-exp((vin-vlow_1)/band)) ;
else
V(out) <+ vin ;
end
endmodule

Title: Re: Clamping Function
Post by cookacounty on 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<vlow_1) soft_limit = vlow_1 -band*(1.0-exp( (in-vlow_1 )/band)) ;
     else                soft_limit = in ;
end
endfunction

The Designer's Guide Community Forum » Powered by YaBB 2.2.2!
YaBB © 2000-2008. All Rights Reserved.