2016-07-08 83 views
0

你好,我有一個(numpy)優化問題。單陣列numpy的verctorizing循環

下面我寫了一段代碼,這對我的計算類型來說很常見。 刺激總是需要一段時間,我認爲應該更短。 我認爲問題在於循環。我已經看過了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具有集成的代碼)運行代碼或者至少有一部分代碼是定時的?您也可以預先計算結果(重疊)。這可能已經被Python優化過了,但它確實看起來很糟糕。 – CodeMonkey

+1

我添加了剖析器summery –

+0

看起來你是對的。大多數功能很少被調用,對總時間貢獻不大。另外,當我的時間,我得到:循環總運行時間:4.29741744758s,總運行時間4.39288849627s。 – CodeMonkey

回答

0

預先計算的靜態值,縮短了從〜我環路4S至0.7秒的執行時間:

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