Tuesday, February 21, 2006

Building Square Wave from Fourier Series

As promised in my previous post, I would like to show how a square waveform can be built from the Fourier Series. I am using Tcl/Tk script to generate a periodic square wave from Fourier Series. The reason I am using Tcl/Tk language for this is because I have never practically done this before although I know it can be done. In case you haven’t heard about Tcl/Tk language before up to this point, you can read the previous post in this blog, The Interesting Tcl Language.

As you have known from my previous post, the periodic signals can be constructed using Fourier Series, which consists of summation of sine and cosine functions at frequencies which are harmonically related. The square wave function F(t) derived from Fourier Series is as follows:

F(t) = sin(t) + sin(3t)/3 + sin(5t)/5 + sin(7t)/7 + sin(9t)/9 + …
= ∑sin(nt)/n (n odd, 0< n <∞) (1.1)

It is too long to describe how the above function is derived. If you are interested to know how it is derived, it is not to difficult this information from the reference books or internet.

Since the square wave is luckily just constructed from summation of sine functions at different harmonic frequency, let’s display a simple sine wave using Tk first.

The Tcl/Tk source code for the above diagram is as follows. As you may agree, it doesn’t take many lines of complicated code to display a sine wave on your computer screen using Tcl/Tk script.

############################################
## sinewave.tcl
## date created : February 21, 2006
## author : http://fpgaforum.blogspot.com
############################################

wm focusmodel . passive
wm geometry . 460x360; update
wm resizable . 1 1
wm deiconify .
wm title . "Sine Wave Generator by FPGA FORUM"

label .msg1 -wraplength 4i -justify center -text "This generates a simple sine wave."
label .msg2 -wraplength 4i -justify center -text "http://fpgaforum.blogspot.com"
pack .msg1 -side top
pack .msg2 -side top

canvas .sinewave -bg black -width 450 -height 300
pack .sinewave

set coordList {}

#2*pi*f0 = w0 = 1/25
#fundamental frequency, f0 = 1/(25*2*pi)

for {set x 0} {$x<=450} {incr x} {
lappend coordList $x [expr sin($x/25.0) * 50 + 150]
}

eval .sinewave create line 0 150 450 150 -fill white -activefill blue
eval .sinewave create line $coordList -fill green -activefill red

##The end of sinewave.tcl
################################################


I purposely multiply the sine function result by a factor of 50 to amplify the sine wave. Otherwise, the sine wave would be too small.

The square wave, if constructed by the first 5 harmonics of sine functions looks like below. Of course, if you complain that this is not a square wave, then you are absolutely right. A perfect square wave is constructed when you have summed all the harmonics of sine functions according to the Equation 1.1.

The Tcl/Tk source code for generating the above square wave is as follows, and once again, the script doesn’t look much different from the one that generates a sine wave.

############################################
## squarewave.tcl
## date created : February 21, 2006
## author : http://fpgaforum.blogspot.com
############################################

wm focusmodel . passive
wm geometry . 460x360; update
wm resizable . 1 1
wm deiconify .
wm title . "Square Wave Generator by FPGA FORUM"

label .msg1 -wraplength 4i -justify center -text "This generates a square wave from the Fourier Series."
label .msg2 -wraplength 4i -justify center -text "http://fpgaforum.blogspot.com"
pack .msg1 -side top
pack .msg2 -side top

canvas .squarewave -bg black -width 450 -height 300
pack .squarewave

set coordList {}

#2*pi*f0 = w0 = 1/25
#fundamental frequency, f0 = 1/(25*2*pi)

for {set x 0} {$x<=450} {incr x} {
set y 0
set z 0
for {set N 1} {$N<=5} {set N [expr {$N + 2}]} {
set z [expr sin($x*$N/25.0) /$N* 50]
set y [expr $y + $z]

}
lappend coordList $x [expr $y+150]
}

eval .squarewave create line 0 150 450 150 -fill white -activefill blue
eval .squarewave create line $coordList -fill green -activefill red

##The end of squarewave.tcl
#####################################################


If the N is 101, then you can see that an almost perfect square waveform is displayed using the squarewave.tcl script. And, it is not too difficult for you to imagine that a perfect square wave will be generated if you keep on increasing the value of N, which is also the Nth harmonic sine wave from the fundamental sine wave frequency.

You may now feel curious about the real applications of the Fourier Series after reading this post. In my next post, I will tell you one real application that has the connection with what I have brought out in this post.

No comments: