如何从.txt文件中的数据绘制FFT图?

时间:2019-03-26 22:25:37

标签: matlab fft

只是让您知道我对Matlab和快速傅立叶变换不了解,所以我需要一些帮助。我在时间和电压(mV)的.txt文件中存储了数据,如下所示。我需要帮助将其绘制在FFT图上。

我已经在线搜索过各种代码,但是由于我主要使用Java,所以一无所知-但这也很难理解,而且我听说过matlab更容易实现。

这是.txt文件的简短摘录:

00:04:05,468    0,0996  
00:04:05,469    0,0797  
00:04:05,471    0,0398  
00:04:05,472    -0,0598 
00:04:05,473    -0,1793 
00:04:05,473    -0,1594 
00:04:05,474    -0,2191 
00:04:05,475    -0,1793 
00:04:05,477    -0,1992 
00:04:05,478    -0,1594 

1 个答案:

答案 0 :(得分:0)

第一步是将数据加载到MATLAB中。有多种方法可以从文本文件加载数据。一个非常简单的解决方案是use the Import Tool in the GUI, which will walk you through the process interactively。另外,您可以使用textscan function以编程方式加载数据。

然后,一旦加载了数据,则需要生成FFT。我一直都觉得这很困惑,既不是MATLAB程序员也不是信号处理专家。

随后是一个非常基本的示例代码序列,带有描述性注释,解释了每个步骤的用途。此代码假定包含从文件中加载的数据的样本矢量被命名为samplessamples应包含电压值。如果您将此变量命名为其他变量,请相应地修改代码。

# Define the sampling rate (frequency), which has units of Hz (samples per second)
Fs = # TODO

# Calculate the time interval (the rate of change), which as units of seconds per sample.
dt = 1/Fs;

# Get the number of samples.
N = length(samples);

# Calculate the total time in seconds.
tt = N/Fs;

# Generate a time vector, starting at time 0, incrementing in intervals of dt,
# and ending at time tt (subtract one unit of time, dt, from the ending value
# to match the length of the sample vector).
t = (0 : dt : tt - dt)';

# Get the length of the time vector.
L = size(t, 1);

# Convert the time vector into a frequency vector,
# for the purposes of plotting it.
dF = Fs/L;                       # change in frequency (Hz)
f = (-Fs/2 : dF : Fs/2 - dF);    # frequency vector (like time vector above)

# Calculate the FFT of your sample vector.
x = fftshift(fft(samples));

# Generate a vector of amplitudes (voltages).
y = abs(x)/L;

# Plot it, with the frequency vector as the x-axis
# and the amplitude (voltage) as the y-axis.
plot(f, y);

上面的代码假设您的样本在整个采样间隔内均匀分布(即以定期的时间间隔收集)。从显示的数据文件摘录中,看来这是一个合理的假设。如果没有,则可以修改代码以将文件中的实际时间值加载到矢量中,然后使用这些值。