0

I am trying to convert a MATLAB line of code to Python using Numpy.

octave:20> fs=10
fs = 10
octave:21> [-fs:1/fs:fs]
ans =
 Columns 1 through 7:
 -10.00000 -9.90000 -9.80000 -9.70000 -9.60000 -9.50000 -9.40000
 Columns 8 through 14:
 -9.30000 -9.20000 -9.10000 -9.00000 -8.90000 -8.80000 -8.70000
 Columns 15 through 21:
 -8.60000 -8.50000 -8.40000 -8.30000 -8.20000 -8.10000 -8.00000
 Columns 22 through 28:
 -7.90000 -7.80000 -7.70000 -7.60000 -7.50000 -7.40000 -7.30000
 Columns 29 through 35:
 -7.20000 -7.10000 -7.00000 -6.90000 -6.80000 -6.70000 -6.60000
 Columns 36 through 42:
 -6.50000 -6.40000 -6.30000 -6.20000 -6.10000 -6.00000 -5.90000
 Columns 43 through 49:
 -5.80000 -5.70000 -5.60000 -5.50000 -5.40000 -5.30000 -5.20000
 Columns 50 through 56:
 -5.10000 -5.00000 -4.90000 -4.80000 -4.70000 -4.60000 -4.50000
 Columns 57 through 63:
 -4.40000 -4.30000 -4.20000 -4.10000 -4.00000 -3.90000 -3.80000
 Columns 64 through 70:
 -3.70000 -3.60000 -3.50000 -3.40000 -3.30000 -3.20000 -3.10000
 Columns 71 through 77:
 -3.00000 -2.90000 -2.80000 -2.70000 -2.60000 -2.50000 -2.40000
 Columns 78 through 84:
 -2.30000 -2.20000 -2.10000 -2.00000 -1.90000 -1.80000 -1.70000
 Columns 85 through 91:
 -1.60000 -1.50000 -1.40000 -1.30000 -1.20000 -1.10000 -1.00000
 Columns 92 through 98:
 -0.90000 -0.80000 -0.70000 -0.60000 -0.50000 -0.40000 -0.30000
 Columns 99 through 105:
 -0.20000 -0.10000 0.00000 0.10000 0.20000 0.30000 0.40000
 Columns 106 through 112:
 0.50000 0.60000 0.70000 0.80000 0.90000 1.00000 1.10000
 Columns 113 through 119:
 1.20000 1.30000 1.40000 1.50000 1.60000 1.70000 1.80000
 Columns 120 through 126:
 1.90000 2.00000 2.10000 2.20000 2.30000 2.40000 2.50000
 Columns 127 through 133:
 2.60000 2.70000 2.80000 2.90000 3.00000 3.10000 3.20000
 Columns 134 through 140:
 3.30000 3.40000 3.50000 3.60000 3.70000 3.80000 3.90000
 Columns 141 through 147:
 4.00000 4.10000 4.20000 4.30000 4.40000 4.50000 4.60000
 Columns 148 through 154:
 4.70000 4.80000 4.90000 5.00000 5.10000 5.20000 5.30000
 Columns 155 through 161:
 5.40000 5.50000 5.60000 5.70000 5.80000 5.90000 6.00000
 Columns 162 through 168:
 6.10000 6.20000 6.30000 6.40000 6.50000 6.60000 6.70000
 Columns 169 through 175:
 6.80000 6.90000 7.00000 7.10000 7.20000 7.30000 7.40000
 Columns 176 through 182:
 7.50000 7.60000 7.70000 7.80000 7.90000 8.00000 8.10000
 Columns 183 through 189:
 8.20000 8.30000 8.40000 8.50000 8.60000 8.70000 8.80000
 Columns 190 through 196:
 8.90000 9.00000 9.10000 9.20000 9.30000 9.40000 9.50000
 Columns 197 through 201:
 9.60000 9.70000 9.80000 9.90000 10.00000

So In Python:

>>> np.arange(-fs, (fs)+(1./fs), 1./fs)
array([ -1.00000000e+01, -9.90000000e+00, -9.80000000e+00,
 -9.70000000e+00, -9.60000000e+00, -9.50000000e+00,
 -9.40000000e+00, -9.30000000e+00, -9.20000000e+00,
 -9.10000000e+00, -9.00000000e+00, -8.90000000e+00,
 -8.80000000e+00, -8.70000000e+00, -8.60000000e+00,
 -8.50000000e+00, -8.40000000e+00, -8.30000000e+00,
 -8.20000000e+00, -8.10000000e+00, -8.00000000e+00,
 -7.90000000e+00, -7.80000000e+00, -7.70000000e+00,
 -7.60000000e+00, -7.50000000e+00, -7.40000000e+00,
 -7.30000000e+00, -7.20000000e+00, -7.10000000e+00,
 -7.00000000e+00, -6.90000000e+00, -6.80000000e+00,
 -6.70000000e+00, -6.60000000e+00, -6.50000000e+00,
 -6.40000000e+00, -6.30000000e+00, -6.20000000e+00,
 -6.10000000e+00, -6.00000000e+00, -5.90000000e+00,
 -5.80000000e+00, -5.70000000e+00, -5.60000000e+00,
 -5.50000000e+00, -5.40000000e+00, -5.30000000e+00,
 -5.20000000e+00, -5.10000000e+00, -5.00000000e+00,
 -4.90000000e+00, -4.80000000e+00, -4.70000000e+00,
 -4.60000000e+00, -4.50000000e+00, -4.40000000e+00,
 -4.30000000e+00, -4.20000000e+00, -4.10000000e+00,
 -4.00000000e+00, -3.90000000e+00, -3.80000000e+00,
 -3.70000000e+00, -3.60000000e+00, -3.50000000e+00,
 -3.40000000e+00, -3.30000000e+00, -3.20000000e+00,
 -3.10000000e+00, -3.00000000e+00, -2.90000000e+00,
 -2.80000000e+00, -2.70000000e+00, -2.60000000e+00,
 -2.50000000e+00, -2.40000000e+00, -2.30000000e+00,
 -2.20000000e+00, -2.10000000e+00, -2.00000000e+00,
 -1.90000000e+00, -1.80000000e+00, -1.70000000e+00,
 -1.60000000e+00, -1.50000000e+00, -1.40000000e+00,
 -1.30000000e+00, -1.20000000e+00, -1.10000000e+00,
 -1.00000000e+00, -9.00000000e-01, -8.00000000e-01,
 -7.00000000e-01, -6.00000000e-01, -5.00000000e-01,
 -4.00000000e-01, -3.00000000e-01, -2.00000000e-01,
 -1.00000000e-01, -3.55271368e-14, 1.00000000e-01,
 2.00000000e-01, 3.00000000e-01, 4.00000000e-01,
 5.00000000e-01, 6.00000000e-01, 7.00000000e-01,
 8.00000000e-01, 9.00000000e-01, 1.00000000e+00,
 1.10000000e+00, 1.20000000e+00, 1.30000000e+00,
 1.40000000e+00, 1.50000000e+00, 1.60000000e+00,
 1.70000000e+00, 1.80000000e+00, 1.90000000e+00,
 2.00000000e+00, 2.10000000e+00, 2.20000000e+00,
 2.30000000e+00, 2.40000000e+00, 2.50000000e+00,
 2.60000000e+00, 2.70000000e+00, 2.80000000e+00,
 2.90000000e+00, 3.00000000e+00, 3.10000000e+00,
 3.20000000e+00, 3.30000000e+00, 3.40000000e+00,
 3.50000000e+00, 3.60000000e+00, 3.70000000e+00,
 3.80000000e+00, 3.90000000e+00, 4.00000000e+00,
 4.10000000e+00, 4.20000000e+00, 4.30000000e+00,
 4.40000000e+00, 4.50000000e+00, 4.60000000e+00,
 4.70000000e+00, 4.80000000e+00, 4.90000000e+00,
 5.00000000e+00, 5.10000000e+00, 5.20000000e+00,
 5.30000000e+00, 5.40000000e+00, 5.50000000e+00,
 5.60000000e+00, 5.70000000e+00, 5.80000000e+00,
 5.90000000e+00, 6.00000000e+00, 6.10000000e+00,
 6.20000000e+00, 6.30000000e+00, 6.40000000e+00,
 6.50000000e+00, 6.60000000e+00, 6.70000000e+00,
 6.80000000e+00, 6.90000000e+00, 7.00000000e+00,
 7.10000000e+00, 7.20000000e+00, 7.30000000e+00,
 7.40000000e+00, 7.50000000e+00, 7.60000000e+00,
 7.70000000e+00, 7.80000000e+00, 7.90000000e+00,
 8.00000000e+00, 8.10000000e+00, 8.20000000e+00,
 8.30000000e+00, 8.40000000e+00, 8.50000000e+00,
 8.60000000e+00, 8.70000000e+00, 8.80000000e+00,
 8.90000000e+00, 9.00000000e+00, 9.10000000e+00,
 9.20000000e+00, 9.30000000e+00, 9.40000000e+00,
 9.50000000e+00, 9.60000000e+00, 9.70000000e+00,
 9.80000000e+00, 9.90000000e+00, 1.00000000e+01])
>>> 

The values seems correct to that of Matlab's output but

octave:22> sincNum = sin(pi*[-fs:1/fs:fs]); 
octave:23> sincNum 
sincNum =
 Columns 1 through 7:
 0.00000 0.30902 0.58779 0.80902 0.95106 1.00000 0.95106
 Columns 8 through 14:
 0.80902 0.58779 0.30902 -0.00000 -0.30902 -0.58779 -0.80902
 Columns 15 through 21:
 -0.95106 -1.00000 -0.95106 -0.80902 -0.58779 -0.30902 0.00000
 Columns 22 through 28:
 0.30902 0.58779 0.80902 0.95106 1.00000 0.95106 0.80902
 Columns 29 through 35:
 0.58779 0.30902 -0.00000 -0.30902 -0.58779 -0.80902 -0.95106
 Columns 36 through 42:
 -1.00000 -0.95106 -0.80902 -0.58779 -0.30902 0.00000 0.30902
 Columns 43 through 49:
 0.58779 0.80902 0.95106 1.00000 0.95106 0.80902 0.58779
 Columns 50 through 56:
 0.30902 -0.00000 -0.30902 -0.58779 -0.80902 -0.95106 -1.00000
 Columns 57 through 63:
 -0.95106 -0.80902 -0.58779 -0.30902 0.00000 0.30902 0.58779
 Columns 64 through 70:
 0.80902 0.95106 1.00000 0.95106 0.80902 0.58779 0.30902
 Columns 71 through 77:
 -0.00000 -0.30902 -0.58779 -0.80902 -0.95106 -1.00000 -0.95106
 Columns 78 through 84:
 -0.80902 -0.58779 -0.30902 0.00000 0.30902 0.58779 0.80902
 Columns 85 through 91:
 0.95106 1.00000 0.95106 0.80902 0.58779 0.30902 -0.00000
 Columns 92 through 98:
 -0.30902 -0.58779 -0.80902 -0.95106 -1.00000 -0.95106 -0.80902
 Columns 99 through 105:
 -0.58779 -0.30902 0.00000 0.30902 0.58779 0.80902 0.95106
 Columns 106 through 112:
 1.00000 0.95106 0.80902 0.58779 0.30902 0.00000 -0.30902
 Columns 113 through 119:
 -0.58779 -0.80902 -0.95106 -1.00000 -0.95106 -0.80902 -0.58779
 Columns 120 through 126:
 -0.30902 -0.00000 0.30902 0.58779 0.80902 0.95106 1.00000
 Columns 127 through 133:
 0.95106 0.80902 0.58779 0.30902 0.00000 -0.30902 -0.58779
 Columns 134 through 140:
 -0.80902 -0.95106 -1.00000 -0.95106 -0.80902 -0.58779 -0.30902
 Columns 141 through 147:
 -0.00000 0.30902 0.58779 0.80902 0.95106 1.00000 0.95106
 Columns 148 through 154:
 0.80902 0.58779 0.30902 0.00000 -0.30902 -0.58779 -0.80902
 Columns 155 through 161:
 -0.95106 -1.00000 -0.95106 -0.80902 -0.58779 -0.30902 -0.00000
 Columns 162 through 168:
 0.30902 0.58779 0.80902 0.95106 1.00000 0.95106 0.80902
 Columns 169 through 175:
 0.58779 0.30902 0.00000 -0.30902 -0.58779 -0.80902 -0.95106
 Columns 176 through 182:
 -1.00000 -0.95106 -0.80902 -0.58779 -0.30902 -0.00000 0.30902
 Columns 183 through 189:
 0.58779 0.80902 0.95106 1.00000 0.95106 0.80902 0.58779
 Columns 190 through 196:
 0.30902 0.00000 -0.30902 -0.58779 -0.80902 -0.95106 -1.00000
 Columns 197 through 201:
 -0.95106 -0.80902 -0.58779 -0.30902 -0.00000
octave:24> 

Are not equal to that of Python's

>>> sincNum = np.sin(np.dot(np.pi, np.array(np.arange(-fs, (fs)+(1./fs), 1./fs))))
>>> sincNum
array([ 1.22464680e-15, 3.09016994e-01, 5.87785252e-01,
 8.09016994e-01, 9.51056516e-01, 1.00000000e+00,
 9.51056516e-01, 8.09016994e-01, 5.87785252e-01,
 3.09016994e-01, 9.55595892e-15, -3.09016994e-01,
 -5.87785252e-01, -8.09016994e-01, -9.51056516e-01,
 -1.00000000e+00, -9.51056516e-01, -8.09016994e-01,
 -5.87785252e-01, -3.09016994e-01, -2.03365646e-14,
 3.09016994e-01, 5.87785252e-01, 8.09016994e-01,
 9.51056516e-01, 1.00000000e+00, 9.51056516e-01,
 8.09016994e-01, 5.87785252e-01, 3.09016994e-01,
 3.11171703e-14, -3.09016994e-01, -5.87785252e-01,
 -8.09016994e-01, -9.51056516e-01, -1.00000000e+00,
 -9.51056516e-01, -8.09016994e-01, -5.87785252e-01,
 -3.09016994e-01, -4.54504897e-14, 3.09016994e-01,
 5.87785252e-01, 8.09016994e-01, 9.51056516e-01,
 1.00000000e+00, 9.51056516e-01, 8.09016994e-01,
 5.87785252e-01, 3.09016994e-01, 5.44547386e-14,
 -3.09016994e-01, -5.87785252e-01, -8.09016994e-01,
 -9.51056516e-01, -1.00000000e+00, -9.51056516e-01,
 -8.09016994e-01, -5.87785252e-01, -3.09016994e-01,
 -6.70117012e-14, 3.09016994e-01, 5.87785252e-01,
 8.09016994e-01, 9.51056516e-01, 1.00000000e+00,
 9.51056516e-01, 8.09016994e-01, 5.87785252e-01,
 3.09016994e-01, 7.77923069e-14, -3.09016994e-01,
 -5.87785252e-01, -8.09016994e-01, -9.51056516e-01,
 -1.00000000e+00, -9.51056516e-01, -8.09016994e-01,
 -5.87785252e-01, -3.09016994e-01, -8.94610910e-14,
 3.09016994e-01, 5.87785252e-01, 8.09016994e-01,
 9.51056516e-01, 1.00000000e+00, 9.51056516e-01,
 8.09016994e-01, 5.87785252e-01, 3.09016994e-01,
 1.00241697e-13, -3.09016994e-01, -5.87785252e-01,
 -8.09016994e-01, -9.51056516e-01, -1.00000000e+00,
 -9.51056516e-01, -8.09016994e-01, -5.87785252e-01,
 -3.09016994e-01, -1.11611792e-13, 3.09016994e-01,
 5.87785252e-01, 8.09016994e-01, 9.51056516e-01,
 1.00000000e+00, 9.51056516e-01, 8.09016994e-01,
 5.87785252e-01, 3.09016994e-01, 1.22691087e-13,
 -3.09016994e-01, -5.87785252e-01, -8.09016994e-01,
 -9.51056516e-01, -1.00000000e+00, -9.51056516e-01,
 -8.09016994e-01, -5.87785252e-01, -3.09016994e-01,
 -1.34359871e-13, 3.09016994e-01, 5.87785252e-01,
 8.09016994e-01, 9.51056516e-01, 1.00000000e+00,
 9.51056516e-01, 8.09016994e-01, 5.87785252e-01,
 3.09016994e-01, 1.46028655e-13, -3.09016994e-01,
 -5.87785252e-01, -8.09016994e-01, -9.51056516e-01,
 -1.00000000e+00, -9.51056516e-01, -8.09016994e-01,
 -5.87785252e-01, -3.09016994e-01, -1.56809261e-13,
 3.09016994e-01, 5.87785252e-01, 8.09016994e-01,
 9.51056516e-01, 1.00000000e+00, 9.51056516e-01,
 8.09016994e-01, 5.87785252e-01, 3.09016994e-01,
 1.67589866e-13, -3.09016994e-01, -5.87785252e-01,
 -8.09016994e-01, -9.51056516e-01, -1.00000000e+00,
 -9.51056516e-01, -8.09016994e-01, -5.87785252e-01,
 -3.09016994e-01, -1.78370472e-13, 3.09016994e-01,
 5.87785252e-01, 8.09016994e-01, 9.51056516e-01,
 1.00000000e+00, 9.51056516e-01, 8.09016994e-01,
 5.87785252e-01, 3.09016994e-01, 1.89151078e-13,
 -3.09016994e-01, -5.87785252e-01, -8.09016994e-01,
 -9.51056516e-01, -1.00000000e+00, -9.51056516e-01,
 -8.09016994e-01, -5.87785252e-01, -3.09016994e-01,
 -2.03484397e-13, 3.09016994e-01, 5.87785252e-01,
 8.09016994e-01, 9.51056516e-01, 1.00000000e+00,
 9.51056516e-01, 8.09016994e-01, 5.87785252e-01,
 3.09016994e-01, 2.14265003e-13, -3.09016994e-01,
 -5.87785252e-01, -8.09016994e-01, -9.51056516e-01,
 -1.00000000e+00, -9.51056516e-01, -8.09016994e-01,
 -5.87785252e-01, -3.09016994e-01, -2.25045609e-13])
>>> 

Am I doing something wrong or missed out something?

asked Mar 20, 2015 at 6:08
2
  • Don't show us 200 values when 10 would be sufficient. Commented Mar 20, 2015 at 6:33
  • You can simplify the python np.sin(np.pi * np.arange(...)) Commented Mar 20, 2015 at 8:01

1 Answer 1

2

Actually, it is the same in both cases. Set precision options in numpy to see:

np.set_printoptions(precision=6, suppress=True)
answered Mar 20, 2015 at 6:47
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.