teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

ソースコードの修正と試したことを修正しました

2020/10/20 09:03

投稿

NEK0o0
NEK0o0

スコア2

title CHANGED
File without changes
body CHANGED
@@ -2,8 +2,7 @@
2
2
 
3
3
  Pythonのプログラミングでの質問です
4
4
  スぺクトログラムの表示などをするプログラムです
5
- 下にあるプログラムなのですがどうも配列の数が合わず
5
+ 下にあるプログラムなのですがどうも配列の数が合わずどうすればいいかご教授願いたいです。
6
- OverlapLengthなどで揃えようとしたのですがうまくいかないため、どうすればいいかご教授願いたいです。
7
6
  質問するのは初めてなためお手柔らかにお願いします---
8
7
 
9
8
 
@@ -13,34 +12,46 @@
13
12
  ValueError: operands could not be broadcast together with shapes (27200,) (27264,)
14
13
  ```
15
14
 
16
- ### 該当のソースコード
15
+ ### 修正後のソースコード
17
16
 
18
17
  ```Python
18
+ # モジュールのインポート
19
19
  import numpy as np
20
20
  import matplotlib.pyplot as plt
21
21
  import soundfile as sf
22
22
  import scipy
23
23
  from scipy import signal as sg
24
24
 
25
+ # 音声の読み込み
25
- x1, fs = sf.read('speech1.wav')
26
+ x, fs = sf.read('speech1.wav')
26
27
 
28
+ window_num = 256 # 窓幅のデータ数
27
- f, t, X1 = sg.stft(x1, fs=fs, 'OverlapLength', 24264)
29
+ stride_num = 128 # ストライド幅のデータ数
28
30
 
31
+ # スペクトログラムの計算
29
- # sf.write('outout.wav', y, fs)
32
+ f, t, X1 = sg.stft(x, fs=fs, nperseg=window_num, noverlap=(window_num-stride_num))
30
33
 
34
+ # 逆STFTによる復号
35
+ _, y = sg.istft(X1, fs=fs, nperseg=window_num, noverlap=(window_num-stride_num))
36
+
37
+ # 出力音声の保存
38
+ sf.write('outout.wav', y, fs)
39
+
40
+ # グラフに表示
41
+ # - 波形
31
42
  plt.figure('Original waveform')
32
- plt.plot(x1)
43
+ plt.plot(x)
33
44
 
45
+ # - 復号した波形
34
- plt.figure('Spectrogram')
46
+ plt.figure('Decryption waveform')
35
- plt.imshow( np.log10(np.abs(X1)), origin='low', cmap='jet', aspect="auto")
36
- plt.show()
47
+ plt.plot(y)
37
48
 
49
+ # - 信号差の波形(ここが分からない。入力信号xと出力信号yの差信号x−yが求めたい)
50
+ plt.figure('Signal difference waveform')
38
- _, x2 = sg.istft(X1, fs)
51
+ plt.plot(x-y)
39
52
 
40
- plt.figure('Signal difference waveform')
41
- plt.plot(x1 - x2)
42
53
  ```
43
54
 
44
55
  ### 試したこと
45
56
 
46
- OverlapLengthなどで要素数をそろえようとした
57
+ 質問への回答を受け下記サイトからnperseg、noverlapを用いて要素数をそろえられないかした。 https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.istft.html