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

📄 bbfilter.m

📁 利用matlab实现的Fir滤波器设计程序。程序有较好的运行界面
💻 M
📖 第 1 页 / 共 2 页
字号:
%--------------------------------------------------------------------------
%利用kaiser窗设计带阻滤波器
%默认输入参数:   N=64
%                beta=5.568  
%                wl=0.2
%                wh=0.8
%输出参数:      低通带边界(wlp)
%               低阻带边界(wls)
%               高通带边界(whp)
%               高阻带边界(whs)
%               通带波纹
%               阻带衰减
%--------------------------------------------------------------------------
function varargout = bbfilter(varargin)
% BBFILTER M-file for bbfilter.fig
%      BBFILTER, by itself, creates a new BBFILTER or raises the existing
%      singleton*.
%
%      H = BBFILTER returns the handle to a new BBFILTER or the handle to
%      the existing singleton*.
%
%      BBFILTER('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in BBFILTER.M with the given input arguments.
%
%      BBFILTER('Property','Value',...) creates a new BBFILTER or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before bbfilter_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to bbfilter_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 bbfilter

% Last Modified by GUIDE v2.5 29-Jun-2007 13:18:51

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @bbfilter_OpeningFcn, ...
                   'gui_OutputFcn',  @bbfilter_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 bbfilter is made visible.
function bbfilter_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 bbfilter (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes bbfilter 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_wl,'string','0.2');
set(handles.edit_wh,'string','0.8');
%--------------------------------------------------------------------------
%由设置的初始值求取滤波器的理想单位脉冲响应
handles.wl=0.2*pi;
handles.wh=0.8*pi;
handles.M=64;
handles.hd=ideal_bb(handles.wl,handles.wh,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.wl = str2double(get(handles.edit_wl,'String'))*pi;
handles.wh = str2double(get(handles.edit_wh,'String'))*pi;
%求取滤波器的理想单位脉冲响应
handles.hd=ideal_bb(handles.wl,handles.wh,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_wkai);
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_h);
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)
[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_bb);
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
%计算通带边界wlp,whp,阻带边界wls,whs
w_w=(As-7.95)/((handles.M-1)*14.36)*2;
wls=0.5*(w_w+2*handles.wl/pi);
wlp=0.5*(2*handles.wl/pi-w_w);
whp=0.5*(w_w+2*handles.wh/pi);
whs=0.5*(2*handles.wh/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',wlp);
set(handles.edit_wlp,'string',str);
str=sprintf('%.2f',wls);
set(handles.edit_wls,'string',str);
str=sprintf('%.2f',whp);
set(handles.edit_whp,'string',str);
str=sprintf('%.2f',whs);
set(handles.edit_whs,'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_bb(wl,wh,M);
alpha=(M-1)/2;
n = [0:1:(M-1)];
m=n-alpha+eps;
hd=sin(wl*m)./(pi*m)+sin(pi*m)./(pi*m)-sin(wh*m)./(pi*m);
% --- Outputs from this function are returned to the command line.
function varargout = bbfilter_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes during object creation, after setting all properties.
function edit_N_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_N (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
function edit_N_Callback(hObject, eventdata, handles)
% hObject    handle to edit_N (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_N as text
%        str2double(get(hObject,'String')) returns contents of edit_N as a double
% --- Executes during object creation, after setting all properties.
function edit_beta_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_beta (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');

⌨️ 快捷键说明

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