由于矢量长度不同,绘制FFT失败

时间:2017-06-06 11:10:22

标签: matlab plot fft spectral-density

我有时间(第1列)和当前幅度(第2列)的csv数据。我想绘制电流的FFT。这实际上是超过30ns的模拟数据,数据步长是1ps。我可以在MATLAB中绘制当前与时间的关系。但是在进行FFT功能时,它根本就没有绘图

Error using plot
Vectors must be the same length.

Error in FFT_Ideal_current_maxstep_1ps (line 25).
plot(f,Y)

任何人都可以帮助我吗?我也附上了MATLAB代码和CSV文件。

我还想绘制功率谱密度。如果有人能提供帮助,那就太好了。我想获得超过2GHz或更高频谱范围的FFT和psd

MATLAB code1:

% open data file
fid = fopen('current_maxstep_1ps.csv');

% Read data in from csv file
readData = textscan(fid,'%f %f','Headerlines',1,'Delimiter',',');

% Extract data from readData
t = readData{1,1}(:,1);
x = readData{1,2}(:,1);

N = length(x);
ts = 0.000000000001;
Fs = 1/ts;
tmax = (N-1)*ts;
tm = 0:ts:tmax;
f = 0:Fs/(N-1):Fs/2;
y = fftshift(fft(x));
Y = abs(y);
plot(f,Y)

还有另一个我试过的MATLAB代码,它绘制了一个图(这里是图片:FFT picture of code 2),但在时域中显示,我想要频谱像频谱上的apmitudes尖峰。

MATLAB Code2:

% open data file
fid = fopen('Ideal_current_maxstep_1ps.csv');

% Read data in from csv file
readData = textscan(fid,'%f %f','Headerlines',1,'Delimiter',',');

% Extract data from readData
xData = readData{1,1}(:,1);
yData = readData{1,2}(:,1);

Ts = 1e-12;
Fs = 1/Ts;
%Fs = 1000;
%Ts = 1/Fs;

X = fft(yData);
plot(xData, abs(X))

1 个答案:

答案 0 :(得分:1)

问题是fY的长度不一样。您可以使用length(f)length(Y)进行检查。原因是fft也计算负频率。因此,您应该按如下方式定义f

f = -Fs/2:Fs/(N-1):Fs/2;

注意 fft是共轭对称的,因为输入数据是真实的。

您可以使用xlim命令限制绘制的频率范围,如下所示:

xlim([0 3*10^9]) % limit x range between 0Hz and 3GHz

enter image description here