The*_*heo 2 matlab plot filtering syntax-error
我想编写一个脚本来绘制带通滤波器的传递函数[H(f)]的图形,| H(f)| 反对频率和H(f)(度)相对于频率的相位,我对matlab非常新,所以语法不是100%,我很困惑,因为一切都是以矩阵形式自动格式化.下面是我的脚本:
% RCL circuit: band-pass filter
R=55590; L=0.9571; C=48.811*10.^(-9); % values of the Resistor and Capacitor
f=(0:60e3); w=2*pi*f; % frequency (f) range of measurements
H=(R./(sqrt(R^2+(w*L-(1./(w*C))).^2))); % Transfer Function
% Magnitude (absolute value) of the transfer function
plot(f,abs(H),'LineWidth',2); grid on; hold on
xlabel('Frequency [Hz]','FontSize',20); ylabel('|H(f)|','FontSize',20)
Run Code Online (Sandbox Code Playgroud)
数字
plot(f,angle(H)*180/pi,'LineWidth',2); grid on; hold on
xlabel('Frequency [Hz]','FontSize',18);
ylabel('phase of H(f) [degrees]','FontSize',20)
Run Code Online (Sandbox Code Playgroud)
这是我使用的传递函数公式
下面是我的实验结果和预期图表的另一张图片,我只是不明白为什么MATLAB没有绘制我想要的东西?

你知道这个bodeplot功能吗?
简单二阶带通的传递函数是:

您只需要将值插入到Matlab的tf函数中并将其绘制为bodeplot:
R = 55590;
L = 0.9571;
C = 48.811*10.^(-9);
% tf creates transfer function object
sys = tf( [R*C 0] , [L*C R*C 1]); % [R*C 0] vector of numerator coeffcients
% R*C*s + 0*1
% [L*C R*C 1] vector of denominator coeff.
% L*C*s^2 + R*C*s + 0*1
bodeplot(sys) % plot command to plot frequency response
% of magnitude and phase
Run Code Online (Sandbox Code Playgroud)
并绘制:

或者,如果您需要更多输出(如幅度和相位)作为变量,请使用bode,图表是相等的.但bodeplot提供更多额外的绘图自定义选项.
关于您的评论,您需要一个线性轴:
你只需要在plot命令之前添加以下行:
P = bodeoptions; % handle to plot options
P.MagScale = 'linear';
P.MagUnits = 'abs';
bodeplot(sys,P) % plot command with custom options
Run Code Online (Sandbox Code Playgroud)
它看起来如下:

要调整频率轴限制,请使用:
P.XLim = [1 60e3];
Run Code Online (Sandbox Code Playgroud)
或类似的大小:
P.YLim = [0 1];
Run Code Online (Sandbox Code Playgroud)
我建议不要使用线性频率轴,但如果你真的想要它,你可以使用:
P.FreqScale = 'linear';
P.FreqUnits = 'Hz'; % optional
Run Code Online (Sandbox Code Playgroud)
如果您想将实验数据与上图一起绘制,请按照此示例进行操作.
用于bode从传递函数中获取格式相同的数据,例如您的实验数据并用于semilogx绘制它.
freqVec = logspace(-1, 3, 5000);
[mag, phs] = bode(sys, freqVec * (2*pi));
mag = db(mag(:));
phs = phs(:);
figure;
subplot(211)
semilogx(freqVec, mag); hold on
semilogx(freqVec, experimentalDataMagnitude); hold off
grid on
title('System Bode Plot')
ylabel('Magnitude (dB)')
subplot(212)
semilogx(freqVec, phs); hold on
semilogx(freqVec, experimentalDataPhase); hold off
grid on
ylabel('Phase (deg)')
xlabel('Frequency (Hz)')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18711 次 |
| 最近记录: |