不同的Python输出
问题描述:
我试图运行Python STFT
(也称为短时傅立叶变换)上两个相同采样数据块。不同的Python输出
但是,由于我的数据的应用程序之一,我需要它的一些被收集到一个.txt
文件。
我很困惑,为什么STFT将致力于为从.csv
进来的数据,而不是生产为从.txt
文件进来的数据正确的输出。两者都是Panda.core.series.Series
数据类型,并且都具有类似的幅度值。测试条件也相同。
,我试图解决的代码是标记代码,Code that isn't returning the expected output
。任何见解或意见将不胜感激!
请注意,我有行result[::4]
,因为我必须摆脱每4个采样中的3个,以匹配两种数据采集方法之间的采样率。未返回预期输出
代码:
import json
result = []
with open("C:\\Users\\Desktop\\data.txt") as file:
for line in file:
result.append(json.loads(line)[-1])
result = result[::4]
result = pd.Series(result)
f, t, Zxx = scipy.signal.stft(result, fs=500, nperseg = 1000)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.pcolormesh(t, f, np.abs(Zxx), vmin=0, vmax=0.001)
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.show()
代码可正常工作:
test = pd.read_csv("C:\\Users\\Documents\\data.csv")
test.head()
test.columns = ['TS', 'Col1', 'Col2', 'Col3', 'Col4']
f, t, Zxx = scipy.signal.stft(test['Col1'], fs=500, nperseg = 1000)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.pcolormesh(t, f, np.abs(Zxx), vmin=0, vmax=0.001)
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.show()
答
你应该能够取代
result = result[::4]
与
result = scipy.signal.decimate(result, 4, ftype='fir')
我指定'fir'
用于过滤器的类型,因为FIR滤波器具有线性相位延迟。这可能会或可能不重要,在你的情况。
您需要抽取之前,使用低通滤波器。 [抽取](https://en.wikipedia.org/wiki/Decimation_(signal_processing)):“仅降级采样会导致高频信号分量被数据的后续用户误解,这是一种称为混叠的失真形式。 “ –
有道理。我应该如何确定创建低通滤波器的频率? – Gary
然后我将如何取低通输出并将其插回STFT? – Gary