File tree Expand file tree Collapse file tree 15 files changed +194
-0
lines changed Expand file tree Collapse file tree 15 files changed +194
-0
lines changed Original file line number Diff line number Diff line change
1
+ import numpy as np
2
+
3
+ def linear_conv (x , h ):
4
+ N = x .size
5
+ z = np .zeros (2 * N - 1 , dtype = complex )
6
+ k = np .arange (N )
7
+ h_addzeros = np .hstack ([h , np .zeros (N )])
8
+ for n in range (2 * N - 1 ):
9
+ z [n ] = np .sum (x [k ] * h_addzeros [n - k ])
10
+ return z
Original file line number Diff line number Diff line change
1
+ import numpy as np
2
+
3
+ def circular_conv (x , h ):
4
+ N = x .size
5
+ z = np .zeros (N , dtype = complex )
6
+ k = np .arange (N )
7
+ for n in range (N ):
8
+ z [n ] = np .sum (x [k ] * h [(n - k ) % N ])
9
+ return z
Original file line number Diff line number Diff line change
1
+ import numpy as np
2
+
3
+ def linear_conv2 (x , h ):
4
+ N = x .size
5
+ z = np .zeros (N , dtype = complex )
6
+ k = np .arange (N )
7
+ h_addzeros = np .hstack ([h , np .zeros (N )])
8
+ for n in range (N ):
9
+ z [n ] = np .sum (x [k ] * h_addzeros [n - k ])
10
+ return z
Original file line number Diff line number Diff line change
1
+ import numpy as np
2
+ import matplotlib .pyplot as plt
3
+ from q01 import linear_conv
4
+ from q02 import circular_conv
5
+ from q03 import linear_conv2
6
+
7
+ x = np .array ([4 ,3 ,2 ,1 ])
8
+ y = np .array ([1 ,0 ,- 1 ,0 ])
9
+
10
+ z1 = linear_conv (x , y )
11
+ z2 = circular_conv (x , y )
12
+ z3 = linear_conv2 (x , y )
13
+
14
+ fig = plt .figure ()
15
+ fig .add_subplot (1 ,3 ,1 )
16
+ plt .stem (z1 )
17
+ plt .title ('q01' )
18
+ fig .add_subplot (1 ,3 ,2 )
19
+ plt .stem (z2 )
20
+ plt .title ('q02' )
21
+ fig .add_subplot (1 ,3 ,3 )
22
+ plt .stem (z3 )
23
+ plt .title ('q03' )
24
+
25
+ fig .savefig ('q04_graph' )
Original file line number Diff line number Diff line change
1
+ import numpy as np
2
+ import matplotlib .pyplot as plt
3
+
4
+ x = np .zeros (14 )
5
+ x [8 ] = 1
6
+ y = np .zeros (10 )
7
+ n = np .arange (4 , 14 ) # ファンシーインデックス
8
+ y = 0.2 * (x [n ] + x [n - 1 ] + x [n - 2 ] + x [n - 3 ] + x [n - 4 ])
9
+
10
+ fig = plt .figure ()
11
+ plt .stem (y )
12
+ fig .savefig ('q05_graph' )
Original file line number Diff line number Diff line change
1
+ import numpy as np
2
+ import matplotlib .pyplot as plt
3
+
4
+ def difference_equation (x , y , n ):
5
+ if n == 0 :
6
+ return 0.4 * x [n ]
7
+ else :
8
+ return 0.3 * y [n - 1 ] + 0.4 * x [n ]
9
+
10
+ x = np .zeros (10 )
11
+ x [5 ] = 1
12
+ y = np .zeros (10 )
13
+
14
+ for n in range (10 ):
15
+ y [n ] = difference_equation (x , y , n )
16
+
17
+ fig = plt .figure ()
18
+ plt .stem (y )
19
+ fig .savefig ('q06_graph' )
Original file line number Diff line number Diff line change
1
+ import numpy as np
2
+
3
+ def difference_equation (a , b , x ):
4
+ def calc_y (n ):
5
+ N = a .size
6
+ M = b .size
7
+ sum_a , sum_b = 0
8
+ if n == 0 :
9
+ return np .sum (b [k_b ] * x [n - k_b ])
10
+ else :
11
+ for k_a in range (N ):
12
+ sum_a += a [k_a ] * calc_y (n - k_a )
13
+ for k_b in range (M ):
14
+ if n - k_b < 0 :
15
+ continue
16
+ else :
17
+ sum_b += b [k_b ] * x [n - k_b ]
18
+
19
+ return (- sum_a + sum_b ) / a [0 ]
20
+
21
+ y = np .zeros (x .size )
22
+ for n in range (x .size ):
23
+ y [n ] = calc_y (n )
24
+
25
+ return y
Original file line number Diff line number Diff line change
1
+ import numpy as np
2
+
3
+ def calc_H (a , b , omega ):
4
+ N = a .size
5
+ M = b .size
6
+ k_a = np .arange (1 , N )
7
+ k_b = np .arange (M )
8
+ sum_a = np .sum (a [k_a ] * np .exp (- 1j * omega * k_a ))
9
+ sum_b = np .sum (b [k_b ] * np .exp (- 1j * omega * k_b ))
10
+
11
+ return sum_b / (1 + sum_a )
Original file line number Diff line number Diff line change
1
+ import numpy as np
2
+ import matplotlib .pyplot as plt
3
+ from q08 import calc_H
4
+
5
+ fs = 16000
6
+ a = np .array ([0 , 0 ])
7
+ b = np .full (3 , 0.33 )
8
+ N = 10000
9
+
10
+ # グラフ用の配列
11
+ omegas = np .zeros (N )
12
+ Hs = np .zeros (N , dtype = complex )
13
+ for i in range (N ):
14
+ f = i / N * fs
15
+ omega = 2 * np .pi * f / fs
16
+ omegas [i ] = omega
17
+ Hs [i ] = calc_H (a , b , omega )
18
+
19
+
20
+ A = np .abs (Hs )
21
+ P = np .rad2deg (np .angle (Hs ))
22
+ # グラフの中心を調整
23
+ omegas = omegas - 3
24
+ A = np .roll (A , 2000 )
25
+ P = np .roll (P , 2000 )
26
+
27
+ fig = plt .figure ()
28
+ fig .add_subplot (1 , 2 , 1 )
29
+ plt .stem (omegas , A )
30
+ plt .title ('Amplitude Characteristics' )
31
+ plt .xlabel ('omega' )
32
+ fig .add_subplot (1 , 2 , 2 )
33
+ plt .stem (omegas , P )
34
+ plt .title ('Phase Characteristics' )
35
+ plt .xlabel ('omega' )
36
+ fig .savefig ('q09_graph' )
Original file line number Diff line number Diff line change
1
+ import numpy as np
2
+ import matplotlib .pyplot as plt
3
+ from q08 import calc_H
4
+
5
+ fs = 16000
6
+ a = np .array ([0 , 0.3 ])
7
+ b = np .array ([0.4 , 0 ])
8
+ N = 4000
9
+
10
+ # グラフ用の配列
11
+ omegas = np .zeros (N )
12
+ Hs = np .zeros (N , dtype = complex )
13
+ for i in range (N ):
14
+ f = i / N * fs
15
+ omega = 2 * np .pi * f / fs
16
+ omegas [i ] = omega
17
+ Hs [i ] = calc_H (a , b , omega )
18
+
19
+
20
+ A = np .abs (Hs )
21
+ P = np .rad2deg (np .angle (Hs ))
22
+ # グラフの中心を調整
23
+ omegas = omegas - 3
24
+ A = np .roll (A , 2000 )
25
+ P = np .roll (P , 2000 )
26
+
27
+ fig = plt .figure ()
28
+ fig .add_subplot (1 , 2 , 1 )
29
+ plt .stem (omegas , A )
30
+ plt .title ('Amplitude Characteristics' )
31
+ plt .xlabel ('omega' )
32
+ fig .add_subplot (1 , 2 , 2 )
33
+ plt .stem (omegas , P )
34
+ plt .title ('Phase Characteristics' )
35
+ plt .xlabel ('omega' )
36
+ fig .savefig ('q10_graph' )
37
+
You can’t perform that action at this time.
0 commit comments