Home
  • Products
  • Solutions
  • Support
  • Company

This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  • Products
  • Solutions
  • Support
  • Company
Community Mixed-Signal Design straight line (best) fit using viva calculator

Stats

  • Locked Locked
  • Replies 18
  • Subscribers 64
  • Views 27600
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

straight line (best) fit using viva calculator

vamshiky
vamshiky over 7 years ago

Hi,

I need to measure peak to peak variation of phase response (from an AC sim) w.r.t a straight line fit(best fit and not the end point fit)  for the same.

Is it doable using viva calculator or verilogAMS ?

 Thanks,

vamshiky

  • Cancel
Parents
  • Andrew Beckett
    Andrew Beckett over 7 years ago

    There's no built-in function to do this, and I'd not written one so far (it's been one of those things I've been meaning to for a while because I think it's come up in the past). So I just quickly wrote this code below.

    To use it, save the code in a file called "abBestFit.ils" and load it from your .cdsinit using load("/path/to/abBestFit.ils") - ensure you keep the .ils suffix. Unfortunately you can't (yet) use the "fx" button in the calculator to register it with the calculator, so for now you'll need to just surround your waveform expression with abBestFit(...) and then  you can plot that.

    Regards,

    Andrew.

    /* abBestFit.ils
    
    Author     A.D.Beckett
    Group      Custom IC (UK), Cadence Design Systems Ltd.
    Language   SKILL
    Date       Apr 05, 2018 
    Modified   Nov 22, 2023 
    By         A.D.Beckett
    
    Compute a best fit line for an input waveform. Uses
    Simple Linear Regression to compute the slope and intercept
    and then constructs a waveform with this slope and intercept
    
    Four functions:
    
    abBestFit(waveform) - returns a best fit waveform
    
    abBestFitSlope(waveform) - returns the slope of the best fit line
    
    abBestFitIntercept(waveform) - returns the y intercept of the best fit line
    
    abBestFitCoeffs(waveform) - returns a list of the intercept and slope.
        Note: this function does not handle family waveforms because it
        returns a list - but is useful for single waveforms
    
    Note, this uses SKILL++ lexical scoping so must keep the .ils
    suffix
    
    From IC618 ISR1 can now be added to the calculator via the fx button
    (also in the expression builder via the + button)
    
    ***************************************************
    
    SCCS Info: @(#) abBestFit.ils 11/22/23.14:16:49 1.4
    
    */
    
    (let ()
      /***************************************************************
      *                                                              *
      *                        (mean vector)                         *
      *                                                              *
      *     Internal function to compute the mean of the values      *
      *                        in the vector                         *
      *                                                              *
      ***************************************************************/
      (defun mean (vector)
        (let ((n (drVectorLength vector)) (sum 0.0))
          (for i 0 (sub1 n)
               (setq sum (plus sum (drGetElem vector i))))
          (quotient sum (if (zerop n) 1 n))
          )
        )
      /***************************************************************
      *                                                              *
      *            (uncorrectedStddev vector vectorMean)             *
      *                                                              *
      * Internal function to compute uncorrected standard deviation  *
      *  of vector. Same as population standard deviation. Pass in   *
      *   vector and mean so don't have to compute the mean again    *
      *                                                              *
      ***************************************************************/
      (defun uncorrectedStddev (vector vectorMean)
        (let ((n (drVectorLength vector)) (sum 0.0))
          (for i 0 (sub1 n)
               (setq sum 
                     (plus sum 
                           (expt 
                             (difference (drGetElem vector i) vectorMean)
                             2))))
          (sqrt (quotient sum (if (zerop n) 1 n)))
          )
        )
      /*****************************************************************
      *                                                                *
      *            (sampleCorrCoeff xVec yVec xMean yMean)             *
      *                                                                *
      * Internal function to compute the sample correlation coefficent *
      *                  between the x and y vectors                   *
      *                                                                *
      *****************************************************************/
      (defun sampleCorrCoeff (xVec yVec xMean yMean)
        (let (xSquared ySquared xy xyMean xSquaredMean ySquaredMean)
          (setq xy (times xVec yVec))
          (setq xSquared (expt xVec 2.0))
          (setq ySquared (expt yVec 2.0))
          (setq xyMean (mean xy))
          (setq xSquaredMean (mean xSquared))
          (setq ySquaredMean (mean ySquared))
          (quotient 
            (difference xyMean (times xMean yMean))
            (sqrt
              (times
                (difference xSquaredMean (expt xMean 2.0))
                (difference ySquaredMean (expt yMean 2.0))
                )))
          ))
      /***************************************************************
      *                                                              *
      *                  (computeCoeffs xVec yVec)                   *
      *                                                              *
      *  Internal function to compute the alpha and beta - intercept *
      *             and slope - from the x and y vectors             *
      *                                                              *
      ***************************************************************/
      (defun computeCoeffs (xVec yVec)
        (let (xMean yMean xStddev yStddev rxy alpha beta)
          (setq xMean (mean xVec))
          (setq yMean (mean yVec))
          (setq xStddev (uncorrectedStddev xVec xMean))
          (setq yStddev (uncorrectedStddev yVec yMean))
          (setq rxy (sampleCorrCoeff xVec yVec xMean yMean))
          (setq beta (times rxy (quotient yStddev xStddev)))
          (setq alpha (difference yMean (times beta xMean)))
          (list alpha beta)
          )
        )
      /***************************************************************
      *                                                              *
      *                    (abBestFitCoeffs wave)                    *
      *                                                              *
      *  Global function to compute alpha and beta - intercept and   *
      *          slope of the best fit line and return them          *
      *                                                              *
      ***************************************************************/
      (defglobalfun abBestFitCoeffs (wave)
        (let (xVec yVec)
          (setq xVec (drGetWaveformXVec wave))
          (setq yVec (drGetWaveformYVec wave))
          (when (eq (drGetWaveformYType wave) 'doublecomplex)
            (error "abBestFitCoeffs: Cannot handle waveforms with complex values; use mag() or db20() or similar to convert to double values")
            )
          (computeCoeffs xVec yVec)
          )
        )
      /***************************************************************
      *                                                              *
      *                       (abBestFit wave)                       *
      *                                                              *
      *           Compute the slope and intercept and then           *
      *  create a waveform (with two points) matching the first and  *
      *             last x points of the input waveform              *
      *                                                              *
      ***************************************************************/
      (defglobalfun abBestFit (wave)
        (cond
          ((drIsWaveform wave)
           (let (xVec yVec len firstX lastX newXVec newYVec newWave)
             ;---------------------------------------------------------------
             ; Compute slope and intercept of best fit line
             ;---------------------------------------------------------------
             (setq xVec (drGetWaveformXVec wave))
             (setq yVec (drGetWaveformYVec wave))
             (when (eq (drGetWaveformYType wave) 'doublecomplex)
               (error "abBestFit: Cannot handle waveforms with complex values; use mag() or db20() or similar to convert to double values")
               )
             (destructuringBind (alpha beta) (computeCoeffs xVec yVec)
               ;-------------------------------------------------------------
               ; Construct best fit line waveform
               ;-------------------------------------------------------------
               (setq len (drVectorLength xVec))
               (setq firstX (drGetElem xVec 0))
               (setq lastX (drGetElem xVec (sub1 len)))
               (setq newXVec (drCreateVec 'double 2))
               (setq newYVec (drCreateVec 'double 2))
               (drAddElem newXVec firstX)
               (drAddElem newXVec lastX)
               (drAddElem newYVec (plus alpha (times beta firstX)))
               (drAddElem newYVec (plus alpha (times beta lastX)))
               ;-------------------------------------------------------------
               ; Sort out attributes for new waveform to match input
               ;-------------------------------------------------------------
               (putpropq newXVec (getq xVec units) units)
               (putpropq newXVec (getq xVec name) name)
               (putpropq newYVec (getq yVec units) units)
               (putpropq newYVec (getq yVec name) name)
               (setq newWave (drCreateWaveform newXVec newYVec))
               (famSetExpr newWave `(abBestFit ,(famGetExpr wave)))
               newWave
               )
             )
           )
          ((famIsFamily wave)
           (famMap 'abBestFit wave)
           )
          (t
            (error "abBestFit: cannot handle %L\n" wave)
            )
          )
        )
      /*******************************************************************
      *                                                                  *
      *                      (abBestFitSlope wave)                       *
      *                                                                  *
      * Compute the slope (beta) of the best fit through a set of points *
      *                                                                  *
      *******************************************************************/
      (defglobalfun abBestFitSlope (wave)
        (cond
          ((drIsWaveform wave)
           (cadr (abBestFitCoeffs wave))
           )
          ((famIsFamily wave)
           (famMap 'abBestFitSlope wave)
           )
          (t
            (error "abBestFitSlope: cannot handle %L\n" wave)
            )
          )
        )
      /************************************************************************
      *                                                                       *
      *                       (abBestFitIntercept wave)                       *
      *                                                                       *
      * Compute the intercept (alpha) of the best fit through a set of points *
      *                                                                       *
      ************************************************************************/
      (defglobalfun abBestFitIntercept (wave)
        (cond
          ((drIsWaveform wave)
           (car (abBestFitCoeffs wave))
           )
          ((famIsFamily wave)
           (famMap 'abBestFitIntercept wave)
           )
          (t
            (error "abBestFitIntercept: cannot handle %L\n" wave)
            )
          )
        )
      )
    
    ;
    ;;;;;;;;;;;;;;;;;;;;;;;;;; GUI builder information ;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ocnmRegGUIBuilder(
     '(nil
      function abBestFit
      name abBestFit
      description "Best Fit Line"
      category ("Custom Functions")
      analysis (nil
          general (nil
            args (wave )
              signals (nil
                    wave (nil
                          prompt "Signal"
                          tooltip "Waveform"
                          )
               )
              params(nil
              )
            inputrange t
          )
      )
      outputs(result)
     )
    )
    ;
    ;;;;;;;;;;;;;;;;;;;;;;;;;; GUI builder information ;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ocnmRegGUIBuilder(
     '(nil
      function abBestFitIntercept
      name abBestFitIntercept
      description "abBestFitIntercept"
      category ("Custom Functions")
      analysis (nil
          general (nil
            args (wave )
              signals (nil
                    wave (nil
                          prompt "Signal"
                          tooltip "Waveform"
                          )
               )
              params(nil
              )
            inputrange t
          )
      )
      outputs(result)
     )
    )
    ;
    ;;;;;;;;;;;;;;;;;;;;;;;;;; GUI builder information ;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ocnmRegGUIBuilder(
     '(nil
      function abBestFitSlope
      name abBestFitSlope
      description "abBestFitSlope"
      category ("Custom Functions")
      analysis (nil
          general (nil
            args (wave )
              signals (nil
                    wave (nil
                          prompt "Signal"
                          tooltip "Waveform"
                          )
               )
              params(nil
              )
            inputrange t
          )
      )
      outputs(result)
     )
    )

    • Cancel
    • Vote Up +1 Vote Down
    • Cancel
  • vamshiky
    vamshiky over 7 years ago in reply to Andrew Beckett

    Thanks for the quick response Andrew, I shall try and get back.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • vamshiky
    vamshiky over 7 years ago in reply to vamshiky

    Hi Andrew,

    Thanks this was very useful, cross checked with matlab as well.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • vamshiky
    vamshiky over 6 years ago in reply to vamshiky

    Hi Andrew,

    I was wondering if we can get end point fit with the same code.. probably with some minor modifications.

    Thanks,

    vamshiky

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 6 years ago in reply to vamshiky

    So you just want a line between the first and last points? That would be very easy (although it wouldn't need any of the existing best fit code because it's much simpler). Before I show you how, I want to make sure I've understood that this is what you meant...

    Regards,

    Andrew.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Reply
  • Andrew Beckett
    Andrew Beckett over 6 years ago in reply to vamshiky

    So you just want a line between the first and last points? That would be very easy (although it wouldn't need any of the existing best fit code because it's much simpler). Before I show you how, I want to make sure I've understood that this is what you meant...

    Regards,

    Andrew.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Children
  • vamshiky
    vamshiky over 6 years ago in reply to Andrew Beckett

    Yes I want a line between first and last points, so that I can find the deviation from this fitted straight line.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 6 years ago in reply to vamshiky

    I wouldn't call it a fitted line, but here's the code nevertheless. Was quite easy to throw together (it does bear some similarities with abBestFit, but it's considerably simpler as it doesn't need to do any linear regression).

    /* abFirstLastLine.il
    
    Author     A.D.Beckett
    Group      Custom IC (UK), Cadence Design Systems Ltd.
    Language   SKILL
    Date       May 16, 2018 
    Modified   
    By         
    
    Construct a line between first and last points in waveform.
    Not a best fit - just a simple line consisting of just the first
    and last points.
    
    Includes function template so this can be added to the calculator
    with the "fx" button in the calculator function panel.
    
    ***************************************************
    
    SCCS Info: @(#) abFirstLastLine.il 05/16/18.22:55:10 1.1
    
    */
    
    /*********************************************************************
    *                                                                    *
    *                       (abFirstLastLine wave)                       *
    *                                                                    *
    * Output a waveform which just contains the first and last points of *
    *                        the input waveform.                         *
    *                                                                    *
    *********************************************************************/
    (defun abFirstLastLine (wave)
      (cond
        ((drIsWaveform wave)
         (let (xVec yVec newXVec newYVec len newWave)
           (setq xVec (drGetWaveformXVec wave))
           (setq yVec (drGetWaveformYVec wave))
           (setq len (drVectorLength xVec))
           (setq newXVec (drCreateVec 'double 2))
           (setq newYVec (drCreateVec 'double 2))
           (drAddElem newXVec (drGetElem xVec 0))
           (drAddElem newXVec (drGetElem xVec (sub1 len)))
           (drAddElem newYVec (drGetElem yVec 0))
           (drAddElem newYVec (drGetElem yVec (sub1 len)))
           ;-----------------------------------------------------------------
           ; Sort out attributes for new waveform to match input
           ;-----------------------------------------------------------------
           (putpropq newXVec (getq xVec units) units)
           (putpropq newXVec (getq xVec name) name)
           (putpropq newYVec (getq yVec units) units)
           (putpropq newYVec (getq yVec name) name)
           (setq newWave (drCreateWaveform newXVec newYVec))
           (famSetExpr newWave `(abFirstLastLine ,(famGetExpr wave)))
           newWave
           )
         )
        ((famIsFamily wave)
         (famMap 'abFirstLastLine wave)
         )
        (t
          (error "abFirstLastLine: cannot handle %L\n" wave)
          )
        )
      )
    
    ;
    ;;;;;;;;;;;;;;;;;;;;;;;;;; GUI builder information ;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ocnmRegGUIBuilder(
     '(nil
      function abFirstLastLine
      name abFirstLastLine
      description "Create line between first and last points"
      category ("Custom Functions")
      analysis (nil
          general (nil
            args (wave )
              signals (nil
                    wave (nil
                          prompt "Waveform"
                          tooltip "Waveform"
                          )
               )
              params(nil
              )
            inputrange t
          )
      )
      outputs(result)
     )
    )
    

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Hannahyrh
    Hannahyrh over 4 years ago in reply to Andrew Beckett

    Hello Andrew,

    Is there a function that can be used to construct a line between two chosen points (not first and last points) in waveform? Or can this abFirstLastLine function choose the points to construct a line?

    Thanks,

    Hannah

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 4 years ago in reply to Hannahyrh

    Hi Hannah,

    No, but it wouldn't be that hard to write one. Perhaps you can expand upon what you want though by answering these questions?

    1. How would you specify the two points that you want to construct the line to go through? Maybe by specifying two x values and then the function determines the y values at those points to decide the line?
    2. Would the line be extrapolated to the first x and last x value in the source waveform? (so in other words, it would have the same x-range as the source waveform, but would go through the points specified by two specific x values)

    That seems the most logical way to do it to me.

    Andrew 

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Hannahyrh
    Hannahyrh over 4 years ago in reply to Andrew Beckett

    Hello Andrew,

    Thank you for replying to me.

    1.The endpoints fit line function (abFirstLastLine) chooses the first and last points of the input waveform. I am finding a way to draw a fit line of the source waveform with two endpoints different from the first and last points of the source waveform. This means I can choose two x values as the input of the function and set these two corresponding points as the endpoints of the line. 

    2. Yes, the line would have the same x-range as the source waveform, but would go through the points specified by two specific x values.

    Appreciate your help!

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 4 years ago in reply to Hannahyrh

    Hannah,

    Try this. You can use the "fx" button in the function panel of the calculator, or the + button in the ADE Explorer/Assembler expression builder to add to the calculator.

    Regards,

    Andrew

    /* abLineThroughTwoPoints.il
    
    Author     A.D.Beckett
    Group      Custom IC (UK), Cadence Design Systems Ltd.
    Language   SKILL
    Date       Mar 23, 2021 
    Modified   
    By         
    
    Construct a line which passes through the y values at two specified
    x points on an input waveform, and has the same x-range as the
    input waveform.
    
    Includes function template so this can be added to the calculator
    with the "fx" button in the calculator function panel.
    
    ***************************************************
    
    SCCS Info: @(#) abLineThroughTwoPoints.il 03/23/21.20:07:57 1.1
    
    */
    
    /***************************************************************
    *                                                              *
    *             (abLineThroughTwoPoints wave x1 x2)              *
    *                                                              *
    *  Produce a waveform which intersects the original waveform   *
    * at two specified x values. The x-range of the result is the  *
    *                same as the original waveform.                *
    *                                                              *
    ***************************************************************/
    
    (defun abLineThroughTwoPoints (wave x1 x2)
      (cond
        ((drIsWaveform wave)
         (let (xVec yVec newXVec newYVec len newWave y1 y2 m c)
           ;-----------------------------------------------------------------
           ; Calculate slope and intercept from two points
           ;-----------------------------------------------------------------
           (setq y1 (value wave x1))
           (setq y2 (value wave x2))
           (if (equal x1 x2)
             (progn
               (setq m 0.0)
               (setq c y1))
             (progn
               (setq m (quotient (difference y2 y1) (difference x2 x1)))
               (setq c (difference y2 (times m x2)))
               ))
           (setq xVec (drGetWaveformXVec wave))
           (setq yVec (drGetWaveformYVec wave))
           (setq len (drVectorLength xVec))
           (setq newXVec (drCreateVec 'double 2))
           (setq newYVec (drCreateVec 'double 2))
           (drAddElem newXVec (drGetElem xVec 0))
           (drAddElem newXVec (drGetElem xVec (sub1 len)))
           (drAddElem newYVec (plus (times (drGetElem xVec 0) m) c))
           (drAddElem newYVec (plus (times (drGetElem xVec (sub1 len)) m) c))
           ;-----------------------------------------------------------------
           ; Sort out attributes for new waveform to match input
           ;-----------------------------------------------------------------
           (putpropq newXVec (getq xVec units) units)
           (putpropq newXVec (getq xVec name) name)
           (putpropq newYVec (getq yVec units) units)
           (putpropq newYVec (getq yVec name) name)
           (setq newWave (drCreateWaveform newXVec newYVec))
           (famSetExpr newWave `(abLineThroughTwoPoints ,(famGetExpr wave)))
           newWave
           )
         )
        ((famIsFamily wave)
         (famMap 'abLineThroughTwoPoints wave x1 x2)
         )
        (t
          (error "abLineThroughTwoPoints: cannot handle %L\n" wave)
          )
        )
      )
    
    ;
    ;;;;;;;;;;;;;;;;;;;;;;;;;; GUI builder information ;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ocnmRegGUIBuilder(
     '(nil
      function abLineThroughTwoPoints
      name abLineThroughTwoPoints
      description "Generate line which intersects curve at two x values"
      category ("Custom Functions")
      analysis (nil
          general (nil
            args (wave x1 x2 )
              signals (nil
                    wave (nil
                          prompt "Waveform"
                          tooltip "Waveform"
                          )
               )
              params(nil
                     x1 (nil
                           prompt "X1"
                           tooltip "X1"
                           guiRowHint 1
                           type float
                           required t
                     )
                     x2 (nil
                           prompt "X2"
                           tooltip "X2"
                           guiRowHint 1
                           type float
                           required t
                     )
              )
            inputrange t
          )
      )
      outputs(result)
     )
    )
    

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Hannahyrh
    Hannahyrh over 4 years ago in reply to Andrew Beckett

    Hello Andrew,

    I tried this function and it works! Thank you so much for your help!

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information