-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset2.py
More file actions
67 lines (55 loc) · 1.78 KB
/
Copy pathset2.py
File metadata and controls
67 lines (55 loc) · 1.78 KB
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
import numpy as np
import matplotlib.pyplot as plt
from typing import Tuple
INF = 8
def plot(
signal,
title=None,
y_range=(-1, 3),
figsize = (8, 3),
x_label='n (Time Index)',
y_label='x[n]',
saveTo=None
):
plt.figure(figsize=figsize)
plt.xticks(np.arange(-INF, INF + 1, 1))
y_range = (y_range[0], max(np.max(signal), y_range[1]) + 1)
# set y range of
plt.ylim(*y_range)
plt.stem(np.arange(-INF, INF + 1, 1), signal)
plt.title(title)
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.grid(True)
if saveTo is not None:
plt.savefig(saveTo)
# plt.show()
def init_signal():
return np.zeros(2 * INF + 1)
def time_reverse_signal(x : np.ndarray) -> np.ndarray:
# implement this function
return x[::-1]
def odd_even_decomposition(x : np.ndarray)->Tuple[np.ndarray, np.ndarray]:
# implement this function
# you can return 2 values from function in the following way
# return value1, value2
rev=x[::-1]
even = (x+rev)/2
odd=(x-rev)/2
return odd,even
def main():
img_root_path = '.'
signal = init_signal()
signal[INF] = 1
signal[INF+1] = .5
signal[INF-1] = 2
signal[INF + 2] = 1
signal[INF - 2] = .5
plot(signal, title='Original Signal(x[n])', saveTo=f'{img_root_path}/x[n].png')
reversed = time_reverse_signal(signal)
plot(reversed, title='x[-n]', saveTo=f'{img_root_path}/x[-n].png')
plot(time_reverse_signal(reversed), title='x[-(-n)]', saveTo=f'{img_root_path}/x[-(-n)].png')
odd_signal, even_signal = odd_even_decomposition(signal)
plot(odd_signal, title='Odd Signal(x[n])', saveTo=f'{img_root_path}/x_odd[n].png')
plot(even_signal, title='Even Signal(x[n])', saveTo=f'{img_root_path}/x_even[n].png')
main()