简体   繁体   English

如何在MATLAB中绘制数字滤波器的频率响应?

[英]How do I plot the frequency response of a digital filter in MATLAB?

I am trying to graph the frequency response. 我试图绘制频率响应图。 I was asked to use filter in MATLAB, but since I've read the manual, I still don't get how it performs a Z transform. 我被要求在MATLAB中使用filter ,但由于我已经阅读了手册,我仍然没有得到它如何执行Z变换。

I have the impulse response of the digital filter written below: 我有下面写的数字滤波器的脉冲响应:

for i=1:22;
   y(i)= 0;
end
x(22) = 1;
for k=23:2523
    x(k) = 0;
end
for n = 22:2522;
    y(n) = ((1/21)*x(n))+((20/21)*y(n-21));
end
plot(y);

It's just a feedback system of y[n] = 1/21*x[n] + 20/21*y[n-21] 它只是一个y[n] = 1/21*x[n] + 20/21*y[n-21]的反馈系统

Below are my calculations to compute the Z transform of the above system, which ultimately determines the impulse response: 下面是我计算上述系统的Z变换的计算,它最终决定了脉冲响应:

Z(y) = Z((1/21)*x(n)+(20/21)*y(n-21))

Y(Z) = (1/21)X(Z)+(20/21)*Z.^-21Y(Z)

Z(Z)-(20/21)*Z.^-21Y(Z) = (1/21)X(Z)

Y(Z)(1-(20/21)*Z.^-21) = (1/21)X(Z)  // divide by X(Z)*(1-(20/21)*Z.^-21)

Y(Z)/X(Z) = (1/21)/(1-(20/21)*Z.^-21)

H(Z) = (1/21)/(1-(20/21)*Z.^-21) // B = 1/21, A = 20/21

H(Z) = (B*Z.^21)/(Z.^21-A)

How can I plot the frequency response of H(Z) ? 如何绘制H(Z)的频率响应? Should I use filter ? 我应该使用filter吗?

If you just need to plot the impulse response it's easy. 如果你只是需要绘制脉冲响应,那很容易。 The impulse response is the response of the digital filter to a Dirac pulse. 脉冲响应是数字滤波器对狄拉克脉冲的响应。 You already have the difference equation, so you're already in 'z' and you don't care about the 's', you don't have to perform the 's' to 'z' transform (which is a topic in itself!). 你已经有了差分方程,所以你已经在'z'并且你不关心's',你不必执行's'到'z'变换(这是一个主题)本身!)。

So just generate a signal x(n) consisting of zeros everywhere except the first sample x(1) being 1. Pass it through the filter (yes, your difference equation). 所以只需生成一个由零组成的信号x(n),除了第一个样本x(1)为1.将其传递过滤器(是的,你的差分方程)。 The y(n) you get is your impulse response h(n). 你得到的y(n)是你的脉冲响应h(n)。 That's basically what you did. 这基本上就是你做的。

And of course if you FFT this h(n) you get the phase and magnitude response. 当然,如果你对这个h(n)进行FFT,你会得到相位和幅度响应。

Use freqz from the signal processing toolbox (hope you have it). 使用信号处理工具箱中的freqz (希望你拥有它)。 You first need to find the Z-transform, which you have already done here: 您首先需要找到您已在此处完成的Z变换:

Y(Z)/X(Z) = (1/21)/(1-(20/21)*Z.^-21)

freqz takes in a vector of coefficients which correspond to the numerator and denominator of your transfer function. freqz接收系数向量,该向量对应于传递函数的分子和分母。 It's called like so: 它被称为如此:

freqz(b, a);

b and a are the numerator and denominator coefficients of your transfer function. ba是传递函数的分子和分母系数。 This will produce a figure that shows the magnitude and phase response (hence frequency response) of the above system. 这将产生一个数字,显示上述系统的幅度和相位响应(因此频率响应)。

Therefore, all you need is to do this: 因此,您只需要这样做:

b = 1/21;
a = [1 zeros(1,20) -(20/21)];
freqz(b, a)

Take special note of the a vector. 特别注意a向量。 It has 1, followed by 20 zeros, then followed by -(20/21) . 它有1,然后是20个零,然后是-(20/21) Because you have a coefficient to the power of -21 and nothing else other than the 1 before it, that means that those coefficients between -1 to -20 are zero , and there are 20 of these coefficients that are zero in total, which is why we need to fill in the vector with zeroes between the 1 and -(20/21) term. 因为你有一个系数到-21的功率,除了它之前的1以外别的什么,这意味着-1到-20之间的那些系数是 ,并且这些系数中有20个总共为零,这是为什么我们需要在1和-(20/21)项之间用零填充向量。

We get: 我们得到:

在此输入图像描述


If you want to plot the poles and zeroes of your filter, use a combination of tf and pzmap : 如果要绘制滤镜的极点和零点,请使用tfpzmap的组合:

sys = tf(b, a, -1);
pzmap(sys);

tf creates a transfer function by specifying the numerator and denominator coefficients of your filter, and -1 implies it's a discrete-time filter, but we don't know what the sampling time is. tf通过指定滤波器的分子和分母系数来创建传递函数, -1表示它是离散时间滤波器,但我们不知道采样时间是多少。 pzmap plots the poles and zeroes in the z-domain with the unit-circle overlaid. pzmap绘制z域中的极点和零点,单位圆覆盖。

We get this: 我们得到这个:

在此输入图像描述

This makes sense as you have no zeroes in your system, and 21 poles, as you have 21 delay elements when examining the discrete-time sequence in your example. 这是有道理的,因为你的系统中没有零和21极,因为你在检查示例中的离散时间序列时有21个延迟元素。

From Matlab's filter documentation: 来自Matlab的filter文档:

filters the input data, x, using a rational transfer function defined by the numerator and denominator coefficients b and a, respectively. 使用由分子和分母系数b和a定义的有理传递函数分别对输入数据x进行滤波。

As you might know, filtering an impulse input would give you the impulse response. 如您所知,过滤脉冲输入将为您提供脉冲响应。 It then remains to obtain those b and a coefficients. 然后,它仍然获得这些ba系数。 You could either obtain those directly from the difference equation 您可以直接从差分方程中获得

y[n] = 1/21*x[n] + 20/21*y[n-21];

(as indicated in the rational transfer function link above ) or equivalently from the rational transfer function you have derived: (如上面有理传递函数链接所示 )或等效地来自你得出的有理传递函数:

%H(Z) = (B*Z.^21)/(Z.^21-A)

In either case, you should get the following a and b coefficients: 在任何一种情况下,您都应该得到以下ab系数:

a = [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -20/21];
% or equivalently: a=zeros(22,1); a(1)=1; a(22)=-20/21;
b = [1/21];

Thus, 从而,

% setup the impulse input
x = zeros(2500,1);
x(1) = 1;

% compute the impulse response
y = filter(b, a, x);

This impulse response can be plotted as you have done: 您可以按照以下方式绘制此脉冲响应:

plot(y);

As far as how this related to the transform H(Z) , the closed form expression you obtained can be evaluated in terms of a Laurent series expansion, which then has the coefficients of the time-domain y (impulse response) series. 至于这与变换H(Z) ,你得到的封闭形式表达式可以用Laurent级数展开来评估,后者具有时域y (脉冲响应)系列的系数。

However, H(z) is an analytic function defined in the |z| > R 但是, H(z)|z| > R定义的解析函数 |z| > R (where R=power(20/21,1/21) in your case) region of convergence in the complex plane. |z| > R (其中R=power(20/21,1/21)您的情况下的R=power(20/21,1/21) )复平面中的收敛区域。 It is more typical to plot the frequency response, which corresponds to the H(z) evaluated on the unit-circle (ie for complex number satisfying |z|=1 or equivalently z = exp(j * theta) with theta in the [0-2pi] range). 更典型的是绘制频率响应,其对应于在单位圆上评估的H(z) (即,对于复数满足|z|=1或等效z = exp(j * theta)其中theta在[ 0-2pi]范围)。 An efficient method to compute values of H(z) at regularly spaced points on that unit-circle is to take the FFT of the impulse response: 计算该单位圆上规则间隔点的H(z)值的有效方法是采用脉冲响应的FFT:

FrequencyResponse = fft(y);
figure(1);
plot(abs(FrequencyResponse));
figure(2);
plot(phase(FrequencyResponse));

PS : the computation of filter and fft can be done in the single call freqz if you have the signal processing toolbox (although you were specifically asked to use filter ). PS :如果您有信号处理工具箱(尽管您被特别要求使用filter ),可以在单个调用freqz完成filterfft的计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM