MATLAB处理语音信号播放,滤波等

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

% Last Modified by GUIDE v2.5 07-Jan-2021 11:07:50

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = shuzixinhao_OutputFcn(~, ~, 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 pushbutton1.      播放
function pushbutton1_Callback(~, ~, ~)
% 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)
global temp;
global Fs;
sound(temp,Fs);%播放音频文件


% --- Executes on button press in pushbutton2.     文件选择 
function pushbutton2_Callback(~, ~, 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)
[filename,pathname]=uigetfile({'*.mp3','ALLFILES(*.*)'},'选择声音文件');
if isequal([filename pathname],[0,0])
    return;
end
str=[pathname filename];%选择的声音文件路径和文件名
global temp;
global Fs;

[temp,Fs]=audioread(str);%temp表示声音数据 Fs表示频率
handles.y=temp;handles.Fs=Fs;


% --- Executes on button press in pushbutton3.     原波形    
function pushbutton3_Callback(~, ~, 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)
global temp;
global temp1;
global Fs;
global t;
global f;

n=length(temp);
t=0:1/Fs:(n-1)/Fs;
df=Fs/length(temp1); %计算谱线间隔
f=0:df:(Fs/2-df); %频谱范围,截取前半段(抽样频率高于最大频率的2倍)
temp1=fft(temp,n);   %快速傅里叶变换

plot(handles.axes1,temp);%画出时域图,放到对应坐标轴中
title(handles.axes1,'初始信号波形');   %绘出时域波

plot(handles.axes2,abs(fftshift(temp1)));
title(handles.axes2,'初始信号频谱');      %绘出频域波



% --- Executes on button press in pushbutton4.     添加噪声并播放
function pushbutton4_Callback(~, ~, ~)
% 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)
global temp;
global s;
global Fs;
global t;

temp=temp(:,1); %第一列
temp=temp';
x=0.5*sin(2*pi*500*t);    %500HZ正弦干扰
s1=temp(1:length(x))+x;
%加入噪音的音频
s = awgn(s1,10);     %加高斯白噪声
sound(s,Fs);



% --- Executes on button press in pushbutton5.        加噪波形
function pushbutton5_Callback(~, 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)
global s;
global S;

n=length(s);   %画出加噪之后,其时域频域
S=fft(s,n);
Sf=abs(S) ;  %幅度
Sf=Sf(1:length(Sf)/2);

plot(handles.axes3, 0:n-1,s);
title(handles.axes3,'加噪声后信号波形')

plot(handles.axes4, 0:n-1,abs(fftshift(S)));
title(handles.axes4,'加噪声后信号信号频谱');




% --- 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)
%FIR低通
global Fs;
global temp;

wp=2*pi*1000/Fs;
ws=2*pi*1200/Fs;
wdelta=ws-wp;
N=ceil(8* pi/wdelta);  %取整
wn=(wp+ws)/2;
[b,a]=fir1(N,wn/pi,hamming(N+1));  %选择窗函数,并归一化截止频率

f2=filter(b,a,temp);

plot(handles.axes5,f2);
title(handles.axes5,'低通滤波器滤波后的时域波形');

F0=fft(f2,1024);
f=Fs*(0:511)/1024;

plot(handles.axes6,f,abs(F0(1:512)));
title(handles.axes6,'低通滤波器滤波后的频谱')

sound(f2,Fs);  %播放滤波后的语音信号sound




% --- 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)
%IIR高通
global Fs;
global temp;

Ts=1/Fs;R1=50;
Wp=2*pi*4400/Fs;
Ws=2* pi*4000/Fs;
Rp=1;
Rl=100;
Wp1=2/Ts*tan(Wp/2);  %将模拟指标转换成数字指标
Ws1=2/Ts*tan(Ws/2);

[N,Wn]=cheb2ord(Wp1,Ws1,Rp,R1,'s'); %选择滤波器的最小阶数
[Z,P,K]=cheb2ap(N,Rl);  %创建切比雪夫模拟滤波器
[Bap,Aap]=zp2tf(Z,P,K);
[b,a]=lp2hp(Bap,Aap,Wn);
[bz,az ]=bilinear(b,a,Fs);  %用双线性变换法实现模拟滤波器到数字滤波器的转换

f1=filter(bz,az,temp);


plot(handles.axes7,f1);  %画出滤波后的时域图
title(handles.axes7,'高通滤波器滤波后的时域波形');

F0=fft(f1,1024);
f=Fs*(0:511)/1024;

plot(handles.axes8,f,abs(F0(1:512)));  %画出滤波后的频谱图
title(handles.axes8,'高通滤波器滤波后的频谱')

sound(f1,Fs);  %播放滤波后的语音信号



% --- Executes on button press in pushbutton8.          带通    
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%IIR带通
global Fs;
global temp;

Ts= 1/Fs;R1=30;
fb1=1200;fb2=5000;fc1=1000;fc2=32000;fs=23000;
W1=2*fb1 *pi/fs;W2=2*fc1*pi/fs;
W3=2*fb2*pi/fs;W4=2*fc2* pi/fs;
Wp=[W1,W3];
Ws=[W2,W4];
Rp=1;
Rl=100;
Wp1=2/Ts*tan(Wp/2);  %将模拟指标转换成数字指标
Ws1 =2/Ts*tan(Ws/2);

[N,Wn]=cheb2ord(Wp1,Ws1,Rp,R1,'s'); %选择滤波器的最小阶数
[Z,P,K]=cheb2ap(N,Rl);   %创建切比雪夫模拟滤波器;
[Bap,Aap]=zp2tf(Z,P,K);
[b,a]=lp2bp(Bap,Aap,2100*2*pi,1800*2*pi);
[bz,az]=bilinear(b,a,Fs);  %用双线性变换法实现模拟滤波器到数字滤波器的转换

f1 =filter(bz,az,temp);

plot(handles.axes9,f1);   %画出滤波后的时域图
title(handles.axes9,'带通滤波器滤波后的时域波形');

F0=fft(f1,1024);
f=Fs*(0:511)/1024;

plot(handles.axes10,f,abs(F0(1:512)));  %画出滤波后的频谱图
title(handles.axes10,'带通滤波器滤波后的频谱')

sound(f1,Fs);  %播放滤波后的语音信号


% --- Executes on button press in pushbutton9.          带阻
function pushbutton9_Callback(~, eventdata, handles)
% hObject    handle to pushbutton9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global Fs;
global temp;
global h_bs;

%设计带阻滤波器
fpd=400;fsd=450;fsu=550;fpu=600;
%转换成数字频率
wpd=fpd*2*pi/Fs;wsd=fsd*2*pi/Fs;wsu=fsu*2*pi/Fs;wpu=fpu*2*pi/Fs;
%计算过滤带宽(与N成反比,因此取小的)
dw=min((wsd-wpd),(wpu-wsu));
%计算上下带中心频率
wcu=(wpu+wsu)/2;wcd=(wsd+wpd)/2;
%由于As=50,查表可知选海明窗,计算海明窗的长度
Nh=ceil(6.6*pi/dw);
%由于是带阻滤波器,因此确保Nh为奇数
Nh=Nh+mod(Nh+1,2);
nh=0:Nh-1;
%根据带阻与低通滤波器的关系求带阻滤波器的脉冲响应

hd_bs=ideallp(pi,Nh)-ideallp(wcu,Nh)+ideallp(wcd,Nh);
%由于输出是中心值归一化为1的窗函数序列,它是列向量,因此需要转置
w_h=(hamming(Nh))';
%利用窗函数法计算实际滤波器脉冲响应
h_bs=hd_bs.*w_h;

f1 =filter(h_bs,3,temp);
%temp_fil=fftfilt(h_bs,temp);
%temp1_fil=abs(fft(temp_fil));
%temp1_fil=temp1_fil(1:length(temp1_fil)/2);

plot(handles.axes11,f1);
title(handles.axes11,'带阻理想带阻滤波器时域 ');
%[NH,whf]=freqz(hd_bs);

F0=fft(f1,1024);
f=Fs*(0:511)/1024;

plot(handles.axes12,f,abs(F0(1:512)));
title(handles.axes12,'带阻滤波器滤波后的频谱');

sound(f1,Fs);  %播放滤波后的语音信号




% --- Executes on button press in pushbutton10.         去噪播放
function pushbutton10_Callback(~, eventdata, handles)
% hObject    handle to pushbutton10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global temp;
global Fs;
global temp_fil;
global h_bs;

temp_fil=fftfilt(h_bs,temp);
sound(temp_fil,Fs);             %播放加噪的语音



% --- Executes on button press in pushbutton11.      去噪波形
function pushbutton11_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton11 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global temp_fil;

temp1_fil=abs(fft(temp_fil));
temp1_fil=temp1_fil(1:length(temp1_fil)/2);
plot(handles.axes13,temp1_fil);
title(handles.axes13,'去噪频域图');

plot(handles.axes14,temp_fil);
title(handles.axes14,'去噪时域图');

%?用ideallp?函数设计FIR?线性相位滤波器
function hd=ideallp(wc,N) %0到N-1之间的理想脉冲响应
%wc=截止频率(弧度)%N=理想滤波器的长度
tao=(N-1)/2;
n=[0:(N-1)];
m=n-tao+eps; %加一个小数以避免0作除数
hd=sin(wc*m)./(pi*m);
%另建一个文件,此函数在主程序中需要被调用
%?用ideallp?函数设计FIR?线性相位滤波器
function hd=ideallp(wc,N) %0到N-1之间的理想脉冲响应
%wc=截止频率(弧度)%N=理想滤波器的长度
tao=(N-1)/2;
n=[0:(N-1)];
m=n-tao+eps; %加一个小数以避免0作除数
hd=sin(wcm)./(pim);

论文链接

GUI制作方法
借鉴其他人的部分代码

  • 3
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MATLAB是一款功能强大的科学计算软件,也是处理语音信号的常用工具之一。在MATLAB中,可以使用一系列函数对语音信号进行滤波处理滤波语音信号处理中的一项重要技术,通过滤波可以去除信号中的噪声、增强信号的频率成分等。常用的滤波器包括低通滤波器、高通滤波器、带通滤波器等。 下面以低通滤波为例,介绍在MATLAB中对语音信号进行滤波处理的步骤: 1. 读取语音信号文件 使用MATLAB中的`audioread`函数读取语音信号文件,将信号存储在一个向量中,例如: ``` [x, fs] = audioread('speech.wav'); ``` 其中`x`为语音信号向量,`fs`为采样率。 2. 设计滤波器 使用MATLAB中的`designfilt`函数设计一个低通滤波器,例如: ``` lpFilt = designfilt('lowpassiir', 'FilterOrder', 8, 'PassbandFrequency', 1000, 'PassbandRipple', 0.2, 'SampleRate', fs); ``` 其中,`'lowpassiir'`表示设计一个无限冲激响应(IIR)的低通滤波器,`'FilterOrder'`表示滤波器的阶数,`'PassbandFrequency'`表示通带截止频率,`'PassbandRipple'`表示通带最大纹波,`'SampleRate'`表示采样率。 3. 对语音信号进行滤波处理 使用MATLAB中的`filter`函数对语音信号进行滤波处理,例如: ``` y = filter(lpFilt, x); ``` 其中,`lpFilt`为设计好的低通滤波器,`x`为原始语音信号,`y`为滤波后的语音信号。 4. 播放滤波后的语音信号 使用MATLAB中的`sound`函数播放滤波后的语音信号,例如: ``` sound(y, fs); ``` 其中,`y`为滤波后的语音信号,`fs`为采样率。 通过以上步骤,就可以在MATLAB中对语音信号进行低通滤波处理,并播放滤波后的语音信号

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值