質問編集履歴
4
コード内容の変更
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
Pythonでのハイパスフィルタが実装できない
|
body
CHANGED
@@ -2,19 +2,18 @@
|
|
2
2
|
現在1秒ごとに波形の表示とその波形のフーリエ変換を行っています。
|
3
3
|
以下のコードが該当するのですが、300ヘルツ以下をカットした状態でプロットしたいです。
|
4
4
|
ハイパスフィルタというのですがPython初心者でありどのようにコードを変更すればよいのかわかりません。
|
5
|
-
わかる方がいらっしゃいましたら
|
5
|
+
わかる方がいらっしゃいましたら変えるコードを教えていただきたいです。
|
6
6
|
何卒宜しくお願い致します。
|
7
7
|
|
8
|
-
以下のサイトのコードを参考に実行しようとしたのですが、
|
8
|
+
以下のサイトのコードを参考に実行しようとしたのですが、このコードを実行すると300ヘルツ以下をカットしたいのに300以上がカットされてしまいます。
|
9
9
|
https://algorithm.joho.info/programming/python/numpy-fast-fourier-transform/#toc3
|
10
10
|
|
11
11
|
```python
|
12
|
+
# -*- coding: utf-8 -*-
|
12
13
|
import wave
|
13
14
|
|
14
15
|
from matplotlib.colors import same_color
|
15
|
-
|
16
16
|
import numpy as np
|
17
|
-
|
18
17
|
import matplotlib.pyplot as plt
|
19
18
|
|
20
19
|
def main():
|
@@ -22,75 +21,60 @@
|
|
22
21
|
wf = wave.open(filename, "r" )
|
23
22
|
|
24
23
|
# WAVファイルの情報を表示
|
25
|
-
|
26
24
|
print ("オーディオチャンネル数(モノラル: 1 ステレオ:2 ) : ", wf.getnchannels())
|
27
|
-
|
28
25
|
print ("サンプルサイズ(バイト数) : ", wf.getsampwidth())
|
29
|
-
|
30
26
|
print ("サンプリングレート : ", wf.getframerate())
|
31
|
-
|
32
27
|
print ("オーディオフレーム数 : ", wf.getnframes())
|
33
|
-
|
34
28
|
print ("圧縮形式 : ", wf.getcomptype())
|
35
|
-
|
36
29
|
print ("(圧縮形式) : ", wf.getcompname())
|
37
|
-
|
38
30
|
print ("パラメータ : ", wf.getparams())
|
39
|
-
|
40
31
|
print ("記録時間(Sec) : ", float(wf.getnframes()) / wf.getframerate() )
|
41
32
|
|
42
33
|
fs = wf.getframerate() # サンプリング周波数
|
34
|
+
|
35
|
+
fc = 300 # カットオフ周波数
|
36
|
+
|
43
37
|
g = wf.readframes(wf.getnframes())
|
44
38
|
g = np.frombuffer(g, dtype= "int16")/32768.0 # -1~1に正規化
|
39
|
+
|
40
|
+
mado = 1#s
|
41
|
+
N = mado*fs # サンプル数=窓(1秒)×サンプリング周波数
|
42
|
+
|
43
|
+
freq = np.linspace(0, 1.0/mado, N)
|
44
|
+
|
45
45
|
wf.close()
|
46
|
-
mado = 1#s
|
47
46
|
for ki in range(round(wf.getnframes()/fs)-1):
|
48
47
|
n0 = ki # サンプリング開始位置
|
49
|
-
N = mado*fs # サンプル数=窓(1秒)×サンプリング周波数
|
50
48
|
gz = g[::2] # サンプル数片方にわける
|
51
|
-
|
52
49
|
gg = gz[ki*N:ki*N+N]
|
50
|
+
# print(fs,ki,N,gz,gg)
|
53
51
|
G = np.fft.fft(gg) # 高速フーリエ変換
|
54
52
|
amp = [np.sqrt(c.real ** 2 + c.imag ** 2) for c in G] # 振幅スペクトル
|
55
53
|
phase = [np.arctan2(int(c.imag), int(c.real)) for c in G] # 位相スペクトル
|
56
54
|
flist = np.fft.fftfreq(N, d=1.0/fs) # 周波数リスト
|
55
|
+
flist[(freq < fc)] = 0
|
56
|
+
flist[(freq > 1/(mado*2))] = 0
|
57
57
|
|
58
58
|
# 波形サンプルを描画
|
59
|
-
|
60
59
|
fig = plt.figure()
|
61
|
-
|
62
60
|
plt.subplot(211)
|
63
|
-
|
64
61
|
plt.plot(np.linspace(n0,n0+mado,N),gg)
|
65
|
-
|
66
62
|
plt.axis([n0, n0+mado, -0.2, 0.2])
|
67
|
-
|
68
63
|
plt.xlabel("時間 [sample]",fontname="MS Gothic")
|
69
|
-
|
70
64
|
plt.ylabel("振幅",fontname="MS Gothic")
|
71
65
|
|
72
66
|
# 振幅スペクトルを描画
|
73
|
-
|
74
67
|
plt.subplot(212)
|
75
|
-
|
76
68
|
plt.plot(flist, amp, marker='o', linestyle='-')
|
77
|
-
|
78
69
|
plt.axis([0, fs/2, 0, int(max(amp))])
|
79
|
-
|
80
70
|
plt.xlabel("周波数 [Hz]",fontname="MS Gothic")
|
81
|
-
|
82
71
|
plt.ylabel("振幅スペクトル",fontname="MS Gothic")
|
83
|
-
|
84
72
|
output ="C:\Users\hiro2\Desktop\sound\kara"
|
85
|
-
|
86
73
|
fig.savefig(output+"\"+str(ki)+"_"+str(mado)+".jpg")
|
87
|
-
|
88
74
|
plt.close()
|
89
|
-
|
90
75
|
fig = plt.figure()
|
91
|
-
|
92
76
|
plt.close()
|
93
77
|
|
94
78
|
if __name__ == '__main__':
|
95
|
-
main()
|
79
|
+
main()
|
96
80
|
```
|
3
コード表示の変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -21,7 +21,7 @@
|
|
21
21
|
filename = "04-agarikata-kara-01-p6TAskMJUgg.wav"
|
22
22
|
wf = wave.open(filename, "r" )
|
23
23
|
|
24
|
-
|
24
|
+
# WAVファイルの情報を表示
|
25
25
|
|
26
26
|
print ("オーディオチャンネル数(モノラル: 1 ステレオ:2 ) : ", wf.getnchannels())
|
27
27
|
|
@@ -55,7 +55,7 @@
|
|
55
55
|
phase = [np.arctan2(int(c.imag), int(c.real)) for c in G] # 位相スペクトル
|
56
56
|
flist = np.fft.fftfreq(N, d=1.0/fs) # 周波数リスト
|
57
57
|
|
58
|
-
|
58
|
+
# 波形サンプルを描画
|
59
59
|
|
60
60
|
fig = plt.figure()
|
61
61
|
|
@@ -69,7 +69,7 @@
|
|
69
69
|
|
70
70
|
plt.ylabel("振幅",fontname="MS Gothic")
|
71
71
|
|
72
|
-
|
72
|
+
# 振幅スペクトルを描画
|
73
73
|
|
74
74
|
plt.subplot(212)
|
75
75
|
|
2
コード表示の変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -8,8 +8,7 @@
|
|
8
8
|
以下のサイトのコードを参考に実行しようとしたのですが、以下のサイトでは1秒おきに実行していないという点で自分のコードと違っていたために動くコードを作成することはできませんでした。
|
9
9
|
https://algorithm.joho.info/programming/python/numpy-fast-fourier-transform/#toc3
|
10
10
|
|
11
|
-
```
|
11
|
+
```python
|
12
|
-
`# -*- coding: utf-8 -*-
|
13
12
|
import wave
|
14
13
|
|
15
14
|
from matplotlib.colors import same_color
|
@@ -93,4 +92,5 @@
|
|
93
92
|
plt.close()
|
94
93
|
|
95
94
|
if __name__ == '__main__':
|
96
|
-
main()`
|
95
|
+
main()`
|
96
|
+
```
|
1
コードについて、調べたこと、試したことの追加
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
Pythonでのハイパスフィルタの実装方法
|
1
|
+
コードPythonでのハイパスフィルタの実装方法
|
body
CHANGED
@@ -5,26 +5,39 @@
|
|
5
5
|
わかる方がいらっしゃいましたら追加するコードを教えていただきたいです。
|
6
6
|
何卒宜しくお願い致します。
|
7
7
|
|
8
|
+
以下のサイトのコードを参考に実行しようとしたのですが、以下のサイトでは1秒おきに実行していないという点で自分のコードと違っていたために動くコードを作成することはできませんでした。
|
9
|
+
https://algorithm.joho.info/programming/python/numpy-fast-fourier-transform/#toc3
|
10
|
+
|
8
11
|
```Python
|
9
|
-
# -*- coding: utf-8 -*-
|
12
|
+
`# -*- coding: utf-8 -*-
|
10
13
|
import wave
|
11
14
|
|
12
15
|
from matplotlib.colors import same_color
|
16
|
+
|
13
17
|
import numpy as np
|
18
|
+
|
14
19
|
import matplotlib.pyplot as plt
|
15
20
|
|
16
21
|
def main():
|
17
22
|
filename = "04-agarikata-kara-01-p6TAskMJUgg.wav"
|
18
23
|
wf = wave.open(filename, "r" )
|
19
24
|
|
20
|
-
|
25
|
+
% WAVファイルの情報を表示
|
26
|
+
|
21
27
|
print ("オーディオチャンネル数(モノラル: 1 ステレオ:2 ) : ", wf.getnchannels())
|
28
|
+
|
22
29
|
print ("サンプルサイズ(バイト数) : ", wf.getsampwidth())
|
30
|
+
|
23
31
|
print ("サンプリングレート : ", wf.getframerate())
|
32
|
+
|
24
33
|
print ("オーディオフレーム数 : ", wf.getnframes())
|
34
|
+
|
25
35
|
print ("圧縮形式 : ", wf.getcomptype())
|
36
|
+
|
26
37
|
print ("(圧縮形式) : ", wf.getcompname())
|
38
|
+
|
27
39
|
print ("パラメータ : ", wf.getparams())
|
40
|
+
|
28
41
|
print ("記録時間(Sec) : ", float(wf.getnframes()) / wf.getframerate() )
|
29
42
|
|
30
43
|
fs = wf.getframerate() # サンプリング周波数
|
@@ -36,33 +49,48 @@
|
|
36
49
|
n0 = ki # サンプリング開始位置
|
37
50
|
N = mado*fs # サンプル数=窓(1秒)×サンプリング周波数
|
38
51
|
gz = g[::2] # サンプル数片方にわける
|
52
|
+
|
39
53
|
gg = gz[ki*N:ki*N+N]
|
40
|
-
# print(fs,ki,N,gz,gg)
|
41
54
|
G = np.fft.fft(gg) # 高速フーリエ変換
|
42
55
|
amp = [np.sqrt(c.real ** 2 + c.imag ** 2) for c in G] # 振幅スペクトル
|
43
56
|
phase = [np.arctan2(int(c.imag), int(c.real)) for c in G] # 位相スペクトル
|
44
57
|
flist = np.fft.fftfreq(N, d=1.0/fs) # 周波数リスト
|
45
58
|
|
46
|
-
|
59
|
+
% 波形サンプルを描画
|
60
|
+
|
47
61
|
fig = plt.figure()
|
62
|
+
|
48
63
|
plt.subplot(211)
|
64
|
+
|
49
65
|
plt.plot(np.linspace(n0,n0+mado,N),gg)
|
66
|
+
|
50
67
|
plt.axis([n0, n0+mado, -0.2, 0.2])
|
68
|
+
|
51
69
|
plt.xlabel("時間 [sample]",fontname="MS Gothic")
|
70
|
+
|
52
71
|
plt.ylabel("振幅",fontname="MS Gothic")
|
53
72
|
|
54
|
-
|
73
|
+
% 振幅スペクトルを描画
|
74
|
+
|
55
75
|
plt.subplot(212)
|
76
|
+
|
56
77
|
plt.plot(flist, amp, marker='o', linestyle='-')
|
78
|
+
|
57
79
|
plt.axis([0, fs/2, 0, int(max(amp))])
|
80
|
+
|
58
81
|
plt.xlabel("周波数 [Hz]",fontname="MS Gothic")
|
82
|
+
|
59
83
|
plt.ylabel("振幅スペクトル",fontname="MS Gothic")
|
84
|
+
|
60
85
|
output ="C:\Users\hiro2\Desktop\sound\kara"
|
86
|
+
|
61
87
|
fig.savefig(output+"\"+str(ki)+"_"+str(mado)+".jpg")
|
88
|
+
|
62
89
|
plt.close()
|
90
|
+
|
63
91
|
fig = plt.figure()
|
92
|
+
|
64
93
|
plt.close()
|
65
94
|
|
66
95
|
if __name__ == '__main__':
|
67
|
-
main()
|
96
|
+
main()`
|
68
|
-
``
|