# 设计性实验:语音及音乐信号的采样、滤波及处理
## 一、实验目的
1、 理解采样率和量化级数对语音信号的影响;
2、 设计滤波器解决实际问题。
3、 了解回声的产生和梳妆滤波器;
4、 混音效果的原理和均衡器的设计;
## 二、实验原理
实验提示
(1)推荐录音及播放软件:CoolEdit;
(2)分析语音及音乐信号的频谱,根据信号的频率特性理解采样定律对信号数字化的工程指导意义;
(3)可用带阻滤波器对50Hz交流电噪声进行去噪处理;
(4)也可研究设计自适应滤波器对50Hz噪声及其它随机环境噪声进行滤波处理。
(5)回声产生可以使用梳妆滤波器,y(n)=x(n)+ax(n-R), a<1(回声衰减系数);或者传输函数为 ![1](img/1.png) 的全通滤波器实现;比较这两种实现方式的区别,分析为什么会有这样的区别;
(6)可以用许多一阶和二阶参数可调的滤波器级联来实现均衡器的功能,滤波器的结构选择结构要求是调整方便,最好调一个参数只影响一个应用指标,且可调参数少;
## 三、实验要求
I、利用电脑的声卡录一段语音信号及音乐信号,
(1)观察使用不同采样率及量化级数所得到的信号的听觉效果,从而确定对不同信号的最佳的采样率;
(2)分析音乐信号的采样率为什么要比语音的采样率高才能得到较好的听觉效果;
(3)注意观察信号中的噪声(特别是50hz交流电信号对录音的干扰,设计一个滤波器去除该噪声。
II、对一段语音信号及音乐信号
(1)设计函数实现一段语音或音乐的回声产生;
(2)设计均衡器,使得得不同频率的混合音频信号,通过一个均衡器后,增强或削减某些频率区域,以便修正低频和高频信号之间的关系;
有图形交互界面更佳。
## 四、实验源码及结果分析
## I、声音及音乐信号的量化采样及滤波
### (1) 源码
#### (a) 采样 量化50hz滤波器
```matlab
function varargout = Visualization(varargin)
% VISUALIZATION MATLAB code for Visualization.fig
% VISUALIZATION, by itself, creates a new VISUALIZATION or raises the existing
% singleton*.
%
% H = VISUALIZATION returns the handle to a new VISUALIZATION or the handle to
% the existing singleton*.
%
% VISUALIZATION('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in VISUALIZATION.M with the given input arguments.
%
% VISUALIZATION('Property','Value',...) creates a new VISUALIZATION or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Visualization_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Visualization_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 Visualization
% Last Modified by GUIDE v2.5 26-Nov-2018 23:08:32
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Visualization_OpeningFcn, ...
'gui_OutputFcn', @Visualization_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 Visualization is made visible.
function Visualization_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 Visualization (see VARARGIN)
% Choose default command line output for Visualization
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Visualization wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Visualization_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 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)
rs = audioread('11.26.aaaa.wav'); %读取文件
FS = 8000; %采样率8K
n = length(rs); %取文件的长度
A = get(handles.slider1,'Value'); %取滑动条的读数
rs = Mix50Hz(rs,n,A); %因为50Hz分量太小,所以人工混入50Hz的噪声
RS_e = fft(rs);
mag_RS_e = abs(RS_e);
f = (0:n-1)*(FS/n); %频率范围
axes(handles.axes1); %定位到图框1绘制图形(左上)
stem(handles.axes1,f,mag_RS_e,'.');grid on;
axis([0 4000 0 inf]);
title("原频谱/Hz");
ylabel('Magnitude');
%------------------------------------------------------
axes(handles.axes2); %定位到图框2绘制图形(右上)
stem(handles.axes2,f,mag_RS_e,'.');grid on;
axis([20 80 0 8]);
title("原频谱20-80Hz部分/Hz");
ylabel('Magnitude');
%------------------------------------------------------
[rs] = RM50Hz(rs); %调用50Hz陷波器
n = length(rs);
RS_e = fft(rs);
mag_RS_e = abs(RS_e);
f = (0:n-1)*(FS/n); %频率范围
axes(handles.axes3); %定位到图框3绘制图形(左下)
stem(handles.axes3,f,mag_RS_e,'.');grid on;
axis([20 80 0 8]);
title("滤波后20-80Hz部分/Hz");
ylabel('Magnitude');
%------------------------------------------------------
axes(handles.axes4); %定位到图框4绘制图形(右下)
stem(handles.axes4,f,mag_RS_e,'.');grid on;
axis([0 4000 0 inf]);
title("滤波后频谱/Hz");
ylabel('Magnitude');
%以上为程序主体|以上为程序主体|以上为程序主体|以上为程序主体|以上为程序主体
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to 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 edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to 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,'defaultUicontro