您好,欢迎来到化拓教育网。
搜索
您的当前位置:首页利用kaiser窗设计低通滤波器程序

利用kaiser窗设计低通滤波器程序

来源:化拓教育网
%-------------------------------------------------------------------------- %利用kaiser窗设计低通滤波器m文件 %默认输入参数: N=

% beta=5.568 % wc=0.5pi

%输出参数: 通带边界(wp) % 阻带边界(ws) % 通带波纹 % 阻带衰减

%-------------------------------------------------------------------------- function varargout = lpfilter(varargin) % LPFILTER M-file for lpfilter.fig

% LPFILTER, by itself, creates a new LPFILTER or raises the existing % singleton*.

%

% H = LPFILTER returns the handle to a new LPFILTER or the handle to % the existing singleton*. %

% LPFILTER('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in LPFILTER.M with the given input arguments. % % % % %

LPFILTER('Property','Value',...) creates a new LPFILTER or raises the existing singleton*. Starting from the left, property value pairs are applied to the GUI before lpfilter_OpeningFunction gets called. An unrecognized property name or invalid value makes property application

% stop. All inputs are passed to lpfilter_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 lpfilter

% Last Modified by GUIDE v2.5 29-Jun-2007 13:21:40

% Begin initialization code - DO NOT EDIT gui_Singleton = 1;

gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @lpfilter_OpeningFcn, ... 'gui_OutputFcn', @lpfilter_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 lpfilter is made visible.

function lpfilter_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 lpfilter (see VARARGIN)

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

% Update handles structure guidata(hObject, handles);

% UIWAIT makes lpfilter wait for user response (see UIRESUME) % uiwait(handles.figure1);

%-------------------------------------------------------------------------- %设置滤波器长度,beta和截止频率编辑框的初始值 %设置窗体的标题

set(handles.figure1,'name','FIR低通滤波器'); set(handles.edit_N,'string','');

set(handles.edit_beta,'string','5.568');

set(handles.edit_fc,'string','0.5');

%-------------------------------------------------------------------------- %由设置的初始值求取滤波器的理想单位脉冲响应 handles.wc=0.5*pi; handles.M=;

handles.hd=ideal_lp(handles.wc,handles.M);

%-------------------------------------------------------------------------- %绘制滤波器的理想单位脉冲响应 axes(handles.axes_hd); n=[0:1:handles.M-1];

plot(n,handles.hd);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55

title('滤波器理想脉冲响应');

axis([0 handles.M-1 min(handles.hd) max(handles.hd)+0.01]); 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_fc,'String'))*pi; %求取滤波器的理想单位脉冲响应 handles.hd=ideal_lp(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_wkaiser); 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_lp); 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; ws=0.5*(w_w+2*handles.wc/pi); wp=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);

%-------------------------------------------------------------------------- %退出程序

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_lp(wc,M); alpha=(M-1)/2; n = [0:1:(M-1)];

m=n-alpha+eps;

hd=sin(wc*m)./(pi*m);

% --- Outputs from this function are returned to the command line. function varargout = lpfilter_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'); else

set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

function edit_beta_Callback(hObject, eventdata, handles) % hObject handle to edit_beta (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_beta as text

% str2double(get(hObject,'String')) returns contents of edit_beta as a double

% --- Executes during object creation, after setting all properties. function edit_fc_CreateFcn(hObject, eventdata, handles) % hObject handle to edit_fc (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_fc_Callback(hObject, eventdata, handles)

% hObject handle to edit_fc (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_fc as text

% str2double(get(hObject,'String')) returns contents of edit_fc as a double

% --- Executes during object creation, after setting all properties. function edit_ws_CreateFcn(hObject, eventdata, handles) % hObject handle to edit_ws (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_ws_Callback(hObject, eventdata, handles)

% hObject handle to edit_ws (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_ws as text

% str2double(get(hObject,'String')) returns contents of edit_ws as a double

% --- Executes during object creation, after setting all properties. function edit_wp_CreateFcn(hObject, eventdata, handles)

% hObject handle to edit_wp (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_wp_Callback(hObject, eventdata, handles) % hObject handle to edit_wp (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_wp as text

% str2double(get(hObject,'String')) returns contents of edit_wp as a double

% --- Executes during object creation, after setting all properties. function edit_rp_CreateFcn(hObject, eventdata, handles)

% hObject handle to edit_rp (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_rp_Callback(hObject, eventdata, handles) % hObject handle to edit_rp (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_rp as text

% str2double(get(hObject,'String')) returns contents of edit_rp as a double

% --- Executes during object creation, after setting all properties. function edit_As_CreateFcn(hObject, eventdata, handles) % hObject handle to edit_As (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_As_Callback(hObject, eventdata, handles) % hObject handle to edit_As (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_As as text

% str2double(get(hObject,'String')) returns contents of edit_As as a double

% --- Executes on button press in lp_radiobutton.

function lp_radiobutton_Callback(hObject, eventdata, handles) % hObject handle to lp_radiobutton (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of lp_radiobutton

% --- Executes on button press in hp_lradiobutton.

function hp_lradiobutton_Callback(hObject, eventdata, handles) % hObject handle to hp_lradiobutton (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of hp_lradiobutton close(handles.figure1); hpfilter

% --- Executes on button press in bp_lradiobutton.

function bp_lradiobutton_Callback(hObject, eventdata, handles) % hObject handle to bp_lradiobutton (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of bp_lradiobutton close(handles.figure1); bpfilter

% --- Executes on button press in bb_lradiobutton.

function bb_lradiobutton_Callback(hObject, eventdata, handles) % hObject handle to bb_lradiobutton (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of bb_lradiobutton

close(handles.figure1); bbfilter

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo9.cn 版权所有 赣ICP备2023008801号-1

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务