7 Discrete Fourier Transform


title: "cf." date: 2026-05-24T13:51:55Z

1from __future__ import print_function
2import numpy as np
3import matplotlib.pyplot as plt
4%matplotlib inline
1from datetime import date
2date.today()
datetime.date(2017, 11, 2)
1author = "kyubyong. https://github.com/Kyubyong/numpy_exercises"
1np.__version__
'1.13.1'

Complex Numbers

Q1. Return the angle of a in radian.

1a = 1+1j
2output = ...
3print(output)
0.785398163397

Q2. Return the real part and imaginary part of a.

1a = np.array([1+2j, 3+4j, 5+6j])
2real = ...
3imag = ...
4print("real part=", real)
5print("imaginary part=", imag)
real part= [ 1.  3.  5.]
imaginary part= [ 2.  4.  6.]

Q3. Replace the real part of a with 9, the imaginary part with [5, 7, 9].

1a = np.array([1+2j, 3+4j, 5+6j])
2...
3...
4print(a)
[ 9.+5.j  9.+7.j  9.+9.j]

Q4. Return the complex conjugate of a.

1a = 1+2j
2output = ...
3print(output)
(1-2j)

Discrete Fourier Transform

Q5. Compuete the one-dimensional DFT of a.

1a = np.exp(2j * np.pi * np.arange(8))
2output = ...
3print(output)
[  8.00000000e+00 -6.85802208e-15j   2.36524713e-15 +9.79717439e-16j
   9.79717439e-16 +9.79717439e-16j   4.05812251e-16 +9.79717439e-16j
   0.00000000e+00 +9.79717439e-16j  -4.05812251e-16 +9.79717439e-16j
  -9.79717439e-16 +9.79717439e-16j  -2.36524713e-15 +9.79717439e-16j]

Q6. Compute the one-dimensional inverse DFT of the output in the above question.

1print("a=", a)
2inversed = ...
3print("inversed=", a)
a= [ 1. +0.00000000e+00j  1. -2.44929360e-16j  1. -4.89858720e-16j
  1. -7.34788079e-16j  1. -9.79717439e-16j  1. -1.22464680e-15j
  1. -1.46957616e-15j  1. -1.71450552e-15j]
inversed= [ 1. +0.00000000e+00j  1. -2.44929360e-16j  1. -4.89858720e-16j
  1. -7.34788079e-16j  1. -9.79717439e-16j  1. -1.22464680e-15j
  1. -1.46957616e-15j  1. -1.71450552e-15j]

Q7. Compute the one-dimensional discrete Fourier Transform for real input a.

1a = [0, 1, 0, 0]
2output = ...
3print(output)
4assert output.size==len(a)//2+1 if len(a)%2==0 else (len(a)+1)//2
5
6# cf.
7output2 = np.fft.fft(a)
8print(output2)
[ 1.+0.j  0.-1.j -1.+0.j]
[ 1.+0.j  0.-1.j -1.+0.j  0.+1.j]

Q8. Compute the one-dimensional inverse DFT of the output in the above question.

1inversed = ...
2print("inversed=", a)
inversed= [0, 1, 0, 0]

Q9. Return the DFT sample frequencies of a.

1signal = np.array([-2, 8, 6, 4, 1, 0, 3, 5], dtype=np.float32)
2fourier = np.fft.fft(signal)
3n = signal.size
4freq = ...
5print(freq)
[ 0.     0.125  0.25   0.375 -0.5   -0.375 -0.25  -0.125]

Window Functions

 1fig = plt.figure(figsize=(19, 10))
 2
 3# Hamming window
 4window = np.hamming(51)
 5plt.plot(np.bartlett(51), label="Bartlett window")
 6plt.plot(np.blackman(51), label="Blackman window")
 7plt.plot(np.hamming(51), label="Hamming window")
 8plt.plot(np.hanning(51), label="Hanning window")
 9plt.plot(np.kaiser(51, 14), label="Kaiser window")
10plt.xlabel("sample")
11plt.ylabel("amplitude")
12plt.legend()
13plt.grid()
14
15plt.show()

png