-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab9.py
139 lines (111 loc) · 3.25 KB
/
Lab9.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 29 19:18:23 2019
@author: holt3393
"""
import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack as fft
##################################
##### TIME ARRAY DEFINITION ######
##################################
steps = 1e-2
tmin = 0
tmax = 2
t = np.arange(tmin,tmax,steps)
##################################
def my_fft(x, fs):
N = len(x)
X_fft = fft.fft(x)
X_fft_shifted = fft.fftshift(X_fft)
freq = np.arange(-N/2,N/2)*fs/N
X_mag = np.abs(X_fft_shifted)/N
X_phi = np.angle(X_fft_shifted)
return X_mag, X_phi, freq
def totalplot(x, t ,funcname, lower, upper):
X_mag, X_phi, freq = my_fft(x, 1000)
plt.figure(figsize=(10,7))
plt.subplot(3,1,1) #Top Figure
plt.plot(t,x)
plt.title('FFT of ' + funcname)
plt.ylabel(funcname)
plt.xlabel("t (s)")
plt.subplot(3,2,3) #Mag 1
plt.stem(freq, X_mag)
plt.ylabel("Magnitude")
plt.subplot(3,2,4)#Mag 2
plt.stem(freq, X_mag)
plt.xlim([lower,upper])
plt.subplot(3,2,5)#Phase 1
plt.stem(freq, X_phi)
plt.ylabel("Phase")
plt.xlabel("Freq. (Hz)")
plt.subplot(3,2,6)#Phase 2
plt.stem(freq, X_phi)
plt.xlim([lower,upper])
plt.xlabel("Freq. (Hz)")
plt.show()
def f1(t):
return np.cos(2*np.pi*t)
def f2(t):
return 5*np.sin(2*np.pi*t)
def f3(t):
x=2*np.cos((4*np.pi*t)-2)+(np.sin((12*np.pi*t)+3)*np.sin((12*np.pi*t)+3))
return x
def b(k):
b = (-2*np.cos(np.pi*k) + np.cos(2*np.pi*k) + 1)/(np.pi*k)
return b
def FSapprox(n,t,T):
x=0
for i in range(1,n+1):
x = x + b(i)*np.sin(i*(2*np.pi/T)*t)
return x
totalplot(f1(t), t, "f1", -2, 2)
totalplot(f2(t), t, "f2", -2, 2)
totalplot(f3(t), t, "f3", -15, 15)
def my_fft_clean(x, fs):
N = len(x)
X_fft = fft.fft(x)
X_fft_shifted = fft.fftshift(X_fft)
freq = np.arange(-N/2,N/2)*fs/N
X_mag = np.abs(X_fft_shifted)/N
X_phi = np.angle(X_fft_shifted)
for i in range(N):
if (np.abs(X_mag[i]) < 1e-10):
X_phi[i] = 0
return X_mag, X_phi, freq
def totalplotclean(x, t ,funcname, lower, upper):
X_mag, X_phi, freq = my_fft_clean(x, 100)
plt.figure(figsize=(10,7))
plt.subplot(3,1,1) #Top Figure
plt.plot(t,x)
plt.title('Clean FFT of ' + funcname)
plt.ylabel(funcname)
plt.xlabel("t (s)")
plt.subplot(3,2,3) #Mag 1
plt.stem(freq, X_mag)
plt.ylabel("Magnitude")
plt.subplot(3,2,4)#Mag 2
plt.stem(freq, X_mag)
plt.xlim([lower,upper])
plt.subplot(3,2,5)#Phase 1
plt.stem(freq, X_phi)
plt.ylabel("Phase")
plt.xlabel("Freq. (Hz)")
plt.subplot(3,2,6)#Phase 2
plt.stem(freq, X_phi)
plt.xlim([lower,upper])
plt.xlabel("Freq. (Hz)")
plt.show()
totalplotclean(f1(t), t, "f1", -2, 2)
totalplotclean(f2(t), t, "f2", -2, 2)
totalplotclean(f3(t), t, "f3", -15, 15)
##################################
##### TIME ARRAY DEFINITION ######
##################################
steps = 1e-2
tmin = 0
tmax = 16
t = np.arange(tmin,tmax,steps)
##################################
totalplotclean(FSapprox(15,t,8), t, "Square Fourier", -2, 2)