Calling all communications systems experts - please share your knowledge here!
'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
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