Friday, May 17, 2013

Basic Euclidean vector operations in C++

Euclidean vectors are very important quantity in mathematics, applied mathematics, physics, engineering etc. Formally vectors are those quantity which have both magnitude and direction. For example velocity. Velocity has both magnitude (Speed, like 2 Km/Hr) and direction (e.g. east). In programmer’s perspective, there are many situation where you need to compute different vector operations. For example consider a moving ball. To simulate the motion of ball, you must calculate the velocity of the ball. The ball has both magnitude (i.e rate of translation of  position of center of the ball) and direction (e.g. axis x or y or z). This is just one example but there are various examples where vector operations are essential.
Now in this article, I will show you how to compute different vector operations like, sum of two vectors, multiplication by scalar, dot product, cross product, normalization etc in C++. I choose C++ instead of C because it is object oriented. The vector can be represented by an object. The components of vector along x, y and z axis will be the data member of the object. The data members are attributes of the object. Similarly different operations will be the member functions of the object. Now the vector object can completely represent the vector quantity.

Tuesday, May 14, 2013

Calculation of Discrete Fourier Transform(DFT) in C/C++ using Naive and Fast Fourier Transform (FFT) method

Discrete Fourier Transform has great importance on Digital Signal Processing (DSP). There are many situations where analyzing the signal in frequency domain is better than that in time domain. The Fourier Transform actually converts the function in time domain to frequency domain,  some processing is done in frequency domain, and finally inverse Fourier transform converts the signal back into time domain. The term discrete means the signal is not continuous rather it is sampled by some sampling frequency i.e. only some samples are taken in certain interval (also called period). The sampling frequency depends upon the frequency of original signal and this must satisfy Nyquist criteria. A simple example of Fourier transform is applying filters in frequency domain of digital image processing. Before looking into the implementation of DFT, I recommend you to first read in detail about the Discrete Fourier Transform in Wikipedia. If you are already familiar with it, then you can see the implementation directly.  The 1 dimensional DFT can be calculated by using the following formula
dfte 

Monday, May 6, 2013

Pascal’s Triangle using C program

Printing Pascal’s Triangle is famous problem in C. You may be familiar with the Pascal triangle from the Math class when you learnt Permutation and Combination or Binomial Coefficients or other class. If you are not familiar then don't worry, just look at Wikipedia to get inside of the Pascal Triangle and see the code below.

There are various methods to generate Pascal’s Triangle in C program. Among them most popular one is by using Combination. Since elements of the triangle represent the Binomial Coefficients of a polynomial and Binomial Coefficient can be calculated using the Combination, I decided to generate Pascal’s Triangle using Combination method. Here is the source code