Contents in this wiki are for entertainment purposes only
This is not fiction ∞ this is psience of mind

File:PWLgenRSW.0.1.0.zip

From Catcliffe Development
Revision as of 12:16, 9 July 2023 by Don (talk | contribs)
Jump to navigation Jump to search

PWLgenRSW.0.1.0.zip(file size: 50 KB, MIME type: application/zip)

Warning: This file type may contain malicious code. By executing it, your system may be compromised.


Summary

A Piece-Wise Linear Function file generator by Codasaurus Hex

VB6 Project PWLgenRSW v.0.1.0

Coded in Vintage VB6

The working VB6 code in the project form module

' Piece-Wise Linear Function Generator for Randomized Square Waves

' File:         PWLgenRSW.frm
' Copyright:    Don@groupKOS.com July 2, 2023
' License:      Universal use of this copyright is granted.

' LTspice XV PWL options
'
' Time Units:
'    s:  seconds
'    ms: milliseconds (1 ms = 0.001 s)
'    us: microseconds (1 µs = 0.000001 s)
'    ns: nanoseconds (1 ns = 0.000000001 s)
'    ps: picoseconds (1 ps = 0.000000000001 s)
'
' Voltage Units:
'
'    V:  volts
'    mV: millivolts (1 mV = 0.001 V)
'    uV: microvolts (1 µV = 0.000001 V)
'
' Current Units:
'
'    A:  amperes
'    mA: milliamperes (1 mA = 0.001 A)
'    uA: microamperes (1 µA = 0.000001 A)

Option Explicit


Private Const DEF_TIME_UNIT = "ms"
Private Const DEF_VALUE_UNIT = "V"
Private Const DEF_AVERAGE_FREQUENCY = 7.83
Private Const DEF_TIME_RESOLUTION = 0.01
Private Const DEF_RANDOM_PERIOD_VARIATION = 0.333
Private Const DEF_PIECE_COUNT = 1000
Private Const DEF_AMPLITUDE_HIGH = 100
Private Const DEF_AMPLITUDE_LOW = 0

Private Enum enmTimeUnits
    s = 1
    ms
    us
    ns
    ps
End Enum
Private Enum enmValueUnits
    V = 1
    mV
    uV
    A
    mA
    uA
End Enum

Private Type udtPiece
    timeStamp   As Long
    timeUnit    As enmTimeUnits
    value       As Double
    valueUnit   As enmValueUnits
End Type

Private Type udtPieceWise
    pieces()                As udtPiece
    pieceCount              As Long
    avgFrequency            As Double
    timeUnits()             As String
    timeUnit                As String
    valueUnit               As String
    timeRes                 As Double
    randomPeriodVariation   As Double
    ampHi                   As Double
    ampLo                   As Double
    timeNdx                 As Long
End Type


Private mtPWL As udtPieceWise

Private Sub cmd_Click(Index As Integer)
    ' Index is shared across an array of buttons of the same name.
    Select Case Index
        Case 1  ' reset
            reset
        Case 2  '  square wave
            ' Adjust randomality to zero
            mtPWL.randomPeriodVariation = 0
            txtRndVariation = 0
            updateUIpieces
        Case 3  ' randomized-edge square wave
            updateUI
        Case 4  ' copy PWL to clipboard
            copyToClipboard Trim(txtPWL.Text)
        Case 5  ' save PWL to file
            If Not SaveFile("PWL." + CStr(Int(Timer)) + ".txt") Then MsgBox "Sadly the file seemed to the code as unsuccessfully saved."
    End Select
    
End Sub


Private Sub Form_Initialize()
    loadsettings
End Sub

Private Sub Form_Unload(Cancel As Integer)
    saveSettings
End Sub


Private Sub Form_Load()
    
    With Me
        .Left = GetSetting(App.EXEName, "settings", "Left", CSng(4000))
        .Top = GetSetting(App.EXEName, "settings", "Top", CSng(2000))
    End With
    
    updateUI

End Sub


Private Sub updateUIpieces()
    
    With mtPWL
        txtPWL.Text = Join(GenerateRandomEdgeSquareWavePieces( _
                                    avgPeriod:=1 / .avgFrequency, _
                                    count:=.pieceCount, _
                                    freq:=.avgFrequency, _
                                    randomVariation:=.randomPeriodVariation, _
                                    amplitudeHi:=.ampHi, _
                                    amplitudeLo:=.ampLo, _
                                    timeRes:=.timeRes, _
                                    timeUnit:=.timeUnit, _
                                    valueUnit:=.valueUnit), vbCrLf)
    
    End With
End Sub

Private Sub cmbTimeUnit_Change()
    On Error Resume Next
    mtPWL.timeUnit = cmbTimeUnit.Text
End Sub
Private Sub cmbTimeUnit_Click()
    On Error Resume Next
    mtPWL.timeUnit = cmbTimeUnit.Text
End Sub

Private Sub cmbValueUnit_Change()
    On Error Resume Next
    mtPWL.valueUnit = cmbValueUnit.Text
End Sub
Private Sub cmbValueUnit_Click()
    On Error Resume Next
    mtPWL.valueUnit = cmbValueUnit.Text
End Sub

Private Sub cmdCopyToClipboard_Click()
    On Error Resume Next
    copyToClipboard txtPWL.Text
End Sub



Private Sub txtAmpHi_Change()
    On Error Resume Next
    mtPWL.ampHi = CDbl(txtAmpHi.Text)
End Sub

Private Sub txtAmpLo_Change()
    On Error Resume Next
    mtPWL.ampLo = CDbl(txtAmpLo.Text)
End Sub

Private Sub txtCount_Change()
    On Error Resume Next
    mtPWL.pieceCount = CLng(txtCount.Text)
End Sub

Private Sub txtAvgFrequency_Change()
    mtPWL.avgFrequency = txtAvgFrequency.Text
End Sub

Private Sub txtRndVariation_Change()
    ' Colorize the textbox background to indicate valid entry
    Dim rv As Double
    
    On Error Resume Next
    
    rv = CDbl(txtRndVariation.Text)
    
    Select Case True
        Case txtRndVariation.Text = "."
        Case txtRndVariation.Text = "0."
        Case rv >= 0 And rv <= 1
            mtPWL.randomPeriodVariation = CDbl(txtRndVariation.Text)
            txtRndVariation.BackColor = -2147483643
        Case rv >= 1
            txtRndVariation.BackColor = vbRed
        Case rv < 0
            txtRndVariation.BackColor = vbRed
    End Select
    
        
End Sub

Private Sub txtTimeResolution_Change()
    On Error Resume Next
    mtPWL.timeRes = CDbl(txtTimeResolution.Text)
End Sub



' Generate square wave PWL sequence as string array
Private Function GenerateRndSqWavePieceArray( _
                        freq As Double, _
                        amplitudeHi As Double, _
                        amplitudeLo As Double, _
                        timeRes As Double, _
                        count As Long, _
                        timeUnit As String, _
                        valueUnit As String) As String()
    
    Dim output() As String
    Dim pieceNdx As Long
    Dim amplitude As String
    Dim period As Double
    Dim phase As Integer
    
    On Error Resume Next
    ReDim output(1 To count)
    
    period = (1 / freq)
    amplitude = CStr(IIf(isOdd(pieceNdx * timeRes / period), amplitudeHi, amplitudeLo))
    
    For pieceNdx = 1 To count
        
        output(pieceNdx) = buildPWLpiece( _
                                timeStamp:=pieceNdx * timeRes, _
                                timeUnit:=timeUnit, _
                                value:=amplitude, _
                                valueUnit:=valueUnit)
        'Debug.Print output(pieceNdx)
    Next
    
    GenerateRndSqWavePieceArray = output
End Function


Private Function isOdd(someNumber As Double) As Boolean
    On Error Resume Next
    isOdd = CBool(Int(someNumber) / 2 <> Int(someNumber) \ 2)
End Function

Private Function buildPWLpiece( _
                        timeStamp As String, _
                        timeUnit As String, _
                        value As String, _
                        valueUnit As String) As String
                        
    'Return format: "[timeStamp][timeUnit][space][value][valueUnit]"
    On Error Resume Next
    
    buildPWLpiece = Join(Array(timeStamp, timeUnit, " ", value, valueUnit), "")
End Function

Public Function copyToClipboard(ByVal stringText As String) As Boolean
    On Error Resume Next
    
    VB.Clipboard.Clear
    VB.Clipboard.SetText stringText
    
    ' Return read-verification success
    copyToClipboard = (Clipboard.GetText() = stringText)
    
End Function

Public Function GenerateRandomEdgeSquareWavePieces( _
                        avgPeriod As Double, _
                        randomVariation As Double, _
                        count As Long, _
                        freq As Double, _
                        amplitudeHi As Double, _
                        amplitudeLo As Double, _
                        timeRes As Double, _
                        timeUnit As String, _
                        valueUnit As String) As String()
    ' This function was adapted from code written by ChatGPT 4.0 (openai.com) June 20, 2023.
    ' Prompted and adapted by XenoEngineer (Don@groupKOS.com)
    Dim i As Long
    Dim timeNdx As Double
    Dim high As Boolean
    Dim output() As String
    Dim randNumber As Double
    Dim timeFormatted As String
    
    On Error Resume Next
    
    ' Initialize the output collection
    ' Set output = New Collection
    ReDim output(1 To mtPWL.pieceCount)
    If Err Then
        MsgBox "GenerateRandomEdgeSquareWavePieces: Failure: Output memory allocation failed."
        Exit Function
    End If
    
    ' Initialize the current time
    timeNdx = 0
    ' timeNdx Is augmented by an average amount in the loop to run parallel with a
    ' fixed frequency, with randomization (plus and minus) added to that in each loop.
    ' This accumulates an average which obtains zero over time, in principle.

    ' Initialize the high variable
    high = True

    ' Create the pieces
    For i = 1 To count
    
        ' Generate +/- random numbers
        ' randNumber = Rnd() * (2 * randomVariation) - randomVariation
        randNumber = Rnd() * (randomVariation) - randomVariation / 2
        
        ' Add the average period and the random number to the current time
        timeNdx = timeNdx + avgPeriod + randNumber
        ' timeNdx Approximately tracks avgPeriod because +/- randNumber-s average to zero.
        
        ' Add the current time and the current (alternating) amplitude-level
        ' to the transitions collection
        
        ' Every transition is to an alternate amplitude.
        output(i) = buildPWLpiece(timeStamp:=Format(timeNdx, "0.0000000000"), _
                    timeUnit:=timeUnit, _
                    value:=IIf(high, mtPWL.ampHi, mtPWL.ampLo), _
                    valueUnit:=valueUnit)

        ' Toggle the amplitude state
        high = Not high
    
    Next i

    ' Return the output
     GenerateRandomEdgeSquareWavePieces = output
End Function


Private Sub reset()
    On Error Resume Next
    
    With mtPWL
        .ampHi = DEF_AMPLITUDE_HIGH
        .ampLo = DEF_AMPLITUDE_LOW
        .avgFrequency = DEF_AVERAGE_FREQUENCY
        .pieceCount = DEF_PIECE_COUNT
        .timeRes = DEF_TIME_RESOLUTION
        .timeUnit = DEF_TIME_UNIT
        .valueUnit = DEF_VALUE_UNIT
    End With
    
    updateUI
End Sub


Private Sub updateUI()
    On Error Resume Next
    
    With Me
        .Caption = App.ProductName
        .Caption = .Caption + "  v." + CStr(App.Major) + "." + CStr(App.Minor) + "." + CStr(App.Revision)
    End With
    
    ' Fill combo boxes
    With cmbTimeUnit
        .AddItem "s": .AddItem "ms": .AddItem "us": .AddItem "ns": .AddItem "ps":    End With
    With cmbValueUnit
        .AddItem "V": .AddItem "mV": .AddItem "uV": .AddItem "mA": .AddItem "uA":    End With
    
    With mtPWL
        txtAmpHi.Text = .ampHi
        txtAmpLo.Text = .ampLo
        cmbTimeUnit.Text = .timeUnit
        cmbValueUnit.Text = .valueUnit
        txtTimeResolution.Text = .timeRes
        txtCount.Text = .pieceCount
        txtAvgFrequency.Text = .avgFrequency
        txtRndVariation.Text = .randomPeriodVariation
    End With
    
    '  Update the pwl text control
    updateUIpieces

End Sub


Private Sub saveSettings()

    With mtPWL
    
        SaveSetting App.EXEName, "settings", "ampHi", .ampHi
        SaveSetting App.EXEName, "settings", "ampLo", .ampLo
        SaveSetting App.EXEName, "settings", "avgFrequency", .avgFrequency
        SaveSetting App.EXEName, "settings", "pieceCount", .pieceCount
        SaveSetting App.EXEName, "settings", "timeRes", .timeRes
        SaveSetting App.EXEName, "settings", "timeUnit", .timeUnit
        SaveSetting App.EXEName, "settings", "valueUnit", .valueUnit
        
        SaveSetting App.EXEName, "settings", "Left", Me.Left
        SaveSetting App.EXEName, "settings", "Top", Me.Top
        
    End With
    
End Sub


Private Sub loadsettings()
    
    With mtPWL
        .ampHi = GetSetting(App.EXEName, "settings", "ampHi", DEF_AMPLITUDE_HIGH)
        .ampLo = GetSetting(App.EXEName, "settings", "ampLo", DEF_AMPLITUDE_LOW)
        .avgFrequency = GetSetting(App.EXEName, "settings", "avgFrequency", DEF_AVERAGE_FREQUENCY)
        .pieceCount = GetSetting(App.EXEName, "settings", "pieceCount", DEF_PIECE_COUNT)
        .timeRes = GetSetting(App.EXEName, "settings", "timeRes", DEF_TIME_RESOLUTION)
        .randomPeriodVariation = GetSetting(App.EXEName, "settings", "randomPeriodVariation", DEF_RANDOM_PERIOD_VARIATION)
        .timeUnit = GetSetting(App.EXEName, "settings", "timeUnit") ', DEF_TIME_UNIT)
        .valueUnit = GetSetting(App.EXEName, "settings", "valueUnit", DEF_VALUE_UNIT)
    End With
    
End Sub

' This function is unused.
Private Function unitName(unit As String) As String
    Select Case unit
        Case "s"
            unitName = "second"
        Case "ms"
            unitName = "millisecond"
        Case "us"
            unitName = "microsecond"
        Case "ns"
            unitName = "nanosecond"
        Case "ps"
            unitName = "picosecond"
        Case "V"
            unitName = "Volt"
        Case "mV"
            unitName = "mVolt"
        Case "uV"
            unitName = "uVolt"
        Case "A"
            unitName = "Amp"
        Case "mA"
            unitName = "mAmp"
        Case "uA"
            unitName = "uAmp"
        
    End Select
End Function


        
Private Function SaveFile(filePath As String) As Boolean
    On Error Resume Next
    
    ' Open a file for binary access
    Open filePath For Binary As #1
    If Not Err Then
        ' Write the array to the file
        Put #1, , txtPWL.Text
    End If
    ' Close the file
    Close #1
    
    SaveFile = Not Err
End Function


File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeDimensionsUserComment
current02:54, 9 July 2023 (50 KB)Don (talk | contribs)A Piece-Wise Linear Function file generator by Codasaurus Hex VB6 Project PWLgenRSW v.0.1.0 Coded in Vintage VB6

There are no pages that use this file.