Matlab fftshift无法正常工作

时间:2014-12-06 13:06:59

标签: matlab fft frequency-analysis

我试图绘制频率为77.5 kHz和幅度为2的正弦波频谱。只有零点不正确,它向左移动。 我的代码:

f1=77.5e3; % frequency of the sinewave 
a1=2; % amplitude of the sinewave 

Fs = 1.55e6; % sampling frequency
dt=1/Fs; % step size 
maxtime = 5*(1/f1);
t=0:dt:maxtime; % time interval in which we want to plot 

x=a1*sin(2*pi*f1*t); % the values for the sinewave

N=length(t);  % this is how many samples we have in the time-domain
X=fft(x)/N; 
X=fftshift(X);
f=[-N/2:1:N/2-1]*Fs/N;  % creates a frequency axis 

figure(1)
plot(f,abs(X))
title('Magnitude Spectrum of x(t)')
xlabel('Frequency [Hz]')
ylabel('|X(f)|')

当我运行此代码时,我的频谱不正确。任何人都可以帮助我吗?

编辑:运行此代码时得到的数字: Incorrect spectrum I get

除了不正确的零点之外,当我从情节中计算出来时,我也会得到一个不正确的频率。我只是不确定如何绘制频率为77.5kHz,幅度为2和采样频率为1.55 MHz的正弦波

1 个答案:

答案 0 :(得分:1)

您的代码是正确的。但是你的信号一旦成为周期性的,就不仅仅是一个正弦波(存在不连续性,因为x的第一个和最后一个样本是相同的)。

您可以尝试最后删除1个样本:

t=0:dt:maxtime; % time interval in which we want to plot 
t = t(1:end-1);

现在峰值位于f1。

相关问题