基于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

相关推荐
通信仿真实验室1 小时前
(73)脉冲幅度调制PAM调制解调通信系统的MATLAB仿真
matlab·信号处理·通信原理·调制解调·通信信号·通信仿真
八年。。1 小时前
MATLAB 中有关figure图表绘制函数设计(论文中常用)
开发语言·笔记·学习·matlab
相醉为友2 小时前
002 MATLAB语言基础
开发语言·matlab
subject625Ruben6 小时前
代码美学:MATLAB制作渐变色
开发语言·matlab
机器学习之心6 小时前
异常检测 | 高斯分布拟合算法异常数据检测(Matlab)
算法·数学建模·matlab·异常数据检测
猪猪虾的业余生活20 小时前
matlab实现,数据曲线毛刺光滑
开发语言·matlab
MATLAB代码顾问1 天前
MATLAB实现多种群遗传算法(multiple population GA,MPGA)
开发语言·matlab
记录无知岁月1 天前
【MATLAB】续行符号对字符串失效
matlab·字符串·技巧·续行
慕容复之巅1 天前
基于Matlab的图像去噪算法仿真(二)
图像处理·算法·matlab
慕容复之巅1 天前
基于Matlab的图像去噪算法仿真
图像处理·算法·matlab