我正在尝试在 Julia 中可视化一个信号及其频谱。
这是我正在尝试的正弦信号:
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")
Run Code Online (Sandbox Code Playgroud)
我希望看到一个很好的图,在 60 Hz 处有一个峰值,但我得到的只是一个奇怪的结果:

我现在忽略负频率。
我应该如何在 Julia 中做到这一点?
hck*_*ckr 10
您fs在代码中调用的不是您的采样率,而是它的倒数:采样周期。
该函数fftfreq将采样率作为其第二个参数。由于您作为第二个参数给出的是采样周期,因此函数返回的频率被错误地缩放(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")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6323 次 |
| 最近记录: |