【发布时间】:2020-04-20 02:03:53
【问题描述】:
我目前面临关于脉冲频率检测的问题。我有一个程序以这种方式记录设备的网络活动。
"device": {
"mac": "b8:27:eb:5c:27:13",
"activity": [
{
"ip" : "224.0.0.251",
"port" : "5353",
"history" : [
{
"timestamp" : "2019-09-23T09:34:30.898836",
"pktsSent" : 6,
"pktsReceived" : 0,
"duration" : "3.972347"
},
...
]
}
]
}
我目前正在尝试实现的是使用其历史来检测活动的频率(如果不是周期性的,则不检测)。想起我的工程学习课程,我想到了 FFT。这是我目前在 Python 中所拥有的:
import numpy as np
import dateutil.parser as dateutil
class FrequencyAnalyzer:
...
def analyze(self, device_mac, activity) -> int:
if not 'history' in activity:
raise FrequencyAnalyzerError("No history could be found for the device '%s' and its activity '%s:%s'" %(device_mac, activity['ip'], activity['port']))
start = int(dateutil.parse(activity['history'][0]['timestamp']).timestamp())
stop = int(dateutil.parse(activity['history'][-1]['timestamp']).timestamp())
size = stop - start
# Array of timestamps
timestamps = np.fromiter( [dateutil.parse(history['timestamp']).timestamp() for history in activity['history'] ], int)
# Array of data, setting 1 if there was an activity at this timestamp, else 0
data = np.fromiter( [ 1 if x in timestamps and x != start else 0 for x in range(start, stop)], int)
# Framerate of 1Hz
frate = 1
fft = np.fft.fft(data)
freqs = np.fft.fftfreq(len(fft))
print(freqs.min(), freqs.max())
# Find the peak in the coefficients
idx = np.argmax(np.abs(fft))
freq = freqs[idx]
freq_in_hertz = abs(freq * frate)
print("Freq in Hz = %s" %str(freq_in_hertz))
精度并不那么重要。目前我已将其固定为 1 秒,但也可能是 1 分钟。
在我正在测试的活动中,每 3 分钟和其他时间戳有一个 mDNS 活动。我想返回所有相关的频率,但现在我想得到 1 个正确的频率。
找到的频率始终为零。我记得使用 FFT 分析诸如音频之类的信号,但不适用于脉冲。不应该是同一个方信号(谐波分解)吗?
我想知道这是否是正确的方法。因为它应该在嵌入式系统中运行,所以我不希望使用像 SciPy 这样有点重的框架。
有什么建议吗?
【问题讨论】:
标签: python numpy signal-processing fft