matlab画伯德图(带谐振点和-3dB带宽)
画伯德图可以⽤simulink或者matlab⾃带的bode函数:
P=bodeoptions;
P.Grid='on';
P.XLim={[1e-2,100]};%设置横轴范围
P.FreqUnits='Hz';%将横坐标单位换位HZ
P.YLim={[-30,3],[-135,10]};%设置纵轴范围
num1=[30,369.8,1367];
den1=[2.152E-4,0.2704,29.99,358.9,1367];
Gx_validation=tf(num1,den1);
[mag,phase,wout]=bode(Gx_validation,P)
%bode(Gx_validation,P)
margin(Gx_validation)
但是其不能准确输出和在图中标出谐振点和-3dB的点(只能⽤figure下的游标数据来标),下⾯根据伯德图的定义,直接⽤传递函数G(iw),算出其幅值和幅⾓:
%this function is to calculate the bode diagram without using bode function
%directly.
figure(1);
%% -----限定坐标轴输出范围-----
axisxmin=1e-2;
axisxmax=1e2;
axisyminmag=-inf;
axisymaxmag=4;
axisyminpha=-inf;
fontweight属性boldaxisymaxpha=45;
%% -----幅频图------
subplot(2,1,1);
f=logspace(-2,2,10000); %频率采样,利⽤logspace在横坐标log之后均匀取点,防⽌线性取点后画图时的不均匀
w=f*2*pi;
wi=complex(0,w); %使⾓频率和虚数单位结合
%输⼊系统传递函数
G=(30*(wi).^2+127.9*wi+163.6)./(1.244e-3*(wi).^4+0.7818*(wi).^3+29.99*(wi).^2+124.1*wi+163.6) ;
magnitude=20*log10(abs(G)); %幅度换算为dB单位
semilogx(f,magnitude);
title('G_{a\_Z} Bode Diagram');
axis([axisxmin,axisxmax,axisyminmag,axisymaxmag]);
box on;
%xlabel('frequency (Hz)','Fontweight','bold','FontSize',20);
ylabel('Magnitude (dB)','Fontweight','bold','FontSize',20);
grid minor; %加密⽹格
set(gca,'xticklabel',[]) %去掉横坐标数字
set(gca,'GridLineStyle','-','XGrid','on','YGrid','on'); %添加横坐标和纵坐标的⽹格
set(gca,'FontSize',10,'Fontweight','bold'); %调节坐标轴字体⼤⼩粗细
hold on;
%% -----相频图------
subplot(2,1,2);
phase=angle(G)*180/pi; %取虚数幅⾓值,并转换弧度为⾓度
semilogx(f,phase);
axis([axisxmin,axisxmax,axisyminpha,axisymaxpha]);
box on;
xlabel('frequency (Hz)','Fontweight','bold','FontSize',10);
ylabel('Phase (deg)','Fontweight','bold','FontSize',10);
grid minor; %加密⽹格
grid minor; %加密⽹格
set(gca,'GridLineStyle','-','XGrid','on','YGrid','on'); %添加横坐标和纵坐标的⽹格
set(gca,'FontSize',10,'Fontweight','bold'); %调节坐标轴字体⼤⼩粗细
hold on;
%% -----添加谐振点-----
subplot(2,1,1);
[magmax,fmax] = max(magnitude); %谐振点为幅频曲线的极值点
fmax = f(fmax);
plot(fmax,magmax,'o','LineWidth',2,'MarkerSize',5,'MarkerEdgeColor','k')
text(fmax,magmax-2,sprintf('Resonance point:\n(%.3f Hz, %.3f dB)',fmax,magmax),...
'VerticalAlignment','top','HorizontalAlignment','right')
%'VerticalAlignment','bottom'
hold on;
%% -----添加-3dB带宽-----
B=abs(magnitude+3);
[x,index]=sort(B); %为了得到最接近-3dB的值,先对绝对值进⾏排序,index为排序前的坐标值mag_3db=magnitude(index(1));
f_3db=f(index(1));
plot(f_3db,mag_3db,'o','LineWidth',2,'MarkerSize',5,'MarkerEdgeColor','r')
text(f_3db,mag_3db,sprintf('Cut-off point:\n(%.3f Hz, %.3f dB)',f_3db,mag_3db),...
'VerticalAlignment','bottom','HorizontalAlignment','left') %在⽬标坐标点处添加⽂字
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论