⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hpfilter.m

📁 利用matlab实现的Fir滤波器设计程序。程序有较好的运行界面
💻 M
📖 第 1 页 / 共 2 页
字号:
%--------------------------------------------------------------------------
%利用kaiser窗设计高通滤波器m文件
%直接运行进入GUI界面
%默认输入参数:   N=64
%                beta=5.568  
%                wc=0.5pi
%输出参数:      通带边界
%               阻带边界
%               通带波纹
%               阻带衰减
%--------------------------------------------------------------------------
function varargout = hpfilter(varargin)
% HPFILTER M-file for hpfilter.fig
%      HPFILTER, by itself, creates a new HPFILTER or raises the existing
%      singleton*.
%
%      H = HPFILTER returns the handle to a new HPFILTER or the handle to
%      the existing singleton*.
%
%      HPFILTER('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in HPFILTER.M with the given input arguments.
%
%      HPFILTER('Property','Value',...) creates a new HPFILTER or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before hpfilter_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to hpfilter_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help hpfilter

% Last Modified by GUIDE v2.5 29-Jun-2007 12:48:22

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @hpfilter_OpeningFcn, ...
                   'gui_OutputFcn',  @hpfilter_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before hpfilter is made visible.
function hpfilter_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to hpfilter (see VARARGIN)

% Choose default command line output for hpfilter
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes hpfilter wait for user response (see UIRESUME)
% uiwait(handles.figure1);
%--------------------------------------------------------------------------
%设置滤波器长度,beta和截止频率编辑框的初始值
%设置窗体的标题
set(handles.figure1,'name','FIR高通滤波器');
set(handles.edit_N,'string','64');
set(handles.edit_beta,'string','5.568');
set(handles.edit_wc,'string','0.5');
%%%%%%%%%%%
%set(handles.hp_hradio,'valve','max');
%%%%%%%%%%%
%--------------------------------------------------------------------------
%由设置的初始值求取滤波器的理想单位脉冲响应
handles.wc=0.5*pi;
handles.M=64;
handles.hd=ideal_hp(handles.wc,handles.M);
%--------------------------------------------------------------------------
%绘制滤波器的理想单位脉冲响应
axes(handles.axes_hd);
n=[0:1:handles.M-1];
plot(n,handles.hd);
title('滤波器理想脉冲响应');
axis([0 handles.M-1 min(handles.hd) max(handles.hd)]);
xlabel('n');
ylabel('hd(n)');
grid on;
guidata(hObject,handles);

% --- Executes on button press in plot.
function plot_Callback(hObject, eventdata, handles)
% hObject    handle to plot (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%--------------------------------------------------------------------------
%从编辑框获取滤波器长度,beta和截止频率的值
handles.M = str2double(get(handles.edit_N,'String'));
handles.beta = str2double(get(handles.edit_beta,'String'));
handles.wc = str2double(get(handles.edit_wc,'String'))*pi;
%求取滤波器的理想单位脉冲响应
handles.hd=ideal_hp(handles.wc,handles.M);
%绘制滤波器的理想单位脉冲响应
axes(handles.axes_hd);
plot(0,0);
n=[0:1:handles.M-1];
plot(n,handles.hd);
title('滤波器理想脉冲响应');
axis([0 handles.M-1 min(handles.hd) max(handles.hd)]);
xlabel('n');
ylabel('hd(n)');
grid on;
%--------------------------------------------------------------------------
%求取kaiser窗函数
handles.w_kai=(kaiser(handles.M,handles.beta))';
%绘制kaiser窗函数
axes(handles.axes_kaiser);
plot(n,handles.w_kai);
title('kaiser窗');
axis([0 handles.M-1 min(handles.w_kai) max(handles.w_kai)]);
xlabel('n');
ylabel('w(n)');
grid on;
%--------------------------------------------------------------------------
%求取滤波器的实际单位脉冲响应
handles.h=handles.hd.*handles.w_kai;
%绘制滤波器的实际单位脉冲响应
axes(handles.axes_hn);
plot(n,handles.h);
title('滤波器实际脉冲响应');
axis([0 handles.M-1 min(handles.h) max(handles.h)]);
xlabel('n');
ylabel('h(n)');
grid on;
%--------------------------------------------------------------------------
%求取滤波器的频率特性,并绘制幅频特性(转换到0—pi)
% figure;
% freqz(handles.h,[1],handles.M*6,1);
[H,w] = freqz(handles.h,[1],handles.M*6,'whole');
% freqz(handles.h,[1],handles.M*6);
H=(H(1:1:handles.M*3))';
w=(w(1:1:handles.M*3))';
mag=abs(H);
db=20*log10((mag+eps)/max(mag));
% pha=angle(H);
%绘制幅频特性(转换到0—pi)
axes(handles.axes_hp);
plot(w/pi,db);
title('FIR滤波器的幅频特性(0-pi)');
axis([0 1 min(db) max(db)]);
xlabel('Frequency in pi units');
ylabel('Decibels');
grid on;
guidata(hObject,handles);
%--------------------------------------------------------------------------
%由求得幅频特性计算通带边界,阻带边界,通带纹波和阻带衰减
%由beta值计算阻带衰减As
if handles.beta<4.5513
    As=fzero(@myfun,20,[],handles.beta)+21;
else
    As=handles.beta/0.1102+8.7;
end
%计算通带边界wp,阻带边界ws
w_w=(As-7.95)/((handles.M-1)*14.36)*2;
wp=0.5*(w_w+2*handles.wc/pi);
ws=0.5*(2*handles.wc/pi-w_w);
% mag=(mag+eps)/max(mag);
%计算通带纹波rp
% deta_w=2*pi/(handles.M*6);
% w_temp=mag(floor(ws/deta_w));
% mag_temp=abs(mag(1:floor(ws/deta_w))-w_temp);
rp=20*log10(max(mag));
%--------------------------------------------------------------------------
%在相应的编辑框中显示通带边界,阻带边界,通带纹波和阻带衰减
str=sprintf('%.2f',wp);
set(handles.edit_wp,'string',str);
str=sprintf('%.2f',ws);
set(handles.edit_ws,'string',str);
str=sprintf('%.4f',rp);
set(handles.edit_rp,'string',str);
str=sprintf('%.2f',-As);
set(handles.edit_As,'string',str);

% --- Executes on button press in exit.
function exit_Callback(hObject, eventdata, handles)
% hObject    handle to exit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close(handles.figure1);

%--------------------------------------------------------------------------
%自定义函数,计算理想高通滤波器的单位脉冲响应
function hd=ideal_hp(wc,M);
alpha=(M-1)/2;
n = [0:1:(M-1)];
m=n-alpha+eps;
hd=sin(pi*m)./(pi*m)-sin(wc*m)./(pi*m);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -