我比较了 Matlab 和 numpy 中的相位和幅度谱。我认为 Matlab 工作正确,但 numpy 计算正确的幅度谱,但相位谱很奇怪。我必须如何更改python代码才能通过numpy正确计算fft?
MATLAB:
fs = 1e4;
dt = 1 / fs;
t = 0:dt:0.5;
F = 1e3;
y = cos(2*pi*F*t);
S = fftshift(fft(y) / length(y));
f_scale = linspace(-1, 1, length(y)) * (fs / 2);
a = abs(S);
phi = (angle(S));
subplot(2, 1, 1)
plot(f_scale, a)
title('amplitude')
subplot(2, 1, 2)
plot(f_scale, phi)
title('phase')
Run Code Online (Sandbox Code Playgroud)
Python:
import numpy as np
import matplotlib.pyplot as plt
fs = 1e4
dt = 1 / fs
t = np.arange(0, 0.5, …
Run Code Online (Sandbox Code Playgroud)