Steven Goddard
Steven Goddard

Reputation: 79

Matlab to Python Translation - Dividing Arrays

Ok I have the following codes and I can't seem to work out why they don't do the same thing, I'm trying to replicate the Matlab:

Matlab

buflen = 1024
overlap = 512

blend = ones(buflen,1);
blend(1:overlap+1) = 0:1/overlap:1;
blend(buflen-overlap:buflen) = 1:-1/overlap:0

Python

buflen = 1024
overlap = 512

blend = np.ones(buflen)
blend[0:overlap+1] = np.arange(0,2)/np.arange(overlap:1)
blend[buflen-overlap-1:buflen] = np.arange(1,-1)/np.arange(overlap,0)

I'm currently stuck at the second line in the main code, In matlab "0:1/overlap:1" produces a 513x1 array from 0 to 1 in steps of 0.001953125.

In Python, "np.arange(0,2)/np.arange(overlap:1)" it just doesn't doesn't work. I can't work out what is going on in Matlab.

Any ideas?

Upvotes: 0

Views: 57

Answers (1)

user6764549
user6764549

Reputation:

I am not sure but is it possible that you are simply misreading the Matlab syntax. Using parenthesis may help clarify a bit:

  1. 0:(1/overlap):1 is not same as
  2. (0:1)/(overlap:1).

The first one means go from 0 to 1 in steps of (1/overlap) whereas second one is trying to divide two arrays.

If you want Python to behave like the first code snippet you should use

np.arange(0, 1 + (1/overlap), 1/overlap)

Or even better as @PaulPanzer suggested

np.linspace(0, 1, 1+overlap)

Upvotes: 2

Related Questions