## Version 3 of Tcl based signal processing fundamentals: Additive Wave Synthesis

Updated 2004-08-11 11:48:20 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

Instead of plotting the sine wave points one by one straight away in the loop, we can first keep them stored in a tcl list, and make a procedure to automatically draw then from a list:

``` # list the points for one waveform
set wave1 {};    # empty list
for {set i 0} {\$i < 100} {incr i} {
lappend wave1 [expr sin(2.0*\$pi*\$i/100)]  ;# append each of the 100 points to the list
}

proc plotlist {l} {
global mc
set xp [llength \$l];
set xscale [expr 300/\$xp];
set yscale 100
set pi 3.1415926535
\$mc del p;
for {set i 0} {\$i < \$xp} {incr i} {
plotpoint [expr 10+\$i*\$xscale] [expr 150-\$yscale*[lindex \$l \$i]]
}
}

# now do the plottin'
plotlist \$wave1```

Additive synthesis, what this page is primarily about, is about adding sine wave components of different frequencies, usually frequencies with integer ratio to the lowest frequency component or 'fundamental' (usually). We can do this in tcl by using the above and taking not just one sine, but various sines, like so:

``` set wave2 {};    # empty list
for {set i 0} {\$i < 100} {incr i} {
set fundamental_phase 2.0*\$pi*\$i/100
lappend wave2 [expr \
0.5*( \
sin(  \$fundamental_phase) + \
sin(2*\$fundamental_phase)   \
)
]
}
plotlist \$wave2```

The extra factor of 0.5 is to make sure the sum of the two sine waves can never become more than 1.0 .