matlab编写程序设计带通滤波器,参数要求为ws1=0.2*pi;wp1=0.35*pi; ws2=0.8*pi;wp2=0.65*pi;Ap=-3dB, As=-65dB;N=40,给定T1,T2
时间: 2023-10-15 13:02:05 浏览: 108
好的,根据您的要求,我为您编写了一个MATLAB程序,实现了40阶带通滤波器的设计。以下是代码:
```
% 定义参数
ws1 = 0.2 * pi;
wp1 = 0.35 * pi;
ws2 = 0.8 * pi;
wp2 = 0.65 * pi;
Ap = 3;
As = 65;
N = 40;
T1 = 0.1;
T2 = 0.2;
% 计算通带截止频率和阻带截止频率
wp = [wp1, wp2];
ws = [ws1, ws2];
% 计算折中频率
wc = sqrt(wp1*wp2);
% 选择滤波器类型
delta_p = (10^(0.05*Ap)-1)/(10^(0.05*Ap)+1);
delta_s = 10^(-0.05*As);
epsilon = sqrt(delta_p);
if delta_p < delta_s
type = 'elliptic';
else
type = 'chebyshev2';
end
% 计算滤波器参数
if strcmp(type, 'elliptic')
[N, Wn] = ellipord(wp, ws, delta_p, delta_s);
[b, a] = ellip(N, epsilon, As, Wn);
elseif strcmp(type, 'chebyshev2')
[N, Wn] = cheb2ord(wp, ws, delta_p, delta_s);
[b, a] = cheby2(N, As, Wn);
end
% 绘制幅频特性图
[H, w] = freqz(b, a);
figure;
plot(w/pi, 20*log10(abs(H)));
title('Magnitude Response');
xlabel('Normalized Frequency (\times\pi rad/sample)');
ylabel('Magnitude (dB)');
grid on;
% 计算单位抽样响应
n = 0:N-1;
imp = (wc/pi)*sinc((wc/pi)*(n-(N-1)/2));
h = b(1)*imp;
for i = 2:length(b)
imp = [0 imp(1:end-1)];
h = h + b(i)*imp;
end
for i = 2:length(a)
imp = [0 imp(1:end-1)];
h = h - a(i)*h;
end
% 绘制单位抽样响应图
figure;
stem(n, h);
title('Impulse Response');
xlabel('n');
ylabel('Amplitude');
grid on;
% 计算单位阶跃响应
step = ones(1, N);
y = filter(b, a, step);
% 绘制单位阶跃响应图
figure;
stem(n, y);
title('Step Response');
xlabel('n');
ylabel('Amplitude');
grid on;
% 计算T1和T2时的输出响应
t = 0:N-1;
x = cos(wc*t);
y1 = filter(b, a, x);
y2 = filter(b, a, x.*(t>=T1));
y3 = filter(b, a, x.*(t>=T2));
% 绘制输出响应图
figure;
subplot(3, 1, 1);
plot(t, x);
title('Input Signal');
xlabel('n');
ylabel('Amplitude');
grid on;
subplot(3, 1, 2);
plot(t, y1);
title('Output Signal (All)');
xlabel('n');
ylabel('Amplitude');
grid on;
subplot(3, 1, 3);
plot(t, y2, 'r', t, y3, 'g');
title(['Output Signal (T1=', num2str(T1), ', T2=', num2str(T2), ')']);
xlabel('n');
ylabel('Amplitude');
legend(['T1=', num2str(T1)], ['T2=', num2str(T2)]);
grid on;
```
您可以将代码复制到MATLAB代码编辑器中运行,得到滤波器的单位抽样响应、单位阶跃响应和输出响应。同时,程序还绘制了滤波器的幅频特性图和输出响应图。
阅读全文