Version 3 of Sine wave roundings illustrated with BWise

Updated 2006-02-09 19:45:29

started by Theo Verelst

When dealing with the solution of (2d order) differential equations, electrical circuits, drawing circles, making musical waves or fourier analysis, sine waves or sine values as function of lets say x are essential.

Lets first draw a sine wave with a reasonable graphical accuracy, make sure you have a (preferably scrollable) Tk canvas to work on, and that the tcl variable mc contains the path to that canvas.

This routine, called with the number of x-steps draws 2 periods of a sine wave, with 1:1 x:y scale ratio, and rouding of y coordinates by truncation:

 proc drawsine { {n 256} } {
   global mc
   set pi 3.1415926535
   $mc del gr
   for {set i 1} {$i < 2*$n} {incr i} {
      $mc create line [expr 100+$i-1] \
         [expr 100+$n-$n*sin(2*$pi*($i-1)/$n)] \
         [expr 100+$i] \
         [expr 100+$n-$n*sin(2*$pi*($i)/$n)] \
            -tag gr
   }
 }

 drawsine 256

http://82.170.247.158/Wiki/sine1m.jpg

Now when we make the y coordinate quantized per 10 of the above pixel widths

 proc drawsine_yq { {n 256} } {
   global mc
   set pi 3.1415926535
   $mc del gr
   for {set i 1} {$i < 2*$n} {incr i} {
      $mc create line [expr 100+$i-1] \
         [expr 100+$n-$n*sin(2*$pi*10*int(($i-1)/10)/$n)] \
         [expr 100+$i] \
         [expr 100+$n-$n*sin(2*$pi*10*int(($i)/10)/$n)] \
            -tag gr
   }
 }

http://82.170.247.158/Wiki/sine3m.jpg

 proc drawsine_yq { {n 256} } {
   global mc
   set pi 3.1415926535
   $mc del gr
   for {set i 1} {$i < 2*$n} {incr i} {
      $mc create line [expr 100+$i-1] \
         [expr 100+$n-$n*sin(2*$pi*10*int(($i-1)/10)/$n)] \
         [expr 100+$i] \
         [expr 100+$n-$n*sin(2*$pi*10*int(($i-1)/10)/$n)] \
            -tag gr
   }
 }

 drawsine_yq 256

http://82.170.247.158/Wiki/sine2m.jpg

the graph looks like above, depending on wether the vertical connection lines are drawn or not.

Alternatively, we can clearly (because even with double accuracy floating point numbers, computer accuracy isn't infinite) quantize the x coordinates, and then interpolate somehow: