【语音去噪】基于matlab GUI FIR音频去噪【含Matlab源码 875期】

一、简介

1 内容简介
利用MATLAB GUI设计平台,用窗函数法设计FIR数字滤波器,对所给出的含有噪声的声音信号进行数字滤波处理,得到降噪的声音信号,进行时域频域分析,同时分析不同窗函数的效果。

2 函数使用
读取.wav音频文件函数:audioread();(老版本为wavread)
MATLAB播放音乐函数:sound();
MATLAB停止播放音乐:clear sound
写入.wav音频文件函数:audiowrite();(老版本为audiowrite)
加入白噪声:noise=(max(x(:,1))/5)*randn(x,2);
y=x+noise;
频谱分析: fft();
fftshift();
Fir滤波: fir1(n,Wn,ftype,window);
窗函数选择: 梯形窗boxcar
三角窗triang
海明窗hamming
汉宁窗hanning
布莱克曼窗blackman
凯塞窗kaiser

3 实现功能

实现的功能有:

3.1 打开文件:选择路径打开wav格式的音频文件,自动生成音频的原始波形与频谱。

3.2 加入噪声:有两种噪声可以选择加入,一种是白噪声,其频率蔓延整个频谱;一种是特定频率的噪声,可通过输入频率加入单一频率的噪声。加入噪声后自动绘制加入噪声后的波形与频谱。

3.3 滤波处理:首先输入滤波器通/阻带的开始频率与截止频率(若为低/高通类型滤波,则只需输入开始频率;若为带通/阻类型,则开始与截止都要输入;输入频率值为真实频率值,可根据频谱图进行判断 ),之后选取窗函数和滤波类型,将会生成滤波处理后的波形与频谱。

3.4 音频播放/停止:可随时播放/停止原始、加噪、滤波处理后的音频。

3.5 图片导出:将波形、频谱图片一张张导出保存,可选的格式有jpg、png、bmp、eps。

3.6 保存文件:将加躁/滤波后的音频导出保存。

二、源代码

function varargout = yanshou(varargin)
% YANSHOU M-file for yanshou.fig
%      YANSHOU, by itself, creates a new YANSHOU or raises the existing
%      singleton*.
%
%      H = YANSHOU returns the handle to a new YANSHOU or the handle to
%      the existing singleton*.
%
%      YANSHOU('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in YANSHOU.M with the given input arguments.
%
%      YANSHOU('Property','Value',...) creates a new YANSHOU or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before yanshou_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to yanshou_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 yanshou

% Last Modified by GUIDE v2.5 23-Apr-2020 14:20:34

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @yanshou_OpeningFcn, ...
                   'gui_OutputFcn',  @yanshou_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(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 yanshou is made visible.
function yanshou_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 yanshou (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = yanshou_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 on button press in open_pushbutton1.
function open_pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to open_pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global x;  %文件
global Fs; %采样频率
global tl; 
global x2;
[filename, pathname] = uigetfile('*.wav', '选择音频文件');
if isequal(filename,0)
   disp('User selected Cancel')
else
   path = fullfile(pathname, filename);
   [handles.x,handles.Fs]=audioread(path);
   x=handles.x;
   Fs=handles.Fs;
   axes(handles.axes1);
   tl=[0:1/Fs:(length(handles.x)-1)/Fs];  %时间尺度
   plot(tl,handles.x);
   title('语音时域波形');
   xlabel('时间/s');
   grid on;
   
   N=length(handles.x);
   df=Fs/N;
   w=[0:df:df*(N-1)] - Fs/2; %频率尺度
   X=fft(handles.x);
   X=fftshift(X);
   axes(handles.axes2);
   plot(w,abs(X)/max(abs(X)));
   axis([-10000,10000,0,1]);
   title('语音频谱');
   xlabel('频率/Hz');
   grid on;
   x2=x;
end



% --- Executes on button press in play_pushbutton2.
function play_pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to play_pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global x2;
global Fs;
sound(x2,Fs);


% --- Executes on button press in add_pushbutton3.
function add_pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to add_pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global x;
global Fs;
global tl;
global x2;
axes(handles.axes1);
size(x);
t=0:1/Fs:(length(x)-1)/Fs;%将所加噪声信号的点数调整到与原始信号相同
Au=0.07;
fn =  get(handles.noise_edit2,'string');       %噪声频率
fn = str2double(fn);
noise=Au*cos(2*pi*fn*t)';     %噪声为频率
x=x+noise;
plot(tl,x);
title('加入噪声后语音时域波形');
xlabel('时间/s');
grid on;
N=length(x);
df=Fs/N;
w=[0:df:df*(N-1)] - Fs/2; %频率尺度
X=fft(x);
X=fftshift(X);
axes(handles.axes2);
plot(w,abs(X)/max(abs(X)));
axis([-10000,10000,0,1]);
title('加入噪声后语音频谱');
xlabel('频率/Hz');
grid on;    
x2=x;

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

global x;
global Fs;
global tl;
global x2;

x1=x;
% fp=1000;
% fs = 1000;
% Wp = 2*fp/Fs;
% Ws = 2*fs/Fs;
% if(Wp >= 1)
%     Wp = 0.99;
% end
% if(Ws >= 1)
%     Ws = 0.99;  
% end
fp =  get(handles.edit3,'string');     
fp = str2double(fp)*2;
if get(handles.radiobutton1,'value')
    [n, Wn]=buttord(Wp,Ws, 2, 15);
    [b, a]=butter(n, Wn,'low');
    axes(handles.axes3);
    [h,w]=freqz(b,a);
    plot(w/pi*Fs/2,abs(h)); 
    x1=filter(b,a,x1);        %调用函数滤波
elseif get(handles.radiobutton4,'value')
    b2=fir1(30, fp/Fs, boxcar(31)); 
    axes(handles.axes3);
    [h,w]=freqz(b2, 1,512); 
    plot(w/pi*Fs/2,20*log(abs(h))); 
    x1=fftfilt(b2,x1);
    elseif get(handles.radiobutton5,'value')
    b2=fir1(30, fp/Fs, triang(31)); 
    axes(handles.axes3);
    [h,w]=freqz(b2, 1,512); 
    plot(w/pi*Fs/2,20*log(abs(h))); 
    x1=fftfilt(b2,x1);
    elseif get(handles.radiobutton6,'value')
    b2=fir1(30, fp/Fs, hamming(31)); 
    axes(handles.axes3);
    [h,w]=freqz(b2, 1,512); 
    plot(w/pi*Fs/2,20*log(abs(h))); 
    x1=fftfilt(b2,x1);
    elseif get(handles.radiobutton7,'value')
    b2=fir1(30, fp/Fs, hanning(31)); 
    axes(handles.axes3);
    [h,w]=freqz(b2, 1,512); 
    plot(w/pi*Fs/2,20*log(abs(h))); 
    x1=fftfilt(b2,x1);
    elseif get(handles.radiobutton8,'value')
    b2=fir1(30, fp/Fs, blackman(31)); 
    axes(handles.axes3);
    [h,w]=freqz(b2, 1,512); 
    plot(w/pi*Fs/2,20*log(abs(h))); 
    x1=fftfilt(b2,x1);
    elseif get(handles.radiobutton9,'value')
    b2=fir1(30,fp/Fs, kaiser(31)); 
    axes(handles.axes3);
    [h,w]=freqz(b2, 1,512); 
    plot(w/pi*Fs/2,20*log(abs(h))); 
    x1=fftfilt(b2,x1);
end;
axes(handles.axes5);
plot(tl,x1);
title('滤除噪声后语音时域波形');
xlabel('时间/s');
N=length(x1);
df=Fs/N;
w=[0:df:df*(N-1)] - Fs/2; %频率尺度
X=fft(x1);
X=fftshift(X);
axes(handles.axes6);
plot(w,abs(X)/max(abs(X)));
axis([-10000,10000,0,1]);
title('滤除噪声后语音频谱');
xlabel('频率/Hz');
grid on;
x2=x1;




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


% --- Executes on button press in pushbutton6.



function noise_edit2_Callback(hObject, eventdata, handles)
% hObject    handle to noise_edit2 (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 noise_edit2 as text
%        str2double(get(hObject,'String')) returns contents of noise_edit2 as a double


% --- Executes during object creation, after setting all properties.
function noise_edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to noise_edit2 (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 during object creation, after setting all properties.
function add_pushbutton3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to add_pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes on button press in guass_pushbutton8.
function guass_pushbutton8_Callback(hObject, eventdata, handles)
% hObject    handle to guass_pushbutton8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global x;
global Fs;
global tl;
global x2;
N=length(x);
axes(handles.axes1);
%x = awgn(x,15);
noise=(max(x(:,1))/5)*randn(N,2);
x=x+noise;
plot(tl,x);
title('加入噪声后语音时域波形');
xlabel('时间/s');
grid on;

三、运行结果

【语音去噪】基于matlab GUI FIR音频去噪【含Matlab源码 875期】_第1张图片

四、备注

版本:2014
完整代码或者代写添加QQ 1564658423

你可能感兴趣的:(matlab,语音处理)