clear all;
Fs=100; %sampling rate
t=-10:(1/Fs):10;
for i=1:20*Fs+1
if -10<t(i) && t(i)<-9
x(i)= -1;
elseif -9<t(i) && t(i)<-8
x(i)=1;
elseif i>2*Fs+1
x(i)=x(i-(2*Fs));
上面显示的是矩形函数的代码,其域[-10 10]和周期2
这是我的positiveFFT函数
function [X,freq]=positiveFFT(x,Fs)
N=length(x); %get the number of points
k=0:N-1; %create a vector from 0 to N-1
T=N/Fs; %get the frequency interval
freq=k/T; %create the frequency range
X=fft(x)/N*2; % normalize the data
%only want the first half of the FFT, since it is redundant
cutOff = ceil(N/2);
%take only the first half of the spectrum
X = X(1:cutOff);
freq = freq(1:cutOff);`
通过这样做,我制作了这个矩形波的幅度谱,但只有当频率为正时。
如何绘制包含负频率的整个幅度谱?
答案 0 :(得分:0)
如果我理解正确,您需要获得频率为-Fs / 2到Fs / 2的矢量以及相应的频谱幅度。如果是这样,您应该使用fftshift
,例如:
N=length(x); %get the number of points
k=0:N-1; %create a vector from 0 to N-1
T=N/Fs; %get the frequency interval
freq=fftshift(k/T); %create the frequency range
X=fftshift(fft(x))/N*2; % normalize the data