【问题标题】:How do I plot a FFT graph from data in a .txt file?如何从 .t​​xt 文件中的数据绘制 FFT 图?
【发布时间】:2019-03-26 22:25:37
【问题描述】:

只是让您知道我对 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 

【问题讨论】:

    标签: matlab fft


    【解决方案1】:

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

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

    下面是一个非常基本的示例代码序列,描述性的 cmets 解释了每个步骤的目的。此代码假定包含从文件加载的数据的样本向量名为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);
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 2012-06-03
      • 2016-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多