现在有一个设计好的界面,用窗函数法设计fir滤波器,想要在这个的基础上加上频率抽样法设计fir滤波器的功能,以及导入音频进行滤波的功能
该回答引用于gpt与OKX安生共同编写:
好的,为了实现您的需求,需要进行以下步骤:
添加频率抽样法设计FIR滤波器的功能:
添加导入音频进行滤波的功能:
为了实现以上功能,您可以使用MATLAB内置的DSP工具箱,其中包含了许多用于数字信号处理的函数和工具。具体实现过程可以参考MATLAB DSP工具箱的文档和示例代码。
function fir_filter_gui
% FIR Filter Design GUI
% Initialize figure window
figure('Name', 'FIR Filter Designer', 'NumberTitle', 'off', ...
'Position', [100 100 800 600], 'Units', 'pixels');
% Define handles structure
handles = struct();
% Set default values
handles.fs = 8000; % Sampling frequency
handles.fc = 1000; % Cutoff frequency
handles.L = 101; % Filter order
handles.method = 'Window'; % Default method
handles.h = [];
% Create widgets
handles.method_group = uibuttongroup('Title', 'Filter Design Method', ...
'FontSize', 12, 'Position', [0.05 0.5 0.2 0.4]);
handles.window_radio = uicontrol('Parent', handles.method_group, ...
'Style', 'radiobutton', 'String', 'Window', 'FontSize', 12, ...
'Units', 'normalized', 'Position', [0.1 0.7 0.8 0.2], ...
'HandleVisibility', 'off', 'Callback', @method_callback);
handles.freqsamp_radio = uicontrol('Parent', handles.method_group, ...
'Style', 'radiobutton', 'String', 'Frequency Sampling', 'FontSize', 12, ...
'Units', 'normalized', 'Position', [0.1 0.4 0.8 0.2], ...
'HandleVisibility', 'off', 'Callback', @method_callback);
handles.filter_type_text = uicontrol('Style', 'text', 'String', 'Filter Type:', ...
'FontSize', 12, 'HorizontalAlignment', 'right', ...
'Units', 'normalized', 'Position', [0.3 0.9 0.15 0.1]);
handles.filter_type_popup = uicontrol('Style', 'popupmenu', ...
'String', {'Lowpass', 'Highpass', 'Bandpass', 'Bandstop'}, ...
'FontSize', 12, 'Units', 'normalized', 'Position', [0.45 0.9 0.2 0.1]);
handles.fs_text = uicontrol('Style', 'text', 'String', 'Sampling Frequency (Hz):', ...
'FontSize', 12, 'HorizontalAlignment', 'right', ...
'Units', 'normalized', 'Position', [0.3 0.8 0.4 0.1]);
handles.fs_edit = uicontrol('Style', 'edit', 'String', num2str(handles.fs), ...
'FontSize', 12, 'Units', 'normalized', 'Position', [0.75 0.8 0.2 0.1]);
handles.fc_text = uicontrol('Style', 'text', 'String', 'Cutoff Frequency (Hz):', ...
'FontSize', 12, 'HorizontalAlignment', 'right', ...
'Units', 'normalized', 'Position', [0.3 0.7 0.4 0.1]);
handles.fc_edit = uicontrol('Style', 'edit', 'String', num2str(handles.fc), ...
'FontSize', 12, 'Units', 'normalized', 'Position', [0.75 0.7 0.2 0.1]);
handles.L_text = uicontrol('Style', 'text', 'String', 'Filter Order:', ...
'FontSize', 12, 'HorizontalAlignment', 'right', ...
'Units', 'normalized', 'Position', [0.3 0.6 0.4 0.1]);
handles.L_edit = uicontrol('Style', 'edit', 'String', num2str(handles.L), ...
'FontSize', 12, 'Units', 'normalized', 'Position', [0.75 0.6 0.2 0.1]);
handles.design_button = uicontrol('Style', 'pushbutton', 'String', 'Design Filter', ...
'FontSize', 12, 'Units', 'normalized', 'Position', [0.35 0.4 0.3 0.1], ...
'Callback', @design_callback);
handles.clear_button = uicontrol('Style', 'pushbutton', 'String', 'Clear Plot', ...
'FontSize', 12, 'Units', 'normalized', 'Position', [0.35 0.3 0.3 0.1], ...
'Callback', @clear_callback);
handles.plot_axes = axes('Units
参考GPT和自己的思路:
非常感谢您的咨询!针对您的问题,我提供以下回答:
对于matlab gui设计FIR滤波器,您可以采用以下步骤来实现:
界面设计:首先,您需要设计一个matlab gui界面,包括频率响应图、滤波器系数显示、输入输出音频等元素。
窗函数法设计:采用现成的matlab函数进行滤波器系数计算,如fir1函数。这里需要注意的是,窗函数的选择对滤波器性能影响非常大,需要选取合适的窗函数。
频率抽样法设计:类似于窗函数法,采用现成的matlab函数进行滤波器系数计算,如fir2函数。需要注意的是,频率抽样法的设计需要明确滤波器的通带、阻带和过渡带等参数。
音频导入和滤波:使用matlab的音频处理函数对导入的音频进行滤波处理,如filter函数。需要注意的是,滤波器的响应特性需要与音频的采样率和信号频率相匹配。
总的来说,要实现matlab gui设计FIR滤波器的功能,需要结合matlab图形界面设计和信号处理算法的知识。如果您对某个环节不熟悉,可以参考matlab官方文档或是咨询matlab专业人士。希望这些信息对您有所帮助!
参考GPT和自己的思路,要在MATLAB GUI中实现窗函数法和频率抽样法设计FIR滤波器的功能以及导入音频进行滤波的功能,可以按照以下步骤操作:
1.在MATLAB中打开GUIDE(GUI开发环境)并创建一个新的GUI。
2.在GUI中添加所需的控件,如文本框、按钮、滑块等。
3.为窗函数法和频率抽样法设计FIR滤波器的按钮添加回调函数,以实现设计滤波器的功能。
4.为导入音频文件的按钮添加回调函数,以实现从文件中读取音频数据的功能。
5.在GUI中添加一个Axes控件,用于显示滤波后的音频信号。
6.在回调函数中使用MATLAB内置的FIR滤波器设计函数(如fir1、fir2等)来设计滤波器,并使用filter函数对输入音频进行滤波。
7.将滤波后的音频数据绘制在Axes控件中。
以下是一个示例代码,其中包含了窗函数法和频率抽样法设计FIR滤波器的功能以及导入音频进行滤波的功能:
function varargout = fir_filter_gui(varargin)
% FIR_FILTER_GUI MATLAB code for fir_filter_gui.fig
% FIR_FILTER_GUI, by itself, creates a new FIR_FILTER_GUI or raises the existing
% singleton*.
%
% H = FIR_FILTER_GUI returns the handle to a new FIR_FILTER_GUI or the handle to
% the existing singleton*.
%
% FIR_FILTER_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FIR_FILTER_GUI.M with the given input arguments.
%
% FIR_FILTER_GUI('Property','Value',...) creates a new FIR_FILTER_GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before fir_filter_gui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to fir_filter_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 fir_filter_gui
% Last Modified by GUIDE v2.5 19-Mar-2023 16:14:58
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @fir_filter_gui_OpeningFcn, ...
'gui_OutputFcn', @fir_filter_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
参考GPT和自己的思路:对于在 MATLAB GUI 中设计 FIR 滤波器,可以按照以下步骤进行:
在 MATLAB 的 GUIDE 工具中创建 GUI 界面。
1 在界面上添加用于输入滤波器参数的控件,例如滑动条、文本框等。
2 在界面上添加用于导入音频文件的控件,例如按钮等。
3 在界面上添加用于绘制滤波器频率响应的控件,例如画布等。
4 使用窗函数法实现 FIR 滤波器的设计,并将结果绘制在频率响应控件上。
5 使用频率抽样法实现 FIR 滤波器的设计,并将结果绘制在频率响应控件上。
6 将导入的音频信号输入到滤波器中,输出滤波后的音频信号,并将结果展示在界面上。
7 下面是一个示例代码,其中使用窗函数法设计低通 FIR 滤波器,并使用 freqz 函数绘制频率响应。您可以在此基础上进行进一步修改,添加频率抽样法设计和导入音频的功能。
function varargout = FIR_filter_gui(varargin)
% FIR_FILTER_GUI MATLAB code for FIR_filter_gui.fig
% FIR_FILTER_GUI, by itself, creates a new FIR_FILTER_GUI or raises the existing
% singleton*.
%
% H = FIR_FILTER_GUI returns the handle to a new FIR_FILTER_GUI or the handle to
% the existing singleton*.
%
% FIR_FILTER_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FIR_FILTER_GUI.M with the given input arguments.
%
% FIR_FILTER_GUI('Property','Value',...) creates a new FIR_FILTER_GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before FIR_filter_gui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to FIR_filter_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 FIR_filter_gui
% Last Modified by GUIDE v2.5 18-Mar-2023 02:47:49
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @FIR_filter_gui_OpeningFcn, ...
'gui_OutputFcn', @FIR_filter_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 on button press in freqsamp_btn.
function freqsamp_btn_Callback(hObject, eventdata, handles)
% hObject handle to freqsamp_btn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get filter parameters from GUI
N = str2double(get(handles.order_edit, 'String'));
Fp = str2double(get(handles.cutoff_edit, 'String'));
Fs = str2double(get(handles.sampling_edit, 'String'));
Wn = Fp / (Fs/2);
% Calculate filter coefficients using freqs function
h = freqs(ones(1, N+1), [1 -exp(1j*2*pi*Wn)], linspace(0, pi, 1024));
h = real(ifft(h)); % Convert to time domain
% Plot filter frequency response
axes(handles.freqresp_axes);
w = linspace(0, pi, 1024);
plot(w/pi*Fs/2, 20*log10(abs(freqz(h, 1, w, Fs))));
grid on;
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
% Plot filter impulse response
axes(handles.impresp_axes);
n = -N:N;
stem(n, h);
grid on;
xlabel('Sample number');
ylabel('Amplitude');
% Save filter coefficients to handles structure
handles.h = h;
% Update handles structure
guidata(hObject, handles);
在这个回调函数中,首先从GUI获取了滤波器参数,包括滤波器阶数、截止频率和采样频率。然后使用 freqs 函数计算滤波器系数。 freqs 函数可以计算数字滤波器的频率响应,它的第一个输入是滤波器的分子系数,第二个输入是滤波器的分母系数,第三个输入是一组频率值。在这里,我们将分子系数设置为全1,分母系数设置为 $1-e^{j2\pi f_c}$,其中 $f_c$ 是归一化的截止频率($0\leq f_c \leq 0.5$)。最后一个输入是一个向量,表示要计算频率响应的频率值。我们使用 linspace 函数生成 1024 个频率值,范围从 0 到 $\pi$。
由于 freqs 函数返回复数频率响应,我们使用 ifft 函数将其转换为时域系数。然后,我们使用 freqz 函数计算滤波器的频率响应,并将其绘制在 GUI 中的“Frequency Response”图形对象中。类似地,我们绘制滤波器的脉冲响应在 GUI 中的“Impulse Response”图形对象中。
最后,我们将滤波器系数保存在 GUI 的 handles 结构中,以便在后面的回调函数中使用。
不知道你这个问题是否已经解决, 如果还没有解决的话:1 matlab版本
2014a
2 参考文献
[1] 沈再阳.精通MATLAB信号处理[M].清华大学出版社,2015.
[2]高宝建,彭进业,王琳,潘建寿.信号与系统——使用MATLAB分析与实现[M].清华大学出版社,2020.
[3]王文光,魏少明,任欣.信号处理与系统分析的MATLAB实现[M].电子工业出版社,2018.
以下答案由GPT-3.5大模型与博主波罗歌共同编写:
在 matlab gui 中添加频率抽样法设计 FIR 滤波器的功能
添加回调函数
在 MATLAB Gui 的编辑界面中选择按钮,右键选择“回调函数”,添加一个新的回调函数 handleFrequencySamplingFilter。
实现回调函数
下面是实现 handleFrequencySamplingFilter 的代码示例:
function handleFrequencySamplingFilter(hObject, eventdata, handles)
% 获取控件输入
fs = str2double(get(handles.edit_sampling_freq, 'String'));
f1 = str2double(get(handles.edit_passband_freq, 'String'));
f2 = str2double(get(handles.edit_stopband_freq, 'String'));
Ap = str2double(get(handles.edit_passband_gain, 'String'));
As = str2double(get(handles.edit_stopband_gain, 'String'));
% 计算滤波器
F = [0 f1 fs/2 fs/2+f2]; % 频率向量
A = [1 1 0 0]; % 响应向量
W = [1/Ap 1/As]; % 权重向量
n = firpmord(F, A, W, fs); % 获取最小阶数和截止频率
b = firpm(n, F, A, W); % 根据阶数获取滤波器系数
% 显示频率响应曲线
freqz(b, 1, 256, fs, handles.plot_frequency_resp);
在回调函数中,首先获取输入参数 fs、f1、f2、Ap 和 As,然后根据这些参数计算出滤波器的系数 b。最后,使用 freqz 函数绘制滤波器的幅频响应曲线并显示在 Axes 控件中。
导入音频进行滤波的功能
在 MATLAB Gui 的编辑界面中选择“应用滤波器”按钮,右键选择“回调函数”,添加一个新的回调函数 handleApplyFilter。
下面是实现 handleImportAudioFile 的代码示例:
function handleImportAudioFile(hObject, eventdata, handles)
% 导入音频文件
[filename, pathname] = uigetfile({'*.wav'}, '选择音频文件');
if ~(isequal(filename,0) || isequal(pathname,0))
handles.audio_file_path = fullfile(pathname, filename);
set(handles.edit_audio_file_path, 'String', handles.audio_file_path);
set(handles.btn_apply_filter, 'Enable', 'on')
end
guidata(hObject, handles);
该回调函数会弹出文件对话框,让用户选择需要导入的音频文件。如果用户选择了文件,就将文件路径记录到 handles 结构体中,并将路径显示在 Textbox 中,同时将“应用滤波器”按钮设置为可用状态。
下面是实现 handleApplyFilter 的代码示例:
function handleApplyFilter(hObject, eventdata, handles)
% 读取音频文件
[x, fs] = audioread(handles.audio_file_path);
% 应用滤波器
b = handles.b;
y = filter(b, 1, x);
% 原始音频信号
axes(handles.plot_audio_signal);
t = (0:size(x,1)-1)/fs;
plot(t, x);
xlabel('Time (s)');
ylabel('Amplitude');
title('Original Audio Signal');
% 滤波后音频信号
axes(handles.plot_filtered_signal);
t = (0:size(y,1)-1)/fs;
plot(t, y);
xlabel('Time (s)');
ylabel('Amplitude');
title('Filtered Audio Signal');
该回调函数会首先读取保存在 handles 结构体中的滤波器系数 b,然后读取用户导入的音频文件,并使用 filter 函数应用滤波器,将原始音频信号和滤波后的音频信号绘制在 Axes 控件中(分别是 plot_audio_signal 和 plot_filtered_signal)。
完整代码示例
function fir_design_gui(varargin)
% FIR_DESIGN_GUI MATLAB code for fir_design_gui.fig
% FIR_DESIGN_GUI, by itself, creates a new FIR_DESIGN_GUI or raises the existing
% singleton*.
%
% H = FIR_DESIGN_GUI returns the handle to a new FIR_DESIGN_GUI or the handle to
% the existing singleton*.
%
% FIR_DESIGN_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FIR_DESIGN_GUI.M with the given input arguments.
%
% FIR_DESIGN_GUI('Property','Value',...) creates a new FIR_DESIGN_GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before fir_design_gui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to fir_design_gui_OpeningFcn via varargin.
%
% *See GUI Options on <a href="matlab:help gui_options">GUIDE's Tools menu</a>. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help fir_design_gui
% Last Modified by GUIDE v2.5 13-Nov-2021 16:41:52
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @fir_design_gui_OpeningFcn, ...
'gui_OutputFcn', @fir_design_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 fir_design_gui is made visible.
function fir_design_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 fir_design_gui (see VARARGIN)
% Choose default command line output for fir_design_gui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes fir_design_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% 初始化所有控件
set(handles.edit_sampling_freq, 'String', '8000');
set(handles.edit_passband_freq, 'String', '1200');
set(handles.edit_stopband_freq, 'String', '2000');
set(handles.edit_passband_gain, 'String', '0.5');
set(handles.edit_stopband_gain, 'String', '40');
set(handles.btn_frequency_sampling, 'Enable', 'off');
set(handles.btn_apply_filter, 'Enable', 'off');
% --- Outputs from this function are returned to the command line.
function varargout = fir_design_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;
% --- Executes on button press in btn_frequency_sampling.
function btn_frequency_sampling_Callback(hObject, eventdata, handles)
% hObject handle to btn_frequency_sampling (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handleFrequencySamplingFilter(hObject, eventdata, handles);
% --- Executes on button press in btn_import_audio_file.
function btn_import_audio_file_Callback(hObject, eventdata, handles)
% hObject handle to btn_import_audio_file (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handleImportAudioFile(hObject, eventdata, handles);
% --- Executes on button press in btn_apply_filter.
function btn_apply_filter_Callback(hObject, eventdata, handles)
% hObject handle to btn_apply_filter (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handleApplyFilter(hObject, eventdata, handles);
function edit_sampling_freq_Callback(hObject, eventdata, handles)
% hObject handle to edit_sampling_freq (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function edit_sampling_freq_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_sampling_freq (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
function edit_passband_freq_Callback(hObject, eventdata, handles)
% hObject handle to edit_passband_freq (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function edit_passband_freq_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_passband_freq (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
function edit_stopband_freq_Callback(hObject, eventdata, handles)
% hObject handle to edit_stopband_freq (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function edit_stopband_freq_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_stopband_freq (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
function edit_passband_gain_Callback(hObject, eventdata, handles)
% hObject handle to edit_passband_gain (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function edit_passband_gain_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_passband_gain (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
function edit_stopband_gain_Callback(hObject, eventdata, handles)
% hObject handle to edit_stopband_gain (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function edit_stopband_gain_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_stopband_gain (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 btn_frequency_sampling.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to btn_frequency_sampling (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function plot_frequency_resp_CreateFcn(hObject, eventdata, handles)
% hObject handle to plot_frequency_resp (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes during object creation, after setting all properties.
function edit_audio_file_path_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_audio_file_path (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes during object creation, after setting all properties.
function plot_audio_signal_CreateFcn(hObject, eventdata, handles)
% hObject handle to plot_audio_signal (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes during object creation, after setting all properties.
function plot_filtered_signal_CreateFcn(hObject, eventdata, handles)
% hObject handle to plot_filtered_signal (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
function handleFrequencySamplingFilter(hObject, eventdata, handles)
% 获取控件输入
fs = str2double(get(handles.edit_sampling_freq, 'String'));
f1 = str2double(get(handles.edit_passband_freq, 'String'));
f2 = str2double(get(handles.edit_stopband_freq, 'String'));
Ap = str2double(get(handles.edit_passband_gain, 'String'));
As = str2double(get(handles.edit_stopband_gain, 'String'));
% 计算滤波器
F = [0 f1 fs/2 fs/2+f2]; % 频率向量
A = [1 1 0 0]; % 响应向量
W = [1/Ap 1/As]; % 权重向量
n = firpmord(F, A, W, fs); % 获取最小阶数和截止频率
b = firpm(n, F, A, W); % 根据阶数获取滤波器系数
% 记录滤波器系数
handles.b = b;
% 显示频率响应曲线
freqz(b, 1, 256, fs, handles.plot_frequency_resp);
% 更新 handles 结构体
guidata(hObject, handles);
function handleImportAudioFile(hObject, eventdata, handles)
% 导入音频文件
[filename, pathname] = uigetfile({'*.wav'}, '选择音频文件');
if ~(isequal(filename,0) || isequal(pathname,0))
handles.audio_file_path = fullfile(pathname, filename);
set(handles.edit_audio_file_path, 'String', handles.audio_file_path);
set(handles.btn_apply_filter, 'Enable', 'on')
end
guidata(hObject, handles);
function handleApplyFilter(hObject, eventdata, handles)
% 读取音频文件
[x, fs] = audioread(handles.audio_file_path);
% 应用滤波器
b = handles.b;
y = filter(b, 1, x);
% 原始音频信号
axes(handles.plot_audio_signal);
t = (0:size(x,1)-1)/fs;
plot(t, x);
xlabel('Time (s)');
ylabel('Amplitude');
title('Original Audio Signal');
% 滤波后音频信号
axes(handles.plot_filtered_signal);
t = (0:size(y,1)-1)/fs;
plot(t, y);
xlabel('Time (s)');
ylabel('Amplitude');
title('Filtered Audio Signal');
如果我的回答解决了您的问题,请采纳!