二、数字均衡器设计简介

随着数字化技术的快速、深入发展, 人们对数字化电子产品所产生的图像、图形以及声音等质量的要求越来越高。在实时数字处理过程中, 与D/A和A/D转换相关的模拟信号重构过程是决定数字系统输出质量的关键。在声音的拾取过程及通过音响设备的传送过程中, 由于设备或器件的原因, 其幅度对频率的响应往往不一致, 这样就达不到原来的听觉效果, 而均衡器就是一个改变放大器频率响应的设备。现有的许多播放器都具有均衡器的功能, 如Winamp等。

MATLAB是一个数据分析和处理功能十分强大的工程实用软件, 它的滤波器设计工具箱为实现声音信号的数字滤波提供了十分方便的函数和命令。

1 设计原理

均衡器的基本功能是调节各频段的信号强弱, 为了满足该功能, 应当具备下面一些基本功能:WAV声音信号获取, 即从文件 (*.wav, *.au等) 读取音频信息;八段数字均衡器设定;滤波, 包括生成低通、高通、带通、带阻的巴特沃斯、切比雪夫滤波器, 并显示滤波器的频率特征曲线;保存, 保存滤波后的音频信息到文件;播放, 播放滤后音频信号并实时显示波形及频谱特性。针对这些要求, 本文采用如下的方法:

(1) 设计出对应八个频段的八个带通滤波器;

(2) 对原始信号分八路用八个带通滤波器进行滤波;

(3) 将八个滤波器的滤波结果加权求和, 权值的设计与均衡器的调节要求一致。

这样最终得到的结果便是所需要的均衡结果。其中第2步中各带通滤波器的输入信号均为原始信号, 而不是“串联”地滤波。设原始输入信号为x (n) , 第i路的输出信号为yi (n) , 第i路的权值为ki, 均衡器的输出信号为y (n) , 则有

【数字信号处理】基于Matlab GUI数字均衡器设计【含Matlab源码 904期】_均衡器

式中, ai、bi为滤波器的参数, N为滤波器的阶数。

数字滤波器一般可用两种方法实现:一种是根据描述数字滤波器的数学模型或信号流程图, 用数字硬件构成专用的数字信号处理机, 即硬件方式;另一种是编写滤波器运算程序, 在计算机上运行, 即软件方式。考虑到软件的灵活性及易于实现, 本文采用软件方式实现数字滤波器。

2 软件设计

在模块划分时应遵循如下规则:改进软件结构提高模块独立性;模块规模应该适中;深度、宽度、扇出和扇入都应适当;模块的作用域应该在控制域之内;力争降低模块接口的复杂程度;设计单入口单出口的模块;模块功能应该可以预测。本着上述的启发式规则, 对软件进行如图1所示的模块划分。

【数字信号处理】基于Matlab GUI数字均衡器设计【含Matlab源码 904期】_ico_02

图1 数字均衡器的模块划分

三、部分源代码

function varargout = balancer(varargin)
% BALANCER MATLAB code for balancer.fig
% BALANCER, by itself, creates a new BALANCER or raises the existing
% singleton*.
%
% H = BALANCER returns the handle to a new BALANCER or the handle to
% the existing singleton*.
%
% BALANCER('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in BALANCER.M with the given input arguments.
%
% BALANCER('Property','Value',...) creates a new BALANCER or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before balancer_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to balancer_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%


% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @balancer_OpeningFcn, ...
'gui_OutputFcn', @balancer_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 balancer is made visible.
function balancer_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to audio_begin
% 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 balancer (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = balancer_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to audio_begin
% 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 audioload.
function audioload_Callback(hObject, eventdata, handles)
% hObject handle to audioload (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
try
[FileName] = uigetfile('*','Select the audio file');
[handles.audio , handles.audioFs ] = audioread(FileName) ;
Audio_f = abs( fft(handles.audio) );
plot( handles.audio_begin,Audio_f );
handles.audioname.String = FileName;

handles.audio_100 = fx_FIR( handles.audioFs, 1 ,100 , handles.audio);
handles.audio_200 = fx_FIR( handles.audioFs, 100 ,200 , handles.audio);
handles.audio_500 = fx_FIR( handles.audioFs, 200 ,500 , handles.audio);
handles.audio_1K = fx_FIR( handles.audioFs, 500 ,1000 , handles.audio);
handles.audio_2K = fx_FIR( handles.audioFs, 1000 ,2000 , handles.audio);
handles.audio_4K = fx_FIR( handles.audioFs, 2000 ,4000 , handles.audio);
handles.audio_8K = fx_FIR( handles.audioFs, 4000 ,8000 , handles.audio);
handles.audio_16K = fx_FIR( handles.audioFs, 8000 ,20000 , handles.audio);

%now_audio = audio_1K;
handles.now_audio = handles.audio_100 + handles.audio_200 + handles.audio_500 + handles.audio_1K + handles.audio_2K + handles.audio_4K + handles.audio_8K + handles.audio_16K;
sound( handles.now_audio , handles.audioFs );

Audio_ff = abs( fft( handles.now_audio ) );
plot( handles.audio_end , Audio_ff );

end
% Update handles structure
guidata(hObject, handles);


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


% --- Executes during object creation, after setting all properties.
function audioname_CreateFcn(hObject, eventdata, handles)
% hObject handle to audioname (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 slider movement.
function Freq100_Callback(hObject, eventdata, handles)
% hObject handle to Freq100 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
try
a = get(handles.Freq100 , 'Value');
b = get(handles.Freq200 , 'Value');
c = get(handles.Freq500 , 'Value');
d = get(handles.Freq1K , 'Value');
e = get(handles.Freq2K , 'Value');
f = get(handles.Freq4K , 'Value');
g = get(handles.Freq8K , 'Value');
h = get(handles.Freq16K , 'Value');

handles.Freq100num.String = num2str( a*100 );
all = get(handles.audiovoice , 'Value');
handles.now_audio = all*( a*handles.audio_100 + b*handles.audio_200 + c*handles.audio_500 + d*handles.audio_1K + e*handles.audio_2K + f*handles.audio_4K + g*handles.audio_8K + h*handles.audio_16K );
Audio_ff = abs( fft( handles.now_audio ) );
plot( handles.audio_end , Audio_ff );

end
guidata(hObject, handles);
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider


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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function slider7_Callback(hObject, eventdata, handles)
% hObject handle to slider7 (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,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider


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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function slider8_Callback(hObject, eventdata, handles)
% hObject handle to slider8 (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,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider


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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function slider9_Callback(hObject, eventdata, handles)
% hObject handle to slider9 (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,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider


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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function slider10_Callback(hObject, eventdata, handles)
% hObject handle to slider10 (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,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider


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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function slider11_Callback(hObject, eventdata, handles)
% hObject handle to slider11 (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,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider


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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end

四、运行结果

【数字信号处理】基于Matlab GUI数字均衡器设计【含Matlab源码 904期】_matlab_03

五、matlab版本及参考文献

1 matlab版本

2014a

2 参考文献

[1] 沈再阳.精通MATLAB信号处理[M].清华大学出版社,2015.

[2]高宝建,彭进业,王琳,潘建寿.信号与系统——使用MATLAB分析与实现[M].清华大学出版社,2020.

[3]王文光,魏少明,任欣.信号处理与系统分析的MATLAB实现[M].电子工业出版社,2018.

[4]肖正安.基于MATLAB的数字均衡器的设计[J].湖北第二师范学院学报. 2008,(08)