LEC Forum

Analysis => 3-Phase Power => Topic started by: Dave Loucks on June 13, 2016, 11:01:44 AM

Title: LTSPICE
Post by: Dave Loucks on June 13, 2016, 11:01:44 AM
You may have noticed that in several of the postings on this site I mentioned that Linear Technology offers a free SPICE program that can be downloaded from their website (http://www.linear.com/designtools/software/#LTspice).  The built in help talks about some tutorials, which while I haven't reviewed in detail, look great from a cursory scan:

There are also independent user groups and forums.  The one I use the most is:  https://groups.yahoo.com/neo/groups/LTspice/info (//http://).

As I've played around with LTSPICE, I too have created various screencast videos that show certain features.  In the interest of sharing what I've learned I've included them here.  Enjoy.


Title: LTSPICE Constant Time Base and Decimation
Post by: Dave Loucks on June 27, 2016, 12:31:05 PM
When running a SPICE simulation, the simulation engine computes a new set of values each sample interval.  However, the sample intervals are not constant.  Likely to save space, the SPICE engine automatically drops sample rate down when there is less high frequency content.

This is great, except that if someone wants to use the data to compute an FFT, in say Excel, the variable sample rate will not work.  Excel's FFT (and pretty much everyone else's!) requires a constant sample rate over the interval.

How do you convert SPICE output with a variable sample rate into a constant sample rate?

One way is to resample at a new constant sample rate.  The new sample rate could be something as simple as the number of samples divided by duration of the time interval over which the original data spanned.

So, I would start by reading the first point and copying to a new file, but the second point wouldn't necessarily (probably won't) be the next sampled value from the original data.  Instead, the program will discard any sampled data that occurs prior to the next new calculated sample interval.  Data that is collected between samples would be linearly interpolated.



'Resample data that includes sample-to-sample time variability between  each sample to a new collection where the sample-to-sample time interval is constant

'New Sample Rate = (# of datapoints) / (time interval of sampled data)

Sub ConstTimeConv()
    Dim sAveSampleRate As Single
    Dim sTimeArray(600000, 4) As Single
    Dim sSlope As Single, sB As Single
    Dim i As Long, lPtr As Long
    Dim sStartTime As Single, sEndTime As Single
    Dim lDataPts As Long, k As Long
   
    lDataPts = Worksheets("input").Range("a2").Value
   
    'resample variable time series data into constant interval data
    'use linear interpolation between samples to compute value at fixed sample time
   
    sStartTime = Worksheets("input").Cells(6, 1).Value
    sEndTime = Worksheets("input").Cells(6 + lDataPts - 1, 1).Value
    sAveSampleRate = (sEndTime - sStartTime) / lDataPts
    Worksheets("input").Range("a3").Value = 1 / sAveSampleRate
   
    lPtr = 6  'preset to first data row
    If lDataPts > 700000 Then lDataPts = 600000
   
    For i = 0 To lDataPts - 1
        'determine the next sample time value
        sTimeArray(i + 1, 0) = sAveSampleRate * (i + 1)
       
        'now determine what the sampled value would be for that new time (straight line interpolation)
        'determine slope between sampled values
        'find the next sampled data time that is just barely larger than this next sample time
        Do
            If Worksheets("input").Cells(lPtr, 1).Value > sTimeArray(i + 1, 0) Then
                Exit Do
            End If
            lPtr = lPtr + 1
            If lPtr > lDataPts Then
                Exit Do
            End If
        Loop
        For k = 0 To 2
            sSlope = (Worksheets("input").Cells(lPtr, 2 + k).Value - Worksheets("input").Cells(lPtr - 1, 2 + k).Value) / _
            (Worksheets("input").Cells(lPtr, 1).Value - Worksheets("input").Cells(lPtr - 1, 1).Value)
           
            'now find b (y-intercept)
            sB = Worksheets("input").Cells(lPtr + 1, 2 + k).Value - sSlope * Worksheets("input").Cells(lPtr + 2, 1).Value
           
            'now solve for the interpolated value
            sTimeArray(i, k + 1) = sTimeArray(i + 1, 0) * sSlope + sB
        Next
       
    Next
   
    'now transfer this array to the sync tab
    Worksheets("input").Cells(6, 6).Resize(lDataPts, 4) = sTimeArray
   
   
End Sub


At this point the data table output will have constant sample-to-sample time intervals.

But what if those intervals are too short?  Maybe your work involves measuring signals in the kHz range, but the sampled data is in the 10's of MHz?

To fix that, you can again resample, but this time you simple skip every so many data points.  This in effect simulates an A/D that samples at a lower sample rate.  This so-called "decimation" (not precisely correct since decimate implies "1 out of 10" being sampled and we can apply any ratio here) reduces the frequency response of the sampled signal while simultaneously increasing the frequency resolution.


Private Sub cmdDecimate_Click()
    Dim gasCD_raw(600000, 4) As Single
    Dim gasCD_truc(600000, 4) As Single
    Dim i As Long, j As Long, k As Integer
    Dim iSkipValue As Integer
   
    j = 0
    iSkipValue = Worksheets("input").Range("a1").Value    'number of values to skip each time
    If iSkipValue < 1 Then iSkipValue = 10              'default to 10 if not given
   
    'transfer decimated data to array variable
    For i = 0 To 599999 - 3
        If i Mod iSkipValue = 0 Then
            For k = 0 To 3
                'transfer each element of row
                gasCD_truc(j, k) = Worksheets("input").Cells(6 + i, k + 6).Value
            Next
            j = j + 1
        End If
    Next
    Worksheets("input").Range("k6").Resize(j, 4) = gasCD_truc
    'put headers over this decimated data
    Sheets("input").Select
    Range("a5:d5").Select
    Selection.Copy
    Application.CutCopyMode = False
    Selection.Copy
    Range("k5").Select
    ActiveSheet.Paste
   
End Sub


An example spreadsheet is attached below.  As I develop later versions, I'll include those too.

Starting with version 5 I sped up the algorithm by not performing as many back-and-forth operations between the underlying VBA code and the worksheet, so it runs quite a bit faster.  Also cleaned up some bugs.

Here's a video tutorial on how to use that resampling and decimator spreadsheet.
(https://pps2.com/images/smf/ltspice/decimate_video.jpg) (https://pps2.com/v/s/1/decimate.php)

I added some new features to this program starting in version 12 (V12), so here's a update tutorial video:
(https://pps2.com/images/smf/ltspice/decimatorV12_play_button.jpg) (https://pps2.com/v/s/1/dv12.php)

Finally, the program moving past version 12 is being called TBAD.xlsm (Time Base and Decimator).  Note that V12 has a memory leak in the "Get Txt File" and the "Auto" function.

V13 (Jan-2018) Fixed the memory leak and added new features.  First feature is it supports up to 800 000 rows and up to 36 columns of data.  Previously a spreadsheet this sized crashed my 32-bit version of Excel even with 32 GB of RAM.  Now I block switch memory in and out and can create spreadsheets of several hundred MB with no problem.  Also this new version exports the "full" resolution as well as the decimated version of the resampled data as a CSV file to import into MATLAB or some other post-processing tool.  You'll need to edit the path to where you want to store these files (I don't include a dialog... you'll need to edit VBA code... sorry... may add this in a future version.... in the meantime, follow the instructions below to change the path to the output directory for those CSV files.)
Note that these programs include the "Mapping" tab.  Mapping, as default (but can be unchecked) performs two roles:

Moved to a new naming scheme in early 2018 (TBAD)

Here's a very abbreviated tutorial on the new features.  The important thing is that this version runs more reliably.  But you'll need to open the VBA editor (like the other versions) and change the path to the destination directory for where you would write the output files created by this program (you can always just copy paste the data from the "resampled" and "decimate" tabs too.)

(http://pps2.com/images/smf/poweranalysis/tbad7.png) (https://pps2.com/v/s/1/tbad7.php)