我想用 去噪数​​据lfilter。我的代码:

    data = []
    with open('Accelerometer.csv', newline=,python,python-3.x,matplotlib,filter,scipy"/>
	














首页 > 解决方案 > Python 3.6 - scipy 引发错误:NotImplementedError: input type '

我想用 去噪数​​据lfilter。我的代码:

    data = []
    with open('Accelerometer.csv', newline=

问题描述

我想用 去噪数​​据lfilter。我的代码:

    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.

Accelerometer.csv(1000 行):

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
...
...
...

错误是:

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

使用 C Sharp 在 Java 中重新创建嵌套静态类

我正在将代码从 Java 移植到 C Sharp。在Java中,我有以下课程。

public class ClassA{
    ...
    private List<ClassA.ClassB> classBs = new ArrayList();

    public classA(){
        for(int i = 0; i < 10; i++){
            this.classBs.add(new ClassA.ClassB());
        }
    }

    public static class ClassB{
        private int value;

        public ClassB(){
            this.value = 0;
        }
    }
}

需要注意的重要一点是,在

this.classBs.add(new ClassA.ClassB());

我们正在创建一个静态内部类的实例。

现在,在 C 语言中,我无法重新创建相同的静态内部类。经过研究,我发现我只能执行以下操作之一,

  1. 将内部类 classB 设为非静态或将 classB 的所有成员设为静态。
  2. 在classA之外定义classB。

两者似乎都没有重新创建 Java 的确切代码。我该如何处理?

标签: pythonpython-3.xmatplotlibfilterscipy

解决方案


使用 pandaspd.read_csv()读取文件确实很有帮助。这会自动将数据转换为方便的格式。然后就可以直接使用了mx = data['X']

如果没有 pandas,您会遇到诸如这里的问题,您忘记跳过标题,而且您的数据都是字符串格式而不是数字格式。它还将日期和时间从字符串格式转换为真正的日期时间格式。

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.

示例图


推荐阅读