質問編集履歴

12

コードの修正

2018/10/02 11:13

投稿

watagasi_
watagasi_

スコア13

test CHANGED
File without changes
test CHANGED
@@ -110,38 +110,40 @@
110
110
 
111
111
 
112
112
 
113
- if(th_min<0):
113
+ if(th_min<0):
114
114
 
115
- waveform_sum += wave_form[th_max]
115
+ waveform_sum += wave_form[th_max]
116
116
 
117
- if (waveform_sum / th_max) * 100 <= 80:
117
+ if (waveform_sum / th_max) * 100 <= 80:
118
118
 
119
- recording_arr[th] = 1
119
+ recording_arr[th] = 1
120
120
 
121
121
 
122
122
 
123
- if(th_min>=0)and(th_max<wave_form_len ):
123
+ if(th_min>=0)and(th_max<wave_form_len ):
124
124
 
125
- waveform_sum += wave_form[th_max]
125
+ waveform_sum += wave_form[th_max]
126
126
 
127
- waveform_sum -= wave_form[th_min]
127
+ waveform_sum -= wave_form[th_min]
128
128
 
129
- if (waveform_sum /diff_th)*100 <=80:
129
+ if (waveform_sum /diff_th)*100 <=80:
130
130
 
131
- recording_arr[th] = 1
131
+ recording_arr[th] = 1
132
132
 
133
133
 
134
134
 
135
- if(th_max>=wave_form_len ):
135
+ if(th_max>=wave_form_len ):
136
136
 
137
- waveform_sum -= wave_form[th_min]
137
+ waveform_sum -= wave_form[th_min]
138
138
 
139
- if (waveform_sum /(wave_form_len -th_min))*100 <=80:
139
+ if (waveform_sum /(wave_form_len -th_min))*100 <=80:
140
140
 
141
- recording_arr[th] = 1
141
+ recording_arr[th] = 1
142
142
 
143
143
 
144
144
 
145
+ th_max += 1
146
+
145
- th_min += 1
147
+ th_min += 1
146
148
 
147
149
  ```

11

追記

2018/10/02 11:13

投稿

watagasi_
watagasi_

スコア13

test CHANGED
File without changes
test CHANGED
@@ -71,3 +71,77 @@
71
71
  th_max += 1
72
72
 
73
73
  ```
74
+
75
+
76
+
77
+ 追記
78
+
79
+ 頂いたご指摘を全て合わせて以下のように実装したところ、正常に動作した上で30秒ほどかかっていた動作が15秒にまで改善されました。
80
+
81
+ 本来扱いたいwave_formの長さは1億ほどなので、もう少し高速化したいとは思いますが、なにか方法はありますでしょうか。
82
+
83
+ ```Python3
84
+
85
+ import numpy as np
86
+
87
+ from numpy.random import *
88
+
89
+
90
+
91
+ sampling_rate = 44110
92
+
93
+ wave_form = randint(0,2,3600000)
94
+
95
+ th_min = - int(0.09*sampling_rate)
96
+
97
+ th_max = int(0.09*sampling_rate)
98
+
99
+ diff_th = th_max - th_min
100
+
101
+ wave_form_len = len(wave_form)
102
+
103
+ recording_arr = np.zeros(wave_form_len)
104
+
105
+ waveform_sum = np.sum(waveform[:th_max-1])
106
+
107
+
108
+
109
+ for th in range(wave_form_len):
110
+
111
+
112
+
113
+ if(th_min<0):
114
+
115
+ waveform_sum += wave_form[th_max]
116
+
117
+ if (waveform_sum / th_max) * 100 <= 80:
118
+
119
+ recording_arr[th] = 1
120
+
121
+
122
+
123
+ if(th_min>=0)and(th_max<wave_form_len ):
124
+
125
+ waveform_sum += wave_form[th_max]
126
+
127
+ waveform_sum -= wave_form[th_min]
128
+
129
+ if (waveform_sum /diff_th)*100 <=80:
130
+
131
+ recording_arr[th] = 1
132
+
133
+
134
+
135
+ if(th_max>=wave_form_len ):
136
+
137
+ waveform_sum -= wave_form[th_min]
138
+
139
+ if (waveform_sum /(wave_form_len -th_min))*100 <=80:
140
+
141
+ recording_arr[th] = 1
142
+
143
+
144
+
145
+ th_min += 1
146
+
147
+ ```

10

正常に動作するコードの訂正

2018/10/02 11:09

投稿

watagasi_
watagasi_

スコア13

test CHANGED
File without changes
test CHANGED
@@ -50,7 +50,7 @@
50
50
 
51
51
 
52
52
 
53
- if(th_min>=0):
53
+ if(th_min>=0)and(th_max<wave_form_len):
54
54
 
55
55
  if (np.sum(wave_form[th_min:th_max])/diff_th)*100 <=80:
56
56
 
@@ -58,7 +58,7 @@
58
58
 
59
59
 
60
60
 
61
- if(th_max>=len(wave_form)):
61
+ if(th_max>=wave_form_len):
62
62
 
63
63
  if (np.sum(wave_form[th_min:])/(wave_form_len -th_min)*100 <=80:
64
64
 

9

2018/10/02 10:43

投稿

watagasi_
watagasi_

スコア13

test CHANGED
File without changes
test CHANGED
@@ -60,7 +60,7 @@
60
60
 
61
61
  if(th_max>=len(wave_form)):
62
62
 
63
- if (np.sum(wave_form[th_min:])/wave_form_len -th_min)*100 <=80:
63
+ if (np.sum(wave_form[th_min:])/(wave_form_len -th_min)*100 <=80:
64
64
 
65
65
  recording_arr[th] = 1
66
66
 

8

2018/10/02 10:36

投稿

watagasi_
watagasi_

スコア13

test CHANGED
File without changes
test CHANGED
@@ -71,9 +71,3 @@
71
71
  th_max += 1
72
72
 
73
73
  ```
74
-
75
-
76
-
77
- wave_formはランダムな0と1で構成された長さ3,600,000のnumpy配列です。
78
-
79
- wwave_form = [0,0,0,1,1,0,1, … ,1,0,1,1,0]のようになっています。

7

2018/10/02 08:53

投稿

watagasi_
watagasi_

スコア13

test CHANGED
File without changes
test CHANGED
@@ -24,7 +24,7 @@
24
24
 
25
25
  sampling_rate = 44110
26
26
 
27
- wave_form = randint(0,1,3600000)
27
+ wave_form = randint(0,2,3600000)
28
28
 
29
29
  th_min = - int(0.09*sampling_rate)
30
30
 

6

2018/10/02 08:50

投稿

watagasi_
watagasi_

スコア13

test CHANGED
File without changes
test CHANGED
@@ -18,9 +18,13 @@
18
18
 
19
19
  import scipy.io.wavfile
20
20
 
21
+ from numpy.random import *
21
22
 
22
23
 
24
+
25
+ sampling_rate = 44110
26
+
23
- sampling_rate, wave_form = scipy.io.wavfile.read(filename)
27
+ wave_form = randint(0,1,3600000)
24
28
 
25
29
  th_min = - int(0.09*sampling_rate)
26
30
 

5

具体例の追加

2018/10/02 08:48

投稿

watagasi_
watagasi_

スコア13

test CHANGED
File without changes
test CHANGED
@@ -67,3 +67,9 @@
67
67
  th_max += 1
68
68
 
69
69
  ```
70
+
71
+
72
+
73
+ wave_formはランダムな0と1で構成された長さ3,600,000のnumpy配列です。
74
+
75
+ wwave_form = [0,0,0,1,1,0,1, … ,1,0,1,1,0]のようになっています。

4

コードの訂正

2018/10/02 08:19

投稿

watagasi_
watagasi_

スコア13

test CHANGED
File without changes
test CHANGED
@@ -34,7 +34,7 @@
34
34
 
35
35
 
36
36
 
37
- for th in range(len(wave_form)):
37
+ for th in range(wave_form_len):
38
38
 
39
39
 
40
40
 

3

コードの訂正

2018/10/02 06:01

投稿

watagasi_
watagasi_

スコア13

test CHANGED
File without changes
test CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
  import numpy as np
18
18
 
19
- import scipy
19
+ import scipy.io.wavfile
20
20
 
21
21
 
22
22
 

2

コードを訂正した

2018/10/02 05:59

投稿

watagasi_
watagasi_

スコア13

test CHANGED
File without changes
test CHANGED
@@ -10,11 +10,15 @@
10
10
 
11
11
 
12
12
 
13
+
14
+
15
+ ```Python3
16
+
13
17
  import numpy as np
14
18
 
15
19
  import scipy
16
20
 
17
- ```Python3.6.0
21
+
18
22
 
19
23
  sampling_rate, wave_form = scipy.io.wavfile.read(filename)
20
24
 

1

コードを訂正した

2018/10/02 05:50

投稿

watagasi_
watagasi_

スコア13

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
 
4
4
 
5
- pythonで以下のような処理を行いたいのですが、for文中の処理が非常に遅く高速化したいです。
5
+ Pythonで以下のような処理を行いたいのですが、for文中の処理が非常に遅く高速化したいです。
6
6
 
7
7
  forを用いない表記や配列の参照を少なくする方法などありますでしょうか。
8
8
 
@@ -14,7 +14,7 @@
14
14
 
15
15
  import scipy
16
16
 
17
-
17
+ ```Python3.6.0
18
18
 
19
19
  sampling_rate, wave_form = scipy.io.wavfile.read(filename)
20
20
 
@@ -61,3 +61,5 @@
61
61
  th_min += 1
62
62
 
63
63
  th_max += 1
64
+
65
+ ```