Professional Documents
Culture Documents
T Pratham - 2020EE10559
Afreen Haider - 2020EE10463
Part A: To write a C program to compute the N point complex valued DFT of an array x[n].
Part D: To write a C function to compute the FFT of a sample domain signal x[n].
Part E: To compare the running time of FFT in Part D with the DFT function in Part A.
2 Requirements:
1. CCS
3 Theory
3.1 Part A: Discrete Fourier Transform
The discrete Fourier Transform is used to find the amplitude and phase spectrum of a discrete
time sequence x[n] of length N as
N
X −1
X(k) = DF T (x[n]) = x[n]e−j2πkn/N ∀k ∈ {0, 1, · · · , N − 1} (1)
n=0
The real part of X(k) can be seen as the correlation between x and cosine function of frequency k
and the imaginary part as that between x and sine function of frequency k. Correlation between
two signals, by its very definition, can be seen as a metric of the similarity between the two
signals. Closer the frequencies of the two signals, higher the magnitude of correlation X(k) will
1
be. To get the frequencies in the signal, we require sampling frequency fs used to sample x[n].
The frequencies in the signal are then given by
fs · k
f= (2)
N
In frequency domain the convolution of the two signals corresponds to multiplication of the
Fourier transforms of the two signals, given by
where X(k) and H(k) are the Fourier transforms of x[n] and h[n] respectively.
The most common type of algorithm used for Fast Fourier Transforms is the Radix-2 Dec-
imation In Time FFT. It assumes the signal size N to be a power of 2. Radix-2 DIT first
computes the DFTs of the even-indexed inputs (x2m = x0 , x2 , · · · xN −2 ) and of the odd indexed
inputs (x2m+1 = x1 , x3 , · · · xN −1 ) and then combines the two results to produce the DFT of the
whole sequence.
N/2−1 N/2−1
X −2πj X −2πj
(2m)k (2m+1)k
X(k) = x2m e N + x2m+1 e N (6)
m=0 m=0
If we denote the DFT of the even indexed inputs x2m by Ek and the DFT of the odd indexed
inputs x2m+1 by Ok , equation (6) above can be re-written as
Due to periodicity of the exponential, we can also simplify the expression for Xk+N/2 as
2
4 Procedure
4.1 Part A: Discrete Fourier Transform
• Equation (1) was modified using Euler’s theorem as:
N
X −1 N
X −1
X(k) = x[n] cos(−j2πkn/N ) − j x[n] sin(−j2πkn/N ) (9)
n=0 n=0
• x[n] was taken in as an array and its complex valued Discrete Fourier Transform was
calculated using the above equation (9)
5 Code
# ifndef COMPLEX_H_
# define COMPLEX_H_
# endif /* COMPLEX_H_ */
Listing 1: Custom struct defined for handling complex numbers in header file complex.h
3
# include < math .h >
# define pi 3.1415926535
# define pi 3.14159265
4
# include < math .h >
# define pi 3.14159265
# define pi 3.14159265
Complex Y [100];
for ( i =0; i <2* n -1; ++ i ) {
Y [ i ]. re = X [ i ]. re * H [ i ]. re - X [ i ]. im * H [ i ]. im ;
Y [ i ]. im = X [ i ]. im * H [ i ]. re + X [ i ]. re * H [ i ]. im ;
}
idft (2* n -1 , Y , y ) ;
}
5
}
}
# define pi 3.14159265
double t = 2.0* pi / n ;
Complex temp ;
for ( i =0; i < n /2; ++ i ) {
temp . re = X [ i ]. re ;
temp . im = X [ i ]. im ;
X [ i ]. re = temp . re + X [ i + n /2]. re * cos ( t * i ) + X [ i + n /2]. im * sin ( t * i ) ;
X [ i ]. im = temp . im + X [ i + n /2]. im * cos ( t * i ) - X [ i + n /2]. re * sin ( t * i ) ;
X [ i + n /2]. re = temp . re - X [ i + n /2]. re * cos ( t * i ) - X [ i + n /2]. im * sin ( t * i ) ;
X [ i + n /2]. im = temp . im - X [ i + n /2]. im * cos ( t * i ) + X [ i + n /2]. re * sin ( t * i ) ;
}
}
}
6
mag (16 , X1 , A1 ) ;
mag (16 , X2 , A2 ) ;
conv (16 , x , h , y ) ;
// convVerify (16 , x , h , y2 ) ;
return 0;
}
7
6 Graphs
6.1 Part A
The graphs in Figure 1a show the Magnitude Response and Figure 1b shows the Phase Response
of the DFT X[k] of the signal input signal x[n].
8
6.2 Part D
The graph in Figure 2a shows the Magnitude Response and Figure 2b shows the Phase Response
of the FFT X2[k] of the signal input signal x[n].
9
7 Observations
1. The result of IDFT x2[n] matches with the input signal x[n] within domain of floating-
point error.
10
Figure 4: IDFT x2[n] of X[k]
11
2. The discrete Fourier transform calculated using DFT formula and FFT match within
floating-point error bounds. Their magnitude and phase responses have been plotted
above in Figure 1 and Figure 2. (PS: The phase responses don’t match at k=8 due to
floating point error around 0. The value is a very small positive float in one and negative
in other. Hence, the phases come as −π and π.)
3. For Part C, the first signal taken is x[n] (same as shown above, used in Part A). The
second signal h[n] taken is shown below (Figure 8). The value of convolution of x[n] and
h[n] is computed and as shown below in Figure 11:
12
Figure 5: DFT X[k] of x[n]
13
Figure 6: FFT X2[k] of x[n]
14
Figure 8: Signal h[n]
15
16
17
PN −1
Figure 11: y[n] = i=0 x[i]h[n − i] = IDFT(X[k]H[k])[n]
4. DFT algorithm requires a nested loop with two stages, each of length n. In each iteration
of the outer loop, there are n iterations of the inner loop. If we consider the time taken
for each innermost loop’s iteration to be c, then the total time taken for the nested loop
to run is cn2 . Other operations in the algorithm are of constant running time.
Therefore the running time of the algorithm is of the order of n2
5. For the FFT algorithm, of the time taken for the algorithm to run for a signal of length
n is given by T (n), then the recursive equation for the running time is given by
From the equation we obtain T (n) to be of the form pnlogn where p is a constant.
18
8 Result
1. DFT has been calculated for a sequence x[n] and its magnitude and phase response has
been plotted in the Graphs section above (Figure 1).
2. IDFT has been calculated and result shown above in observations in Figure 4. The IDFT
matches the original input signal within error bounds.
3. Time-domain convolution has been calculated correctly using DFT and IDFT and has
been demonstrated above in the observations in Figure 11.
4. FFT algorithm has been implemented correctly and has been demonstrated in previous
sections. The Discrete Fourier Transform calculated using FFT matches the one calculated
using DFT formula in Part A. The magnitude and phase response has been shown for
FFT as well in Graphs section above (Figure 2).
5. From the comparison between the running times of DFT and FFT we obtain that FFT
is a more efficient algorithm with a running time that is less than that of DFT.
19