質問編集履歴

1

2018/09/26 05:30

投稿

jetcojapan
jetcojapan

スコア14

test CHANGED
@@ -1 +1 @@
1
- Pythonでこのようなエラーが発生しました。
1
+ Python3333
test CHANGED
@@ -5,203 +5,3 @@
5
5
  対処法を教えていただけると幸いです。
6
6
 
7
7
  また他に良い方法がありましたら教えてください。
8
-
9
- ```# -*- coding: utf-8 -*
10
-
11
- import pyaudio
12
-
13
- import wave
14
-
15
-
16
-
17
- def main():
18
-
19
- rec_time = 0.2 # 録音時間[s]
20
-
21
- file_path = "output.wav" #音声を保存するファイル名
22
-
23
- fmt = pyaudio.paInt16 # 音声のフォーマット
24
-
25
- ch = 1 # チャンネル1(モノラル)
26
-
27
- sampling_rate = 48000 # サンプリング周波数
28
-
29
- chunk =1024*6 # チャンク(データ点数)
30
-
31
- audio https://teratail.com/questions/input#= pyaudio.PyAudio()
32
-
33
- index = 1 # 録音デバイスのインデックス番号(デフォルト1)
34
-
35
-
36
-
37
- stream = audio.open(format=fmt, channels=ch, rate=sampling_rate, input=True,
38
-
39
- input_device_index = index,
40
-
41
- frames_per_buffer=chunk)
42
-
43
- print("recording start...")
44
-
45
-
46
-
47
- # 録音処理
48
-
49
- frames = []
50
-
51
- for i in range(0, int(sampling_rate / chunk * rec_time)):
52
-
53
- data = stream.read(chunk)
54
-
55
- frames.append(data)
56
-
57
-
58
-
59
- print("recording end...")
60
-
61
-
62
-
63
- # 録音終了処理
64
-
65
- stream.stop_stream()
66
-
67
- stream.close()
68
-
69
- audio.terminate()
70
-
71
-
72
-
73
- # 録音データをファイルに保存
74
-
75
- wav = wave.open(file_path, 'wb')
76
-
77
- wav.setnchannels(ch)
78
-
79
- wav.setsampwidth(audio.get_sample_size(fmt))
80
-
81
- wav.setframerate(sampling_rate)
82
-
83
- wav.writeframes(b''.join(frames))
84
-
85
- wav.close()
86
-
87
-
88
-
89
- if __name__ == '__main__':
90
-
91
- main()
92
-
93
-
94
-
95
-
96
-
97
- # coding:utf-8
98
-
99
- # Last Change:2014_08_27_00:48:01.
100
-
101
-
102
-
103
- import numpy as np
104
-
105
- import scipy.signal as sig
106
-
107
- import scipy.io.wavfile as scw
108
-
109
- import matplotlib.pyplot as plt
110
-
111
-
112
-
113
- # 自己相関関数
114
-
115
- # r(m) = sum( x(n)*x(n+m) ) n=0~N-m-1, m=0~N-1
116
-
117
- def auto_correlate(dt):
118
-
119
- cor = sig.correlate(dt,dt,mode="full")
120
-
121
- return cor[cor.size/1:]
122
-
123
-
124
-
125
- # 初出peakの検出
126
-
127
- def calc_first_peak_index(array):
128
-
129
- i = 0
130
-
131
- while array[i] > array[i+1]:
132
-
133
- i += 1
134
-
135
- while array[i] < array[i+1]:
136
-
137
- i += 1
138
-
139
- return i
140
-
141
-
142
-
143
- if __name__ == '__main__':
144
-
145
-
146
-
147
- src_name = 'output.wav'
148
-
149
-
150
-
151
- fs, dt = scw.read(src_name)
152
-
153
- dt = dt //(2 ** 10)
154
-
155
- ts = 1 //fs
156
-
157
-
158
-
159
- # 自己相関係数の計算
160
-
161
- win_size = 2 ** 10 # 512
162
-
163
- N = 2 ** 10
164
-
165
- start = 1000
166
-
167
-
168
-
169
- cor = auto_correlate(dt[start:start+N])
170
-
171
- peak_index = calc_first_peak_index(cor)
172
-
173
- freq = 1 / (peak_index * ts)
174
-
175
- print(freq)
176
-
177
-
178
-
179
- plt.plot(np.arange(cor.size) * ts, cor)
180
-
181
- plt.show()
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
- ```
190
-
191
- エラー内容
192
-
193
- ```
194
-
195
- Traceback (most recent call last):
196
-
197
- File "/home/pi/uk.py", line 81, in <module>
198
-
199
- cor = auto_correlate(dt[start:start+N])
200
-
201
- File "/home/pi/uk.py", line 57, in auto_correlate
202
-
203
- return cor[cor.size/1:]
204
-
205
- TypeError: slice indices must be integers or None or have an __index__ method
206
-
207
- ```