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

news2024/9/28 23:23:36

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

程序主要内容:

效果展示:

主要代码:

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

classdef WaveCreater < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure              matlab.ui.Figure
        Slider2               matlab.ui.control.Slider
        Slider_2              matlab.ui.control.Slider
        Slider                matlab.ui.control.Slider
        witdthN               matlab.ui.control.NumericEditField
        Label_7               matlab.ui.control.Label
        delayN                matlab.ui.control.NumericEditField
        Label_6               matlab.ui.control.Label
        Button_3              matlab.ui.control.Button
        CheckBox              matlab.ui.control.CheckBox
        Button_2              matlab.ui.control.Button
        DropDown_2            matlab.ui.control.DropDown
        Label_5               matlab.ui.control.Label
        EroP                  matlab.ui.control.NumericEditField
        Label_4               matlab.ui.control.Label
        timessEditField       matlab.ui.control.NumericEditField
        timessEditFieldLabel  matlab.ui.control.Label
        sEditField            matlab.ui.control.EditField
        sEditFieldLabel       matlab.ui.control.Label
        intensi               matlab.ui.control.NumericEditField
        Label_3               matlab.ui.control.Label
        KHzEditField          matlab.ui.control.NumericEditField
        Label_2               matlab.ui.control.Label
        DropDown              matlab.ui.control.DropDown
        Label                 matlab.ui.control.Label
        Button                matlab.ui.control.Button
        UIAxes                matlab.ui.control.UIAxes
    end


    properties (Access = private)
        Check=false; % Description
    end

    methods (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';

        end
    end


    % Callbacks that handle component events
    methods (Access = private)

        % Value changed function: CheckBox
        function CheckBoxValueChanged(app, event)
            if app.CheckBox.Value==1
                app.Check=true;
                app.Button.Enable="off";
            else
                app.Button.Enable='on';
            end
            if app.Check
                app.CreatW;
            end

        end

        % Button pushed function: Button
        function ButtonPushed(app, event)
            app.CreatW;
        end

        % Value changed function: DropDown
        function DropDownValueChanged(app, event)
            if app.Check
                app.CreatW;
            end
            if app.DropDown.ValueIndex==3
                app.witdthN.Enable='on';
            end

        end

        % Value changed function: KHzEditField
        function KHzEditFieldValueChanged(app, event)
            if app.Check
                app.CreatW;
            end
            if app.Slider.Value~=app.KHzEditField.Value
                app.Slider.Value=app.KHzEditField.Value;
            end
        end

        % Value changed function: intensi
        function intensiValueChanged(app, event)
            if app.Check
                app.CreatW;
            end
            if  app.Slider_2.Value~=app.intensi.Value
                app.Slider_2.Value=app.intensi.Value;
            end
        end

        % Value changed function: sEditField
        function sEditFieldValueChanged(app, event)
            if app.Check
                app.CreatW;
            end

        end

        % Value changed function: timessEditField
        function timessEditFieldValueChanged(app, event)
            if app.Check
                app.CreatW;
            end

        end

        % Value changed function: EroP
        function EroPValueChanged(app, event)
            if app.Check
                app.CreatW;
            end
        end

        % Button pushed function: Button_2
        function 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

            % 绘制信号
            figure
            plot(t, x);


        end

        % Value changed function: witdthN
        function witdthNValueChanged(app, event)
            if app.Check
                app.CreatW;
            end

        end

        % Value changed function: delayN
        function delayNValueChanged(app, event)
            if app.Check
                app.CreatW;
            end
            if app.Slider2.Value~=app.delayN.Value
                app.Slider2.Value=app.delayN.Value;
            end
        end

        % Button pushed function: Button_3
        function 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');
            end
        end

        % Value changed function: Slider
        function SliderValueChanged(app, event)
            app.KHzEditField.Value = app.Slider.Value;
            if app.Check
                app.CreatW;
            end
        end

        % Value changed function: Slider_2
        function Slider_2ValueChanged(app, event)
            app.intensi.Value = app.Slider_2.Value;
            if app.Check
                app.CreatW;
            end
        end

        % Value changed function: Slider2
        function Slider2ValueChanged(app, event)
            app.delayN.Value = app.Slider2.Value;
            if app.Check
                app.CreatW;
            end
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.Position = [100 100 755 568];
            app.UIFigure.Name = 'MATLAB App';

            % Create UIAxes
            app.UIAxes = uiaxes(app.UIFigure);
            title(app.UIAxes, '信号波形')
            xlabel(app.UIAxes, '时间(μs)')
            ylabel(app.UIAxes, '信号强度')
            app.UIAxes.Position = [14 19 458 505];

            % Create Button
            app.Button = uibutton(app.UIFigure, 'push');
            app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
            app.Button.Position = [524 80 100 23];
            app.Button.Text = '生成信号';

            % Create Label
            app.Label = uilabel(app.UIFigure);
            app.Label.HorizontalAlignment = 'right';
            app.Label.Position = [546 533 53 22];
            app.Label.Text = '信号类型';

            % Create DropDown
            app.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_2
            app.Label_2 = uilabel(app.UIFigure);
            app.Label_2.HorizontalAlignment = 'right';
            app.Label_2.Position = [516 492 84 22];
            app.Label_2.Text = '信号频率(KHz)';

            % Create KHzEditField
            app.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_3
            app.Label_3 = uilabel(app.UIFigure);
            app.Label_3.HorizontalAlignment = 'right';
            app.Label_3.Position = [555 403 53 22];
            app.Label_3.Text = '信号幅度';

            % Create intensi
            app.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 sEditFieldLabel
            app.sEditFieldLabel = uilabel(app.UIFigure);
            app.sEditFieldLabel.HorizontalAlignment = 'right';
            app.sEditFieldLabel.Position = [508 226 90 22];
            app.sEditFieldLabel.Text = '显示范围(μs)';

            % Create sEditField
            app.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 timessEditFieldLabel
            app.timessEditFieldLabel = uilabel(app.UIFigure);
            app.timessEditFieldLabel.HorizontalAlignment = 'right';
            app.timessEditFieldLabel.Position = [489 185 110 22];
            app.timessEditFieldLabel.Text = '采样率(times/μs)';

            % Create timessEditField
            app.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_4
            app.Label_4 = uilabel(app.UIFigure);
            app.Label_4.HorizontalAlignment = 'right';
            app.Label_4.Position = [486 112 112 22];
            app.Label_4.Text = '噪声相对强度(%)';

            % Create EroP
            app.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_5
            app.Label_5 = uilabel(app.UIFigure);
            app.Label_5.HorizontalAlignment = 'right';
            app.Label_5.Position = [546 150 53 22];
            app.Label_5.Text = '噪声类型';

            % Create DropDown_2
            app.DropDown_2 = uidropdown(app.UIFigure);
            app.DropDown_2.Items = {'高斯'};
            app.DropDown_2.Position = [614 150 100 22];
            app.DropDown_2.Value = '高斯';

            % Create Button_2
            app.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 CheckBox
            app.CheckBox = uicheckbox(app.UIFigure);
            app.CheckBox.ValueChangedFcn = createCallbackFcn(app, @CheckBoxValueChanged, true);
            app.CheckBox.Text = '实时生成';
            app.CheckBox.Position = [646 81 70 22];

            % Create Button_3
            app.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_6
            app.Label_6 = uilabel(app.UIFigure);
            app.Label_6.HorizontalAlignment = 'right';
            app.Label_6.Position = [539 321 65 22];
            app.Label_6.Text = '相位偏移角';

            % Create delayN
            app.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_7
            app.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 witdthN
            app.witdthN = uieditfield(app.UIFigure, 'numeric');
            app.witdthN.ValueChangedFcn = createCallbackFcn(app, @witdthNValueChanged, true);
            app.witdthN.Enable = 'off';
            app.witdthN.Position = [361 533 100 22];

            % Create Slider
            app.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_2
            app.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 Slider2
            app.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 created
            app.UIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = WaveCreater

            % Create UIFigure and components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.UIFigure)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end

Code文件下载:

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2066766.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

顶顶通呼叫中心中间件-一句话语音识别安装步骤

顶顶通呼叫中心中间件-一句话语音识别安装步骤&#xff0c;对接mod_vad。一句话识别&#xff08;http接口提交录音文件识别&#xff09; 一、安装asrproxy 1、将下载软件压缩包上传到需要安装的服务器中 2、SSH终端依次执行以下命令&#xff1a; mkdir -p /ddt/asrproxysud…

抖音商城随身wifi销量排行榜!排名第一的格行随身wifi怎么样?

对于经常出差办公&#xff0c;或者酷爱旅行的人来说随身wifi简直是必备神器&#xff0c;但是随身wifi行业乱象频发&#xff0c;不小心就会踩坑。这不&#xff0c;刚去青岛旅游回来的同事正在吐槽&#xff0c;旅游前特意买个随身wifi&#xff0c;咨询时商家一顿夸&#xff0c;结…

王丹妮演绎“美女与蛇” 红黑对撞下的超现实美学

今日&#xff0c;香港演员王丹妮&#xff08;Louise Wong&#xff09;释出一组时尚大片。 这组大片以浓郁饱满的红色和深邃奢华的黑色为主调&#xff0c;搭配超现实风装置&#xff0c;尽显神秘诗意之美。黑色背景与红色装置象征锐意与优雅的交锋&#xff1b;神秘面罩下&#xf…

当移动端H5中的display:flex不生效时,给我整破防了

情况&#xff1a; 在项目开发中遇到一个“更多”按钮放置于卡片的右下角时&#xff0c;在安卓9版本的浏览器打开项目&#xff0c;结果测试出来“更多”按钮样式错乱&#xff0c;做了这么久的开发&#xff0c;在移动端给我整破防了。。。 <style> display:flex; justify…

MyBatis-Plus 三、(进阶使用)

一、typeHandler 的使用 1、存储json格式字段 如果字段需要存储为json格式&#xff0c;可以使用JacksonTypeHandler处理器。使用方式非常简单&#xff0c;如下所示&#xff1a; 只需要加上两个注解即可&#xff1a; TableName(autoResultMap true) 表示自动…

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

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

Netty03-进阶

三. Netty 进阶 1. 粘包与半包 1.1 粘包现象 服务端代码 public class HelloWorldServer {static final Logger log LoggerFactory.getLogger(HelloWorldServer.class);void start() {NioEventLoopGroup boss new NioEventLoopGroup(1);NioEventLoopGroup worker new Ni…

高性能4G灯杆网关,未来智慧城市的神经中枢

在智慧城市的建设浪潮中&#xff0c;灯杆作为城市基础设施的重要组成部分&#xff0c;正在经历一场革命性的转变。SG600 4G灯杆网关就是这场革命的核心产品&#xff0c;它将普通的路灯转变为集照明、监控、通信、环境监测等多功能于一体的智慧终端。 产品优势&#xff1a; 高度…

[图解]分析工作流开始01

1 00:00:02,650 --> 00:00:04,200 需求工作流结束之后 2 00:00:04,610 --> 00:00:06,880 我们就要进入分析工作流了 3 00:00:07,890 --> 00:00:09,020 在分析工作流里面 4 00:00:09,030 --> 00:00:13,060 我们使用类图、序列图 5 00:00:13,710 --> 00:00:16…

深度学习入门-03

PS&#xff1a;基于小土堆视频学习https://www.bilibili.com/video/BV1hE411t7RN?p6&vd_source22926f91481026cd10af799bb45e448b 1、Dateset Dateset就是我们的目标数据&#xff0c;告诉我们如何获取数据&#xff0c;距离&#xff1a;从多种类型的数据中&#xff0c;提取…

Go语言反射入门:理解类型与值的动态操作

简介 Go 语言的反射机制是一种在运行时检查程序本身的能力&#xff0c;它允许程序在运行时动态地操作对象的类型和值。 基本概念 1.反射与类型 在静态类型语言中&#xff0c;变量的类型在编译时确定。反射允许在运行时查询和修改变量的类型信息。 2.接口与反射 Go 中的接口…

云等保安全合规解决方案

在当今数字化时代&#xff0c;云计算已成为企业数字化转型的基石&#xff0c;它不仅极大地提升了数据处理能力、降低了运营成本&#xff0c;还促进了业务模式的创新与发展。然而&#xff0c;随着云服务的广泛应用&#xff0c;云环境的安全性问题也日益凸显&#xff0c;成为制约…

Tita的OKR:研发人员的OKR

当您要建立一个以产品为中心的团队&#xff0c;并希望你的团队有一个产品的心态和时刻围绕你的客户&#xff0c;此工程研发 OKR 示例就是实现此目标的伟大方法。您将在以下文章中找到相关的技术研发 OKR 示例。 技术研发团队是任何组织中的重要组成部分&#xff0c;正确的OKR可…

U盘车载专用音乐合集 3068首 24G

包含3068首适合车载播放的音乐。 拿走的麻烦评论一下&#xff0c;感谢&#xff01;&#xff01;&#xff01; 拿走的麻烦评论一下&#xff0c;感谢&#xff01;&#xff01;&#xff01; 拿走的麻烦评论一下&#xff0c;感谢&#xff01;&#xff01;&#xff01; 链接&#…

主流短视频评论采集python爬虫(含一二级评论内容)

声明 仅用于学习交流&#xff0c;不用于其他用途 正文 随着主流短视频评论采集更新需要登录&#xff0c;由于不懈的努力&#xff0c;攻破这一难点&#xff0c;不需要登录采集作品所有评论信息 话不多说上代码看效果&#xff1a; 输入作品id: 这样就拿到评论信息了&#xff…

c++中调用函数时出现“warning C4715: “controlMode”: 不是所有的控件路径都返回值”警告的问题

调用函数时出现“warning C4715: “controlMode”: 不是所有的控件路径都返回值”警告的问题 问题描述解决方案 问题描述 如图所示&#xff0c;我的函数定义如下 在编译的时候&#xff0c;会出现如下警告 warning C4715: “controlMode”: 不是所有的控件路径都返回值 解决方案…

易通博客项目测试报告

目录 1. 项目背景1.1 测试目标 2. 项目功能3.测试分类3.1 功能测试3.2 自动化测试3.2.1自动化测试遇到的问题以及解决&#xff1a;3.2.2 自动化测试结果 3.3 性能测试 4. 基于场景性能测试下遇到的一些问题4.1 解决博客id不存在的问题&#xff1f;4.2 添加博客请求响应失败&…

[Other]-安装ruby、ascli、ascp

最近新接到这样一个需求&#xff0c;将生物原始数据上传到某中心&#xff0c;其中用到ascp命令&#xff0c;阴差阳错的装了ruby、ascli&#xff0c;这里就都一并介绍下安装方式&#xff0c;由于服务器老旧默认安装时ruby2.0&#xff0c;又 升级到2.7等引发的一系列问题&#xf…

Anaconda与conda、pip与conda的区别

Anaconda与conda、pip与conda的区别 1. 引言1.1 背景介绍1.2 文章目的 2. 什么是Anaconda&#xff1f;2.1 Anaconda简介2.2 Anaconda的优势2.3 Anaconda的安装与配置 3. 什么是Conda&#xff1f;3.1 Conda简介3.2 Conda的功能和用途3.3 Conda与Anaconda的关系 4. 什么是Pip&…

如何将精益生产目标的设定与企业财务状况相结合?

在探讨如何将精益生产目标的设定与企业财务状况相结合时&#xff0c;我们首先需要明确两者的核心要素及其相互关系。精益生产&#xff0c;起源于丰田生产方式&#xff0c;旨在通过消除浪 费、持续改进和全员参与&#xff0c;实现生产过程的最大化效率和最小化成本。而企业财务状…