数字化模拟符号

2024-05-13 20:45:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个代表数字输出的CSV值数组。它是用模拟示波器采集的,所以它不是一个完美的数字信号。我试图过滤掉数据,以获得一个完美的数字信号来计算周期(可能会有所不同)。 我还想定义我从这个过滤得到的最大误差。在

像这样:

enter image description here

创意

在数据上加上树形图。这是一个伪代码:

for data_point_raw in data_array:
    if data_point_raw < 0.8: data_point_perfect = LOW
    if data_point_raw > 2  : data_point_perfect = HIGH

else:
    #area between thresholds
    if previous_data_point_perfect == Low : data_point_perfect = LOW
    if previous_data_point_perfect == HIGH: data_point_perfect = HIGH

有两个问题困扰着我。在

  1. 这似乎是数字信号处理中常见的问题,但我还没有找到一个预定义的标准函数。这是进行过滤的好方法吗?在
  2. 怎样才能得到最大误差?在

Tags: csv数据datarawif代表数字数组
3条回答

这不是你问题的答案,只是一个可能有帮助的建议。我写在这里,因为我不能把图像在评论。在

我认为您应该在任何处理之前以某种方式规范化数据。

在规格化到0…1的范围后,您应该应用过滤器。在

enter image description here

这里有一些代码可能会有所帮助。在

from __future__ import division

import numpy as np


def find_transition_times(t, y, threshold):
    """
    Given the input signal `y` with samples at times `t`,
    find the times where `y` increases through the value `threshold`.

    `t` and `y` must be 1-D numpy arrays.

    Linear interpolation is used to estimate the time `t` between
    samples at which the transitions occur.
    """
    # Find where y crosses the threshold (increasing).
    lower = y < threshold
    higher = y >= threshold
    transition_indices = np.where(lower[:-1] & higher[1:])[0]

    # Linearly interpolate the time values where the transition occurs.
    t0 = t[transition_indices]
    t1 = t[transition_indices + 1]
    y0 = y[transition_indices]
    y1 = y[transition_indices + 1]
    slope = (y1 - y0) / (t1 - t0)
    transition_times = t0 + (threshold - y0) / slope

    return transition_times


def periods(t, y, threshold):
    """
    Given the input signal `y` with samples at times `t`,
    find the time periods between the times at which the
    signal `y` increases through the value `threshold`.

    `t` and `y` must be 1-D numpy arrays.
    """
    transition_times = find_transition_times(t, y, threshold)
    deltas = np.diff(transition_times)
    return deltas


if __name__ == "__main__":
    import matplotlib.pyplot as plt

    # Time samples
    t = np.linspace(0, 50, 501)
    # Use a noisy time to generate a noisy y.
    tn = t + 0.05 * np.random.rand(t.size)
    y = 0.6 * ( 1 + np.sin(tn) + (1./3) * np.sin(3*tn) + (1./5) * np.sin(5*tn) +
               (1./7) * np.sin(7*tn) + (1./9) * np.sin(9*tn))

    threshold = 0.5
    deltas = periods(t, y, threshold)
    print("Measured periods at threshold %g:" % threshold)
    print(deltas)
    print("Min:  %.5g" % deltas.min())
    print("Max:  %.5g" % deltas.max())
    print("Mean: %.5g" % deltas.mean())
    print("Std dev: %.5g" % deltas.std())

    trans_times = find_transition_times(t, y, threshold)

    plt.plot(t, y)
    plt.plot(trans_times, threshold * np.ones_like(trans_times), 'ro-')
    plt.show()

输出:

^{pr2}$

Plot

可以使用numpy.histogram和/或matplotlib.pyplot.hist进一步分析periods(t, y, threshold)返回的数组。在

如果你真的只对周期感兴趣,你可以画出傅里叶变换,你会在信号频率出现的地方有一个峰值(所以你有周期)。傅里叶域的峰值越宽,周期测量中的误差就越大

import numpy as np

data = np.asarray(my_data)

np.fft.fft(data)

相关问题 更多 >