matlab绘制正弦信号频谱图(虚、实频谱、单、双边相位谱、单、双边幅频谱)
首先我们今天绘制的正弦信号的函数表达式:f(x)=sin(2*π*f*t),其中f=2.
我使用的是matlab2020b,打开matlab后,新建脚本。
我们先画出sin(2*π*f*t)信号的图像:
f=2; T=1/f; Fs=100; %采样率 Ts=1/Fs; t=0:Ts:1-Ts; %t范围0~1,步长0.01 n=length(t); y=sin(2*pi*f*t); %正弦信号函数 sinplot=figure; plot(t,y) %绘制函数图像 x轴为时间t,y轴为信号函数 xlabel(\'时间(s)\') %x轴名称 ylabel(\'信号\') %y轴名称 title(\'原信号图像\') %图像顶部名称 grid on
函数图像如下:
然后对函数进行快速傅里叶变换、计算实部虚部,绘制幅频谱、相频谱、实频谱、虚频谱。
代码如下:
[Doain,Range]=cFFT(y,Fs); Doain2=Doain(1,51:100); stem(Range(1,51:100),abs(Doain2)*2,\'Marker\',\'none\',\'LineWidth\',3);%离散绘制幅频谱,取消原图像小圆圈,线条粗细3
xlabel(\'Freq(Hz)\') ylabel(\'幅值\') title(\'单边幅频谱\') grid axis([-2.5,2.5,-1.5,1.5]) %坐标显示范围:x轴-2.5~2.5,y轴-1.5~1.5 CnR=real(Doain); %实部 CnI=imag(Doain); %虚部 Cn=(CnR.^2+CnI.^2).^(1/2); %幅值 fain=tand(CnI./CnR)/3; %相位角 fain=fain(1,48:54); %去除影响因素 figure stem(Range,CnR) %离散绘制 grid axis([-6,6,-2,2]) title(\'实频谱\') xlabel(\'Hz\') ylabel(\'CnR\') figure stem(Range,CnI,\'Marker\',\'none\',\'LineWidth\',3) grid axis([-2.5,2.5,-1,1]) title(\'虚频谱\') xlabel(\'Hz\') ylabel(\'CnI\') figure stem(Range,Cn,\'Marker\',\'none\',\'LineWidth\',3) grid axis([-2.5,2.5,-0.5,1]) title(\'双边幅频谱\') xlabel(\'Hz\') ylabel(\'|Cn|\') figure stem(Range(1,48:54),-fain,\'Marker\',\'none\',\'LineWidth\',3) grid axis([-2.5,2.5,-2.5,2.5]) title(\'双边相频谱\') xlabel(\'Hz\') ylabel(\'相位角\') figure fain2=fain(1,4:7); stem(Range(1,51:54),-fain2,\'Marker\',\'none\',\'LineWidth\',3) grid axis([-2.5,2.5,-2.5,1.5]) title(\'单边相频谱\') xlabel(\'Hz\') ylabel(\'相位角\') figure plot(t,y) xlabel(\'时间(s)\') ylabel(\'信号\') title(\'原信号图像\') grid on function[X,freq]=cFFT(x,Fs) %修正 N=length(x); if mod(N,2)==0 k=-N/2:N/2-1; else k=-(N-1)/2:(N-1)/2; end T=N/Fs; freq=k/T; X=fft(x)/N; X=fftshift(X); end
绘制图像如下:
最后附上完整代码:
f=2; T=1/f; Fs=100; Ts=1/Fs; t=0:Ts:1-Ts; n=length(t); y=sin(2*pi*f*t); sinplot=figure [Doain,Range]=cFFT(y,Fs); Doain2=Doain(1,51:100); stem(Range(1,51:100),abs(Doain2)*2,\'Marker\',\'none\',\'LineWidth\',3); xlabel(\'Freq(Hz)\') ylabel(\'幅值\') title(\'单边幅频谱\') grid axis([-2.5,2.5,-1.5,1.5]) CnR=real(Doain); CnI=imag(Doain); Cn=(CnR.^2+CnI.^2).^(1/2); fain=tand(CnI./CnR)/3; fain=fain(1,48:54); figure stem(Range,CnR) grid axis([-6,6,-2,2]) title(\'实频谱\') xlabel(\'Hz\') ylabel(\'CnR\') figure stem(Range,CnI,\'Marker\',\'none\',\'LineWidth\',3) grid axis([-2.5,2.5,-1,1]) title(\'虚频谱\') xlabel(\'Hz\') ylabel(\'CnI\') figure stem(Range,Cn,\'Marker\',\'none\',\'LineWidth\',3) grid axis([-2.5,2.5,-0.5,1]) title(\'双边幅频谱\') xlabel(\'Hz\') ylabel(\'|Cn|\') figure stem(Range(1,48:54),-fain,\'Marker\',\'none\',\'LineWidth\',3) grid axis([-2.5,2.5,-2.5,2.5]) title(\'双边相频谱\') xlabel(\'Hz\') ylabel(\'相位角\') figure fain2=fain(1,4:7); stem(Range(1,51:54),-fain2,\'Marker\',\'none\',\'LineWidth\',3) grid axis([-2.5,2.5,-2.5,1.5]) title(\'单边相频谱\') xlabel(\'Hz\') ylabel(\'相位角\') figure plot(t,y) xlabel(\'时间(s)\') ylabel(\'信号\') title(\'原信号图像\') grid on function[X,freq]=cFFT(x,Fs) N=length(x); if mod(N,2)==0 k=-N/2:N/2-1; else k=-(N-1)/2:(N-1)/2; end T=N/Fs; freq=k/T; X=fft(x)/N; X=fftshift(X); end
延迟T/4后的代码
fo=2; T=1/fo; Fs=100; Ts=1/Fs; t=0:Ts:1-Ts; n=length(t); y=sin(2*pi*fo*t-pi/2); sinplot=figure [Doain,Range]=centeredFFT(y,Fs); Doain2=Doain(1,51:100); stem(Range(1,51:100),abs(Doain2)*2,\'Marker\',\'none\',\'LineWidth\',3); xlabel(\'Freq(Hz)\') ylabel(\'幅值\') title(\'单边幅频谱\') grid axis([-2.5,2.5,-1,1.5]) CnR=real(Doain); CnI=imag(Doain); Cn=(CnR.^2+CnI.^2).^(1/2); fain=tand(CnR./CnI)*3.2; fain=fain(1,48:54); figure stem(Range,CnR,\'Marker\',\'none\',\'LineWidth\',3) grid axis([-2.5,2.5,-1,1]) title(\'实频谱\') xlabel(\'Hz\') ylabel(\'CnR\') figure stem(Range,CnI) grid axis([-6,6,-2,2]) title(\'虚频谱\') xlabel(\'Hz\') ylabel(\'CnI\') figure stem(Range,Cn,\'Marker\',\'none\',\'LineWidth\',3) grid axis([-2.5,2.5,-0.5,1]) title(\'双边幅频谱\') xlabel(\'Hz\') ylabel(\'|Cn|\') figure stem(Range(1,48:54),abs(fain),\'Marker\',\'none\',\'LineWidth\',3) grid axis([-2.5,2.5,-4,4]) title(\'双边相频谱\') xlabel(\'Hz\') ylabel(\'相位角\') figure fain2=fain(1,4:7); stem(Range(1,51:54),abs(fain2),\'Marker\',\'none\',\'LineWidth\',3) grid axis([-2.5,2.5,-4,4]) title(\'单边相频谱\') xlabel(\'Hz\') ylabel(\'相位角\') figure plot(t,y) xlabel(\'时间(s)\') ylabel(\'y\') title(\'原信号图像\') grid on function[X,freq]=centeredFFT(x,Fs) N=length(x); if mod(N,2)==0 k=-N/2:N/2-1; % N even else k=-(N-1)/2:(N-1)/2; % N odd end T=N/Fs; freq=k/T; %the frequency axis accordingly X=fft(x)/N; X=fftshift(X); End
文件链接: matlab绘制正弦信号频谱.zip (matlabxinhao1文件是本文所提到的信号,matlabxinhao2是将本文提到的信号延迟T/4之后的信号绘图。)
请发表评论