计算n阶离散差:TypeError:-:'list'和'list'

时间:2018-09-18 13:51:01

标签: python-3.x numpy

当我想计算二维列表的n阶离散差时,出现以下错误。

  

错误:

TypeError: unsupported operand type(s) for -: 'list' and 'list'

代码

import numpy as np

single_waveform = [[219.09683883101852, 219.16303915895062, 219.2642264660494, 219.34081790123457, 219.51174768518518, 219.5255039544753, 219.6387533757716, 219.78383632330247], [219.9265668402778], [220.0330362654321, 220.02853780864197, 219.95662519290124]]

sample_rate = 200
derivative_1 = np.diff(single_waveform, n=1) * float(sample_rate)

print(derivative_1)

如何解决此问题?

2 个答案:

答案 0 :(得分:1)

我假设single_waveform实际上包含多个波形,但是在这种情况下为3。您可以尝试:

import numpy as np

single_waveform = [[219.09683883101852, 219.16303915895062, 219.2642264660494, 219.34081790123457, 219.51174768518518, 219.5255039544753, 219.6387533757716, 219.78383632330247], [219.9265668402778], [220.0330362654321, 220.02853780864197, 219.95662519290124]]

sample_rate = 200
derivative_1 = [np.diff(sw, n=1) * float(sample_rate) for sw in single_waveform]

print(derivative_1)

这是您想做的吗?

答案 1 :(得分:0)

此操作的数组需要具有相同的尺寸。 一种解决方案是:

import numpy as np

single_waveform = [[219.09683883101852, 219.16303915895062, 219.2642264660494, 219.34081790123457, 219.51174768518518, 219.5255039544753, 219.6387533757716, 219.78383632330247], [219.9265668402778], [220.0330362654321, 220.02853780864197, 219.95662519290124]]

sample_rate = 200

derivative_1 = []

for array in single_waveform:
    np.diff(array, n=1) * float(sample_rate)
    derivative_1.append(np.diff(array,n=1))

print(derivative_1)