MATLAB GUI之信号发生器

效果展示

image
image
image
image

框图展示

image

代码设计

点击查看代码
function varargout = WaveDisplay(varargin)
% WAVEDISPLAY MATLAB code for WaveDisplay.fig
%      WAVEDISPLAY, by itself, creates a new WAVEDISPLAY or raises the existing
%      singleton*.
%
%      H = WAVEDISPLAY returns the handle to a new WAVEDISPLAY or the handle to
%      the existing singleton*.
%
%      WAVEDISPLAY('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in WAVEDISPLAY.M with the given input arguments.
%
%      WAVEDISPLAY('Property','Value',...) creates a new WAVEDISPLAY or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before WaveDisplay_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to WaveDisplay_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 WaveDisplay

% Last Modified by GUIDE v2.5 22-Aug-2025 08:56:16

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @WaveDisplay_OpeningFcn, ...
                   'gui_OutputFcn',  @WaveDisplay_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 WaveDisplay is made visible.
function WaveDisplay_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 WaveDisplay (see VARARGIN)

% Choose default command line output for WaveDisplay
handles.output = hObject;
diaplay_wave(handles);      %绘制初始波形
% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = WaveDisplay_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 slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (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
Am = get(handles.slider1,'Value');      % 获取滑动条1幅度值
Am_arg = sprintf('%f',Am);              % 数据格式化为字符串或字符向量
set(handles.edit1,'String',Am_arg);     % 编辑框1显示幅度值
diaplay_wave(handles);

% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (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 slider2_Callback(hObject, eventdata, handles)
% hObject    handle to slider2 (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
Ph = get(handles.slider2,'Value');        % 获取滑动条2相位值
Ph_arg = sprintf('%f',Ph);                % 数据格式化为字符串或字符向量
set(handles.edit2,'String',Ph_arg);       % 编辑框2显示相位值
diaplay_wave(handles);

% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider2 (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 slider3_Callback(hObject, eventdata, handles)
% hObject    handle to slider2 (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
Fr = get(handles.slider3,'Value');        % 获取滑动条3频率值
Fr_arg = sprintf('%f',Fr);                % 数据格式化为字符串或字符向量
set(handles.edit3,'String',Fr_arg);       % 编辑框3显示频率值
diaplay_wave(handles);

% --- Executes during object creation, after setting all properties.
function slider3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider2 (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



function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (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 edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double
Am = str2double(get(handles.edit1,'String'));   % 字符串或字符向量格式化为数据
set(handles.slider1,'Value',Am);                % 滑动条1的值跟着文本框1变化 
diaplay_wave(handles);                          % 重新绘制波形

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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 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
Ph = str2double(get(handles.edit2,'String'));   % 字符串或字符向量格式化为数据
set(handles.slider2,'Value',Ph);                % 滑动条2的值跟着文本框2变化
diaplay_wave(handles);                          % 重新绘制波形

% --- 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,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (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 edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double
Fr = str2double(get(handles.edit3,'String'));   % 字符串或字符向量格式化为数据
set(handles.slider3,'Value',Fr);                % 滑动条3的值跟着文本框3变化
diaplay_wave(handles);                          % 重新绘制波形

function diaplay_wave(handles)
global Fs
global Am
global Ph
global Fr
Fs = 44100;                             % 采样频率Fs(Hz)
dt = 1.0/Fs;
T = 2;                                   
N = T/dt;                               % 最大X坐标
t = linspace(0,T,N);                    % X坐标轴及其范围 

% 获取文本框1字符/字符串,并格式化为数据类型(幅度)
Am = str2double(get(handles.edit1,'String'));       
% 获取文本框2字符/字符串,并格式化为数据类型(相位)
Ph = str2double(get(handles.edit2,'String'));       
% 获取文本框3字符/字符串,并格式化为数据类型(频率)                                        
Fr = str2double(get(handles.edit3,'String'));     

var = get(handles.popupmenu1,'Value'); 
switch var
    case 1 
        f0 = Am*sin(2*pi*Fr*t + Ph);            % 正弦函数
    case 2 
        f0 = Am*cos(2*pi*Fr*t + Ph);            % 余弦函数
    case 3 
        f0 = Am*square(2*pi*Fr*t + Ph);         % 方波
    case 4  
        f0 = Am*sawtooth(2*pi*Fr*t + Ph,0.5);   % 三角波 
    case 5  
        f0 = Am*sawtooth(pi*Fr*t + Ph);         % 锯齿波
    otherwise
        disp('Error Option!');
end                                                                                
    
plot(handles.axes1,t, f0, 'b', 'LineWidth', 1.5);       % 绘制函数图像,红色,线宽为1.5
axis(handles.axes1,[0 0.5 -2500 2500]);                 % 确定横/纵坐标轴范围
grid(handles.axes1);


% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (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 popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1
diaplay_wave(handles);                          % 重新绘制波形

% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (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

posted @ 2025-08-22 10:19  AlwaysComb  阅读(12)  评论(0)    收藏  举报