2016-07-08 5 views
0

こんにちは私は(numpy)最適化の問題があります。単一配列のバーチャライジングループnumpy

以下、私は自分のタイプの計算によく使われるコードを書いています。 私は短いと思っています。 問題はループだと思います。私はnumpyのlinalg部分を見ましたが、解決策が見つかりません。私はまた、データベクトル化の方法で検索しかし、私はそれではない多くの経験を持っているので...私は...私は誰かが私を助けることを願って ...

import numpy as np 
from scipy import signal 
from scipy.fftpack import fft 

fs = 44100 # frequency sample 
T = 5 # time max 
t = np.arange(0, T*fs)/fs # time array 

x = np.sin(2 * np.pi * 100 * t) + 0.7 * np.sin(2 * np.pi * 880 * t) + 0.2 * np.sin(2 * np.pi * 2400 * t) 

# Define Window length and window: 
wl = 4 # window lenght 
overlap = 0.5 
W = signal.get_window('hanning', wl) # window 

Wx = np.zeros(len(x)) 
ul = wl 
# loop added for window 
if (len(x)/wl) % wl == 0: 
    while ul <= len(Wx): 
     Wx[ul-wl:ul] += x[ul-wl:ul] * W 
     ul += wl * overlap 
else: 
    dsample = (len(x)/wl) % wl # delta in samples between mod (x/windw length) 
    x = np.append(x, np.zeros(wl - dsample)) 
    while ul <= len(Wx): 
     Wx[ul-wl:ul] += x[ul-wl:ul] * W 
     ul += wl * overlap 

NFFT = np.int(2 ** np.ceil(np.log2(len(x)))) 
NFFW = np.int(2 ** np.ceil(np.log2(len(Wx)))) 

# Frequency spectrums 
X = fft(x, NFFT) 
WX = fft(Wx, NFFW) 

プロファイラを任意の解決策を見つけることができません:静的な値を事前計算

%run -p example.py 
      110367 function calls (110366 primitive calls) in 19.998 seconds 

    Ordered by: internal time 

    ncalls tottime percall cumtime percall filename:lineno(function) 
     1 19.561 19.561 19.994 19.994 example.py:6(<module>) 
    110258 0.233 0.000 0.233 0.000 {built-in method len} 
     2 0.181 0.091 0.189 0.095 basic.py:169(fft) 
     2 0.008 0.004 0.008 0.004 basic.py:131(_fix_shape) 
     2 0.008 0.004 0.008 0.004 {built-in method concatenate} 
     1 0.003 0.003 0.003 0.003 {built-in method compile} 
     2 0.002 0.001 0.002 0.001 {built-in method arange} 
     2 0.001 0.000 0.001 0.000 {built-in method open} 
     4 0.000 0.000 0.000 0.000 {built-in method zeros} 
     1 0.000 0.000 19.998 19.998 interactiveshell.py:2496(safe_execfile) 
     2/1 0.000 0.000 19.998 19.998 {built-in method exec} 
     1 0.000 0.000 0.000 0.000 windows.py:615(hann) 
     1 0.000 0.000 19.997 19.997 py3compat.py:108(execfile) 
     1 0.000 0.000 0.000 0.000 {method 'read' of '_io.BufferedReader' objects} 
     2 0.000 0.000 0.008 0.004 function_base.py:3503(append) 
     1 0.000 0.000 0.000 0.000 posixpath.py:318(normpath) 
     1 0.000 0.000 0.000 0.000 windows.py:1380(get_window) 
     1 0.000 0.000 0.000 0.000 posixpath.py:145(dirname) 
     4 0.000 0.000 0.000 0.000 {built-in method array} 
     2 0.000 0.000 0.000 0.000 {built-in method round} 
     1 0.000 0.000 0.000 0.000 {built-in method getcwd} 
     2 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:2264(_handle_fromlist) 
     2 0.000 0.000 0.000 0.000 basic.py:116(_asfarray) 
     4 0.000 0.000 0.000 0.000 basic.py:24(istype) 
     2 0.000 0.000 0.000 0.000 fromnumeric.py:1281(ravel) 
     8 0.000 0.000 0.000 0.000 {built-in method isinstance} 
     1 0.000 0.000 0.000 0.000 posixpath.py:70(join) 
     2 0.000 0.000 0.000 0.000 numeric.py:462(asanyarray) 
     1 0.000 0.000 0.000 0.000 posixpath.py:355(abspath) 
     8 0.000 0.000 0.000 0.000 {built-in method hasattr} 
     1 0.000 0.000 19.998 19.998 <string>:1(<module>) 
     1 0.000 0.000 0.000 0.000 syspathcontext.py:64(__exit__) 
     1 0.000 0.000 0.000 0.000 posixpath.py:221(expanduser) 
     1 0.000 0.000 0.000 0.000 _bootlocale.py:23(getpreferredencoding) 
     1 0.000 0.000 0.000 0.000 syspathcontext.py:57(__enter__) 
     1 0.000 0.000 0.000 0.000 syspathcontext.py:54(__init__) 
     4 0.000 0.000 0.000 0.000 {built-in method issubclass} 
     3 0.000 0.000 0.000 0.000 posixpath.py:38(_get_sep) 
     2 0.000 0.000 0.000 0.000 {method 'ravel' of 'numpy.ndarray' objects} 
     2 0.000 0.000 0.000 0.000 numeric.py:392(asarray) 
     7 0.000 0.000 0.000 0.000 {method 'append' of 'list' objects} 
     1 0.000 0.000 0.000 0.000 {built-in method nl_langinfo} 
     5 0.000 0.000 0.000 0.000 {method 'startswith' of 'str' objects} 
     1 0.000 0.000 0.000 0.000 codecs.py:306(__init__) 
     1 0.000 0.000 0.000 0.000 posixpath.py:60(isabs) 
     1 0.000 0.000 0.000 0.000 {method 'split' of 'str' objects} 
     1 0.000 0.000 0.000 0.000 codecs.py:257(__init__) 
     2 0.000 0.000 0.000 0.000 {method 'setdefault' of 'dict' objects} 
     1 0.000 0.000 0.000 0.000 {method 'rfind' of 'str' objects} 
     1 0.000 0.000 0.000 0.000 {method 'remove' of 'list' objects} 
     1 0.000 0.000 0.000 0.000 {method 'join' of 'str' objects} 
     1 0.000 0.000 0.000 0.000 {method 'rstrip' of 'str' objects} 
     1 0.000 0.000 0.000 0.000 {method 'endswith' of 'str' objects} 
     1 0.000 0.000 0.000 0.000 {method 'insert' of 'list' objects} 
     1 0.000 0.000 0.000 0.000 {built-in method getdefaultencoding} 
     1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} 
     1 0.000 0.000 0.000 0.000 py3compat.py:13(no_code) 
+0

あなたは「問題はループだと思う」と言います。プロファイラ(Anaconda/Spyderに統合されたもの)を使用してコードを実行しましたか、コードの少なくとも一部を実行しましたか?また、インクリメント(wl * overlap)を事前に計算することもできます。これは既にPythonによって最適化されているかもしれませんが、確かに悪いです。 – CodeMonkey

+1

私はプロファイラの夏を追加しました –

+0

あなたが正しいように見えます。ほとんどの関数はあまり呼び出されず、合計時間にあまり寄与しません。また、私はそれを得る時:私は得る:ループの間の合計実行時間:4.29741744758s、合計実行時間4.39288849627s。 – CodeMonkey

答えて

0

は〜4秒から0.7sの実行時間に私のループを短縮:

nEntries = len(Wx) 
step = int(wl * overlap) 
while ul <= nEntries: 
    Wx[ul-wl:ul] += x[ul-wl:ul] * W 
    ul += step