在MATLAB中用波特图绘制最大增益和截止频率

93ze6v8z  于 2023-03-03  发布在  Matlab
关注(0)|答案(2)|浏览(983)

我想通过MATLAB曲线图来突出仿真滤波器和实际滤波器之间的差异。我试图显示两种滤波器的最大增益和两个截止频率。理想情况下,波特图上的每一点都用一个圆圈表示,圆圈上有一条向下指向x轴的虚线。我可以使用getPeakGain函数获得最大增益值,然后转换为dB和Hz。我不知道如何计算截止频率。对于两者,我都不知道如何绘制它们。
到目前为止,我的代码如下。

clear all;
s = tf('s');
G = (1.5791e08*s^2)/((s+1.257e04)^2*(s^2 + 62.83*s + 987))

options = bodeoptions;
options.Title.String = {["Bode Diagram For Designed Components"]}
options.FreqUnits = 'Hz'; % or 'rad/second', 'rpm', etc.
options.Grid = 'on';

figure(1)
subplot(1,2,1)
bode(G,options);
[gpeak,fpeak] = getPeakGain(G);
gpeak_dB = 20*log10(gpeak)
fpeak_hz = fpeak/(2*pi)

clear all;
s = tf('s');
G = (1.5039e08*s^2)/((s+1.257e04)*(s+1.197e04)*(s+41.18)*(s+21.65))

options = bodeoptions;
options.Title.String = {["Bode Diagram For Digikey Components"]}
options.FreqUnits = 'Hz'; % or 'rad/second', 'rpm', etc.
options.Grid = 'on';

figure(1)
subplot(1,2,2)
bode(G,options);
[gpeak,fpeak] = getPeakGain(G);
gpeak_dB = 20*log10(gpeak)
fpeak_hz = fpeak/(2*pi)
tzxcd3kk

tzxcd3kk1#

要计算滤波器的截止频率,可以使用幅度图上的-3 dB点,该点是增益比最大增益低3 dB时的频率。截止频率定义为滤波器传递函数幅值为-3 dB时的频率(或线性范围内最大增益的0.707倍)。您可以使用波特函数找到截止频率,然后查找传递函数幅度为-3 dB时的频率。

clear all;

% Define the transfer function for the simulated filter
s = tf('s');
G_sim = (1.5791e08*s^2)/((s+1.257e04)^2*(s^2 + 62.83*s + 987));

% Define the transfer function for the real-life filter
G_real = (1.5039e08*s^2)/((s+1.257e04)*(s+1.197e04)*(s+41.18)*(s+21.65));

% Set up Bode plot options
options = bodeoptions;
options.Title.String = {["Bode Diagram For Designed Components"], ["Bode Diagram For Digikey Components"]};
options.FreqUnits = 'Hz'; % or 'rad/second', 'rpm', etc.
options.Grid = 'on';

% Plot the Bode diagrams for both filters
figure(1)
subplot(1,2,1)
[mag_sim,phase_sim,wout_sim] = bode(G_sim,options);
subplot(1,2,2)
[mag_real,phase_real,wout_real] = bode(G_real,options);

% Calculate and plot the maximum gain and cutoff frequency for the simulated filter
[max_gain_sim, max_gain_sim_idx] = max(mag_sim(:));
f_cutoff_sim = wout_sim(find(mag_sim>=max_gain_sim*0.707,1));
subplot(1,2,1)
hold on
plot(f_cutoff_sim, -3, 'ro')
plot(wout_sim(max_gain_sim_idx), 20*log10(max_gain_sim), 'ro')
hold off

% Calculate and plot the maximum gain and cutoff frequency for the real-life filter
[max_gain_real, max_gain_real_idx] = max(mag_real(:));
f_cutoff_real = wout_real(find(mag_real>=max_gain_real*0.707,1));
subplot(1,2,2)
hold on
plot(f_cutoff_real, -3, 'ro')
plot(wout_real(max_gain_real_idx), 20*log10(max_gain_real), 'ro')
hold off
vnjpjtjt

vnjpjtjt2#

1.-没有数据,无从得知:捕获TF样品

Mathworks支持句柄的使用,但是直接修改输入字段中的选项是MATLAB的一个特性,因此已经做了工作来保持两种方法都一样,而不发生冲突。
对于这个特殊的问题,会发生以下奇怪的事情:

close all;clear all;clc

s = tf('s');
G = (1.5791e08*s^2)/((s+1.257e04)^2*(s^2 + 62.83*s + 987))

op1 = bodeoptions;
op1.Title.String = {["Bode Diagram For Designed Components"]}
op1.FreqUnits = 'Hz'; 
op1.Grid = 'on';
op1.PhaseVisible='off'

数字; bode(G)%与bode(G,op 1)相同

前2行工作正常。
然而,G是必需的,因此当试图拉取这样的值时,例如用下面的

figure
[Gmag,Gph,wout]=bode(G);

空的图表连坐标轴都没有。
以下方法也不起作用

figure
[Gmag,Gph,wout]=bode(G,op1);

又是空的。
别问我为什么,但是当你像这样打了两次电话给波德

figure
bode(G)
[Gmag,Gph,wout]=bode(G,op1);

现在它又起作用了。
获得波特图,并且还捕获传递函数样本。
然而bode仍然忽略了我添加的保持相位关闭的选项和你打开电网的选项。
Bode从MATLAB的早期版本就已经存在了。我的猜测是bodebodeplot主要用于学术界,与直接使用G相比并不常见,因此有一段时间没有更新了。
GmagGph大小检查

size(Gmag)
size(Gph)

 =
     1     1    94
 =
     1     1    94

还有一件怪事GmagGph显示为3D,尽管两者实际上都是1D。
下面的方法也不管用。

op1.Grid = 'on';
op1.PhaseVisible='off'

2.-频率分辨率不足

在没有G值的情况下,找到1dB带宽或3dB或您选择的任何截止幅度值(用于确定多少通带被视为信号,哪些通带被视为带外)的唯一方法是直接在图上使用“* 标记 ”,方法与我们在示波器、频谱分析仪和网络分析仪等测量仪器上使用的方法相同。
MATLAB允许在同一图形曲线上使用多个标记v,然后调用DATA TIPS
假设您正在寻找
*-1dB带宽**,与通带峰值-1dB处的截止值相同。
可以直观地看出,所寻找的峰值离0 dB不远。

查看-1dB附近2个相邻标记的阅读

如果+17% -19%可以,您可以继续使用默认输入频率,但可能需要更好的幅度分辨率。
为此目的,必须使用更高密度的频率,并且这样做的方式是定义比默认量更多的输入频率,在这种情况下为94

close all;

op1 = bodeoptions;
op1.Title.String = {['Bode Diagram For Designed Components']}
op1.FreqUnits = 'Hz'; 
op1.Grid = 'on';
op1.PhaseVisible='off';

fin=[1:1e2:1e5];  % [Hz]

figure
bode(G,op1)
ax=gca
hold(ax,'on')
[GmagdB,Gph,fout]=bode(G,fin,op1);

GmagdB=squeeze(GmagdB);
Gph=squeeze(Gph);

如前所述,有94输入频率,现在有1e3频率,但需要挤压来移除空洞维度,因为无论出于何种原因,bode都会产生1x 1xN值,而不仅仅是1xN。

3.-预期1dB带宽

可以手动检查预期结果:

4.-简化:直接在G上工作

尽管具有图形句柄,但是Plot不能将新的迹线引导到所生成的图上。

fin=[0:10:1e7];
G2 = (1.5791e08*(2*pi*fin*1j).^2)./((2*pi*fin*1j+1.257e04).^2.*((2*pi*fin*1j).^2 + 62.83*(2*pi*fin*1j) + 987));

G2magdB=10*log10(abs(G2));
n=find(G2magdB>-10);

figure
ax2=gca
plot(ax2,fin,G2magdB)
hold(ax2,'on')
grid on
xlabel('f[Hz]');ylabel('|G| [dB]');
plot(ax2,fin(n),G2magdB(n),'r*')
axis([0 .5e6 -35 1.5])

5.-bodebodeplot速度较慢

bodebodeplot是有用的函数,但与直接处理可用数据相比,这两个函数都比较慢。
这个G并不是一个真正锐利的滤镜。
此外,传递函数看起来像一个LPF,但在真正接近0 Hz时会出现陷波。
你真的想让DC通过吗?也许不是过滤器的最佳选择。

相关问题