Version 1 of Tcl based signal processing fundamentals: Additive Wave Synthesis

Updated 2004-08-11 11:17:54 by TV

started by Theo Verelst

When wanting to examplify what frequency varied sine waves have as a spectrum, because that's important while analysing many types of measured or synthesized digital signal, for which tcl is also used, the page on that subject got a bit full. I though some of these very fundamental sujects deserve a place amoung the many pages about these important signal processing subjects, which can be successfully and interestingly programmed in tcl. In fact I prefer to use Tk, too, because it is one of the powers of tcl/tk to be a great interactive program development environment with great scientific value as such. Not many computer languages are really suitable for interactive scientific development , which is probably the main driving force for improvements in computer programming (apart from all kinds of (un-) human, political and 'imperialistic' considerations, and un-technical logics).

Waves can in tcl be natually represented in sampled form as lists. That means that when a wave like a sine is chopped in pieces, or neater put: approximated in x (or time) and y axis sense by rounding it to points on a rectangular grid (like on uses in mathematics school sheets), those point can be stored in order (from left to right) by putting the approximate values in a tcl list of values.

A sine wave is a special repetative wave pattern, because our ears perceive it has having only one frequency, so it is per definition the most 'dull' or harmonics-poor wave. It isn't hard to make a list with sine values for one sine wave in tcl. Lets say we want ten values for one full wave, we'd write:

   set pi 3.1415926535
   for {set i 0} {$i < 10} {incr i} {
      puts [expr sin(2.0*$pi*$i/10)]

Later on we'll put these points in a tcl list, in order, to reuse them, instead of just listing the sine values on the console. To graphically make clear what our wave looks like, we can plot the values in a graph, like so:

 # First, a few condensed procs to do make a canvas and draw points on it
 # It's always handy to be able to change to which canvas we use
 set mc .c
 # make the simplest decent canvas
 pack [canvas $mc] -expand y -fill both
 # define the point drawing procedure
 proc plotpoint {x y {size 3} {colour black} {tag p}} {
    global mc;
    $mc create oval \
     [expr $x-$size/2] [expr $y-$size/2] \
     [expr $x+$size/2] [expr $y+$size/2] \
     -outline $colour -fill $colour -tag $tag
 # now the same as above, except scaled and as graphics points
 set pi 3.1415926535
 set yscale 100
 set xscale 20
 for {set i 0} {$i < 10} {incr i} {
    plotpoint [expr 10+$i*$xscale] [expr 150-$yscale*sin(2.0*$pi*$i/10)]

This gives an impression of a sine wave, though it's not all to clear unless we use more points to approximate this important tcl function:

 set xp 50;                 # number of X coordinates, or points
 set xscale [expr 200/$xp]; # horizontal space per point in canvas pixels
 $mc del p;                 # delete all graphs with tag 'p' from the canvas first
 for {set i 0} {$i < $xp} {incr i} {
    plotpoint [expr 10+$i*$xscale] [expr 150-$yscale*sin(2.0*$pi*$i/$xp)]

The resulting graph