基于Matlab GUI的信号发生器界面程序示例

server/2024/9/22 23:50:10/

前些日子,被一朋友拜托了一课设,不是很难,但基于matlab GUI的设计中文论坛资源较少,所以我做完顺便分享一下。

程序主要内容:

效果展示:

主要代码:

代码展示,复制粘贴不能直接执行,请在code文件中下载,并在matlab界面中打开该code文件执行,才可正常运行。

classdef WaveCreater < matlab.apps.AppBase% Properties that correspond to app componentsproperties (Access = public)UIFigure              matlab.ui.FigureSlider2               matlab.ui.control.SliderSlider_2              matlab.ui.control.SliderSlider                matlab.ui.control.SliderwitdthN               matlab.ui.control.NumericEditFieldLabel_7               matlab.ui.control.LabeldelayN                matlab.ui.control.NumericEditFieldLabel_6               matlab.ui.control.LabelButton_3              matlab.ui.control.ButtonCheckBox              matlab.ui.control.CheckBoxButton_2              matlab.ui.control.ButtonDropDown_2            matlab.ui.control.DropDownLabel_5               matlab.ui.control.LabelEroP                  matlab.ui.control.NumericEditFieldLabel_4               matlab.ui.control.LabeltimessEditField       matlab.ui.control.NumericEditFieldtimessEditFieldLabel  matlab.ui.control.LabelsEditField            matlab.ui.control.EditFieldsEditFieldLabel       matlab.ui.control.Labelintensi               matlab.ui.control.NumericEditFieldLabel_3               matlab.ui.control.LabelKHzEditField          matlab.ui.control.NumericEditFieldLabel_2               matlab.ui.control.LabelDropDown              matlab.ui.control.DropDownLabel                 matlab.ui.control.LabelButton                matlab.ui.control.ButtonUIAxes                matlab.ui.control.UIAxesendproperties (Access = private)Check=false; % Descriptionendmethods (Access = private)function x = CreatW(app)% 参数设置Fs = app.timessEditField.Value;       % 采样率(每秒采样点数)T = 1/Fs;        % 采样周期L = str2double(app.sEditField.Value)*Fs;        % 信号长度t = (0:L-1)*T;   % 时间向量N = length(t); % 假设 t 是时间向量f =app.KHzEditField.Value/1000;          % 信号频率(Hz/μs)A = app.intensi.Value;           % 信号幅度ep=app.EroP.Value;          %噪声值x = zeros(1, N); % 预分配信号数组Wid=app.witdthN.Value;%锯齿波宽度dT=(1/f)*app.delayN.Value/(2*pi);if app.DropDown.ValueIndex==1    % 正弦波参数x = A * (sin(2*pi*f*(t+dT)) + ep/100*rand(1,L));elseif app.DropDown.ValueIndex==2 %方波x = A* (square(2*pi*(t+dT)*f, 50)+ep/100*rand(1,L));elseif app.DropDown.ValueIndex==3  %锯齿波x = A*(sawtooth(f*pi*(t+dT)*2, Wid)+rand(1,L)*ep/100);end% 绘制信号plot(app.UIAxes,t, x);app.Button_2.Enable="on";app.Button_3.Enable='on';endend% Callbacks that handle component eventsmethods (Access = private)% Value changed function: CheckBoxfunction CheckBoxValueChanged(app, event)if app.CheckBox.Value==1app.Check=true;app.Button.Enable="off";elseapp.Button.Enable='on';endif app.Checkapp.CreatW;endend% Button pushed function: Buttonfunction ButtonPushed(app, event)app.CreatW;end% Value changed function: DropDownfunction DropDownValueChanged(app, event)if app.Checkapp.CreatW;endif app.DropDown.ValueIndex==3app.witdthN.Enable='on';endend% Value changed function: KHzEditFieldfunction KHzEditFieldValueChanged(app, event)if app.Checkapp.CreatW;endif app.Slider.Value~=app.KHzEditField.Valueapp.Slider.Value=app.KHzEditField.Value;endend% Value changed function: intensifunction intensiValueChanged(app, event)if app.Checkapp.CreatW;endif  app.Slider_2.Value~=app.intensi.Valueapp.Slider_2.Value=app.intensi.Value;endend% Value changed function: sEditFieldfunction sEditFieldValueChanged(app, event)if app.Checkapp.CreatW;endend% Value changed function: timessEditFieldfunction timessEditFieldValueChanged(app, event)if app.Checkapp.CreatW;endend% Value changed function: EroPfunction EroPValueChanged(app, event)if app.Checkapp.CreatW;endend% Button pushed function: Button_2function Button_2Pushed(app, event)% 参数设置Fs = app.timessEditField.Value;       % 采样率(每秒采样点数)T = 1/Fs;        % 采样周期L = str2double(app.sEditField.Value)*Fs;        % 信号长度t = (0:L-1)*T;   % 时间向量N = length(t); % 假设 t 是时间向量f =app.KHzEditField.Value/1000;          % 信号频率(Hz/μs)A = app.intensi.Value;           % 信号幅度ep=app.EroP.Value;          %噪声值x = zeros(1, N); % 预分配信号数组Wid=app.witdthN.Value;%锯齿波宽度dT=(1/f)*app.delayN.Value/(2*pi);if app.DropDown.ValueIndex==1    % 正弦波参数x = A * (sin(2*pi*f*(t+dT)) + ep/100*rand(1,L));elseif app.DropDown.ValueIndex==2 %方波x = A* (square(2*pi*(t+dT)*f, 50)+ep/100*rand(1,L));elseif app.DropDown.ValueIndex==3  %锯齿波x = A*(sawtooth(f*pi*(t+dT)*2, Wid)+rand(1,L)*ep/100);end% 绘制信号figureplot(t, x);end% Value changed function: witdthNfunction witdthNValueChanged(app, event)if app.Checkapp.CreatW;endend% Value changed function: delayNfunction delayNValueChanged(app, event)if app.Checkapp.CreatW;endif app.Slider2.Value~=app.delayN.Valueapp.Slider2.Value=app.delayN.Value;endend% Button pushed function: Button_3function Button_3Pushed(app, event)% 获取用户选择的文件名x = CreatW(app);[filename, pathname] = uiputfile({'*.mat', 'MATLAB Files (*.mat)';...'*.*', 'All Files (*.*)'}, ...'Save Parameters');% 如果用户选择了文件名if ~isequal(filename,0)% 拼接完整的文件路径fullPath = fullfile(pathname, filename);% 这里可以保存你的参数或数据到选择的文件中% 例如,保存一个变量 myData 到 MAT 文件save(fullPath, 'x');endend% Value changed function: Sliderfunction SliderValueChanged(app, event)app.KHzEditField.Value = app.Slider.Value;if app.Checkapp.CreatW;endend% Value changed function: Slider_2function Slider_2ValueChanged(app, event)app.intensi.Value = app.Slider_2.Value;if app.Checkapp.CreatW;endend% Value changed function: Slider2function Slider2ValueChanged(app, event)app.delayN.Value = app.Slider2.Value;if app.Checkapp.CreatW;endendend% Component initializationmethods (Access = private)% Create UIFigure and componentsfunction createComponents(app)% Create UIFigure and hide until all components are createdapp.UIFigure = uifigure('Visible', 'off');app.UIFigure.Position = [100 100 755 568];app.UIFigure.Name = 'MATLAB App';% Create UIAxesapp.UIAxes = uiaxes(app.UIFigure);title(app.UIAxes, '信号波形')xlabel(app.UIAxes, '时间(μs)')ylabel(app.UIAxes, '信号强度')app.UIAxes.Position = [14 19 458 505];% Create Buttonapp.Button = uibutton(app.UIFigure, 'push');app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);app.Button.Position = [524 80 100 23];app.Button.Text = '生成信号';% Create Labelapp.Label = uilabel(app.UIFigure);app.Label.HorizontalAlignment = 'right';app.Label.Position = [546 533 53 22];app.Label.Text = '信号类型';% Create DropDownapp.DropDown = uidropdown(app.UIFigure);app.DropDown.Items = {'正弦波', '方波', '锯齿波'};app.DropDown.ValueChangedFcn = createCallbackFcn(app, @DropDownValueChanged, true);app.DropDown.Position = [614 533 100 22];app.DropDown.Value = '正弦波';% Create Label_2app.Label_2 = uilabel(app.UIFigure);app.Label_2.HorizontalAlignment = 'right';app.Label_2.Position = [516 492 84 22];app.Label_2.Text = '信号频率(KHz)';% Create KHzEditFieldapp.KHzEditField = uieditfield(app.UIFigure, 'numeric');app.KHzEditField.Limits = [100 200];app.KHzEditField.ValueChangedFcn = createCallbackFcn(app, @KHzEditFieldValueChanged, true);app.KHzEditField.HorizontalAlignment = 'center';app.KHzEditField.Position = [615 492 100 22];app.KHzEditField.Value = 100;% Create Label_3app.Label_3 = uilabel(app.UIFigure);app.Label_3.HorizontalAlignment = 'right';app.Label_3.Position = [555 403 53 22];app.Label_3.Text = '信号幅度';% Create intensiapp.intensi = uieditfield(app.UIFigure, 'numeric');app.intensi.Limits = [0 10];app.intensi.ValueChangedFcn = createCallbackFcn(app, @intensiValueChanged, true);app.intensi.HorizontalAlignment = 'center';app.intensi.Position = [623 403 100 22];app.intensi.Value = 1;% Create sEditFieldLabelapp.sEditFieldLabel = uilabel(app.UIFigure);app.sEditFieldLabel.HorizontalAlignment = 'right';app.sEditFieldLabel.Position = [508 226 90 22];app.sEditFieldLabel.Text = '显示范围(μs)';% Create sEditFieldapp.sEditField = uieditfield(app.UIFigure, 'text');app.sEditField.InputType = 'digits';app.sEditField.ValueChangedFcn = createCallbackFcn(app, @sEditFieldValueChanged, true);app.sEditField.HorizontalAlignment = 'center';app.sEditField.Position = [613 226 100 22];app.sEditField.Value = '20';% Create timessEditFieldLabelapp.timessEditFieldLabel = uilabel(app.UIFigure);app.timessEditFieldLabel.HorizontalAlignment = 'right';app.timessEditFieldLabel.Position = [489 185 110 22];app.timessEditFieldLabel.Text = '采样率(times/μs)';% Create timessEditFieldapp.timessEditField = uieditfield(app.UIFigure, 'numeric');app.timessEditField.Limits = [1 100];app.timessEditField.ValueChangedFcn = createCallbackFcn(app, @timessEditFieldValueChanged, true);app.timessEditField.HorizontalAlignment = 'center';app.timessEditField.Position = [614 185 100 22];app.timessEditField.Value = 5;% Create Label_4app.Label_4 = uilabel(app.UIFigure);app.Label_4.HorizontalAlignment = 'right';app.Label_4.Position = [486 112 112 22];app.Label_4.Text = '噪声相对强度(%)';% Create EroPapp.EroP = uieditfield(app.UIFigure, 'numeric');app.EroP.Limits = [0 100];app.EroP.ValueChangedFcn = createCallbackFcn(app, @EroPValueChanged, true);app.EroP.HorizontalAlignment = 'center';app.EroP.Position = [613 112 100 22];app.EroP.Value = 5;% Create Label_5app.Label_5 = uilabel(app.UIFigure);app.Label_5.HorizontalAlignment = 'right';app.Label_5.Position = [546 150 53 22];app.Label_5.Text = '噪声类型';% Create DropDown_2app.DropDown_2 = uidropdown(app.UIFigure);app.DropDown_2.Items = {'高斯'};app.DropDown_2.Position = [614 150 100 22];app.DropDown_2.Value = '高斯';% Create Button_2app.Button_2 = uibutton(app.UIFigure, 'push');app.Button_2.ButtonPushedFcn = createCallbackFcn(app, @Button_2Pushed, true);app.Button_2.Enable = 'off';app.Button_2.Position = [523 41 185 32];app.Button_2.Text = '自定义波形图像';% Create CheckBoxapp.CheckBox = uicheckbox(app.UIFigure);app.CheckBox.ValueChangedFcn = createCallbackFcn(app, @CheckBoxValueChanged, true);app.CheckBox.Text = '实时生成';app.CheckBox.Position = [646 81 70 22];% Create Button_3app.Button_3 = uibutton(app.UIFigure, 'push');app.Button_3.ButtonPushedFcn = createCallbackFcn(app, @Button_3Pushed, true);app.Button_3.Enable = 'off';app.Button_3.Position = [524 10 190 23];app.Button_3.Text = '保存波形数据';% Create Label_6app.Label_6 = uilabel(app.UIFigure);app.Label_6.HorizontalAlignment = 'right';app.Label_6.Position = [539 321 65 22];app.Label_6.Text = '相位偏移角';% Create delayNapp.delayN = uieditfield(app.UIFigure, 'numeric');app.delayN.Limits = [0 6.28318530717959];app.delayN.ValueChangedFcn = createCallbackFcn(app, @delayNValueChanged, true);app.delayN.HorizontalAlignment = 'center';app.delayN.Position = [619 321 100 22];% Create Label_7app.Label_7 = uilabel(app.UIFigure);app.Label_7.HorizontalAlignment = 'right';app.Label_7.Enable = 'off';app.Label_7.Position = [63 533 283 22];app.Label_7.Text = '锯齿波上升比(为0是标准锯齿波,为0.5是三角波)';% Create witdthNapp.witdthN = uieditfield(app.UIFigure, 'numeric');app.witdthN.ValueChangedFcn = createCallbackFcn(app, @witdthNValueChanged, true);app.witdthN.Enable = 'off';app.witdthN.Position = [361 533 100 22];% Create Sliderapp.Slider = uislider(app.UIFigure);app.Slider.Limits = [100 200];app.Slider.ValueChangedFcn = createCallbackFcn(app, @SliderValueChanged, true);app.Slider.Position = [564 469 150 3];app.Slider.Value = 100;% Create Slider_2app.Slider_2 = uislider(app.UIFigure);app.Slider_2.Limits = [0 10];app.Slider_2.ValueChangedFcn = createCallbackFcn(app, @Slider_2ValueChanged, true);app.Slider_2.Position = [565 382 150 3];% Create Slider2app.Slider2 = uislider(app.UIFigure);app.Slider2.Limits = [0 6.28318530717959];app.Slider2.ValueChangedFcn = createCallbackFcn(app, @Slider2ValueChanged, true);app.Slider2.Position = [568 294 150 3];% Show the figure after all components are createdapp.UIFigure.Visible = 'on';endend% App creation and deletionmethods (Access = public)% Construct appfunction app = WaveCreater% Create UIFigure and componentscreateComponents(app)% Register the app with App DesignerregisterApp(app, app.UIFigure)if nargout == 0clear appendend% Code that executes before app deletionfunction delete(app)% Delete UIFigure when app is deleteddelete(app.UIFigure)endend
end

Code文件下载:

网盘链接: https://pan.baidu.com/s/1e3WKdpiMstkRgEr1AlJdzw
提取码: bq9u


http://www.ppmy.cn/server/105046.html

相关文章

Openstack 与 Ceph集群搭建(中): Ceph部署

文章目录 一、部署前说明1. ceph 版本选择依据2. ceph网络要求3. 硬件要求 二、部署架构三、部署过程1. 通用步骤2. 部署管理节点创建账号安装Cephadm运行bootstrap 3. 登录Ceph web4. 将其他节点加入集群同步ceph key安装ceph CLI命令行添加主机节点到集群添加OSD节点将监控节…

多商户小程序审核存在商户入口无法通过

小程序拒绝如下&#xff1a; 需要注意的地方如下&#xff1a; 关闭店铺展示关闭商户入驻关闭diy中的申请入口、店铺街入口等关闭个人中心广告的申请入口关闭分销关闭支付宝

使用Instrumentation创建代理程序监测Java对象信息

文章目录 创建代理使用代理监测测试代码运行配置运行效果 总结 Instrumentation 是Java提供的一种能够在程序运行时检查和修改类定义的技术。使用Instrumentation&#xff0c;可以构建一个独立于应用程序的代理程序&#xff0c;检测和协助运行在JVM上的程序&#xff0c;甚至可以…

idea启动报错Improperly specified VM option.

我本来是想解决idea启动占内存的问题&#xff0c;在网上找了个修改启动参数&#xff0c;这么改的 因为格式不正确&#xff0c;idea启动报错&#xff1a; Improperly specified VM option. To fix the problem, edit your JVM optionsand remove the options that are obsolete…

https://developer.nvidia.com/cuda-toolkit-archive

CUDA Toolkit Archive | NVIDIA Developerhttps://developer.nvidia.com/cuda-toolkit-archive

spring整合redis

1.导入依赖 <!-- spring-data-redis 依赖--> <dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>2.7.18</version> </dependency> <dependency><…

Sketch for mac(专业矢量绘图设计软件100.3版) 中文激活版 一键快速安装!

Sketch 是一款专为 macOS 设计的专业矢量图形编辑软件&#xff0c;自发布以来便成为 UI/UX 设计师首选的工具之一。其简洁高效的用户界面、强大的设计功能&#xff0c;以及与 macOS 系统的深度集成&#xff0c;使得 Sketch 在设计领域享有很高的声誉。无论是移动应用设计、网页…

使用密钥文件 SSH 登录服务器:Windows、macOS使用终端或连接工具

文章目录 使用密钥文件 SSH 登录服务器1. Windows端方法 1&#xff1a;使用 ssh 命令指定密钥文件方法 2&#xff1a;使用 SSH 配置文件 2. macOS端方法 1&#xff1a;使用 ssh 命令指定密钥文件方法 2&#xff1a;使用 SSH 配置文件方法 3&#xff1a;使用 ssh-add 命令&#…