简体   繁体   English

SciPy:检测时间序列数据的频率

[英]SciPy: Detecting frequency of time-series data

I have a file containing several hundred thousand (time, value) points and I would like to estimate the frequency of the waveform from the data points.我有一个包含几十万(时间,值)点的文件,我想从数据点估计波形的频率。 I have been looking at using SciPy's find_peaks function to select a single point for each peak and average the time between peaks to find the frequency.我一直在研究使用 SciPy 的find_peaks函数为每个峰值选择一个点并平均峰值之间的时间以找到频率。 However, the find_peaks function is selecting many peaks instead of just one as seen below.但是, find_peaks函数选择了许多峰,而不是只选择一个峰,如下所示。

在此处输入图像描述

Close up of a single peak:单峰特写:

在此处输入图像描述

I'm trying to use the prominence argument to filter out the noise, but I can't understand why it's not working.我正在尝试使用prominence参数来过滤掉噪音,但我不明白为什么它不起作用。 Here is the relevant code:以下是相关代码:

    peak_idxs = list(
        sig.find_peaks(
            data,
            prominence=0.15,
            width=(0, 0.05 * sample_freq),
        )[0]
    )
    troph_idxs = list(
        sig.find_peaks(
            np.negative(data),
            prominence=0.15,
            width=(0, 0.05 * sample_freq),
        )[0]
    )
    peak_criticals = [timestamps[i] for i in peak_idxs]
    troph_criticals = [timestamps[i] for i in troph_idxs]
    t_criticals_freqs = [
        1 / (b - a) for a, b in zip(peak_criticals[0:], peak_criticals[1:])
    ] + [1 / (b - a) for a, b in zip(troph_criticals[0:], troph_criticals[1:])]

    freq = statistics.mean(t_criticals_freqs)

    ax.plot(timestamps, data)
    ax.plot(
        [timestamps[i] for i in peak_idxs + troph_idxs],
        [data[i] for i in peak_idxs + troph_idxs],
        "r*",
    )

It will be much more robust and easier to compute the spectrogram:计算频谱图将更加健壮和容易:

from scipy.signal import welch

frequencies, power = welch(my_signal, my_signals_sampling_rate)
dominant_frequency = frequencies[np.argmax(power)]

The sampling rate is 1/dt where dt is the time in seconds between consecutive samples.采样率为1/dt ,其中dt是连续采样之间的时间,以秒为单位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM