用matlab gui设计FIR滤波器(用窗函数法和频率抽样法)

现在有一个设计好的界面,用窗函数法设计fir滤波器,想要在这个的基础上加上频率抽样法设计fir滤波器的功能,以及导入音频进行滤波的功能

建议阅读:https://blog.csdn.net/Gxust_Veneno/article/details/119705339?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167905845216800215075912%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=167905845216800215075912&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~pc_rank_34-1-119705339-null-null.142^v74^pc_new_rank,201^v4^add_ask,239^v2^insert_chatgpt&utm_term=%E7%94%A8matlab%20gui%E8%AE%BE%E8%AE%A1FIR%E6%BB%A4%E6%B3%A2%E5%99%A8%EF%BC%88%E7%94%A8%E7%AA%97%E5%87%BD%E6%95%B0%E6%B3%95%E5%92%8C%E9%A2%91%E7%8E%87%E6%8A%BD%E6%A0%B7%E6%B3%95%EF%BC%89&spm=1018.2226.3001.4449

该回答引用于gpt与OKX安生共同编写:
  • 该回答引用于gpt与OKX安生共同编写:

好的,为了实现您的需求,需要进行以下步骤:

  • 添加频率抽样法设计FIR滤波器的功能:

    • 在GUI界面上增加一个按钮或菜单,用于选择并切换窗函数法和频率抽样法。
    • 当用户选择频率抽样法时,程序应该提示用户输入滤波器类型、采样频率、截止频率等参数,并根据这些参数计算出FIR滤波器的系数。
    • 系数计算完成后,可以将其显示在界面上,并保存到文件中以备以后使用。
  • 添加导入音频进行滤波的功能:

    • 在GUI界面上增加一个按钮或菜单,用于导入音频文件。
    • 导入音频文件后,程序应该将其显示在界面上,并提供一些控件(如滑块)用于控制滤波器参数。
    • 用户可以通过控件调节滤波器参数,并实时预览滤波效果。当用户满意时,可以将滤波后的音频保存到文件中。

为了实现以上功能,您可以使用MATLAB内置的DSP工具箱,其中包含了许多用于数字信号处理的函数和工具。具体实现过程可以参考MATLAB DSP工具箱的文档和示例代码。

  • 这里提供一个MATLAB GUI设计FIR滤波器(窗函数法和频率抽样法)以及导入音频进行滤波的代码,您可以参考一下:

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滤波器,您可以采用以下步骤来实现:

  1. 界面设计:首先,您需要设计一个matlab gui界面,包括频率响应图、滤波器系数显示、输入输出音频等元素。

  2. 窗函数法设计:采用现成的matlab函数进行滤波器系数计算,如fir1函数。这里需要注意的是,窗函数的选择对滤波器性能影响非常大,需要选取合适的窗函数。

  3. 频率抽样法设计:类似于窗函数法,采用现成的matlab函数进行滤波器系数计算,如fir2函数。需要注意的是,频率抽样法的设计需要明确滤波器的通带、阻带和过渡带等参数。

  4. 音频导入和滤波:使用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 结构中,以便在后面的回调函数中使用。

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

以下答案由GPT-3.5大模型与博主波罗歌共同编写:
在 matlab gui 中添加频率抽样法设计 FIR 滤波器的功能

  1. 添加 GUI 控件
    要添加频率抽样法设计 FIR 滤波器的功能,需要在 GUI 界面上添加控件,包括以下几个:
  • 一个 Textbox,用于输入采样频率 fs;
  • 一个 Textbox,用于输入通带截止频率 f1;
  • 一个 Textbox,用于输入阻带截止频率 f2;
  • 一个 Textbox,用于输入通带最大衰减 dB;
  • 一个 Textbox,用于输入阻带最小衰减 dB;
  • 一个 Button,用于触发频率抽样法设计 FIR 滤波器的计算;
  • 一个 Axes,用于显示滤波器的幅频响应曲线。
  1. 添加回调函数
    在 MATLAB Gui 的编辑界面中选择按钮,右键选择“回调函数”,添加一个新的回调函数 handleFrequencySamplingFilter。

  2. 实现回调函数

下面是实现 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 控件中。

导入音频进行滤波的功能

  1. 添加 GUI 控件
    要添加导入音频进行滤波的功能,需要在 GUI 界面上添加以下几个控件:
  • 一个 Button,用于触发导入音频文件的功能;
  • 一个 Textbox,用于显示导入的音频文件路径;
  • 一个 Button,用于触发滤波器应用的功能;
  • 一个 Axes,用于显示原始音频信号和滤波后的音频信号。
  1. 添加回调函数
    在 MATLAB Gui 的编辑界面中选择“导入音频”按钮,右键选择“回调函数”,添加一个新的回调函数 handleImportAudioFile。

在 MATLAB Gui 的编辑界面中选择“应用滤波器”按钮,右键选择“回调函数”,添加一个新的回调函数 handleApplyFilter。

  1. 实现回调函数

下面是实现 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');

如果我的回答解决了您的问题,请采纳!