【滤波器】基于FIR+IIR滤波器实现音频信号去噪含Matlab源码
1 简介
针对传统的滤波器设计方法效率低,方法复杂,不能满足高效高精度的需要等缺点,基于 MATLAB 研究了分别使用窗函数法和双线性变换法的 FIR 和 IIR 滤波器.将加入噪声的信号分别通过两种滤波器,滤除加入的噪声,对滤波前后的信号进行对比分析.通过仿真实验表明, FIR 滤波器与 IIR 的 Butterworth 滤波器都能很好地克服传统滤波器的不足,通过语谱图直观地对比发现基于窗函数法设计 FIR 滤波器比双线性法设计的 Butterworth 滤波器能更好地达到预定的去噪效果.
2 部分代码
function varargout = filter_2(varargin)% FILTER_2 MATLAB code for filter_2.fig% FILTER_2, by itself, creates a new FILTER_2 or raises the existing% singleton*.%% H = FILTER_2 returns the handle to a new FILTER_2 or the handle to% the existing singleton*.%% FILTER_2('CALLBACK',hObject,eventData,handles,...) calls the local% function named CALLBACK in FILTER_2.M with the given input arguments.%% FILTER_2('Property','Value',...) creates a new FILTER_2 or raises the% existing singleton*. Starting from the left, property value pairs are% applied to the GUI before filter_2_OpeningFcn gets called. An% unrecognized property name or invalid value makes property application% stop. All inputs are passed to filter_2_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 filter_2% Last Modified by GUIDE v2.5 16-May-2013 12:02:23% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @filter_2_OpeningFcn, ... 'gui_OutputFcn', @filter_2_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []);if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1});endif 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 filter_2 is made visible.function filter_2_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 filter_2 (see VARARGIN)global response;global analog;global window1;global digital;global analog_choose;response=1;analog=1;analog_choose=1;window1=1;digital=1;%set(handles.function_value,'String','Butterworth');%set(handles.method_value,'String','direct form');set(handles.order,'Enable','off');% Choose default command line output for filter_2handles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes filter_2 wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.function varargout = filter_2_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 structurevarargout{1} = handles.output;% --- Executes on button press in IIR_button.function IIR_button_Callback(hObject, eventdata, handles)% hObject handle to IIR_button (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global analog_choose;global response;global analog;if(get(hObject,'Value')==1) %当选中IIR时屏蔽FIR模块 set(handles.order_information,'String','order'); set(handles.FIR_button,'Value',0); set(handles.filter_information,'String','IIR'); set(handles.window_function,'Enable','off'); set(handles.order,'Enable','off'); set(handles.view_window,'Enable','off'); set(handles.analog_filter,'Enable','on'); set(handles.Apass,'Enable','on'); set(handles.Astop,'Enable','on'); set(handles.order,'Enable','off'); set(handles.specify_order,'Enable','on'); set(handles.specify_order,'Value',0);%对阶数的处理 set(handles.custom_order,'Enable','on');set(handles.custom_order,'String',''); set(handles.min_order,'Enable','on');set(handles.min_order,'Value',0); %get(handles.analog_method,'Value'); if( analog_choose==1) set(handles.method_value,'String','direct form'); else set(handles.method_value,'String','bilinear'); end switch(analog) case 1 set(handles.function_value,'String','Butterworth'); case 2 set(handles.function_value,'String','chebyshev1'); case 3 set(handles.function_value,'String','chebyshev2'); end switch(response) case 1 set(handles.type_information,'String','Lowpass'); case 2 set(handles.type_information,'String','Highpass'); case 3 set(handles.type_information,'String','Bandpass'); case 4 set(handles.type_information,'String','Bandstop'); endelse set(handles.window_function,'Enable','on'); set(handles.order,'Enable','on'); set(handles.view_window,'Enable','on'); set(handles.order,'Enable','on');end% Hint: get(hObject,'Value') returns toggle state of IIR_button% --- Executes on button press in FIR_button.function FIR_button_Callback(hObject, eventdata, handles)% hObject handle to FIR_button (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global digital;global response;global window1;if(get(hObject,'Value')==1) %当选中FIR时屏蔽IIR模块 set(handles.IIR_button,'Value',0); set(handles.filter_information,'String','FIR'); set(handles.window_function,'Enable','on'); % set(handles.order,'Enable','on'); set(handles.view_window,'Enable','on'); set(handles.analog_filter,'Enable','off'); set(handles.Apass,'Enable','off'); set(handles.Astop,'Enable','off'); %set(handles.order,'Enable','on'); if(digital==2)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%处理阶数 set(handles.specify_order,'Enable','off'); set(handles.custom_order,'Enable','off'); set(handles.min_order,'Enable','off'); set(handles.window_function,'Enable','off'); set(handles.view_window,'Enable','off'); set(handles.order_information,'String','sample') set(handles.order,'Enable','on'); end switch(response) case 1 set(handles.type_information,'String','Lowpass'); case 2 set(handles.type_information,'String','Highpass'); case 3 set(handles.type_information,'String','Bandpass'); case 4 set(handles.type_information,'String','Bandstop'); end if(digital==1) set(handles.method_value,'String','window'); else set(handles.method_value,'String','FSI'); end switch(window1) case 1 set(handles.function_value,'String','Hamming'); case 2 set(handles.function_value,'String','Blackman'); case 3 set(handles.function_value,'String','Hann'); case 4 set(handles.function_value,'String','Rectangular'); endelse set(handles.analog_filter,'Enable','on'); set(handles.Apass,'Enable','on'); set(handles.Astop,'Enable','on'); set(handles.order,'Enable','off'); end% Hint: get(hObject,'Value') returns toggle state of FIR_button% --- If Enable == 'on', executes on mouse press in 5 pixel border.% --- Otherwise, executes on mouse press in 5 pixel border or over filter_information.function filter_information_ButtonDownFcn(hObject, eventdata, handles)% hObject handle to filter_information (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)function Fs_edit_Callback(hObject, eventdata, handles)% hObject handle to Fs_edit (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 Fs_edit as text% str2double(get(hObject,'String')) returns contents of Fs_edit as a double% --- Executes during object creation, after setting all properties.function Fs_edit_CreateFcn(hObject, eventdata, handles)% hObject handle to Fs_edit (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');endfunction Fstop1_edit_Callback(hObject, eventdata, handles)% hObject handle to Fstop1_edit (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 Fstop1_edit as text% str2double(get(hObject,'String')) returns contents of Fstop1_edit as a double% --- Executes during object creation, after setting all properties.function Fstop1_edit_CreateFcn(hObject, eventdata, handles)% hObject handle to Fstop1_edit (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');endfunction Fpass1_edit_Callback(hObject, eventdata, handles)% hObject handle to Fpass1_edit (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 Fpass1_edit as text% str2double(get(hObject,'String')) returns contents of Fpass1_edit as a double% --- Executes during object creation, after setting all properties.function Fpass1_edit_CreateFcn(hObject, eventdata, handles)% hObject handle to Fpass1_edit (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');endfunction Fpass2_edit_Callback(hObject, eventdata, handles)% hObject handle to Fpass2_edit (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 Fpass2_edit as text% str2double(get(hObject,'String')) returns contents of Fpass2_edit as a double% --- Executes during object creation, after setting all properties.function Fpass2_edit_CreateFcn(hObject, eventdata, handles)% hObject handle to Fpass2_edit (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');endfunction Fstop2_edit_Callback(hObject, eventdata, handles)% hObject handle to Fstop2_edit (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 Fstop2_edit as text% str2double(get(hObject,'String')) returns contents of Fstop2_edit as a double% --- Executes during object creation, after setting all properties.function Fstop2_edit_CreateFcn(hObject, eventdata, handles)% hObject handle to Fstop2_edit (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');end% --- Executes on selection change in filter_type.function filter_type_Callback(hObject, eventdata, handles)% hObject handle to filter_type (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global response;val=get(hObject,'Value');switch val case 1 set(handles.type_information,'String','Lowpass'); set(handles.Fstop1,'String','Fstop'); set(handles.Fpass1,'String','Fpass'); set(handles.Fpass2,'Visible','off');%当为低通滤波器时屏蔽Fpass2 Fstop2并把Fstop1 Fpass1设为Fstop Fpass set(handles.Fstop2,'Visible','off'); set(handles.Fpass2_edit,'Visible','off'); set(handles.Fstop2_edit,'Visible','off'); response=1; case 2 set(handles.type_information,'String','Highpass'); set(handles.Fstop1,'String','Fstop'); set(handles.Fpass1,'String','Fpass'); set(handles.Fpass2,'Visible','off');%当为高通滤波器时屏蔽Fpass2 Fstop2并把Fstop1 Fpass1设为Fstop Fpass set(handles.Fstop2,'Visible','off'); set(handles.Fpass2_edit,'Visible','off'); set(handles.Fstop2_edit,'Visible','off'); response=2; case 3 set(handles.type_information,'String','Bandpass'); set(handles.Fstop1,'String','Fstop1'); set(handles.Fpass1,'String','Fpass1'); set(handles.Fpass2,'Visible','on');%当为高通滤波器时屏蔽Fpass2 Fstop2并把Fstop1 Fpass1设为Fstop Fpass set(handles.Fstop2,'Visible','on'); set(handles.Fpass2_edit,'Visible','on'); set(handles.Fstop2_edit,'Visible','on'); response=3; case 4 set(handles.type_information,'String','Bandstop'); set(handles.Fstop1,'String','Fstop1'); set(handles.Fpass1,'String','Fpass1'); set(handles.Fpass2,'Visible','on');%当为高通滤波器时屏蔽Fpass2 Fstop2并把Fstop1 Fpass1设为Fstop Fpass set(handles.Fstop2,'Visible','on'); set(handles.Fpass2_edit,'Visible','on'); set(handles.Fstop2_edit,'Visible','on'); response=4; otherwise set(handles.type_information,'String','Lowpass'); set(handles.Fstop1,'String','Fstop'); set(handles.Fpass1,'String','Fpass'); set(handles.Fpass2,'Visible','off');%当为低通滤波器时屏蔽Fpass2 Fstop2并把Fstop1 Fpass1设为Fstop Fpass set(handles.Fstop2,'Visible','off'); set(handles.Fpass2_edit,'Visible','off'); set(handles.Fstop2_edit,'Visible','off'); response=1;endguidata(hObject, handles);% Hints: contents = cellstr(get(hObject,'String')) returns filter_type contents as cell array% contents{get(hObject,'Value')} returns selected item from filter_type% --- Executes during object creation, after setting all properties.function filter_type_CreateFcn(hObject, eventdata, handles)% hObject handle to filter_type (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: popupmenu controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');end% --- Executes on selection change in analog_filter.function analog_filter_Callback(hObject, eventdata, handles)% hObject handle to analog_filter (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global analog;val_1=get(hObject,'Value');switch val_1 case 1 %set(handles.analog_filter,'String','Butterworth'); analog=1;set(handles.function_value,'String','Butterworth');%反馈IIR滤波器设计所用到的函数 case 2 set(handles.function_value,'String','ChebyshevI'); analog=2; case 3 set(handles.function_value,'String','ChebyshevII'); analog=3; otherwise set(handles.function_value,'String','Butterworth'); analog=1; endguidata(hObject, handles);% Hints: contents = cellstr(get(hObject,'String')) returns analog_filter contents as cell array% contents{get(hObject,'Value')} returns selected item from analog_filter% --- Executes during object creation, after setting all properties.function analog_filter_CreateFcn(hObject, eventdata, handles)% hObject handle to analog_filter (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: popupmenu controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');end% --- Executes on selection change in digital_filter.function digital_filter_Callback(hObject, eventdata, handles)% hObject handle to digital_filter (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global digital;val_2=get(hObject,'Value');set(handles.order_information,'String','order');switch val_2 case 1 % set(handles.digital_filter,'String','window'); digital=1; set(handles.method_value,'String','window function');%反馈IIR滤波器的方法 set(handles.view_window,'Enable','on'); set(handles.window_function,'Enable','on'); set(handles.specify_order,'Enable','on'); set(handles.specify_order,'Value',0); set(handles.custom_order,'Enable','on');set(handles.custom_order,'String',''); set(handles.min_order,'Enable','on');set(handles.min_order,'Value',0); if(get(handles.FIR_button,'Value')) set(handles.order,'Enable','off');end; case 2 % set(handles.digital_filter,'String','frequency response invariance'); digital=2; set(handles.method_value,'String','FSI'); set(handles.view_window,'Enable','off'); set(handles.window_function,'Enable','off'); set(handles.order,'Enable','on'); if(get(handles.FIR_button,'Value')==1) set(handles.specify_order,'Enable','off'); set(handles.custom_order,'Enable','off'); set(handles.min_order,'Enable','off'); set(handles.window_function,'Enable','off'); set(handles.view_window,'Enable','off'); set(handles.order_information,'String','sample'); end otherwise % set(handles.digital_filter,'String','window'); digital=1;set(handles.method_value,'String','window function');endguidata(hObject, handles);% Hints: contents = cellstr(get(hObject,'String')) returns digital_filter contents as cell array% contents{get(hObject,'Value')} returns selected item from digital_filter% --- Executes during object creation, after setting all properties.function digital_filter_CreateFcn(hObject, eventdata, handles)% hObject handle to digital_filter (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: popupmenu controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');end% --- Executes on selection change in window_function.function window_function_Callback(hObject, eventdata, handles)global window1;val_4=get(hObject,'Value');switch val_4 case 1 window1=1;set(handles.function_value,'String','Hamming');%反馈函数信息 case 2 window1=2; set(handles.function_value,'String','Blackman'); case 3 window1=3;set(handles.function_value,'String','Hann'); case 4 window1=4; set(handles.function_value,'String','Rectangular'); otherwise % set(handles.digital_filter,'String','window'); window1=1;endguidata(hObject, handles);% hObject handle to window_function (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: contents = cellstr(get(hObject,'String')) returns window_function contents as cell array% contents{get(hObject,'Value')} returns selected item from window_function% --- Executes during object creation, after setting all properties.function window_function_CreateFcn(hObject, eventdata, handles)% hObject handle to window_function (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: popupmenu controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');end% --- Executes on button press in view_window.function Apass_Callback(hObject, eventdata, handles)% hObject handle to Apass (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 Apass as text% str2double(get(hObject,'String')) returns contents of Apass as a double% --- Executes during object creation, after setting all properties.function Apass_CreateFcn(hObject, eventdata, handles)% hObject handle to Apass (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');endfunction Astop_Callback(hObject, eventdata, handles)% hObject handle to Astop (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB
3 仿真结果


4 参考文献
[1]洪灿梅, 刘爱莲, 刘名扬,等. FIR滤波器与IIR滤波器去噪效果对比研究[J]. 微型机与应用, 2015, 34(21):4.
博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。
部分理论引用网络文献,若有侵权联系博主删除。


