在Python中确定数组的频率
我有一个充满浮点数的示例文件,如下所示:
-0.02 3.04 3.04 3.02 3.02 3.06 3.04 3.02 3.04 3.02 3.04 3.02
3.04 3.02 3.04 3.04 3.04 3.02 3.04 3.02 3.04 3.02 3.04 3.02
3.06 3.02 3.04 3.02 3.04 3.02 3.02 3.06 3.04 3.02 3.04 3.02
3.04 3.02 3.04 3.04 3.04 3.02 3.04 3.02 3.02 3.06 3.04 3.02
3.06 3.02 3.04 -0.02 -0.02 -0.02 -0.02 -0.02 -0.02 -0.04 -0.02 -0.04
这些数字放置在文本文件中。我正在尝试读取文本文件并确定该信号的频率。该数据是从数字示波器捕获的。我可以在示波器显示中看到频率,但我还想通过在 Python 中处理它来验证它。我在PC端使用Python从设备捕获数据。
尽管我可以用 Python 做一些低级的事情,但我对文本处理完全是新手。我想我需要首先将文件中的数据加载到数组中,然后执行 FFT 或更简单的算法,生成以 Hz 为单位的整数。
理论上,我知道如何进行傅里叶分析,并且我可以在纸上对任何给定的信号进行分析。对于给定的数据集,我不知道从哪里开始使用 Python。我已经尝试过 scipy-numpy 的文档,但对我来说效果不太好。
我希望得到有经验的用户的指导。
I have a sample file filled with floating point numbers as follows:
-0.02 3.04 3.04 3.02 3.02 3.06 3.04 3.02 3.04 3.02 3.04 3.02
3.04 3.02 3.04 3.04 3.04 3.02 3.04 3.02 3.04 3.02 3.04 3.02
3.06 3.02 3.04 3.02 3.04 3.02 3.02 3.06 3.04 3.02 3.04 3.02
3.04 3.02 3.04 3.04 3.04 3.02 3.04 3.02 3.02 3.06 3.04 3.02
3.06 3.02 3.04 -0.02 -0.02 -0.02 -0.02 -0.02 -0.02 -0.04 -0.02 -0.04
These numbers are placed in a text file. I am trying to read the text file and determine the frequency of this signal. This data is captured from a digital oscilloscope. I can see the frequency in the scope display but I also want to validate it by processing it in Python. I capture data from the device with Python on the PC side.
Even though I can do some low-level stuff in Python, I am a total newbie to text processing. I suppose I need to first load the data in file to an array and then perform an FFT or a simpler algorithm that will yield to an integer in Hz.
In theory I know how to perform a Fourier analysis and I can do it in paper for any given signal. I have no idea where to start in Python for a given dataset. I already tried documentation of scipy-numpy but didn't work that well for me.
I would appreciate guidance from experienced users.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的问题中不清楚文件中的值到底代表什么。但假设它们指示连续的电压样本,您可以使用将文件加载到 Numpy 数组中
,然后计算傅里叶变换,
然后您可以绘制 FFT 的幅度,
并且您看到的内容应与示波器上显示的内容相匹配。
如果您想识别频谱中的主要频率,则必须在某个阈值处切割数组,例如:
freq
的内容(因此还有peaks
code>) 是以采样率为单位的频率。例如,如果您的示波器每微秒采样一次波形,则freq
中的值以兆赫为单位。因此,如果您输入理想的 1 kHz 信号(例如,您将在 0.001 MHz 处获得峰值),因此您会发现
peaks = array([-0.001, 0.001])
。It's not clear from your question exactly what the values in the file represent. But assuming that they indicate consecutive voltage samples, you can load the file into a Numpy array using
and then compute the Fourier transform as
You can then plot the magnitudes of the FFT as
and what you see should match up with what displays on the oscilloscope.
If you want to identify the dominant frequencies in the spectrum, you'll have to cut the array at some threshold, e.g. something like this:
The contents of
freq
(and thus alsopeaks
) are the frequencies in units of the sampling rate. For example, if your oscilloscope samples the waveform every microsecond, the values infreq
are in megahertz. So if you feed in an ideal 1 kHz signal, which you can do with e.g.you would get a peak at 0.001 MHz, and accordingly you will find that
peaks = array([-0.001, 0.001])
.