如何使用Matlab';s过滤器命令,用于绘制系统的脉冲响应?

如何使用Matlab';s过滤器命令,用于绘制系统的脉冲响应?,matlab,plot,filter,sequence,Matlab,Plot,Filter,Sequence,我试图通过使用Matlab的滤波命令,而不使用其他预先存在的Matlab函数,绘制IIR和FIR系统的脉冲响应图。过滤器必须能够处理样本输入,如绘图([12],[01.8])。问题是这样的: function iplot(b, a) % IPLOT Impulse response of system. % IPLOT(B,A,N) is the N-point impulse response % of the filter B/A: %

我试图通过使用Matlab的滤波命令,而不使用其他预先存在的Matlab函数,绘制IIR和FIR系统的脉冲响应图。过滤器必须能够处理样本输入,如绘图([12],[01.8])。问题是这样的:

function iplot(b, a)

% IPLOT Impulse response of system.
% IPLOT(B,A,N) is the N-point impulse response
% of the filter B/A:

%                          -1                -nb 
%     
%           B(z) b(1) + b(2)z + .... + b(nb+1)z
%
%    H(z) = ---- = --------------------------------- 
% 
%                          -1                -n         
%           A(z) a(1) + a(2)z + .... + a(na+1)z


% If h[n] FIR, then N = length(h);

% given numerator and denominator coefficients in vectors B and A.

% N is specified according to the following rule:

% If If h[n] is IIR and increasing (i.e. |h[n]|-->inf as n-->inf),

% then N=20;

% h[n] is IIR and decreasing (i.e. |h[n]|-->0 as n-->inf), 

% then the maximum N is determined such that

% rms value of h(1:N) = 0.999 * rms value of h(1:1000).

% However, in this case, N must also be chosen such that 10 <=

% N <= 100
功能iplot(b,a)
%IPLOT系统的脉冲响应。
%IPLOT(B,A,N)是N点脉冲响应
%过滤器B/A的配置:
%-1-nb
%     
%B(z)B(1)+B(2)z+b(nb+1)z
%
%H(z)=----------------------------------
% 
%-1-n
%A(z)A(1)+A(2)z+..+a(na+1)z
%如果h[n]FIR,则n=长度(h);
%给定向量B和A中的分子和分母系数。
%N根据以下规则指定:
%如果h[n]是IIR并在增加(即| h[n]|-->inf作为n-->inf),
%N=20;
%h[n]是IIR并在减小(即| h[n]|-->0作为n-->inf),
%然后确定最大N,使得
%h的均方根值(1:N)=0.999*h的均方根值(1:1000)。

%然而,在这种情况下,还必须选择N,以确保第一行代码是正确的。但是,在调用
filter
时,需要反转
b
a
的顺序
b
代表分子系数,
a
代表分母系数。那也在你的文档串里

您正确地将输入指定为单个输入。但是,打印代码不正确。
plot
的工作原理是,它接受一组
x
值和一组
y
值,并将这些点成对绘制。因为您正在绘制离散输出信号,我建议您使用
stem
而不是
plot
。这样,每个点都会用一条从水平轴到感兴趣点的线来绘制

在任何情况下,都需要提供一组
x
值。具体来说,您需要一个从0到999的向量。
y
值由
滤波器产生的输出脉冲响应组成。当前,
x
值包含输出脉冲响应,而
y
值是一组零。这将被可视化为一组聚集在
y=0
的点,这可能不是您想要的

因此,您只需执行以下操作:

h = filter(b,a,[1,zeros(1,999)]); 
plot(0:999, h, 'b');  %// Change
关于我的建议,我将使用
stem

stem(0:999, h, 'b');
代码的最后一部分是绘制过滤器的极点。您还应该制作一个单独的图形,并绘制这些结果。您没有这样做,所以当您尝试绘制极点时,包含脉冲响应的图形将被覆盖。在当前窗口打开的情况下多次调用
绘图
,将使用上次调用
绘图
覆盖绘图。因此,在继续之前,生成一个新的
窗口:

figure; %// New window
h = filter(b,a,[1,zeros(1,999)]); 
stem(0:999, h, 'b');  %// Change

figure; %// New window
poles = roots(a); %// Find roots
%// Plot the roots as single dots
plot(real(poles), imag(poles), 'b.');

然而,这并不能回答您的问题陈述。问题陈述说:

如果
h[n]
为FIR,则
n=长度(h)
; 给定向量
B
A
中的分子和分母系数

N
是根据以下规则指定的: 如果
h[n]
是IIR并在增加(即
|h[n]|-->inf作为n-->inf
), 然后
N=20

h[n]
是IIR并在减小(即
|h[n]|-->0作为n-->inf
), 然后确定最大N,使得
h(1:N)的
rms
值=0.999*h(1:1000)的rms值
。 但是,在这种情况下,还必须选择
N
,以便
10=0)%//检查是否增加
N=20;
else%//递减情况
%//查找RMS值
N=圆形(0.999*rms(h));

%//确保10不清楚你在问什么。您生成的代码有什么问题?前3行看起来应该与您描述的完全一样(尽管您的x轴和y轴可能会从传统预期的方向翻转)。非常感谢您的帮助!干情节澄清了很多!然而,我的图2似乎仍然没有绘制任何东西,我的图1实际上是集群的。这可能是因为我的轴格式不正确吗?也许我应该以某种方式使用linspace?@KarmaPimp-检查我的编辑。我忘了解释选择
N
的情况。图1是集群的,因为我们正在绘制太多的点!哦,我明白了,是的,那是因为。我想也许你不需要在你的解决方案中再这样做了,但我有点困惑,为什么你不再使用stem而不是plot?@KarmaPimp-是的,对不起。我的意思是使用
stem
。复制和粘贴错误。很抱歉回复太晚!是的,他们确实帮了我很大的忙!!非常感谢你。保重:D
figure; %// New window
h = filter(b,a,[1,zeros(1,999)]); 
stem(0:999, h, 'b');  %// Change

figure; %// New window
poles = roots(a); %// Find roots
%// Plot the roots as single dots
plot(real(poles), imag(poles), 'b.');
figure; %// New window
h = filter(b,a,[1,zeros(1,999)]); 

%// Decide on the value of N
if length(a) == 1
    N = length(h);
else
    d = diff(h);
    if all(d >= 0) %// Check for increasing
        N = 20;
    else %// Decreasing case
        %// Find RMS value
        N = round(0.999*rms(h));

        %// Ensure that 10 <= N <= 100
        if N < 10
            N = 10;
        end
        if N > 100
            N = 100;
        end
    end
end

%// Plot just up to the first N values
stem(0:N-1, h(1:N), 'b');  

figure; %// New window
poles = roots(a); %// Find roots
%// Plot the roots as single dots
plot(real(poles), imag(poles), 'b.');