2024年2月21日发(作者:)

用 ideallp 函数设计 FIR 线性相位滤波器

% G038

% 用 ideallp 函数设计 FIR 线性相位滤波器

%

h_fig1 = figure; % 创建第1个图形窗口

set(h_fig1, 'unit', 'normalized', 'position', [0.0, 0.0, 0.99, 0.94]);

set(h_fig1, 'defaultuicontrolunits', 'normalized');set(gcf, 'color', 'w')

h_text1 = uicontrol(gcf, 'Style', 'text', 'Position', [0.2, 0.9, 0.7, 0.07],... % 在第 1 个图形窗中创建文本框

'String', 'FIR 线性相位滤波器 ', 'FontName', '黑体', 'ForegroundColor', 'r', ...

'FontSize', 38, 'FontWeight', 'Bold', 'BackgroundColor', [1, 1, 1]); set(gcf, 'color', 'w')

h_text2 = uicontrol(gcf,'style','text', 'BackgroundColor', 'w', 'Position', [0.01, 0.58, 0.2, 0.1],... % 在第 1 个图形窗中创建文本框

'string', '滤波器类型', 'ForegroundColor', 'r', 'FontWeight', 'Bold', 'FontSize', 20)

h_pushbutton1 = uicontrol(h_fig1, 'Style', 'PushButton', 'Position', [0.07, 0.2, 0.08, 0.07],... % 创建命令钮 h_pushbutton2,定义其属性

'string', '退出', 'BackgroundColor', [0.8 0.9 0.8], 'ForegroundColor', 'r', 'FontSize', 14, 'FontWeight', 'Bold',...

'callback', 'delete(h_fig1)')

hr_1 = uicontrol(gcf,'style','radio', 'BackgroundColor', 'w',... % 创建第 1 个 radio 钮

'string','低通', 'ForegroundColor', 'r', 'FontWeight', 'Bold', 'FontSize', 10,...

'position',[0.04,0.53,0.08,0.05]); set(hr_1,'value',get(hr_1,'Max'));

set(hr_1, 'callback', [... % 定义第 1 个 radio 钮的回调函数

'set(hr_1,''value'',get(hr_1,''max'')),', 'set(hr_2,''value'',get(hr_2,''min'')),',...

'set(hr_3,''value'',get(hr_3,''min'')),', 'set(hr_4,''value'',get(hr_4,''min'')),',...

'cb05(h_axes1, h_axes2, h_axes3, h_axes4, 1)']);

hr_2 = uicontrol(gcf,'style','radio', 'BackgroundColor', 'w',... % 创建第 2 个 radio 钮

'string','高通', 'ForegroundColor', 'r', 'FontWeight', 'Bold', 'FontSize', 10,...

'position',[0.12,0.53,0.08,0.05]); set(hr_2,'value',get(hr_2,'Min'));

set(hr_2, 'callback', [... % 定义第 2 个 radio 钮的回调函数

'set(hr_2,''value'',get(hr_2,''max'')),', 'set(hr_1,''value'',get(hr_1,''min'')),',...

'set(hr_3,''value'',get(hr_3,''min'')),', 'set(hr_4,''value'',get(hr_4,''min'')),',...

'cb05(h_axes1, h_axes2, h_axes3, h_axes4, 2)']);

hr_3 = uicontrol(gcf,'style','radio', 'BackgroundColor', 'w',... % 创建第 3 个 radio 钮

'string','带通', 'ForegroundColor', 'r', 'FontWeight', 'Bold', 'FontSize', 10,...

'position',[0.04,0.45,0.17,0.05]); set(hr_3,'value',get(hr_3,'Min'));

set(hr_3, 'callback', [... % 定义第 3 个 radio 钮的回调函数

'set(hr_3,''value'',get(hr_3,''max'')),', 'set(hr_1,''value'',get(hr_1,''min'')),',...

'set(hr_2,''value'',get(hr_2,''min'')),', 'set(hr_4,''value'',get(hr_4,''min'')),',...

'cb05(h_axes1, h_axes2, h_axes3, h_axes4, 3)']);

hr_4 = uicontrol(gcf,'style','radio', 'BackgroundColor', 'w',... % 创建第 4 个 radio 钮

'string','带阻', 'ForegroundColor', 'r', 'FontWeight', 'Bold', 'FontSize', 10,...

'position',[0.12,0.45,0.08,0.05]); set(hr_4,'value',get(hr_4,'Min'));

set(hr_4, 'callback', [... % 定义第 4 个 radio 钮的回调函数

'set(hr_4,''value'',get(hr_4,''max'')),', 'set(hr_1,''value'',get(hr_1,''min'')),',...

'set(hr_2,''value'',get(hr_2,''min'')),', 'set(hr_3,''value'',get(hr_3,''min'')),',...

'cb05(h_axes1, h_axes2, h_axes3, h_axes4, 4)']);

h_axes1 = axes('Box', 'on', 'Position', [0.27, 0.525, 0.3, 0.3]) % 在第 2 个图形窗中创建轴对象

h_axes2 = axes('Box', 'on', 'Position', [0.65, 0.525, 0.3, 0.3]) % 在第 2 个图形窗中创建轴对象

h_axes3 = axes('Box', 'on', 'Position', [0.27, 0.095, 0.3, 0.3]) % 在第 2 个图形窗中创建轴对象

h_axes4 = axes('Box', 'on', 'Position', [0.65, 0.095, 0.3, 0.3]) % 在第 2 个图形窗中创建轴对象

% -------------------------------------------------------------------------

cb05(h_axes1, h_axes2, h_axes3, h_axes4, 1);

function cb05(a1, a2, a3, a4, k)

% 计算 Kaiser 窗

M = 45; As = 60; n = [0:1:M-1];

beta = 0.1102*(As-8.7)+1

w_kai = (kaiser(M,beta))';

% 给定滤波器的参数

wc1 = pi/3; wc2 = 2*pi/3;

% 根据参数 k,计算滤波器的理想冲激响应

if k==1 % 低通

hd = ideallp(wc1,M)

end

if k==2 % 高通

hd = ideallp(pi,M) - ideallp(wc1,M)

end

if k==3 % 带通

hd = ideallp(wc2,M)-ideallp(wc1,M);

end

if k==4 % 带阻

hd = ideallp(wc1,M) + ideallp(pi,M) - ideallp(wc2,M);

end

% ---------------------------------------------------

% 设计低通滤波器

h = hd .* w_kai;

[db,mag,pha,grd,w] = myfreqz(h,[1]);

% 画出低通滤波器的特性

subplot(a1); stem(n,hd,'.'); grid; title('(1)理想冲激响应', 'FontWeight', 'Bold')

xlabel('n', 'FontSize', 12, 'FontWeight', 'Bold'); ylabel('hd(n)', 'FontSize', 12, 'FontWeight', 'Bold');

axis([-1 M -0.5 0.8]); set(a1, 'LineWidth', 2);

subplot(a2); stem(n,w_kai,'.'); grid; title('(2)凯泽窗', 'FontWeight', 'Bold')

xlabel('n', 'FontSize', 12, 'FontWeight', 'Bold'); ylabel('w(n)', 'FontSize', 12, 'FontWeight', 'Bold');

axis([-1 M 0 1.1]); set(a2, 'LineWidth', 2);

subplot(a3); stem(n,h,'.'); grid; title('(3)实际冲激响应', 'FontWeight', 'Bold')

xlabel('n', 'FontSize', 12, 'FontWeight', 'Bold'); ylabel('h(n)', 'FontSize', 12, 'FontWeight', 'Bold')

axis([-1 M -0.5 0.8]); set(a3, 'LineWidth', 2);

subplot(a4); plot(w/pi,db);

xlabel('频率(单位: pi )', 'FontSize', 12, 'FontWeight', 'Bold'); ylabel('分贝数', 'FontSize', 12, 'FontWeight', 'Bold')

title('(4)滤波器模频特性', 'FontSize', 12, 'FontWeight', 'Bold'); grid;

axis([0 1 -110 10]); set(a4, 'LineWidth', 2);

set(gca,'XTickMode','manual','XTick',[0;0.333;0.667;1])

set(gca,'XTickLabelMode','manual', 'XTickLabels',[' 0 ';'1/3';'2/3';' 1 '])

set(gca,'YTickMode','manual','YTick',[-60,0])

set(gca,'YTickLabelMode','manual','YTickLabels',['60';' 0'])

function [y1]=cb06(a1, a2, x, k)

Nx=length(x); nx=0:Nx-1;

nx1=-Nx:2*Nx-1; x1=x(mod(nx1,Nx)+1);

[y1,ny1]=sigshift(x1,nx1,k); % 2 是移位点数

RN=(nx1>=0) & (nx1

RN1=(ny1>=0) & (ny1

subplot(a1), stem([-6,-5,ny1],[3,2,y1],'.');

xlabel('n', 'fontsize', 12); ylabel('y1(n)', 'fontsize', 12);

title('(3) 移位周期序列 y1'); grid;

axis([-6,12,0,10]);

subplot(a2), stem([-6,-5,ny1],[0,0,RN1.*y1],'.'); % stem([-6,-5,ny1],[0,0,RN1.*y1],'.');

xlabel('n', 'fontsize', 12); ylabel('y(n)', 'fontsize', 12);

title('(4) 主值序列 y'); grid;

axis([-6,12,0,10])