The Designer's Guide Community
Forum
Welcome, Guest. Please Login or Register. Please follow the Forum guidelines.
Mar 29th, 2024, 1:37am
Pages: 1
Send Topic Print
Detecting Minimum and maximum voltage (Read 3816 times)
Dan M
New Member
*
Offline



Posts: 3

Detecting Minimum and maximum voltage
Oct 15th, 2016, 10:40am
 
I have the following VerilogA code and it's not working so well

module sample (in);

input in;
electrical in;

real minv, maxv;

analog begin
  @(initial_step) begin minv=V(in)-0.01; maxv=V(in)+0.01; end
  @(cross(V(in)-minv,-1)) minv=V(in)-0.01;
  @(cross(V(in)-maxv,1)) maxv=V(in)+0.01;
  @(timer(1n,1n)) begin $display ("%f %f",minv,maxv); minv=V(in)-0.01; maxv=V(in)+0.01; end
end
endmodule

So effectively I'm trying to find the minimum and maximum voltage on 'in' over 1 nanosecond blocks of time.  I'm putting in the '0.01' offsets to try to provide some sort of "hysteresis" so it's not constantly triggering the @cross, with the understanding that it obviously throws in up 10mV of error.

What I'm seeing is that the @crosss statements aren't triggering consistently.  If I force a lot of time steps with an @(timer(100f,100f)) it seems to work better, but that's a horrible solution.  If I increase time or voltage tolerance in the @cross it seems to work better, but not much, and if I add to hysteresis it works a lot better, but to get good results I need to change the 10mV to well over 100mV, and even then it's not perfect, and I'm not comfortable with the lack of accuracy.  The problem seems to come when there is a very high frequency signal (i.e. on input edges).

If anyone has some thoughts as to how to maek this work better, I'd definitely appreciate it.

Thanks!
Back to top
 
 
View Profile   IP Logged
Ken Kundert
Global Moderator
*****
Offline



Posts: 2384
Silicon Valley
Re: Detecting Minimum and maximum voltage
Reply #1 - Oct 16th, 2016, 1:52pm
 
I recommend simply getting rid of the cross events. And of course, you need to add if statements so you only overwrite minv and maxv if the input is small or larger than the saved value.

Code:
module sample (in);
input in;
electrical in;
real minv, maxv;
analog begin
    @(initial_step) begin
	  minv=V(in);
	  maxv=V(in);
    end
    if (V(in) < minv)
	  minv = V(in);
    if (V(in) > maxv)
	  maxv = V(in);
    @(timer(1n,1n)) begin
	  $display ("minimum = %rV, maximum = %rV", minv, maxv);
	  minv=V(in);
	  maxv=V(in);
    end
end
endmodule 



-Ken
Back to top
 
 
View Profile WWW   IP Logged
Dan M
New Member
*
Offline



Posts: 3

Re: Detecting Minimum and maximum voltage
Reply #2 - Oct 17th, 2016, 4:57am
 
Any particular reason why the @cross is causing problems?
Back to top
 
 
View Profile   IP Logged
Ken Kundert
Global Moderator
*****
Offline



Posts: 2384
Silicon Valley
Re: Detecting Minimum and maximum voltage
Reply #3 - Oct 19th, 2016, 9:03pm
 
Fundamentally cross functions are used to model thresholds in your circuit. But what you are trying to model is essentially a peak detector. No thresholds involved.  Instead you were trying to use cross functions to detect when the signal had changed by some small amount. Doing so is inherently inefficient.

-Ken
Back to top
 
 
View Profile WWW   IP Logged
Geoffrey_Coram
Senior Fellow
******
Offline



Posts: 1998
Massachusetts, USA
Re: Detecting Minimum and maximum voltage
Reply #4 - Oct 26th, 2016, 8:13am
 
The cross event is trying to resolve precisely when the crossing occurred, and the simulator may reject timepoints that are computed too far past the crossing time.  But you don't actually care when the crossing occurred.
Back to top
 
 

If at first you do succeed, STOP, raise your standards, and stop wasting your time.
View Profile WWW   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.