├── LICENSE ├── README.md ├── yanshou.fig └── yanshou.m /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 fzzfbyx 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 数字信号处理音频FIR去噪滤波器(基于MATLAB GUI的开发) 2 | 3 | ## 1、内容简介 4 |        利用MATLAB GUI设计平台,用窗函数法设计FIR数字滤波器,对所给出的含有噪声的声音信号进行数字滤波处理,得到降噪的声音信号,进行时域频域分析,同时分析不同窗函数的效果。将文件解压至一个目录下,运行m文件即可使用。 5 | ## 2、函数使用 6 | 读取.wav音频文件函数:audioread();(老版本为wavread) 7 | MATLAB播放音乐函数:sound(); 8 | MATLAB停止播放音乐:clear sound 9 | 写入.wav音频文件函数:audiowrite();(老版本为audiowrite) 10 | 加入白噪声:noise=(max(x(:,1))/5)*randn(x,2); 11 |                       y=x+noise; 12 | 频谱分析:    fft(); 13 |                       fftshift(); 14 | Fir滤波:       fir1(n,Wn,ftype,window); 15 | 窗函数选择: 16 | 梯形窗boxcar 17 | 三角窗triang 18 | 海明窗hamming 19 | 汉宁窗hanning 20 | 布莱克曼窗blackman 21 | 凯塞窗kaiser 22 | 23 | ## 3、实现功能 24 | ![**音频滤波GUI界面**](https://img-blog.csdnimg.cn/20200423205510136.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNjc5NTcz,size_16,color_FFFFFF,t_70) 25 | 实现的功能有: 26 | 27 | 1、打开文件:选择路径打开wav格式的音频文件,自动生成音频的原始波形与频谱。 28 | 29 | 2、加入噪声:有两种噪声可以选择加入,一种是白噪声,其频率蔓延整个频谱;一种是特定频率的噪声,可通过输入频率加入单一频率的噪声。加入噪声后自动绘制加入噪声后的波形与频谱。 30 | 31 | 3、滤波处理:首先输入滤波器通/阻带的开始频率与截止频率(若为低/高通类型滤波,则只需输入开始频率;若为带通/阻类型,则开始与截止都要输入;输入频率值为真实频率值,可根据频谱图进行判断 ),之后选取窗函数和滤波类型,将会生成滤波处理后的波形与频谱。 32 | 33 | 4、音频播放/停止:可随时播放/停止原始、加噪、滤波处理后的音频。 34 | 35 | 5、图片导出:将波形、频谱图片一张张导出保存,可选的格式有jpg、png、bmp、eps。 36 | 37 | 6、保存文件:将加躁/滤波后的音频导出保存。 38 | ## 4、操作实例 39 | 选取音乐“卢本伟语音包”,转换为wav格式导入,得到结果如下 40 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200423205724129.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNjc5NTcz,size_16,color_FFFFFF,t_70) 41 | 添加白噪声后,此时播放音频能听到显著杂音。而从原始信号的频谱来看,初始音频的频率主要集中在0-1000Hz,因此我们可以选用低通滤波器,阻带开始频率设为1000Hz,选用矩形窗进行滤波,得到结果如下:![在这里插入图片描述](https://img-blog.csdnimg.cn/20200423210029364.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNjc5NTcz,size_16,color_FFFFFF,t_70) 42 | 由于白噪声遍布于整个频谱,对于噪声频谱于音频频谱的重叠部分,我们无法通过FIR滤波器进行滤除,依然会有小部分杂音存在。若噪声为特定单一频率的噪声,我们可以较好地将其去除。对于该音频添加5000Hz的特定频率,通过设计带阻滤波器,阻带范围为4500-5500Hz对其进行滤波,如下图所示: 43 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200423210122656.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNjc5NTcz,size_16,color_FFFFFF,t_70) 44 | ## 5、窗函数对比 45 | 仍选用上例中的5000Hz频率噪声,同时增加噪声幅度,如下图所示:![在这里插入图片描述](https://img-blog.csdnimg.cn/20200423210657818.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNjc5NTcz,size_16,color_FFFFFF,t_70) 46 | 选用带阻滤波器,设置阻带范围4000-6000Hz,观察各窗函数对其滤波的效果。(每行从左到右分别是:矩形窗,三角窗,海明窗,汉宁窗,布莱克曼窗,凯塞窗,下同) 47 | 48 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200423215240121.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNjc5NTcz,size_16,color_FFFFFF,t_70) 49 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200423215535706.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNjc5NTcz,size_16,color_FFFFFF,t_70) 50 | 51 | 该种情况下滤波效果的总体排序为:凯瑟窗>矩形窗>汉宁窗>海明窗=三角窗>布莱克曼窗。 52 | ## 6、声明 53 | 本项目是本人基于其他滤波GUI设计的二次开发,大部分内容均为原创,若有侵权请及时告知。 54 | 55 | CSDN链接:https://blog.csdn.net/qq_42679573/article/details/105716092 -------------------------------------------------------------------------------- /yanshou.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fzzfbyx/Audio-FIR-denoising-filter-MATLAB_GUI/9b61bb2759a52f64a532e2d4b2853b0aa9fb3cca/yanshou.fig -------------------------------------------------------------------------------- /yanshou.m: -------------------------------------------------------------------------------- 1 | function varargout = yanshou(varargin) 2 | % YANSHOU M-file for yanshou.fig 3 | % YANSHOU, by itself, creates a new YANSHOU or raises the existing 4 | % singleton*. 5 | % 6 | % H = YANSHOU returns the handle to a new YANSHOU or the handle to 7 | % the existing singleton*. 8 | % 9 | % YANSHOU('CALLBACK',hObject,eventData,handles,...) calls the local 10 | % function named CALLBACK in YANSHOU.M with the given input arguments. 11 | % 12 | % YANSHOU('Property','Value',...) creates a new YANSHOU or raises the 13 | % existing singleton*. Starting from the left, property value pairs are 14 | % applied to the GUI before yanshou_OpeningFcn gets called. An 15 | % unrecognized property name or invalid value makes property application 16 | % stop. All inputs are passed to yanshou_OpeningFcn via varargin. 17 | % 18 | % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one 19 | % instance to run (singleton)". 20 | % 21 | % See also: GUIDE, GUIDATA, GUIHANDLES 22 | 23 | % Edit the above text to modify the response to help yanshou 24 | 25 | % Last Modified by GUIDE v2.5 23-Apr-2020 14:20:34 26 | 27 | % Begin initialization code - DO NOT EDIT 28 | gui_Singleton = 1; 29 | gui_State = struct('gui_Name', mfilename, ... 30 | 'gui_Singleton', gui_Singleton, ... 31 | 'gui_OpeningFcn', @yanshou_OpeningFcn, ... 32 | 'gui_OutputFcn', @yanshou_OutputFcn, ... 33 | 'gui_LayoutFcn', [] , ... 34 | 'gui_Callback', []); 35 | if nargin && ischar(varargin{1}) 36 | gui_State.gui_Callback = str2func(varargin{1}); 37 | end 38 | 39 | if nargout 40 | [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); 41 | else 42 | gui_mainfcn(gui_State, varargin{:}); 43 | end 44 | % End initialization code - DO NOT EDIT 45 | 46 | 47 | % --- Executes just before yanshou is made visible. 48 | function yanshou_OpeningFcn(hObject, eventdata, handles, varargin) 49 | % This function has no output args, see OutputFcn. 50 | % hObject handle to figure 51 | % eventdata reserved - to be defined in a future version of MATLAB 52 | % handles structure with handles and user data (see GUIDATA) 53 | % varargin command line arguments to yanshou (see VARARGIN) 54 | 55 | % Choose default command line output for yanshou 56 | handles.output = hObject; 57 | 58 | % Update handles structure 59 | guidata(hObject, handles); 60 | 61 | % UIWAIT makes yanshou wait for user response (see UIRESUME) 62 | % uiwait(handles.figure1); 63 | 64 | 65 | % --- Outputs from this function are returned to the command line. 66 | function varargout = yanshou_OutputFcn(hObject, eventdata, handles) 67 | % varargout cell array for returning output args (see VARARGOUT); 68 | % hObject handle to figure 69 | % eventdata reserved - to be defined in a future version of MATLAB 70 | % handles structure with handles and user data (see GUIDATA) 71 | 72 | % Get default command line output from handles structure 73 | varargout{1} = handles.output; 74 | 75 | 76 | % --- Executes on button press in open_pushbutton1. 77 | function open_pushbutton1_Callback(hObject, eventdata, handles) 78 | % hObject handle to open_pushbutton1 (see GCBO) 79 | % eventdata reserved - to be defined in a future version of MATLAB 80 | % handles structure with handles and user data (see GUIDATA) 81 | global x; %文件 82 | global Fs; %采样频率 83 | global tl; 84 | global x2; 85 | [filename, pathname] = uigetfile('*.wav', '选择音频文件'); 86 | if isequal(filename,0) 87 | disp('User selected Cancel') 88 | else 89 | path = fullfile(pathname, filename); 90 | [handles.x,handles.Fs]=audioread(path); 91 | x=handles.x; 92 | Fs=handles.Fs; 93 | axes(handles.axes1); 94 | tl=[0:1/Fs:(length(handles.x)-1)/Fs]; %时间尺度 95 | plot(tl,handles.x); 96 | title('语音时域波形'); 97 | xlabel('时间/s'); 98 | grid on; 99 | 100 | N=length(handles.x); 101 | df=Fs/N; 102 | w=[0:df:df*(N-1)] - Fs/2; %频率尺度 103 | X=fft(handles.x); 104 | X=fftshift(X); 105 | axes(handles.axes2); 106 | plot(w,abs(X)/max(abs(X))); 107 | axis([-10000,10000,0,1]); 108 | title('语音频谱'); 109 | xlabel('频率/Hz'); 110 | grid on; 111 | x2=x; 112 | end 113 | 114 | 115 | 116 | % --- Executes on button press in play_pushbutton2. 117 | function play_pushbutton2_Callback(hObject, eventdata, handles) 118 | % hObject handle to play_pushbutton2 (see GCBO) 119 | % eventdata reserved - to be defined in a future version of MATLAB 120 | % handles structure with handles and user data (see GUIDATA) 121 | global x2; 122 | global Fs; 123 | sound(x2,Fs); 124 | 125 | 126 | % --- Executes on button press in add_pushbutton3. 127 | function add_pushbutton3_Callback(hObject, eventdata, handles) 128 | % hObject handle to add_pushbutton3 (see GCBO) 129 | % eventdata reserved - to be defined in a future version of MATLAB 130 | % handles structure with handles and user data (see GUIDATA) 131 | global x; 132 | global Fs; 133 | global tl; 134 | global x2; 135 | axes(handles.axes1); 136 | size(x); 137 | t=0:1/Fs:(length(x)-1)/Fs;%将所加噪声信号的点数调整到与原始信号相同 138 | Au=0.07; 139 | fn = get(handles.noise_edit2,'string'); %噪声频率 140 | fn = str2double(fn); 141 | noise=Au*cos(2*pi*fn*t)'; %噪声为频率 142 | x=x+noise; 143 | plot(tl,x); 144 | title('加入噪声后语音时域波形'); 145 | xlabel('时间/s'); 146 | grid on; 147 | N=length(x); 148 | df=Fs/N; 149 | w=[0:df:df*(N-1)] - Fs/2; %频率尺度 150 | X=fft(x); 151 | X=fftshift(X); 152 | axes(handles.axes2); 153 | plot(w,abs(X)/max(abs(X))); 154 | axis([-10000,10000,0,1]); 155 | title('加入噪声后语音频谱'); 156 | xlabel('频率/Hz'); 157 | grid on; 158 | x2=x; 159 | 160 | % --- Executes on button press in low_pushbutton5. 161 | function low_pushbutton5_Callback(hObject, eventdata, handles) 162 | % hObject handle to low_pushbutton5 (see GCBO) 163 | % eventdata reserved - to be defined in a future version of MATLAB 164 | % handles structure with handles and user data (see GUIDATA) 165 | 166 | global x; 167 | global Fs; 168 | global tl; 169 | global x2; 170 | 171 | x1=x; 172 | % fp=1000; 173 | % fs = 1000; 174 | % Wp = 2*fp/Fs; 175 | % Ws = 2*fs/Fs; 176 | % if(Wp >= 1) 177 | % Wp = 0.99; 178 | % end 179 | % if(Ws >= 1) 180 | % Ws = 0.99; 181 | % end 182 | fp = get(handles.edit3,'string'); 183 | fp = str2double(fp)*2; 184 | if get(handles.radiobutton1,'value') 185 | [n, Wn]=buttord(Wp,Ws, 2, 15); 186 | [b, a]=butter(n, Wn,'low'); 187 | axes(handles.axes3); 188 | [h,w]=freqz(b,a); 189 | plot(w/pi*Fs/2,abs(h)); 190 | x1=filter(b,a,x1); %调用函数滤波 191 | elseif get(handles.radiobutton4,'value') 192 | b2=fir1(30, fp/Fs, boxcar(31)); 193 | axes(handles.axes3); 194 | [h,w]=freqz(b2, 1,512); 195 | plot(w/pi*Fs/2,20*log(abs(h))); 196 | x1=fftfilt(b2,x1); 197 | elseif get(handles.radiobutton5,'value') 198 | b2=fir1(30, fp/Fs, triang(31)); 199 | axes(handles.axes3); 200 | [h,w]=freqz(b2, 1,512); 201 | plot(w/pi*Fs/2,20*log(abs(h))); 202 | x1=fftfilt(b2,x1); 203 | elseif get(handles.radiobutton6,'value') 204 | b2=fir1(30, fp/Fs, hamming(31)); 205 | axes(handles.axes3); 206 | [h,w]=freqz(b2, 1,512); 207 | plot(w/pi*Fs/2,20*log(abs(h))); 208 | x1=fftfilt(b2,x1); 209 | elseif get(handles.radiobutton7,'value') 210 | b2=fir1(30, fp/Fs, hanning(31)); 211 | axes(handles.axes3); 212 | [h,w]=freqz(b2, 1,512); 213 | plot(w/pi*Fs/2,20*log(abs(h))); 214 | x1=fftfilt(b2,x1); 215 | elseif get(handles.radiobutton8,'value') 216 | b2=fir1(30, fp/Fs, blackman(31)); 217 | axes(handles.axes3); 218 | [h,w]=freqz(b2, 1,512); 219 | plot(w/pi*Fs/2,20*log(abs(h))); 220 | x1=fftfilt(b2,x1); 221 | elseif get(handles.radiobutton9,'value') 222 | b2=fir1(30,fp/Fs, kaiser(31)); 223 | axes(handles.axes3); 224 | [h,w]=freqz(b2, 1,512); 225 | plot(w/pi*Fs/2,20*log(abs(h))); 226 | x1=fftfilt(b2,x1); 227 | end; 228 | axes(handles.axes5); 229 | plot(tl,x1); 230 | title('滤除噪声后语音时域波形'); 231 | xlabel('时间/s'); 232 | N=length(x1); 233 | df=Fs/N; 234 | w=[0:df:df*(N-1)] - Fs/2; %频率尺度 235 | X=fft(x1); 236 | X=fftshift(X); 237 | axes(handles.axes6); 238 | plot(w,abs(X)/max(abs(X))); 239 | axis([-10000,10000,0,1]); 240 | title('滤除噪声后语音频谱'); 241 | xlabel('频率/Hz'); 242 | grid on; 243 | x2=x1; 244 | 245 | 246 | 247 | 248 | % --- Executes during object creation, after setting all properties. 249 | function low_pushbutton5_CreateFcn(hObject, eventdata, handles) 250 | % hObject handle to low_pushbutton5 (see GCBO) 251 | % eventdata reserved - to be defined in a future version of MATLAB 252 | % handles empty - handles not created until after all CreateFcns called 253 | 254 | 255 | % --- Executes on button press in pushbutton6. 256 | 257 | 258 | 259 | function noise_edit2_Callback(hObject, eventdata, handles) 260 | % hObject handle to noise_edit2 (see GCBO) 261 | % eventdata reserved - to be defined in a future version of MATLAB 262 | % handles structure with handles and user data (see GUIDATA) 263 | 264 | % Hints: get(hObject,'String') returns contents of noise_edit2 as text 265 | % str2double(get(hObject,'String')) returns contents of noise_edit2 as a double 266 | 267 | 268 | % --- Executes during object creation, after setting all properties. 269 | function noise_edit2_CreateFcn(hObject, eventdata, handles) 270 | % hObject handle to noise_edit2 (see GCBO) 271 | % eventdata reserved - to be defined in a future version of MATLAB 272 | % handles empty - handles not created until after all CreateFcns called 273 | 274 | % Hint: edit controls usually have a white background on Windows. 275 | % See ISPC and COMPUTER. 276 | if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 277 | set(hObject,'BackgroundColor','white'); 278 | end 279 | 280 | 281 | % --- Executes during object creation, after setting all properties. 282 | function add_pushbutton3_CreateFcn(hObject, eventdata, handles) 283 | % hObject handle to add_pushbutton3 (see GCBO) 284 | % eventdata reserved - to be defined in a future version of MATLAB 285 | % handles empty - handles not created until after all CreateFcns called 286 | 287 | 288 | % --- Executes on button press in guass_pushbutton8. 289 | function guass_pushbutton8_Callback(hObject, eventdata, handles) 290 | % hObject handle to guass_pushbutton8 (see GCBO) 291 | % eventdata reserved - to be defined in a future version of MATLAB 292 | % handles structure with handles and user data (see GUIDATA) 293 | global x; 294 | global Fs; 295 | global tl; 296 | global x2; 297 | N=length(x); 298 | axes(handles.axes1); 299 | %x = awgn(x,15); 300 | noise=(max(x(:,1))/5)*randn(N,2); 301 | x=x+noise; 302 | plot(tl,x); 303 | title('加入噪声后语音时域波形'); 304 | xlabel('时间/s'); 305 | grid on; 306 | 307 | df=Fs/N; 308 | w=[0:df:df*(N-1)] - Fs/2; %频率尺度 309 | X=fft(x); 310 | X=fftshift(X); 311 | axes(handles.axes2); 312 | plot(w,abs(X)/max(abs(X))); 313 | axis([-10000,10000,0,1]); 314 | title('加入噪声后语音频谱'); 315 | xlabel('频率/Hz'); 316 | grid on; 317 | x2=x; 318 | 319 | 320 | function high_pushbutton9_Callback(hObject, eventdata, handles) 321 | % hObject handle to high_pushbutton9 (see GCBO) 322 | % eventdata reserved - to be defined in a future version of MATLAB 323 | % handles structure with handles and user data (see GUIDATA) 324 | global x; 325 | global Fs; 326 | global tl; 327 | global x2; 328 | 329 | x1=x; 330 | % fp = 3000; 331 | % fs = fp-300; 332 | % Wp = 2*fp/Fs; 333 | % Ws = 2*fs/Fs; 334 | % if(Wp >= 1) 335 | % Wp = 0.99; 336 | % end 337 | % if(Ws >= 1) 338 | % Ws = 0.99; 339 | % end 340 | fp = get(handles.edit3,'string'); 341 | fp = str2double(fp)*2; 342 | if get(handles.radiobutton1,'value') 343 | [n, Wn]=buttord(Wp,Ws, 2, 15); 344 | [b, a]=butter(n, Wn,'high'); 345 | axes(handles.axes3); 346 | [h,w]=freqz(b,a); 347 | plot(w/pi*Fs/2,abs(h)); 348 | x1=filter(b,a,x1); %调用函数滤波 349 | elseif get(handles.radiobutton4,'value') 350 | b2=fir1(30, fp/Fs,'high',boxcar(31)); 351 | axes(handles.axes3); 352 | [h,w]=freqz(b2, 1,512); 353 | plot(w/pi*Fs/2,20*log(abs(h))); 354 | x1=fftfilt(b2,x1); 355 | elseif get(handles.radiobutton5,'value') 356 | b2=fir1(30, fp/Fs,'high', triang(31)); 357 | axes(handles.axes3); 358 | [h,w]=freqz(b2, 1,512); 359 | plot(w/pi*Fs/2,20*log(abs(h))); 360 | x1=fftfilt(b2,x1); 361 | elseif get(handles.radiobutton6,'value') 362 | b2=fir1(30,fp/Fs,'high', hamming(31)); 363 | axes(handles.axes3); 364 | [h,w]=freqz(b2, 1,512); 365 | plot(w/pi*Fs/2,20*log(abs(h))); 366 | x1=fftfilt(b2,x1); 367 | elseif get(handles.radiobutton7,'value') 368 | b2=fir1(30, fp/Fs, 'high',hanning(31)); 369 | axes(handles.axes3); 370 | [h,w]=freqz(b2, 1,512); 371 | plot(w/pi*Fs/2,20*log(abs(h))); 372 | x1=fftfilt(b2,x1); 373 | elseif get(handles.radiobutton8,'value') 374 | b2=fir1(30, fp/Fs, 'high',blackman(31)); 375 | axes(handles.axes3); 376 | [h,w]=freqz(b2, 1,512); 377 | plot(w/pi*Fs/2,20*log(abs(h))); 378 | x1=fftfilt(b2,x1); 379 | elseif get(handles.radiobutton9,'value') 380 | b2=fir1(30,fp/Fs,'high', kaiser(31)); 381 | axes(handles.axes3); 382 | [h,w]=freqz(b2, 1,512); 383 | plot(w/pi*Fs/2,20*log(abs(h))); 384 | x1=fftfilt(b2,x1); 385 | end; 386 | axes(handles.axes5); 387 | plot(tl,x1); 388 | title('滤除噪声后语音时域波形'); 389 | xlabel('时间/s'); 390 | N=length(x1); 391 | df=Fs/N; 392 | w=[0:df:df*(N-1)] - Fs/2; %频率尺度 393 | X=fft(x1); 394 | X=fftshift(X); 395 | axes(handles.axes6); 396 | plot(w,abs(X)/max(abs(X))); 397 | axis([-10000,10000,0,1]); 398 | title('滤除噪声后语音频谱'); 399 | xlabel('频率/Hz'); 400 | grid on; 401 | x2=x1; 402 | 403 | % --- Executes on button press in bandpass_pushbutton10. 404 | function bandpass_pushbutton10_Callback(~, eventdata, handles) 405 | % hObject handle to bandpass_pushbutton10 (see GCBO) 406 | % eventdata reserved - to be defined in a future version of MATLAB 407 | % handles structure with handles and user data (see GUIDATA) 408 | global x; 409 | global Fs; 410 | global tl; 411 | global x2; 412 | 413 | x1=x; 414 | % fp = [2000,3000]; 415 | % fs = [1800,3300]; 416 | % Wp = 2*fp/Fs; 417 | % Ws = 2*fs/Fs; 418 | % if(Wp >= 1) 419 | % Wp = 0.99; 420 | % end 421 | % if(Ws >= 1) 422 | % Ws = 0.99; 423 | % end 424 | fp = get(handles.edit3,'string'); 425 | fp = str2double(fp)*2; 426 | fs = get(handles.edit4,'string'); 427 | fs = str2double(fs)*2; 428 | if get(handles.radiobutton1,'value') 429 | [n, Wn]=buttord(Wp,Ws, 2, 15); 430 | [b, a]=butter(n, Wn,'bandpass'); 431 | axes(handles.axes3); 432 | [h,w]=freqz(b,a); 433 | plot(w/pi*Fs/2,abs(h)); 434 | x1=filter(b,a,x1); 435 | elseif get(handles.radiobutton4,'value') 436 | b2=fir1(30,[fp/Fs fs/Fs],boxcar(31)); 437 | axes(handles.axes3); 438 | [h,w]=freqz(b2, 1,512); 439 | plot(w/pi*Fs/2,20*log(abs(h))); 440 | x1=fftfilt(b2,x1); 441 | elseif get(handles.radiobutton5,'value') 442 | b2=fir1(30,[fp/Fs fs/Fs], triang(31)); 443 | axes(handles.axes3); 444 | [h,w]=freqz(b2, 1,512); 445 | plot(w/pi*Fs/2,20*log(abs(h))); 446 | x1=fftfilt(b2,x1); 447 | elseif get(handles.radiobutton6,'value') 448 | b2=fir1(30,[fp/Fs fs/Fs],hamming(31)); 449 | axes(handles.axes3); 450 | [h,w]=freqz(b2, 1,512); 451 | plot(w/pi*Fs/2,20*log(abs(h))); 452 | x1=fftfilt(b2,x1); 453 | elseif get(handles.radiobutton7,'value') 454 | b2=fir1(30,[fp/Fs fs/Fs],hanning(31)); 455 | axes(handles.axes3); 456 | [h,w]=freqz(b2, 1,512); 457 | plot(w/pi*Fs/2,20*log(abs(h))); 458 | x1=fftfilt(b2,x1); 459 | elseif get(handles.radiobutton8,'value') 460 | b2=fir1(30,[fp/Fs fs/Fs],blackman(31)); 461 | axes(handles.axes3); 462 | [h,w]=freqz(b2, 1,512); 463 | plot(w/pi*Fs/2,20*log(abs(h))); 464 | x1=fftfilt(b2,x1); 465 | elseif get(handles.radiobutton9,'value') 466 | b2=fir1(30,[fp/Fs fs/Fs],kaiser(31)); 467 | axes(handles.axes3); 468 | [h,w]=freqz(b2, 1,512); 469 | plot(w/pi*Fs/2,20*log(abs(h))); 470 | x1=fftfilt(b2,x1); 471 | end; 472 | axes(handles.axes5); 473 | plot(tl,x1); 474 | title('滤除噪声后语音时域波形'); 475 | xlabel('时间/s'); 476 | N=length(x1); 477 | df=Fs/N; 478 | w=[0:df:df*(N-1)] - Fs/2; %频率尺度 479 | X=fft(x1); 480 | X=fftshift(X); 481 | axes(handles.axes6); 482 | plot(w,abs(X)/max(abs(X))); 483 | axis([-10000,10000,0,1]); 484 | title('滤除噪声后语音频谱'); 485 | xlabel('频率/Hz'); 486 | grid on; 487 | x2=x1; 488 | 489 | % --- Executes on button press in stop_pushbutton11. 490 | function stop_pushbutton11_Callback(hObject, eventdata, handles) 491 | % hObject handle to stop_pushbutton11 (see GCBO) 492 | % eventdata reserved - to be defined in a future version of MATLAB 493 | % handles structure with handles and user data (see GUIDATA) 494 | global x; 495 | global Fs; 496 | global tl; 497 | global x2; 498 | 499 | x1=x; 500 | % fp = [1200,3300]; 501 | % fs = [1500,3000]; 502 | % Wp = 2*fp/Fs; 503 | % Ws = 2*fs/Fs; 504 | % if(Wp >= 1) 505 | % Wp = 0.99; 506 | % end 507 | % if(Ws >= 1) 508 | % Ws = 0.99; 509 | % end 510 | fp = get(handles.edit3,'string'); 511 | fp = str2double(fp)*2; 512 | fs = get(handles.edit4,'string'); 513 | fs = str2double(fs)*2; 514 | if get(handles.radiobutton1,'value') 515 | [n, Wn]=buttord(Wp,Ws, 2, 15); 516 | [b, a]=butter(n, Wn,'stop'); 517 | axes(handles.axes3); 518 | [h,w]=freqz(b,a); 519 | plot(w/pi*Fs/2,abs(h)); 520 | x1=filter(b,a,x1); 521 | elseif get(handles.radiobutton4,'value') 522 | b2=fir1(30,[fp/Fs fs/Fs],'stop',boxcar(31)); 523 | axes(handles.axes3); 524 | [h,w]=freqz(b2, 1,512); 525 | plot(w/pi*Fs/2,20*log(abs(h))); 526 | x1=fftfilt(b2,x1); 527 | elseif get(handles.radiobutton5,'value') 528 | b2=fir1(30, [fp/Fs fs/Fs],'stop', triang(31)); 529 | axes(handles.axes3); 530 | [h,w]=freqz(b2, 1,512); 531 | plot(w/pi*Fs/2,20*log(abs(h))); 532 | x1=fftfilt(b2,x1); 533 | elseif get(handles.radiobutton6,'value') 534 | b2=fir1(30, [fp/Fs fs/Fs],'stop', triang(31)); 535 | axes(handles.axes3); 536 | [h,w]=freqz(b2, 1,512); 537 | plot(w/pi*Fs/2,20*log(abs(h))); 538 | x1=fftfilt(b2,x1); 539 | elseif get(handles.radiobutton7,'value') 540 | b2=fir1(30, [fp/Fs fs/Fs], 'stop',hanning(31)); 541 | axes(handles.axes3); 542 | [h,w]=freqz(b2, 1,512); 543 | plot(w/pi*Fs/2,20*log(abs(h))); 544 | x1=fftfilt(b2,x1); 545 | elseif get(handles.radiobutton8,'value') 546 | b2=fir1(30, [fp/Fs fs/Fs], 'stop',blackman(31)); 547 | axes(handles.axes3); 548 | [h,w]=freqz(b2, 1,512); 549 | plot(w/pi*Fs/2,20*log(abs(h))); 550 | x1=fftfilt(b2,x1); 551 | elseif get(handles.radiobutton9,'value') 552 | b2=fir1(30, [fp/Fs fs/Fs],'stop',kaiser(31)); 553 | axes(handles.axes3); 554 | [h,w]=freqz(b2, 1,512); 555 | plot(w/pi*Fs/2,20*log(abs(h))); 556 | x1=fftfilt(b2,x1); 557 | end; 558 | axes(handles.axes5); 559 | plot(tl,x1); 560 | title('滤除噪声后语音时域波形'); 561 | xlabel('时间/s'); 562 | N=length(x1); 563 | df=Fs/N; 564 | w=[0:df:df*(N-1)] - Fs/2; %频率尺度 565 | X=fft(x1); 566 | X=fftshift(X); 567 | axes(handles.axes6); 568 | plot(w,abs(X)/max(abs(X))); 569 | axis([-10000,10000,0,1]); 570 | title('滤除噪声后语音频谱'); 571 | xlabel('频率/Hz'); 572 | grid on; 573 | x2=x1; 574 | 575 | 576 | % --- Executes on button press in radiobutton1. 577 | function radiobutton1_Callback(hObject, eventdata, handles) 578 | % hObject handle to radiobutton1 (see GCBO) 579 | % eventdata reserved - to be defined in a future version of MATLAB 580 | % handles structure with handles and user data (see GUIDATA) 581 | 582 | % Hint: get(hObject,'Value') returns toggle state of radiobutton1 583 | 584 | 585 | % --- Executes on button press in radiobutton2. 586 | function radiobutton2_Callback(hObject, eventdata, handles) 587 | % hObject handle to radiobutton2 (see GCBO) 588 | % eventdata reserved - to be defined in a future version of MATLAB 589 | % handles structure with handles and user data (see GUIDATA) 590 | 591 | % Hint: get(hObject,'Value') returns toggle state of radiobutton2 592 | 593 | 594 | % --- Executes on button press in invert_pushbutton12. 595 | function invert_pushbutton12_Callback(hObject, eventdata, handles) 596 | % hObject handle to invert_pushbutton12 (see GCBO) 597 | % eventdata reserved - to be defined in a future version of MATLAB 598 | % handles structure with handles and user data (see GUIDATA) 599 | global x; 600 | global x2; 601 | 602 | N=length(x); 603 | for i=1:1:N 604 | x2(i)= x(N-i+1); 605 | end; 606 | 607 | 608 | 609 | % --- Executes on button press in save_pushbutton16. 610 | function save_pushbutton16_Callback(hObject, eventdata, handles) 611 | % hObject handle to save_pushbutton16 (see GCBO) 612 | % eventdata reserved - to be defined in a future version of MATLAB 613 | % handles structure with handles and user data (see GUIDATA) 614 | global x2; 615 | global Fs; 616 | [filename, pathname] = uiputfile('*.wav', '保存音频文件'); 617 | if isequal(filename,0) || isequal(pathname,0) 618 | disp('User pressed cancel') 619 | else 620 | path= fullfile(pathname, filename); 621 | audiowrite(x2,Fs,path); 622 | end 623 | 624 | 625 | % --- Executes on button press in pushbutton17. 626 | function pushbutton17_Callback(hObject, eventdata, handles) 627 | % hObject handle to pushbutton17 (see GCBO) 628 | % eventdata reserved - to be defined in a future version of MATLAB 629 | % handles structure with handles and user data (see GUIDATA) 630 | clear sound; 631 | 632 | 633 | % --- Executes on button press in radiobutton4. 634 | function radiobutton4_Callback(hObject, eventdata, handles) 635 | % hObject handle to radiobutton4 (see GCBO) 636 | % eventdata reserved - to be defined in a future version of MATLAB 637 | % handles structure with handles and user data (see GUIDATA) 638 | 639 | % Hint: get(hObject,'Value') returns toggle state of radiobutton4 640 | 641 | 642 | % --- Executes on button press in radiobutton5. 643 | function radiobutton5_Callback(hObject, eventdata, handles) 644 | % hObject handle to radiobutton5 (see GCBO) 645 | % eventdata reserved - to be defined in a future version of MATLAB 646 | % handles structure with handles and user data (see GUIDATA) 647 | 648 | % Hint: get(hObject,'Value') returns toggle state of radiobutton5 649 | 650 | 651 | % --- Executes on button press in radiobutton6. 652 | function radiobutton6_Callback(hObject, eventdata, handles) 653 | % hObject handle to radiobutton6 (see GCBO) 654 | % eventdata reserved - to be defined in a future version of MATLAB 655 | % handles structure with handles and user data (see GUIDATA) 656 | 657 | % Hint: get(hObject,'Value') returns toggle state of radiobutton6 658 | 659 | 660 | % --- Executes on button press in radiobutton7. 661 | function radiobutton7_Callback(hObject, eventdata, handles) 662 | % hObject handle to radiobutton7 (see GCBO) 663 | % eventdata reserved - to be defined in a future version of MATLAB 664 | % handles structure with handles and user data (see GUIDATA) 665 | 666 | % Hint: get(hObject,'Value') returns toggle state of radiobutton7 667 | 668 | 669 | % --- Executes on button press in radiobutton8. 670 | function radiobutton8_Callback(hObject, eventdata, handles) 671 | % hObject handle to radiobutton8 (see GCBO) 672 | % eventdata reserved - to be defined in a future version of MATLAB 673 | % handles structure with handles and user data (see GUIDATA) 674 | 675 | % Hint: get(hObject,'Value') returns toggle state of radiobutton8 676 | 677 | 678 | % --- Executes on button press in radiobutton9. 679 | function radiobutton9_Callback(hObject, eventdata, handles) 680 | % hObject handle to radiobutton9 (see GCBO) 681 | % eventdata reserved - to be defined in a future version of MATLAB 682 | % handles structure with handles and user data (see GUIDATA) 683 | 684 | % Hint: get(hObject,'Value') returns toggle state of radiobutton9 685 | 686 | 687 | 688 | function edit3_Callback(hObject, eventdata, handles) 689 | % hObject handle to edit3 (see GCBO) 690 | % eventdata reserved - to be defined in a future version of MATLAB 691 | % handles structure with handles and user data (see GUIDATA) 692 | 693 | % Hints: get(hObject,'String') returns contents of edit3 as text 694 | % str2double(get(hObject,'String')) returns contents of edit3 as a double 695 | 696 | 697 | % --- Executes during object creation, after setting all properties. 698 | function edit3_CreateFcn(hObject, eventdata, handles) 699 | % hObject handle to edit3 (see GCBO) 700 | % eventdata reserved - to be defined in a future version of MATLAB 701 | % handles empty - handles not created until after all CreateFcns called 702 | 703 | % Hint: edit controls usually have a white background on Windows. 704 | % See ISPC and COMPUTER. 705 | if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 706 | set(hObject,'BackgroundColor','white'); 707 | end 708 | 709 | 710 | 711 | function edit4_Callback(hObject, eventdata, handles) 712 | % hObject handle to edit4 (see GCBO) 713 | % eventdata reserved - to be defined in a future version of MATLAB 714 | % handles structure with handles and user data (see GUIDATA) 715 | 716 | % Hints: get(hObject,'String') returns contents of edit4 as text 717 | % str2double(get(hObject,'String')) returns contents of edit4 as a double 718 | 719 | 720 | % --- Executes during object creation, after setting all properties. 721 | function edit4_CreateFcn(hObject, eventdata, handles) 722 | % hObject handle to edit4 (see GCBO) 723 | % eventdata reserved - to be defined in a future version of MATLAB 724 | % handles empty - handles not created until after all CreateFcns called 725 | 726 | % Hint: edit controls usually have a white background on Windows. 727 | % See ISPC and COMPUTER. 728 | if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 729 | set(hObject,'BackgroundColor','white'); 730 | end 731 | 732 | 733 | % --- Executes on button press in pushbutton25. 734 | function pushbutton25_Callback(hObject, eventdata, handles) 735 | % hObject handle to pushbutton25 (see GCBO) 736 | % eventdata reserved - to be defined in a future version of MATLAB 737 | % handles structure with handles and user data (see GUIDATA) 738 | new_f_handle=figure('visible','off'); %新建一个不可见的figure 739 | new_axes=copyobj(handles.axes1,new_f_handle); %axes1是GUI界面内要保存图线的Tag,将其copy到不可见的figure中 740 | set(new_axes,'Units','normalized','Position',[0.1 0.1 0.8 0.8]);%将图线缩放 741 | [filename pathname fileindex]=uiputfile({'*.png';'*.bmp';'*.jpg';'*.eps';},'图片保存为'); 742 | if filename~=0%未点“取消”按钮或未关闭 743 | file=strcat(pathname,filename); 744 | switch fileindex %根据不同的选择保存为不同的类型 745 | case 1 746 | print(new_f_handle,'-dpng',file);% print(new_f_handle,'-dpng',filename);效果一样,将图像打印到指定文件中 747 | fprintf('>>已保存到:%s\n',file); 748 | case 2 749 | print(new_f_handle,'-dbmp',file); 750 | fprintf('>>已保存到:%s\n',file); 751 | case 3 752 | print(new_f_handle,'-djpeg',file); 753 | fprintf('>>已保存到:%s\n',file); 754 | case 4 755 | print(new_f_handle,'-depsc',file); 756 | fprintf('>>已保存到:%s\n',file); 757 | end 758 | msgbox(' 图线已成功保存!','完成!'); 759 | end 760 | new_f_handle=figure('visible','off'); %新建一个不可见的figure 761 | new_axes=copyobj(handles.axes2,new_f_handle); %axes1是GUI界面内要保存图线的Tag,将其copy到不可见的figure中 762 | set(new_axes,'Units','normalized','Position',[0.1 0.1 0.8 0.8]);%将图线缩放 763 | [filename pathname fileindex]=uiputfile({'*.png';'*.bmp';'*.jpg';'*.eps';},'图片保存为'); 764 | if filename~=0%未点“取消”按钮或未关闭 765 | file=strcat(pathname,filename); 766 | switch fileindex %根据不同的选择保存为不同的类型 767 | case 1 768 | print(new_f_handle,'-dpng',file);% print(new_f_handle,'-dpng',filename);效果一样,将图像打印到指定文件中 769 | fprintf('>>已保存到:%s\n',file); 770 | case 2 771 | print(new_f_handle,'-dbmp',file); 772 | fprintf('>>已保存到:%s\n',file); 773 | case 3 774 | print(new_f_handle,'-djpeg',file); 775 | fprintf('>>已保存到:%s\n',file); 776 | case 4 777 | print(new_f_handle,'-depsc',file); 778 | fprintf('>>已保存到:%s\n',file); 779 | end 780 | msgbox(' 图线已成功保存!','完成!'); 781 | end 782 | new_f_handle=figure('visible','off'); %新建一个不可见的figure 783 | new_axes=copyobj(handles.axes3,new_f_handle); %axes1是GUI界面内要保存图线的Tag,将其copy到不可见的figure中 784 | set(new_axes,'Units','normalized','Position',[0.1 0.1 0.8 0.8]);%将图线缩放 785 | [filename pathname fileindex]=uiputfile({'*.png';'*.bmp';'*.jpg';'*.eps';},'图片保存为'); 786 | if filename~=0%未点“取消”按钮或未关闭 787 | file=strcat(pathname,filename); 788 | switch fileindex %根据不同的选择保存为不同的类型 789 | case 1 790 | print(new_f_handle,'-dpng',file);% print(new_f_handle,'-dpng',filename);效果一样,将图像打印到指定文件中 791 | fprintf('>>已保存到:%s\n',file); 792 | case 2 793 | print(new_f_handle,'-dbmp',file); 794 | fprintf('>>已保存到:%s\n',file); 795 | case 3 796 | print(new_f_handle,'-djpeg',file); 797 | fprintf('>>已保存到:%s\n',file); 798 | case 4 799 | print(new_f_handle,'-depsc',file); 800 | fprintf('>>已保存到:%s\n',file); 801 | end 802 | msgbox(' 图线已成功保存!','完成!'); 803 | end 804 | new_f_handle=figure('visible','off'); %新建一个不可见的figure 805 | new_axes=copyobj(handles.axes5,new_f_handle); %axes1是GUI界面内要保存图线的Tag,将其copy到不可见的figure中 806 | set(new_axes,'Units','normalized','Position',[0.1 0.1 0.8 0.8]);%将图线缩放 807 | [filename pathname fileindex]=uiputfile({'*.png';'*.bmp';'*.jpg';'*.eps';},'图片保存为'); 808 | if filename~=0%未点“取消”按钮或未关闭 809 | file=strcat(pathname,filename); 810 | switch fileindex %根据不同的选择保存为不同的类型 811 | case 1 812 | print(new_f_handle,'-dpng',file);% print(new_f_handle,'-dpng',filename);效果一样,将图像打印到指定文件中 813 | fprintf('>>已保存到:%s\n',file); 814 | case 2 815 | print(new_f_handle,'-dbmp',file); 816 | fprintf('>>已保存到:%s\n',file); 817 | case 3 818 | print(new_f_handle,'-djpeg',file); 819 | fprintf('>>已保存到:%s\n',file); 820 | case 4 821 | print(new_f_handle,'-depsc',file); 822 | fprintf('>>已保存到:%s\n',file); 823 | end 824 | msgbox(' 图线已成功保存!','完成!'); 825 | end 826 | new_f_handle=figure('visible','off'); %新建一个不可见的figure 827 | new_axes=copyobj(handles.axes6,new_f_handle); %axes1是GUI界面内要保存图线的Tag,将其copy到不可见的figure中 828 | set(new_axes,'Units','normalized','Position',[0.1 0.1 0.8 0.8]);%将图线缩放 829 | [filename pathname fileindex]=uiputfile({'*.png';'*.bmp';'*.jpg';'*.eps';},'图片保存为'); 830 | if filename~=0%未点“取消”按钮或未关闭 831 | file=strcat(pathname,filename); 832 | switch fileindex %根据不同的选择保存为不同的类型 833 | case 1 834 | print(new_f_handle,'-dpng',file);% print(new_f_handle,'-dpng',filename);效果一样,将图像打印到指定文件中 835 | fprintf('>>已保存到:%s\n',file); 836 | case 2 837 | print(new_f_handle,'-dbmp',file); 838 | fprintf('>>已保存到:%s\n',file); 839 | case 3 840 | print(new_f_handle,'-djpeg',file); 841 | fprintf('>>已保存到:%s\n',file); 842 | case 4 843 | print(new_f_handle,'-depsc',file); 844 | fprintf('>>已保存到:%s\n',file); 845 | end 846 | msgbox(' 图线已成功保存!','完成!'); 847 | end 848 | --------------------------------------------------------------------------------