【问题标题】:How to visualize FFT of a signal in Julia?如何在 Julia 中可视化信号的 FFT?
【发布时间】:2019-09-25 13:50:17
【问题描述】:

我正在尝试在 Julia 中可视化信号及其频谱。

我找到了FFTW 包,它提供了 FFT 和 DSP 的频率。

这是我正在尝试的,带有正弦信号:

using Plots
using FFTW
using DSP

# Number of points
N = 2^14 - 1
# Sample rate
fs = 1 / (1.1 * N)
# Start time
t0 = 0
tmax = t0 + N * fs

# time coordinate
t = [t0:fs:tmax;]

# signal
signal = sin.(2π * 60 * t)  # sin (2π f t)

# Fourier Transform of it
F = fft(signal)
freqs = fftfreq(length(t), fs)
freqs = fftshift(freqs)

# plots
time_domain = plot(t, signal, title = "Signal")
freq_domain = plot(freqs, abs.(F), title = "Spectrum")
plot(time_domain, freq_domain, layout = 2)
savefig("Wave.pdf")

我希望看到一个峰值在 60​​ Hz 的漂亮图,但我得到的只是一个奇怪的结果:

我暂时忽略负频率。

我应该如何在 Julia 中做到这一点?

【问题讨论】:

    标签: julia fft fftw


    【解决方案1】:

    您在代码中调用的fs 不是您的采样率,而是它的倒数:采样周期。

    函数fftfreq 将采样rate 作为其第二个参数。由于您作为第二个参数给出的是采样周期,因此函数返回的频率被 (1/(Ts^2)) 错误地缩放。

    我将fs重命名为Ts,并将第二个参数fftfreq改为采样率1.0/Ts。我认为您还需要转换fft 的结果。

    # Number of points 
    N = 2^14 - 1 
    # Sample period
    Ts = 1 / (1.1 * N) 
    # Start time 
    t0 = 0 
    tmax = t0 + N * Ts
    # time coordinate
    t = t0:Ts:tmax
    
    # signal 
    signal = sin.(2π * 60 .* t) # sin (2π f t) 
    
    # Fourier Transform of it 
    F = fft(signal) |> fftshift
    freqs = fftfreq(length(t), 1.0/Ts) |> fftshift
    
    # plots 
    time_domain = plot(t, signal, title = "Signal")
    freq_domain = plot(freqs, abs.(F), title = "Spectrum", xlim=(-1000, +1000)) 
    plot(time_domain, freq_domain, layout = 2)
    savefig("Wave.pdf")
    

    【讨论】:

      猜你喜欢
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      • 2012-12-03
      • 2011-08-15
      • 1970-01-01
      相关资源
      最近更新 更多