0

How can I fix this?

I'm trying to use the interp function in python numpy. However, a unique problem has occurred.

My data has a length of 946, and I'm trying to resample it by a factor of 40 with the following code:

x = ppg
time = np.arange(0, len(x)/25, 0.04)
new_time = np.arange(0, len(x) / 25, 1/1000)
x = np.interp(new_time, time, x)

But this line generates an error:

ValueError Traceback (most recent call last)
Cell In[74], line 4
2 time = np.arange(0, len(x)/25, 0.04)
3 new_time = np.arange(0, len(x) / 25, 1/1000)
----> 4 x = np.interp(new_time, time, x)
5 print(len(time), len(new_time))
6 print(len(x))

File <array_function internals>:180, in interp(*args, **kwargs)

File c:\Users\user\anaconda3\envs\cardioception\lib\site-packages\numpy\lib\function_base.py:1570, in interp(x, xp, fp, left, right, period)
1567 xp = np.concatenate((xp[-1:]-period, xp, xp[0:1]+period))
1568 fp = np.concatenate((fp[-1:], fp, fp[0:1]))
-> 1570 return interp_func(x, xp, fp, left, right)

ValueError: fp and xp are not of the same length.

To debug this, I checked and found that len(time) and len(new_time) don't match.

print(len(time), len(new_time))
print(len(x))

947 37840
946

This problem is related to Python's issue with the number 946.0000000000001 appearing as a result of the calculation 946/25 * 25.

Therefore, it seems that a different approach is needed to use this code. Do you have any good ideas to solve this problem? Thank you.

Bong
  • 13
  • 3
  • Does this answer your question? [numpy \`arange\` exceeds end value?](https://stackoverflow.com/questions/35376101/numpy-arange-exceeds-end-value) – HMH1013 Apr 06 '23 at 10:20

0 Answers0