-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·94 lines (64 loc) · 1.91 KB
/
main.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
#!/usr/bin/python3
# -----------------------------------------------------------------
# simple script contain common fourier functions for ex:
# DFT, FFT, FOURIER-SERIES, ....., etc.
#
#
#
# Author:N84.
#
# Create Date:Wed Jun 22 23:21:41 2022.
# ///
# ///
# ///
# -----------------------------------------------------------------
from math import (pi, sqrt, sin, cos)
from cmath import exp # the normal exp from math module will not work.
from utils import *
# todo: make the script work from the command line directly, by passing args.
clear()
def fft():
"""
:ARGS:
:RETURNS:
:INFO:
"""
pass
def dft(sequence: list):
"""
:ARGS:
sequence:list => the discrete sequence;
:RETURNS:
return list;
:INFO:
calculate discrete-fourier-transform for given sequence.
"""
assert isinstance(
sequence, list), f"the given sequence is not 'list' type its '{type(sequence)}'"
N = len(sequence)
# define x(k):
def x(k: int): return sum(sequence_value*exp((-1j*2*pi*k*i)/N)
for i, sequence_value in enumerate(sequence))
return [x(k) for k in range(N)]
def idft(sequence: list):
"""
:ARGS:
sequence:list => the dft discrete sequence;
:RETURNS:
return list;
:INFO:
calculate inverse-discrete-fourier-transform for given sequence.
"""
assert isinstance(
sequence, list), f"the given sequence is not 'list' type its '{type(sequence)}'"
N = len(sequence)
# define x(n):
def x(k: int): return (1/N) * sum(sequence_value*exp((1j*2*pi*k*i)/N)
for i, sequence_value in enumerate(sequence))
return [x(k) for k in range(N)]
def main():
s = [*range(1, 101)]
dft_s = dft(s)
print_sequence(dft_s, "dft")
if __name__ == "__main__":
main()