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

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

程序主要内容:

效果展示:

主要代码:

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

Matlab 复制代码
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

相关推荐
做科研的周师兄34 分钟前
【MATLAB 实战】栅格数据 K-Means 聚类(分块处理版)—— 解决大数据内存溢出、运行卡顿问题
人工智能·算法·机器学习·matlab·kmeans·聚类
hoiii1872 小时前
基于LSB匹配的隐写术MATLAB实现程序
开发语言·matlab
民乐团扒谱机3 小时前
【微实验】基于MATLAB的一维条材下料优化问题求解
数学建模·matlab·线性规划·最优化模型·整数线性规划
步达硬件3 小时前
【Matlab】批量自定义图像处理
开发语言·matlab
崇山峻岭之间3 小时前
Matlab学习记录32
开发语言·学习·matlab
机器学习之心4 小时前
MATLAB灰狼优化算法(GWO)改进物理信息神经网络(PINN)光伏功率预测
神经网络·算法·matlab·物理信息神经网络
ghie909014 小时前
基于MATLAB的TLBO算法优化实现与改进
开发语言·算法·matlab
wuk99814 小时前
VSC优化算法MATLAB实现
开发语言·算法·matlab
2401_8633186317 小时前
机动车防撞击系统设计
matlab
jllllyuz21 小时前
MATLAB实现蜻蜓优化算法
开发语言·算法·matlab