-
Notifications
You must be signed in to change notification settings - Fork 0
/
dft_idft.py
141 lines (115 loc) · 3.11 KB
/
dft_idft.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
140
141
#! /usr/bin/python3
import sys
import math
import matplotlib.pyplot as pyplot
import numpy
N_0 = 12
OMEGA_0 = numpy.float128(2 * numpy.float128(math.pi) / N_0)
def unit_step(n):
result = 0
if (n >= 0):
result = 1
#if
return numpy.float128(result)
#def unit
def x_time(n):
result = 0
A = numpy.float128(7.0)
k_0 = numpy.float128(2.0)
theta = numpy.float128(20.0)
#result = A * math.cos((2 * math.pi * k_0 / N_0 * n) + theta)
#result = unit_step(n) - unit_step(n - N_0)
if (0 <= n <= N_0):
result = numpy.float128(0.5)**numpy.float128(n)
return result
#x_time
def dft_at_k(k):
x_laplace_k = numpy.float128(0.0)
for n in range(0, N_0):
x_laplace_k += x_time(n) * numpy.float128(math.e)**(-1j * k * OMEGA_0 * n)
#for n
return x_laplace_k
#dft_at_k
def idft_at_n(n):
x_time_n = numpy.float128(0.0)
for k in range(0, N_0 - 1):
x_time_n += dft_at_k(k) * numpy.float128(math.e)**(+1j * k * OMEGA_0 * n)
#for k
x_time_n /= N_0
return x_time_n
#idft
def main(argc, argv):
pyplot.figure()
pyplot.title("y = x[n]")
pyplot.xlabel("n")
pyplot.ylabel("y = x[n]")
pyplot.grid()
pyplot.xlim(-1, (N_0-1) + 1)
for n in range(0, (N_0-1) + 1):
result = x_time(n)
print("(" + str(n) + ", " + str(result) + ")")
print()
pyplot.plot([n, n], [0, result], "ro-")
#for n
pyplot.ion()
pyplot.show()
input("Done plotting x[n]. Press any key to continue...")
#k v real
pyplot.figure()
pyplot.xlabel("K")
pyplot.ylabel("Magnitude")
pyplot.grid()
pyplot.xlim((-1, N_0))
#pyplot.ylim((-limit, limit))
#k v imag
pyplot.figure()
pyplot.xlabel("K")
pyplot.ylabel("Phase")
pyplot.grid()
pyplot.xlim((-1, N_0))
#pyplot.ylim((-limit, limit))
'''
#real v imag
pyplot.figure()
pyplot.xlabel("Real")
pyplot.ylabel("Imaginary")
pyplot.grid()
#pyplot.xlim((-1, N_0-1))
#pyplot.ylim((-limit, limit))
'''
for k in range(0, N_0):
result = dft_at_k(k)
print(result.real)
print(result.imag)
print()
pyplot.figure(2)
pyplot.plot([k, k], [0, abs(result)], "ro-")
#pyplot.scatter(k, result.real)
pyplot.figure(3)
pyplot.plot([k, k], [0, numpy.angle(result)], "ro-")
#pyplot.scatter(k, result.imag)
'''
pyplot.figure(4)
#pyplot.plot([result.real, result.real], [0, result.imag], "ro-")
pyplot.scatter(result.real, result.imag)
'''
#for k
pyplot.ion()
pyplot.show()
print()
input("Done calculating DFT. Press any key to continue...")
#pyplot.close("all")
pyplot.figure()
pyplot.xlim(-1, (N_0-1) + 1)
for n in range(0, N_0):
result = idft_at_n(n)
print(abs(result))
pyplot.plot([n, n], [0, abs(result)], "ro-")
#for n
pyplot.ion()
pyplot.show()
input("Done calculating IDFT. Press any key to continue...")
pyplot.close("all")
return 0
#main
sys.exit(main(len(sys.argv), sys.argv))