Md. Rezwanul Haque
Md. Rezwanul Haque

Reputation: 2950

Calculate the n-th order discrete difference: TypeError: unsupported operand type(s) for -: 'list' and 'list'

I am getting following error when I want to try to calculate the n-th order discrete difference a 2-D list.

Error:

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

Code

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)

How can solve this issue?

Upvotes: 1

Views: 200

Answers (2)

The arrays need have the same dimensions for this operation. one solution is:

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)

Upvotes: 0

B. Biehler
B. Biehler

Reputation: 101

I assume single_waveform does in fact contain more than one waveform, but in this case 3. Than you might try:

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)

Is this what you wanted to do?

Upvotes: 1

Related Questions