Wednesday, 15 September 2010

numpy - Python indexing issue (converting MATLAB code) -



numpy - Python indexing issue (converting MATLAB code) -

i'm trying convert piece of matlab code python , running problem.

t = linspace(0,1,256); s = sin(2*pi*(2*t+5*t.^2)); h = conj(s(length(s):-1:1));

the above line h meant calculate impulse response, python code:

import numpy np t = np.linspace(0,1,256) s = np.sin(2*np.pi*(2*t+5*t**2)) h = np.conj(s[len(s),-1,1])

gives me error indexerror: index 256 out of bounds axis 0 size 256. know has indexing s array, how can prepare it?

remember python zero-indexed, while matlab 1-indexed. note matlab piece notation includes endpoint, whereas python piece notation excludes endpoint.

s(length(s):-1:1) mutual matlab idiom reversing vector. python has nicer syntax: s[::-1]. direct translation s[len(s)-1:-1:-1].

also note matlab start:step:stop corresponds python start:stop:step; position of step argument different.

python numpy

No comments:

Post a Comment