1

I have a working VBA code that generates a sine wave given the constant amplitude and RPM. The problem is that the RPM changes abruptly n times within the whole dataset, whereas in between it is constant. This abrupt change interrupts the continuity of my sine wave.

Does anyone have an idea how to make my sine wave continuous despite the abrupt changes in RPM? I.e., the frequency of the sine wave should change while the amplitude remains constant. Thank you in advance!

Sub CalcStroke()

Dim i, Stroke As Long
Dim Pi As Double
Dim Time, RPM, Wave As Variant

Pi = WorksheetFunction.Pi()

Time = Range(Cells(3, 9), Cells(3, 9).End(xlDown))
RPM = Range(Cells(3, 10), Cells(3, 10).End(xlDown))
Stroke = Cells(3, 7)

Wave = Cells(3, 5).Resize(UBound(Time, 1), 1)

For i = LBound(Time) To UBound(Time)

    Wave(i, 1) = 0.5 * Stroke * Sin(2 * Pi * RPM(i, 1) / 60 * Time(i, 1))

Next i

Cells(3, 5).Resize(UBound(Time, 1), 1) = Wave

End Sub

The discontinued sine wave

Based on some online research, I tried to adopt a technique called phase continuity, but unsuccessfuly. The code results in an extremly frequent change in frequency of the sine wave.

Sub PhaseContinuity()

    Dim i, Stroke As Long
    Dim Pi, PreviousPhase, CurrentPhase, PhaseAdjustment As Double
    Dim Time, RPM, Wave As Variant

    Pi = WorksheetFunction.Pi()

    Time = Range(Cells(3, 9), Cells(3, 9).End(xlDown))
    RPM = Range(Cells(3, 10), Cells(3, 10).End(xlDown))
    Stroke = Cells(3, 7)

    Wave = Cells(3, 5).Resize(UBound(Time, 1), 1)

    'Initialize the phases
    PreviousPhase = 0
    CurrentPhase = 0

    For i = LBound(Time) To UBound(Time)
        'Calculate the phase adjustment based on RPM change
        CurrentPhase = (2 * Pi * RPM(i, 1) / 60 * Time(i, 1)) + PreviousPhase
        Dim PhaseAdjustment As Double
        If i > LBound(Time) Then
            ' Ensure phase continuity by adjusting for phase jumps
            PhaseAdjustment = CurrentPhase - PreviousPhase
            If PhaseAdjustment > Pi Then
                PhaseAdjustment = PhaseAdjustment - 2 * Pi
            ElseIf PhaseAdjustment < -Pi Then
                PhaseAdjustment = PhaseAdjustment + 2 * Pi
            End If
        End If
        'Update the phase for the next iteration
        PreviousPhase = CurrentPhase + PhaseAdjustment

        'Calculate the new sine wave value using adjusted phase
        Wave(i, 1) = 0.5 * Stroke * Sin(CurrentPhase + PhaseAdjustment)
    Next i

    Cells(3, 5).Resize(UBound(Time, 1), 1) = Wave

End Sub
Fevzi
  • 15
  • 4
  • Write your code so that you plot only half cycles of the sine wave.. Allow the frequency parameter to change as required, but only change to the new frequency at the end of a half cycle. – freeflow Aug 24 '23 at 12:25
  • Thanks for the comment. The idea is great as it would provide a smooth transition between frequencies. The only problem is that such a sine wave would get out of phase with other associated signals in my dataset! So, in my case, the transition doesn't need to be smooth but rather continuous, so no jumping of the amplitude when the frequency changes. Imagine an oscillating motion with a mechanically fixed amplitude, driven by an electric engine that can switch its RPM abruptly but the motion continues without interruption. In that case, the amplitude of the oscillating motion is continuous. – Fevzi Aug 24 '23 at 13:01

0 Answers0