[Numpy-discussion] help to speed up the python code

David Cournapeau david at ar.media.kyoto-u.ac.jp
Fri Oct 31 01:40:04 EDT 2008


frank wang wrote:
> Hi, Bob,
>  
> The problem is that I want to resample my data with another sampling
> rate. the two rates is very close. I use the formula:
>  
> s(t)=sum(a_k*sinc(t-kTs)).
>  
> the new sampling rate is Ts', so I have
> s(nTs')=sum(a_k*sinc(nTs'-kTs)). The sum index k is over the (-P, P),
> Centered at n. The n is start from zero. THe code is using two for
> loops and it is slow. The length of s(nTs) is very long, so it takes
> quite long time to do it.

Additionally, assuming you still want to do it yourself in python, the
above is a convolution, and both numpy and matlab have 'fast' (e.g.
implemented in C) implementation of this; see numpy.convolve. You can
have much faster speed than O(M * P) where M is the length of the
original signal using FFT, but if either the sinc 'filter' or your
signal is large, it is more difficult to do it without using large
amount of memory (you have to split the input signal, and even the
filter impulse response  if it is large, and use overlapp-kind of
techniques, see
http://ccrma.stanford.edu/~jos/sasp/Example_Overlap_Add_Convolution.html
for an example in matlab).

cheers,

David



More information about the NumPy-Discussion mailing list