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.

Sunday, February 19, 2006

History and Beauty of Sine Function


Still remember when was the first time you learn about sine function? Still remember how the sine function was derived when you were in the secondary school? I first learnt about the sine function from the trigonometry chapter at 14 years old if I remember correctly. 10 years later, I almost forgot that the sine function introduction was from a right-angled triangle. Sine function of the angle A (not the right angle) in a right angular triangle is the length of the side opposite to the angle A divided by the length of hypotenuse, which is the longest side of a right-angle triangular. Cosine function of the angle A, on the other hand, is defined as the length of the adjacent side of the angle A divided by the length of hypotenuse. From the above explanation, it is not too difficult to think that cosine function is an extension from the sine function. Perhaps this is the reason why cosine function is named as co-sine function.


Everything looks so simple when I was beginning to learn the sine and cosine functions. Never could I have thought that these simple functions have so much influence on today’s mankind history. No kidding! What would happen if there was no sine function nowadays? The influence of the sine function is too much way beyond anyone imagination.


An example of the importance of Sine function is shown obviously in Fourier Series. Fourier Series was discovered by a French mathematician, Jean Baptiste Joseph Fourier (1768-1830, one of the French Revolution contributors) when he was studying and analyzing the heat flow in a metal rod. Therefore, the Fourier Series was named in honor of him. According to the Fourier Series, a periodic function can be represented by an infinite sum of sine or cosine functions that are harmonically related. For an instance, a square wave, which doesn’t seems to be any sinusoidal at all, can be represented by a Fourier Series. (If I have the time, I would like to prove it to you in graphics next time.) If you think that this is an easy statement, then you are totally wrong. Fourier Series as well as Fourier Transform which bears his name, are considered among the greatest discoveries in scientific and engineering discipline.


There are too many periodic motions or waveforms around us all the time. To name a few, signals transmitted by the cell phone base-station, television and radio stations are sinusoidal and periodic. The alternating current (a.c.) power sources generate voltages and currents are in sinusoidal form. Function or Signal Generators generate different kind of periodic waveforms in your laboratories. The generation and analyzing of the periodic motions or waveforms are made possible through Fourier Series. Anyway, the Fourier Series will not be possible if there was no Sine function before Joseph Fourier.

Isn’t it amazing? Why sinusoidal shape matters? Why not square, triangular, or circle? This is really intriguing.

Monday, February 06, 2006

The Interesting Tcl Language


People pronounce Tcl as "Tickel". Tcl means Tool Command Language. Usually, you see Tcl paired with its young brother, Tk. Tk is the Tcl toolkit for building graphical user interfaces.
I was first introduced by this cool Tcl language about 3 years ago when I tried to find a solution to automate the compilation of Quartus II overnight and during the weekend. Basically, I had about twenty different Quartus II projects to compile and each of them took a few hours to complete due to the large logic usage and strict timing requirement inside the designs. If I didn't automate this process, I would be wasting my time to do the manual push-button compilation after every one or two hours of compilation, which was kind of stupid. So, that was my first purpose of using the Tcl language.

However, soon after that, I found out that the Tcl language is not merely a scripting language that is limited for automation process. It can do much more than that, such as file processing, easy interface with the PC serial port without any external DLL, easy construction of GUI, interface with a DLL written by you or others, etc. Besides that, I also found out that most of the FPGA tools like Quartus II, ISE and famous third-party simulation tool, ModelSim, provide the API and platform for you to command them in Tcl language.

One of the reasons people use Tcl language is that the script written in Tcl language works regardless of the operating system used. It saves the hassle of providing the software in different versions just to support different OS.
If you never heard about the Tcl and are interested with this Tcl language, you can go to ActiveState to download the ActiveTcl software, and it is freely distributed. There are many Tcl application examples that you can easily download from the website for reference. If you would like to know even more details about this cool language, you can read the Practical Programming in Tcl and Tk written by Brent B. Welch, Ken Jones with Jeffrey Hobbs. This Tcl/Tk bestseller comes with a CD-ROM that includes all the examples used in the book and also the ActiveTcl software which may have been outdated. This book (fourth edition) has been my Tcl/Tk bible since I start using Tcl and Tk and it still is.

Saturday, February 04, 2006

The Art of Prototyping


There is another huge advantage of using FPGA which I had left out in my previous post. In fact, this is one of the significant reasons the engineers are using the FPGAs. Seeing is Believing! Many IC Designers design their "designs" with simulation tools that cost them hundreds of thousand or even millions dollars, but sometimes all the simulation results before the first silicon is still not enough. So, the IC Designers work with the engineers who are familiar with the FPGA devices and tools to create a prototype for their designed IC using the RTL codes written by them. The prototype can reduce a lot of risk on the IC designed because if there is any bug found on the RTL codes, it can still be fixed, re-compiled on the FPGA tool and then verified on the FPGA again within a short period of time. As I told you in my previous post, the FPGA can be re-configured with any new design easily. In my working experience, the most impressive prototype that I had seen so far is the prototyping of a RF wireless chip in a Xilinx FPGA. Of course, the Xilinx FPGA only prototypes the digital portion of the RF chip, such as the microcontroller, JTAG (Joint-Test-Action-Group), etc.

The prototype can at least verify the functionalities of the new IC designed. But then you may ask, what is the difference between the FPGA prototype and the final product IC? Timing. It is difficult to match the timing between the FPGA and the ASIC (short form for the Application Specific IC, usually the IC designed has a specific application, such as graphics chip, USB device chip, etc) due to the different architectures inside the ASIC and the FPGA. Anyway, even if the FPGA cannot run as fast as the targetted ASIC frequency, it doesn't stop people from prototyping their final product in FPGA unless their final product is an analog device. You may also be curious, if that is the case, why don't the IC Design companies just sell their "ASICs" designs in the FPGAs? Well, the reason is simple, the cost is different. If they have the confidence that many people will love their ICs and their ASICs is going to make them a lot of money, then it is certainly very much more cost-efficient to go for ASICs. Two three years ago, FPGA vendors such as Altera and Xilinx have rolled out many low cost FPGAs such Cyclone series and Spartan series FPGAs. These low cost FPGAs certainly worth considered if the designs do not target very high frequency and high performance. However, if you are targetting the high-end FPGAs such as Stratix series or Virtex series FPGA, then you might consider selling your product with volume in ASIC because the high-end FPGAs are very expensive! Or, you may consider Altera HardCopy II as Altera promises "seamless" migration from FPGA to ASIC at NO pain!