The Designer's Guide Community
Forum
Welcome, Guest. Please Login or Register. Please follow the Forum guidelines.
Dec 5th, 2024, 4:30pm
Pages: 1
Send Topic Print
Clamping Function (Read 886 times)
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
 
 
View Profile   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 (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
 

Back to top
 
« Last Edit: Feb 21st, 2023, 9:02pm by cookacounty »  
View Profile   IP Logged
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<vlow_1) soft_limit = vlow_1 -band*(1.0-exp( (in-vlow_1 )/band)) ;
	else		    soft_limit = in ;
end
endfunction

 

Back to top
 
 
View Profile   IP Logged
Pages: 1
Send Topic Print
Copyright 2002-2024 Designer’s Guide Consulting, Inc. Designer’s Guide® is a registered trademark of Designer’s Guide Consulting, Inc. All rights reserved. Send comments or questions to editor@designers-guide.org. Consider submitting a paper or model.