Joined: 03 Oct 2004 Posts: 235 Location: Gothenburg, Sweden
Posted: Wed Feb 15, 2017 10:52 am Post subject: Generate Triangle and Sine wave from double intergration
If you want to generate a sinewave, a very simple way is to doubleintegrate a constant.
Integration is the same as accumulation in the digital domain so all you need is to DIM two variables acting as accumulators.
The first integration generates a triangle wave and the second a sine. The sine phase will lag the triangle by 90 degrees.
In the case below, 256 loops are needed for one sine period, but you can experiment with other lengths depending of how
finegrained curve you want
(Forget about the figures in the Picture. They belong to Another application. I just wanted to illustrate the principle)
Dim Ramp_direction As Integer
Dim Ramp_value As Integer
Dim Sine_value As Long
Dim Triangle_value as long
do Ramp_value = Ramp_value + Ramp_direction
Sine_value = Sine_value + Ramp_value
Select Case Ramp_value '256 accumulations per period
Case 127 :
Ramp_direction = -1
Sine_value = 0 'Slope down
Case -127 :
Ramp_direction = 1
Sine_value = 0 'Slope up
End Select
Triangle_value = Ramp_value * 64 'Make Triangle ampliude equal to Sine
waitus 39 'Frequency wiil be approx 1kHz (256*39us = 1ms)
loop
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum