• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Mixed-Signal Design
  3. straight line (best) fit using viva calculator

Stats

  • Locked Locked
  • Replies 18
  • Subscribers 64
  • Views 29800
  • 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
  • zeloufi
    zeloufi over 7 years ago in reply to Andrew Beckett

    Hello Andrew

    I use positively this function and it is great !
    Thank you very much !

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Reply
  • zeloufi
    zeloufi over 7 years ago in reply to Andrew Beckett

    Hello Andrew

    I use positively this function and it is great !
    Thank you very much !

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Children
No Data

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