classdef DynUkM_app2 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                     matlab.ui.Figure
        Phase3Spinner                matlab.ui.control.Spinner
        Phase3SpinnerLabel           matlab.ui.control.Label
        Freq3Spinner                 matlab.ui.control.Spinner
        Freq3SpinnerLabel            matlab.ui.control.Label
        Amp3Spinner                  matlab.ui.control.Spinner
        Amp3SpinnerLabel             matlab.ui.control.Label
        CalculateButton              matlab.ui.control.Button
        sampling_freqEditField       matlab.ui.control.NumericEditField
        sampling_freqEditFieldLabel  matlab.ui.control.Label
        timeEditField                matlab.ui.control.NumericEditField
        timeEditFieldLabel           matlab.ui.control.Label
        Phase2Spinner                matlab.ui.control.Spinner
        Phase2SpinnerLabel           matlab.ui.control.Label
        Freq2Spinner                 matlab.ui.control.Spinner
        Freq2SpinnerLabel            matlab.ui.control.Label
        Amp2Spinner                  matlab.ui.control.Spinner
        Amp2SpinnerLabel             matlab.ui.control.Label
        Phase1Spinner                matlab.ui.control.Spinner
        Phase1SpinnerLabel           matlab.ui.control.Label
        Freq1Spinner                 matlab.ui.control.Spinner
        Freq1SpinnerLabel            matlab.ui.control.Label
        Amp1Spinner                  matlab.ui.control.Spinner
        Amp1SpinnerLabel             matlab.ui.control.Label
        UIAxes5                      matlab.ui.control.UIAxes
        UIAxes2                      matlab.ui.control.UIAxes
        UIAxes4                      matlab.ui.control.UIAxes
        UIAxes3                      matlab.ui.control.UIAxes
        UIAxes                       matlab.ui.control.UIAxes
    end

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

        % Value changed function: Amp1Spinner
        function Amp1SpinnerValueChanged(app, event)
            amp1 = app.Amp1Spinner.Value;
            
        end

        % Value changed function: Freq1Spinner
        function Freq1SpinnerValueChanged(app, event)
            freq1 = app.Freq1Spinner.Value;
            
        end

        % Value changed function: Phase1Spinner
        function Phase1SpinnerValueChanged(app, event)
            phase1 = app.Phase1Spinner.Value;
            
        end

        % Value changed function: Amp2Spinner
        function Amp2SpinnerValueChanged(app, event)
            amp2 = app.Amp2Spinner.Value;
            
        end

        % Value changed function: Freq2Spinner
        function Freq2SpinnerValueChanged(app, event)
            freq2 = app.Freq2Spinner.Value;
            
        end

        % Value changed function: Phase2Spinner
        function Phase2SpinnerValueChanged(app, event)
            phase2 = app.Phase2Spinner.Value;
            
        end

        % Value changed function: timeEditField
        function timeEditFieldValueChanged(app, event)
            tk = app.timeEditField.Value;
            
        end

        % Value changed function: sampling_freqEditField
        function sampling_freqEditFieldValueChanged(app, event)
            fs = app.sampling_freqEditField.Value;
            
        end

        % Callback function: CalculateButton, UIAxes, UIAxes2, UIAxes3, 
        % UIAxes4
        function ButtonPushed(app, event)
            amp1 = app.Amp1Spinner.Value;
            freq1 = app.Freq1Spinner.Value;
            phase1 = app.Phase1Spinner.Value;
            
            amp2 = app.Amp2Spinner.Value;
            freq2 = app.Freq2Spinner.Value;
            phase2 = app.Phase2Spinner.Value;

            amp3 = app.Amp3Spinner.Value;
            freq3 = app.Freq3Spinner.Value;
            phase3 = app.Phase3Spinner.Value;
            
            tk = app.timeEditField.Value;
            fs = app.sampling_freqEditField.Value;
            t=0:1/fs:tk-1/fs;

            y1=amp1*sin(2*pi*freq1*t+phase1*pi/180);
            y2=amp2*sin(2*pi*freq2*t+phase2*pi/180);
            y3=amp3*sin(2*pi*freq3*t+phase3*pi/180);
            y=y1+y2+y3;

            Y=abs(fft(y))/((tk*fs)/2); 
            f=0:1/tk:fs/2-1/tk;
            df=1/(tk);

            plot(app.UIAxes, t,y1)
            app.UIAxes.XLim = [0 tk];
            app.UIAxes.YLim = [-10 10];
            plot(app.UIAxes2, t,y2)
            app.UIAxes2.XLim = [0 tk];
            app.UIAxes2.YLim = [-10 10];
            plot(app.UIAxes5, t,y3)
            app.UIAxes5.XLim = [0 tk];
            app.UIAxes5.YLim = [-1 1];
            plot(app.UIAxes3, t,y)
            app.UIAxes3.XLim = [0 tk];
            %app.UIAxes3.YLim = [-20 20];
            plot(app.UIAxes4, f,Y(1:(tk*fs)/2) );
            app.UIAxes4.XLim = [0 fs/2-1/tk];
           
        end

        % Callback function
        function NPSpinnerValueChanged(app, event)
%             NP = app.NPSpinner.Value;
            
        end

        % Callback function
        function WLSpinnerValueChanged(app, event)
%             WL = app.WLSpinner.Value;
            
        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 = [50 50 1500 750];
            app.UIFigure.Name = 'MATLAB App';

            % Create UIAxes
            app.UIAxes = uiaxes(app.UIFigure);
            title(app.UIAxes, 'signal 1')
            xlabel(app.UIAxes, 't')
            ylabel(app.UIAxes, 'y_1')
            zlabel(app.UIAxes, 'Z')
            app.UIAxes.XGrid = 'on';
            app.UIAxes.YGrid = 'on';
            app.UIAxes.Box = 'on';
            app.UIAxes.ButtonDownFcn = createCallbackFcn(app, @ButtonPushed, true);
            app.UIAxes.Position = [420 550 550 180];

            % Create UIAxes3
            app.UIAxes3 = uiaxes(app.UIFigure);
            title(app.UIAxes3, 'vibration')
            xlabel(app.UIAxes3, 't')
            ylabel(app.UIAxes3, 'y')
            zlabel(app.UIAxes3, 'Z')
            app.UIAxes3.XGrid = 'on';
            app.UIAxes3.YGrid = 'on';
            app.UIAxes3.Box = 'on';
            app.UIAxes3.ButtonDownFcn = createCallbackFcn(app, @ButtonPushed, true);
            app.UIAxes3.Position = [1026 350 440 300];

            % Create UIAxes4
            app.UIAxes4 = uiaxes(app.UIFigure);
            title(app.UIAxes4, 'vibration spectrum')
            xlabel(app.UIAxes4, 'frequency, Hz')
            ylabel(app.UIAxes4, 'A')
            zlabel(app.UIAxes4, 'Z')
            app.UIAxes4.XGrid = 'on';
            app.UIAxes4.YGrid = 'on';
            app.UIAxes4.ColorOrder = [0 0.4471 0.7412;0.851 0.3255 0.098;0.9294 0.6941 0.1255;0.4941 0.1843 0.5569;0.4667 0.6745 0.1882;0.302 0.7451 0.9333;0.6353 0.0784 0.1843;1 1 1];
            app.UIAxes4.LineStyleOrder = {'-'};
            app.UIAxes4.Box = 'on';
            app.UIAxes4.ButtonDownFcn = createCallbackFcn(app, @ButtonPushed, true);
            app.UIAxes4.Position = [1026 121 440 230];

            % Create UIAxes2
            app.UIAxes2 = uiaxes(app.UIFigure);
            title(app.UIAxes2, 'signal 2')
            xlabel(app.UIAxes2, 't')
            ylabel(app.UIAxes2, 'y_2')
            zlabel(app.UIAxes2, 'Z')
            app.UIAxes2.XGrid = 'on';
            app.UIAxes2.YGrid = 'on';
            app.UIAxes2.Box = 'on';
            app.UIAxes2.ButtonDownFcn = createCallbackFcn(app, @ButtonPushed, true);
            app.UIAxes2.Position = [420 350 550 180];

            % Create UIAxes5
            app.UIAxes5 = uiaxes(app.UIFigure);
            title(app.UIAxes5, 'signal 3')
            xlabel(app.UIAxes5, 't')
            ylabel(app.UIAxes5, 'y_3')
            zlabel(app.UIAxes5, 'Z')
            app.UIAxes5.XGrid = 'on';
            app.UIAxes5.YGrid = 'on';
            app.UIAxes5.Box = 'on';
            app.UIAxes5.Position = [420 135 550 180];

            % Create Amp1SpinnerLabel
            app.Amp1SpinnerLabel = uilabel(app.UIFigure);
            app.Amp1SpinnerLabel.HorizontalAlignment = 'right';
            app.Amp1SpinnerLabel.Position = [15 704 46 22];
            app.Amp1SpinnerLabel.Text = 'Amp1';

            % Create Amp1Spinner
            app.Amp1Spinner = uispinner(app.UIFigure);
            app.Amp1Spinner.Limits = [0 10];
            app.Amp1Spinner.ValueChangedFcn = createCallbackFcn(app, @Amp1SpinnerValueChanged, true);
            app.Amp1Spinner.Position = [75 698 50 40];
            app.Amp1Spinner.Value = 5;

            % Create Freq1SpinnerLabel
            app.Freq1SpinnerLabel = uilabel(app.UIFigure);
            app.Freq1SpinnerLabel.HorizontalAlignment = 'right';
            app.Freq1SpinnerLabel.Position = [141 704 46 22];
            app.Freq1SpinnerLabel.Text = 'Freq1';

            % Create Freq1Spinner
            app.Freq1Spinner = uispinner(app.UIFigure);
            app.Freq1Spinner.Limits = [0 100];
            app.Freq1Spinner.ValueChangedFcn = createCallbackFcn(app, @Freq1SpinnerValueChanged, true);
            app.Freq1Spinner.Position = [200 698 50 40];
            app.Freq1Spinner.Value = 2;

            % Create Phase1SpinnerLabel
            app.Phase1SpinnerLabel = uilabel(app.UIFigure);
            app.Phase1SpinnerLabel.HorizontalAlignment = 'right';
            app.Phase1SpinnerLabel.Position = [279 704 46 22];
            app.Phase1SpinnerLabel.Text = 'Phase1';

            % Create Phase1Spinner
            app.Phase1Spinner = uispinner(app.UIFigure);
            app.Phase1Spinner.Step = 5;
            app.Phase1Spinner.Limits = [0 360];
            app.Phase1Spinner.ValueChangedFcn = createCallbackFcn(app, @Phase1SpinnerValueChanged, true);
            app.Phase1Spinner.Position = [325 695 50 40];

            % Create Amp2SpinnerLabel
            app.Amp2SpinnerLabel = uilabel(app.UIFigure);
            app.Amp2SpinnerLabel.HorizontalAlignment = 'right';
            app.Amp2SpinnerLabel.Position = [15 644 46 22];
            app.Amp2SpinnerLabel.Text = 'Amp2';

            % Create Amp2Spinner
            app.Amp2Spinner = uispinner(app.UIFigure);
            app.Amp2Spinner.Limits = [0 10];
            app.Amp2Spinner.ValueChangedFcn = createCallbackFcn(app, @Amp2SpinnerValueChanged, true);
            app.Amp2Spinner.Position = [75 638 50 40];
            app.Amp2Spinner.Value = 2;

            % Create Freq2SpinnerLabel
            app.Freq2SpinnerLabel = uilabel(app.UIFigure);
            app.Freq2SpinnerLabel.HorizontalAlignment = 'right';
            app.Freq2SpinnerLabel.Position = [141 644 46 22];
            app.Freq2SpinnerLabel.Text = 'Freq2';

            % Create Freq2Spinner
            app.Freq2Spinner = uispinner(app.UIFigure);
            app.Freq2Spinner.Limits = [0 100];
            app.Freq2Spinner.ValueChangedFcn = createCallbackFcn(app, @Freq2SpinnerValueChanged, true);
            app.Freq2Spinner.Position = [200 638 50 40];
            app.Freq2Spinner.Value = 25;

            % Create Phase2SpinnerLabel
            app.Phase2SpinnerLabel = uilabel(app.UIFigure);
            app.Phase2SpinnerLabel.HorizontalAlignment = 'right';
            app.Phase2SpinnerLabel.Position = [279 644 46 22];
            app.Phase2SpinnerLabel.Text = 'Phase2';

            % Create Phase2Spinner
            app.Phase2Spinner = uispinner(app.UIFigure);
            app.Phase2Spinner.Step = 5;
            app.Phase2Spinner.Limits = [0 360];
            app.Phase2Spinner.ValueChangedFcn = createCallbackFcn(app, @Phase2SpinnerValueChanged, true);
            app.Phase2Spinner.Position = [325 638 50 40];

            % Create timeEditFieldLabel
            app.timeEditFieldLabel = uilabel(app.UIFigure);
            app.timeEditFieldLabel.HorizontalAlignment = 'right';
            app.timeEditFieldLabel.Position = [15 523 28 22];
            app.timeEditFieldLabel.Text = 'time';

            % Create timeEditField
            app.timeEditField = uieditfield(app.UIFigure, 'numeric');
            app.timeEditField.Limits = [0 10];
            app.timeEditField.ValueChangedFcn = createCallbackFcn(app, @timeEditFieldValueChanged, true);
            app.timeEditField.Position = [75 522 30 30];
            app.timeEditField.Value = 10;

            % Create sampling_freqEditFieldLabel
            app.sampling_freqEditFieldLabel = uilabel(app.UIFigure);
            app.sampling_freqEditFieldLabel.HorizontalAlignment = 'right';
            app.sampling_freqEditFieldLabel.Position = [232 524 81 22];
            app.sampling_freqEditFieldLabel.Text = 'sampling_freq';

            % Create sampling_freqEditField
            app.sampling_freqEditField = uieditfield(app.UIFigure, 'numeric');
            app.sampling_freqEditField.Limits = [0 10000];
            app.sampling_freqEditField.ValueChangedFcn = createCallbackFcn(app, @sampling_freqEditFieldValueChanged, true);
            app.sampling_freqEditField.Position = [325 522 50 30];
            app.sampling_freqEditField.Value = 200;

            % Create CalculateButton
            app.CalculateButton = uibutton(app.UIFigure, 'push');
            app.CalculateButton.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
            app.CalculateButton.BackgroundColor = [0 1 0];
            app.CalculateButton.Position = [133 293 100 22];
            app.CalculateButton.Text = 'Calculate';

            % Create Amp3SpinnerLabel
            app.Amp3SpinnerLabel = uilabel(app.UIFigure);
            app.Amp3SpinnerLabel.HorizontalAlignment = 'right';
            app.Amp3SpinnerLabel.Position = [15 592 46 22];
            app.Amp3SpinnerLabel.Text = 'Amp3';

            % Create Amp3Spinner
            app.Amp3Spinner = uispinner(app.UIFigure);
            app.Amp3Spinner.Step = 0.1;
            app.Amp3Spinner.Limits = [0 1];
            app.Amp3Spinner.Position = [75 586 50 40];
            app.Amp3Spinner.Value = 0.1;

            % Create Freq3SpinnerLabel
            app.Freq3SpinnerLabel = uilabel(app.UIFigure);
            app.Freq3SpinnerLabel.HorizontalAlignment = 'right';
            app.Freq3SpinnerLabel.Position = [141 589 46 22];
            app.Freq3SpinnerLabel.Text = 'Freq3';

            % Create Freq3Spinner
            app.Freq3Spinner = uispinner(app.UIFigure);
            app.Freq3Spinner.Step = 10;
            app.Freq3Spinner.Limits = [0 1000];
            app.Freq3Spinner.Position = [200 583 50 40];
            app.Freq3Spinner.Value = 80;

            % Create Phase3SpinnerLabel
            app.Phase3SpinnerLabel = uilabel(app.UIFigure);
            app.Phase3SpinnerLabel.HorizontalAlignment = 'right';
            app.Phase3SpinnerLabel.Position = [279 589 46 22];
            app.Phase3SpinnerLabel.Text = 'Phase3';

            % Create Phase3Spinner
            app.Phase3Spinner = uispinner(app.UIFigure);
            app.Phase3Spinner.Step = 5;
            app.Phase3Spinner.Limits = [0 360];
            app.Phase3Spinner.Position = [325 583 50 40];

            % 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 = DynUkM_app2

            % 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