# -*- coding: utf-8 -*-
# 2009-03-12 Moritz

import matplotlib
matplotlib.rc('font', size=8) # adjust font size of plot
import matplotlib.pyplot as plt
import numpy as np
plt.close('all')

# Number of points and sampling frequency
npts=33
df=20.0

# Set generator frequency
fg = 2.0 # in Hz

# Generate f0 Hz sine wave
time = np.arange(npts,dtype='f')/df # time in seconds
y = np.sin(2*np.pi*fg * time + np.pi/5)

# Multiply by Hanning window, other possibel fcts:
#np.flat, np.hamming, np.bartlett, np.blackman
win_fct = np.hanning(npts)
y_win = y * win_fct

# Do the Fast Fourier Transformation for Real valued data
fy = np.fft.rfft(y, n=npts)
fy_win = np.fft.rfft(y_win, n=npts)


#
# Plot the whole filtering process
#
freq = np.linspace(0,df/2,len(fy))
plt.subplot(121)
plt.title('Time Domain')
plt.plot(time, y, label="Original Data", linewidth=2)
plt.plot(time, y_win, label="Tapered Data", linewidth=2)
plt.plot(time, win_fct, label="The Taper", linewidth=2)
plt.legend()
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
#
plt.subplot(122)
plt.title('Frequency Domain')
plt.bar(freq, abs(fy), label="no Taper", width=20./npts)
plt.bar(freq, abs(fy_win), label="with Taper", width=20./npts, color='g')
plt.legend()
plt.xlabel('Frequency [Hz]')
plt.ylabel('Arbitrary Amplitude')

plt.figure()
plt.title('Frequency Domain')
plt.plot(freq, np.angle(fy), label="no Taper", linewidth=3)
plt.plot(freq, np.angle(fy_win), label="with Taper", linewidth=3)
plt.legend()
plt.xlabel('Frequency [Hz]')
plt.ylabel('Phase')
#
plt.show()
