A2-Sinusoids and DFT
A2-Sinusoids and DFT
A2-Sinusoids and DFT
Introduction
The second programming assignment is for you to get a better understanding of
some basic concepts in audio signal processing related with the Discrete Fourier
Transform (DFT). You will write snippets of code to generate sinusoids, to
implement the DFT and to implement the inverse DFT. There are five parts
in this assignment. 1) Generate a sinusoid, 2) Generate a complex sinusoid, 3)
Implement the DFT, 4) Implement the IDFT and 5) Compute the magnitude
spectrum of an input sequence. The fifth part of this assignment is optional and
will not contribute to the final grade.
Relevant Concepts
Sinusoid: A real sinusoid in discrete time domain can be expressed by:
1
Inverse Discrete Fourier Transform: The IDFT of a spectrum X of length
N can be expressed by:
N 1
1 X
x[n] = X[k]ej2kn/N n = 0, ..., N 1 (4)
N
k=0
where, n is an integer value expressing the discrete time index, k is an inte-
ger value expressing the discrete frequency index, and N is the length of the
spectrum X.
2
If you run your function using N=5 and k=1, the function should return the
following numpy array cSine: array([1.0 + 0.j, 0.30901699 - 0.95105652j,
-0.80901699 - 0.58778525j, -0.80901699 + 0.58778525j, 0.30901699 +
0.95105652j])
def genComplexSine(k, N):
"""
Inputs:
k (integer) = frequency index of the complex sinusoid
of the DFT
N (integer) = length of complex sinusoid in samples
Output:
The function should return a numpy array
cSine (numpy array) = The generated complex sinusoid
(length N)
"""
## Your code here
3
of length N , the function should return its IDFT x, also of length N . Assume
that the frequency index of the input spectrum ranges from 0 to N 1.
The input argument to the function is a numpy array X of the frequency
spectrum and the function should return a numpy array x, the IDFT of X.
Remember to scale the output appropriately.
If you run your function using X = np.array([1 ,1 ,1 ,1]), the function
should return the following numpy array x:
array([ 1.0 +0.0 j, -4.5924255e-17 +5.5511151e-17j, 0.000000e+00
+6.12323400e-17j, 8.22616137e-17 +8.32667268e-17j])
Notice that the output numpy array is essentially [1, 0, 0, 0]. Instead of
exact 0 we get very small numerical values of the order of 1015 , which can be
ignored. Also, these small numerical errors are machine dependent and might
be different in your case.
In addition, an interesting test of the IDFT function can be done by provid-
ing the output of the DFT of a sequence as the input to the IDFT. See if you
get back the original time domain sequence.
def IDFT(X):
"""
Input:
X (numpy array) = frequency spectrum (length N)
Output:
The function should return a numpy array of length N
x (numpy array) = The IDFT of the frequency spectrum X
(length N)
"""
## Your code here
4
"""
## Your code here
Grading
Only the first four parts of this assignment are graded and the fifth part is
optional. The maximum number of points obtained in this assignment is 10.