基于Matlab的语音信号分析与处理+简单的小波变换去噪恢复代码(含GUI界面)

语音信号分析与处理部分代码为:

[x,fs]=audioread('music.wav'); %打开语音信号
 sound(x,fs); %播放语音信号
N=length(x); %长度
n=0:N-1;
w=2*n*pi/N;
y1=fft(x); %对原始信号做FFT变换
figure(1);
subplot(2,1,1);
plot(n,x) %做原始语音信号的时域波形图
title('原始语音信号时域图');
xlabel('时间t');
ylabel('幅值');
subplot(2,1,2); %做原始语音信号的频谱图
plot(w/pi,abs(y1));
title('原始语音信号频谱')
xlabel('频率Hz');
ylabel('幅度');

%加入噪声
[x,fs]=audioread('music.wav');
n=length(x);
x_p=fft(x,n);
f=fs*(0:n/2-1)/n;
figure(1)
subplot(2,1,1);
plot(x);
title('原始语音信号采样后的时域波形');
xlabel('时间轴')
ylabel('幅值A')
subplot(2,1,2);
plot(f,abs(x_p(1:n/2)));
title('原始语音信号采样后的频谱图');
xlabel('频率Hz');
ylabel('频率幅值');
noise=0.005*randn(1,n);
x_z=x+noise';
sound(x_z,fs)
n=length(x);
x_zp=fft(x_z,n);
f=fs*(0:n/2-1)/n;
figure(2)
subplot(2,1,1);
plot(x_z);
title('加噪语音信号时域波形');
xlabel('时间轴')
ylabel('幅值A')
subplot(2,1,2);
plot(f,abs(x_zp(1:n/2)));
title('加噪语音信号频谱图');
xlabel('频率Hz');
ylabel('频率幅值');


提取声音
[pyr,fs]=audioread('music.wav');%声音读取


%原始信号时域和频域波形
n=length(pyr);
pyr1=fft(pyr,n);     %快速傅里叶变换
figure(1)
subplot(2,2,1);
plot(pyr);  
xlabel('时间');
ylabel('幅度');
title('初始信号波形');   %绘出时域波
grid on
subplot(2,2,2);      %绘出频域频谱
plot(abs(fftshift(pyr1)));
title('初始信号频谱');
xlabel('频率');
ylabel('幅度');
grid on
% pause(10);
 
%加入噪声
noise=0.01*randn(n,1);%加噪声
s=pyr+noise;
%pause;
sound(s,fs);  %播放加噪的语音
n=length(s);   %画出加噪之后,其时域频域
S=fft(s,n);
figure(2)
subplot(2,1,1)
plot(s);
title('加噪声后信号波形')
xlabel('时间');
ylabel('幅度');
grid on;
subplot(2,1,2)
plot(abs(fftshift(S)));
xlabel('频率');
ylabel('幅度');
title('加噪声后信号信号频谱');
grid on;
pause

%设计IIR低通滤波器 
rp=2,rs=80;
Ft=8000;
Fp=1000;
Fs=2000;                                       
wp=2*pi*Fp/Ft;
ws=2*pi*Fs/Ft;         %求出待设计的模拟滤波器的边界频率
[n,wn]=buttord(wp,ws,rp,rs,'s');    %低通滤波器的阶数和截止频率
[b,a]=butter(n,wn,'s');             %S域频率响应的参数即:滤波器的传输函数
[bz,az]=bilinear(b,a,0.5);          %利用双线性变换实现频率响应S域到Z域的变换
%低通滤波器特性
figure(3);
[h,w]=freqz(bz,az);
title('IIR低通滤波器');
plot(w*fs/(2*pi),abs(h));
grid on
 %滤波
z=filter(bz,az,s);
%回放滤波后的信号
pause;
sound(z,fs);                   


%滤波后画出其频谱,波形
Z=fft(z);                           %滤波后的信号频谱
figure(4)
subplot(2,2,1);
plot(z);
title('低通滤波后的信号波形');
xlabel('时间');
ylabel('幅度');
grid;
subplot(2,2,2)
plot(s)
title('加噪声后信号波形');
 xlabel('时间');
ylabel('幅度');
grid;
subplot(2,2,3);
plot(abs(fftshift(S)));
title('加噪声后信号频谱');
xlabel('频率');
ylabel('幅度');
grid;
subplot(2,2,4);
plot(abs(fftshift(Z)));
title('低通滤波后信号的频谱');
xlabel('频率');
ylabel('幅度');
grid;

GUI代码为:

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

% Last Modified by GUIDE v2.5 09-Dec-2021 23:31:50

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

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

% Update handles structure
guidata(hObject, handles);

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


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

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


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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 button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[x,fs]=audioread('music.wav'); %打开语音信号
 sound(x,fs); %播放语音信号
 
 
 
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[x,fs]=audioread('music.wav');
n=length(x);
x_p=fft(x,n);
f=fs*(0:n/2-1)/n;
se=0.005*randn(1,n);
x_z=x+noise';
sound(x_z,fs)

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


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[x,fs]=audioread('music.wav'); %打开语音信号
% sound(x,fs); %播放语音信号
N=length(x); %长度
n=0:N-1;
w=2*n*pi/N;
axes(handles.axes1);
y1=fft(x); %对原始信号做FFT变换
plot(n,x) %做原始语音信号的时域波形图
title('原始语音信号时域图');
xlabel('时间t');
ylabel('幅值');

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[x,fs]=audioread('music.wav'); %打开语音信号
% sound(x,fs); %播放语音信号
N=length(x); %长度
n=0:N-1;
w=2*n*pi/N;
axes(handles.axes2);
y1=fft(x); %对原始信号做FFT变换
plot(w/pi,abs(y1));
title('原始语音信号频谱')
xlabel('频率Hz');
ylabel('幅度');

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


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

你可能感兴趣的:(matlab,开发语言)