In [1]:
from __future__ import print_function
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

In [2]:
from datetime import date
date.today()


Out[2]:
datetime.date(2017, 11, 2)

In [3]:
author = "kyubyong. https://github.com/Kyubyong/numpy_exercises"

In [4]:
np.__version__


Out[4]:
'1.13.1'

Complex Numbers

Q1. Return the angle of a in radian.


In [5]:
a = 1+1j
output = ...
print(output)


0.785398163397

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


In [6]:
a = np.array([1+2j, 3+4j, 5+6j])
real = ...
imag = ...
print("real part=", real)
print("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].


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


[ 9.+5.j  9.+7.j  9.+9.j]

Q4. Return the complex conjugate of a.


In [8]:
a = 1+2j
output = ...
print(output)


(1-2j)

Discrete Fourier Transform

Q5. Compuete the one-dimensional DFT of a.


In [9]:
a = np.exp(2j * np.pi * np.arange(8))
output = ...
print(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.


In [10]:
print("a=", a)
inversed = ...
print("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.


In [11]:
a = [0, 1, 0, 0]
output = ...
print(output)
assert output.size==len(a)//2+1 if len(a)%2==0 else (len(a)+1)//2

# cf.
output2 = np.fft.fft(a)
print(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.


In [12]:
inversed = ...
print("inversed=", a)


inversed= [0, 1, 0, 0]

Q9. Return the DFT sample frequencies of a.


In [13]:
signal = np.array([-2, 8, 6, 4, 1, 0, 3, 5], dtype=np.float32)
fourier = np.fft.fft(signal)
n = signal.size
freq = ...
print(freq)


[ 0.     0.125  0.25   0.375 -0.5   -0.375 -0.25  -0.125]

Window Functions


In [14]:
fig = plt.figure(figsize=(19, 10))

# Hamming window
window = np.hamming(51)
plt.plot(np.bartlett(51), label="Bartlett window")
plt.plot(np.blackman(51), label="Blackman window")
plt.plot(np.hamming(51), label="Hamming window")
plt.plot(np.hanning(51), label="Hanning window")
plt.plot(np.kaiser(51, 14), label="Kaiser window")
plt.xlabel("sample")
plt.ylabel("amplitude")
plt.legend()
plt.grid()

plt.show()



In [ ]: