我需要的是可以运行的matlab代码来实现上述要求
时间: 2024-03-16 12:44:26 浏览: 68
好的,以下是一个简单的MATLAB程序,实现了巴特沃斯滤波器的模拟设计和数字转换,并以GUI的形式展示了滤波器的频率响应幅度图:
```matlab
function varargout = butterworth_gui(varargin)
% BUTTERWORTH_GUI MATLAB code for butterworth_gui.fig
% BUTTERWORTH_GUI, by itself, creates a new BUTTERWORTH_GUI or raises the existing
% singleton*.
%
% H = BUTTERWORTH_GUI returns the handle to a new BUTTERWORTH_GUI or the handle to
% the existing singleton*.
%
% BUTTERWORTH_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in BUTTERWORTH_GUI.M with the given input arguments.
%
% BUTTERWORTH_GUI('Property','Value',...) creates a new BUTTERWORTH_GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before butterworth_gui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to butterworth_gui_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', @butterworth_gui_OpeningFcn, ...
'gui_OutputFcn', @butterworth_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 butterworth_gui is made visible.
function butterworth_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 butterworth_gui (see VARARGIN)
% Choose default command line output for butterworth_gui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes butterworth_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = butterworth_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 design_button.
function design_button_Callback(hObject, eventdata, handles)
% hObject handle to design_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% read input parameters from GUI
order = str2double(get(handles.order_edit, 'String'));
cutoff = str2double(get(handles.cutoff_edit, 'String'));
ripple = str2double(get(handles.ripple_edit, 'String'));
fs = str2double(get(handles.fs_edit, 'String'));
% design analog Butterworth filter
[b, a] = butter(order, 2*cutoff/fs);
% convert to digital filter using bilinear transform
[z, p, k] = butter(order, 2*cutoff/fs);
[bz, az] = bilinear(z, p, k, fs);
% plot frequency response
freq = linspace(0, fs/2, 10000);
[h, w] = freqz(bz, az, freq);
plot(handles.freq_axes, freq, 20*log10(abs(h)))
title(handles.freq_axes, 'Frequency Response')
xlabel(handles.freq_axes, 'Frequency (Hz)')
ylabel(handles.freq_axes, 'Magnitude (dB)')
% save filter coefficients
handles.b = bz;
handles.a = az;
guidata(hObject, handles);
% --- Executes on button press in clear_button.
function clear_button_Callback(hObject, eventdata, handles)
% hObject handle to clear_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% clear input fields
set(handles.order_edit, 'String', '');
set(handles.cutoff_edit, 'String', '');
set(handles.ripple_edit, 'String', '');
set(handles.fs_edit, 'String', '');
% clear frequency response plot
cla(handles.freq_axes);
% clear filter coefficients
handles.b = [];
handles.a = [];
guidata(hObject, handles);
% --- Executes on selection change in transform_popup.
function transform_popup_Callback(hObject, eventdata, handles)
% hObject handle to transform_popup (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns transform_popup contents as cell array
% contents{get(hObject,'Value')} returns selected item from transform_popup
% read selected transform method from GUI
contents = cellstr(get(hObject,'String'));
method = contents{get(hObject,'Value')};
% convert filter coefficients using selected method
if strcmp(method, 'Impulse Invariant')
if ~isempty(handles.b) && ~isempty(handles.a)
[bz, az] = impinvar(handles.b, handles.a);
handles.b = bz;
handles.a = az;
guidata(hObject, handles);
% plot frequency response
freq = linspace(0, fs/2, 10000);
[h, w] = freqz(bz, az, freq);
plot(handles.freq_axes, freq, 20*log10(abs(h)))
title(handles.freq_axes, 'Frequency Response')
xlabel(handles.freq_axes, 'Frequency (Hz)')
ylabel(handles.freq_axes, 'Magnitude (dB)')
end
elseif strcmp(method, 'Bilinear Transform')
if ~isempty(handles.b) && ~isempty(handles.a)
[bz, az] = bilinear(handles.b, handles.a, fs);
handles.b = bz;
handles.a = az;
guidata(hObject, handles);
% plot frequency response
freq = linspace(0, fs/2, 10000);
[h, w] = freqz(bz, az, freq);
plot(handles.freq_axes, freq, 20*log10(abs(h)))
title(handles.freq_axes, 'Frequency Response')
xlabel(handles.freq_axes, 'Frequency (Hz)')
ylabel(handles.freq_axes, 'Magnitude (dB)')
end
end
% --- Executes during object creation, after setting all properties.
function transform_popup_CreateFcn(hObject, eventdata, handles)
% hObject handle to transform_popup (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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
function order_edit_Callback(hObject, eventdata, handles)
% hObject handle to order_edit (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 order_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to order_edit (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
function cutoff_edit_Callback(hObject, eventdata, handles)
% hObject handle to cutoff_edit (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 cutoff_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to cutoff_edit (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
function ripple_edit_Callback(hObject, eventdata, handles)
% hObject handle to ripple_edit (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 ripple_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to ripple_edit (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
function fs_edit_Callback(hObject, eventdata, handles)
% hObject handle to fs_edit (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 fs_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to fs_edit (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
```
需要注意的是,上述代码中的GUI设计需要在MATLAB的GUIDE中完成,这里就不再赘述了。此外,程序中使用了MATLAB内置的butter函数来进行模拟滤波器的设计,以及bilinear函数和impinvar函数来进行数字滤波器的转换。用户需要在GUI中输入滤波器的阶数、截止频率、通带最大纹波和采样频率等参数,然后选择数字滤波器的转换方法,点击设计按钮,程序会计算滤波器的系数,并绘制滤波器的频率响应幅度图。用户还可以点击清除按钮清除输入和输出,重新进行设计。
阅读全文