Je veux débruiter les données avec lfilter. Mon code :

    data = []
    with open('Accelerometer.csv', newline='') as csvfile:
        spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
        for row in spamreader:
            data.append(row)
    
    timeInMS = []
    mx = []
    my = []
    mz = []
    for rowIndex in range(0, len(data)):
        print(rowIndex)
        timeInMS.append(data[rowIndex][1])
    
        mx.append(data[rowIndex][2])
        my.append(data[rowIndex][3])
        mz.append(data[rowIndex][4])
    # Data for plotting
    
    n = 15  # the larger n is, the smoother curve will be
    b = [1.0 / n] * n
    a = 1
    
    fMx = lfilter(b, a, mx)
    fMy = lfilter(b, a, my)
    fMz = lfilter(b, a, mz)
    
    # plotting the line 1 points
    plt.plot(timeInMS, fMx, label="x")
    
    # plotting the line 2 points
    plt.plot(timeInMS, fMy, label="y")
    
    # plotting the line 3 points
    plt.plot(timeInMS, fMz, label="z")
    
    plt.xlabel('x - axis')
    plt.ylabel('y - axis')
    plt.title('Changes on Accelerometer')
    plt.legend()
    plt.show() # Display a figure.

Accéléromètre.csv (1000 lignes) :

Timestamp,Milliseconds,X,Y,Z
2020-10-05 10:19:23,11,0.02633622,0.2633622,9.933543
2020-10-05 10:19:23,18,0.037110128,0.25498247,9.938332
2020-10-05 10:19:23,29,0.034715924,0.25258827,9.939528
2020-10-05 10:19:23,37,0.037110128,0.25139117,9.944317
2020-10-05 10:19:23,47,0.039504327,0.2621651,9.932345
2020-10-05 10:19:23,58,0.035913024,0.25378537,9.927557
2020-10-05 10:19:23,68,0.037110128,0.2633622,9.931149
2020-10-05 10:19:23,78,0.027533319,0.25857377,9.932345
2020-10-05 10:19:23,87,0.02633622,0.25258827,9.94312
...
...
...

Et l'erreur est:

Traceback (most recent call last):
  File "Accelerometer/main.py", line 32, in <module>
    fMx = lfilter(b, a, mx)
  File "Accelerometer\venv\lib\site-packages\scipy\signal\signaltools.py", line 1883, in lfilter
    raise NotImplementedError("input type '%s' not supported" % dtype)
NotImplementedError: input type '<U32' not supported
0
Marci 5 oct. 2020 à 11:36

1 réponse

Meilleure réponse

Il est vraiment utile d'utiliser des pandas pd.read_csv() pour lire dans votre fichier. Cela mettrait automatiquement les données dans un format pratique. Ensuite, vous pouvez directement utiliser mx = data['X'].

Sans pandas, vous rencontrez des problèmes comme ici, où vous oubliez de sauter l'en-tête, et de plus vos données sont toutes au format chaîne au lieu de numérique. Il convertit également la date et l'heure d'un format de chaîne en un format date-heure réel.

import pandas as pd
import matplotlib.pyplot as plt
from scipy.signal import lfilter

data = pd.read_csv('Accelerometer.csv')

n = 15  # the larger n is, the smoother curve will be
b = [1.0 / n] * n
a = 1

fMx = lfilter(b, a, data['X'])
fMy = lfilter(b, a, data['Y'])
fMz = lfilter(b, a, data['Z'])

# plotting the line 1 points
plt.plot(data['Milliseconds'], fMx, label="x")

# plotting the line 2 points
plt.plot(data['Milliseconds'], fMy, label="y")

# plotting the line 3 points
plt.plot(data['Milliseconds'], fMz, label="z")

plt.xlabel('x - axis')
plt.ylabel('y - axis')
plt.title('Changes on Accelerometer')
plt.legend()
plt.show()  # Display a figure.

example plot

1
JohanC 5 oct. 2020 à 09:14