验证单个数组numpy的循环

时间:2016-07-08 05:59:37

标签: python-3.x numpy vectorization

您好我有一个(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)

1 个答案:

答案 0 :(得分:0)

预计算静态值可将我的循环从~4s缩短到0.7s执行时间:

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