if np.isnan(pitches[i]): ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
时间: 2023-09-24 16:03:11 浏览: 187
这个错误发生的原因是pitches[i]这个值是一个数组,而不是一个标量。因此,如果要判断pitches[i]是否为NaN,需要使用np.isnan(pitches[i][0])而不是np.isnan(pitches[i])。这里的[0]是因为pitches[i]这个数组只有一个元素,我们只需要判断这个元素是否为NaN即可。以下是修改后的代码:
```python
x_esti = np.array([0, 0]).reshape(2, 1)
P = 1.0 * np.eye(2)
y_filt4 = np.zeros_like(y)
for i in range(len(y)):
if np.isnan(pitches[i][0]):
pitch = pitch
else:
pitch = librosa.hz_to_midi(pitches[i][0]) - 69
z_meas = np.array([pitch, 0]).reshape(2, 1)
x_esti, P = kalman_filter(z_meas, x_esti, P)
y_filt4[i] = y[i] - x_esti[0]
```
这样修改后,就可以正常运行了。
相关问题
if not np.isnan(ls).all(): m = np.append(ma, ls) ma = m else: x = np.append(xc, i) xc = x
# As an AI language model, I cannot determine the context and purpose of the code. However, I can explain what this code block does.
This code block contains an if-else statement. It checks if all the values in the numpy array 'ls' are NaN (Not a Number). If they are not all NaN, it appends the values of 'ls' to another numpy array 'ma'. If all the values in 'ls' are NaN, it appends a variable 'i' to another numpy array 'xc'.
Here's a breakdown of the code:
```
if not np.isnan(ls).all():
```
This line checks if all the values in the numpy array 'ls' are NaN. The 'np.isnan' function returns a boolean array of the same shape as 'ls' with True for NaN values and False for non-NaN values. The 'all' function returns True if all the values in the boolean array are True, which means all the values in 'ls' are NaN. The 'not' keyword negates this result, so if any value in 'ls' is not NaN, the condition is True.
```
m = np.append(ma, ls)
ma = m
```
If the condition is True, this block of code appends the values of 'ls' to the numpy array 'ma' using the 'np.append' function. The result is stored in a new numpy array 'm', which is then assigned to 'ma'.
```
else:
```
If the condition is False, this block of code is executed.
```
x = np.append(xc, i)
xc = x
```
This block of code appends a variable 'i' to the numpy array 'xc' using the 'np.append' function. The result is stored in a new numpy array 'x', which is then assigned to 'xc'.
if np.isnan(center_x) or np.isnan(center_y): angle = old_angle,什么意思
这段代码的意思是,如果变量 center_x 或 center_y 中存在 NaN(Not a Number)值,就将 angle 的值设为 old_angle。 NaN 是一种特殊的数值,表示无效或未定义的数值。所以这行代码的目的是检查 center_x 和 center_y 是否为有效的数值,如果不是,则将 angle 设置为之前的旧值。
阅读全文