classdef DUM_app4 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                       matlab.ui.Figure
        numberofaveragesDropDown       matlab.ui.control.DropDown
        numberofaveragesDropDownLabel  matlab.ui.control.Label
        parameterforrmsenvelopeLabel   matlab.ui.control.Label
        parameterforpeakenvelopeLabel  matlab.ui.control.Label
        WLSpinner                      matlab.ui.control.Spinner
        WLSpinnerLabel                 matlab.ui.control.Label
        NPSpinner                      matlab.ui.control.Spinner
        NPSpinnerLabel                 matlab.ui.control.Label
        CalculateButton                matlab.ui.control.Button
        sampling_freqEditField         matlab.ui.control.NumericEditField
        sampling_freqEditFieldLabel    matlab.ui.control.Label
        UIAxes6                        matlab.ui.control.UIAxes
        UIAxes5                        matlab.ui.control.UIAxes
        UIAxes4                        matlab.ui.control.UIAxes
        UIAxes3                        matlab.ui.control.UIAxes
    end

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

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

        % Callback function: CalculateButton, UIAxes3, UIAxes4, UIAxes5, 
        % UIAxes6
        function ButtonPushed(app, event)
            load Dane_lab4
            t=yt(:,1);
            z=yt(:,2);
            tk=t(size(t,1)); %czas trwania pomiaru
            Fs = app.sampling_freqEditField.Value;
            
            NP = app.NPSpinner.Value;
            [yupper_peak,ylower_peak] = envelope(z,NP,'peak');
            WL = app.WLSpinner.Value;
            [yupper_rms,ylower_rms] = envelope(z,WL,'rms');
            
            NA_ling_value = app.numberofaveragesDropDown.Value;
            if NA_ling_value == '1'
                NA=1;
            elseif NA_ling_value == '2'
                NA=2;
            elseif NA_ling_value == '4'
                NA=4;
            elseif NA_ling_value == '5'
                NA=5;
            elseif NA_ling_value == '10'
                NA=10;
            elseif NA_ling_value == '20'
                NA=20;
            elseif NA_ling_value == '25'
                NA=25;
            elseif NA_ling_value == '50'
                NA=50;
            end

            L=48000*tk/NA;
            Buf=buffer(z,L);
            Buf_env_peak=buffer(yupper_peak,L);
            Buf_env_rms=buffer(yupper_rms,L);


            Y=abs(fft(z))/((tk*Fs)/2);
            Y1=abs(fft(Buf,L))/(L/2);
            Y1_ave=mean(Y1')';
            
            Y_env_peak=abs(fft(yupper_peak))/((tk*Fs)/2);
            Y1_env_peak=abs(fft(Buf_env_peak,L))/(L/2);
            Y1_env_peak_ave=mean(Y1_env_peak')';

            Y_env_rms=abs(fft(yupper_rms))/((tk*Fs)/2);
            Y1_env_rms=abs(fft(Buf_env_rms,L))/(L/2);
            Y1_env_rms_ave=mean(Y1_env_rms')';

            f=0:1/tk:Fs/2-1/tk;
            f1 = 0:Fs/L:Fs/2-Fs/L;
            df=1/(tk);

            plot(app.UIAxes3, t,z, t,yupper_peak,'r', t,ylower_peak,'r', t,yupper_rms,'g', t,ylower_rms,'g' )
            app.UIAxes3.XLim = [0 tk];
            plot(app.UIAxes4, f,Y(1:(tk*Fs)/2), f1,Y1_ave(1:L/2) );
            %app.UIAxes4.XLim = [0 Fs/2-1/tk];
            app.UIAxes4.XLim = [0 500];
            plot(app.UIAxes5, f,Y_env_peak(1:(tk*Fs)/2), f1,Y1_env_peak_ave(1:L/2) )
            %app.UIAxes5.XLim = [0 Fs/2-1/tk];
            app.UIAxes5.XLim = [0 500];
            plot(app.UIAxes6, f,Y_env_rms(1:(tk*Fs)/2), f1,Y1_env_rms_ave(1:L/2) )
            %app.UIAxes6.XLim = [0 Fs/2-1/tk];
            app.UIAxes6.XLim = [0 500];
           
        end

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

        % Value changed function: WLSpinner
        function WLSpinnerValueChanged(app, event)
            WL = app.WLSpinner.Value;
            
        end

        % Value changed function: numberofaveragesDropDown
        function numberofaveragesDropDownValueChanged(app, event)
            NA_ling_value = app.numberofaveragesDropDown.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 UIAxes3
            app.UIAxes3 = uiaxes(app.UIFigure);
            title(app.UIAxes3, 'vibration+envelope')
            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 = [20 326 621 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 = [700 500 790 230];

            % Create UIAxes5
            app.UIAxes5 = uiaxes(app.UIFigure);
            title(app.UIAxes5, '"peak" envelope spectrum')
            xlabel(app.UIAxes5, 'frequency, Hz')
            ylabel(app.UIAxes5, 'A')
            zlabel(app.UIAxes5, 'Z')
            app.UIAxes5.XGrid = 'on';
            app.UIAxes5.YGrid = 'on';
            app.UIAxes5.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.UIAxes5.LineStyleOrder = {'-'};
            app.UIAxes5.Box = 'on';
            app.UIAxes5.ButtonDownFcn = createCallbackFcn(app, @ButtonPushed, true);
            app.UIAxes5.Position = [700 250 790 230];

            % Create UIAxes6
            app.UIAxes6 = uiaxes(app.UIFigure);
            title(app.UIAxes6, '"rms" envelope spectrum')
            xlabel(app.UIAxes6, 'frequency, Hz')
            ylabel(app.UIAxes6, 'A')
            zlabel(app.UIAxes6, 'Z')
            app.UIAxes6.XGrid = 'on';
            app.UIAxes6.YGrid = 'on';
            app.UIAxes6.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.UIAxes6.LineStyleOrder = {'-'};
            app.UIAxes6.Box = 'on';
            app.UIAxes6.ButtonDownFcn = createCallbackFcn(app, @ButtonPushed, true);
            app.UIAxes6.Position = [700 0 790 230];

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

            % Create sampling_freqEditField
            app.sampling_freqEditField = uieditfield(app.UIFigure, 'numeric');
            app.sampling_freqEditField.Limits = [0 48000];
            app.sampling_freqEditField.ValueChangedFcn = createCallbackFcn(app, @sampling_freqEditFieldValueChanged, true);
            app.sampling_freqEditField.Position = [578 647 79 30];
            app.sampling_freqEditField.Value = 48000;

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

            % Create NPSpinnerLabel
            app.NPSpinnerLabel = uilabel(app.UIFigure);
            app.NPSpinnerLabel.HorizontalAlignment = 'right';
            app.NPSpinnerLabel.Position = [26 651 46 22];
            app.NPSpinnerLabel.Text = 'NP';

            % Create NPSpinner
            app.NPSpinner = uispinner(app.UIFigure);
            app.NPSpinner.Limits = [0 10000];
            app.NPSpinner.ValueChangedFcn = createCallbackFcn(app, @NPSpinnerValueChanged, true);
            app.NPSpinner.Position = [86 645 59 40];
            app.NPSpinner.Value = 1000;

            % Create WLSpinnerLabel
            app.WLSpinnerLabel = uilabel(app.UIFigure);
            app.WLSpinnerLabel.HorizontalAlignment = 'right';
            app.WLSpinnerLabel.Position = [276 648 46 22];
            app.WLSpinnerLabel.Text = 'WL';

            % Create WLSpinner
            app.WLSpinner = uispinner(app.UIFigure);
            app.WLSpinner.Limits = [0 10000];
            app.WLSpinner.ValueChangedFcn = createCallbackFcn(app, @WLSpinnerValueChanged, true);
            app.WLSpinner.Position = [331 642 55 40];
            app.WLSpinner.Value = 100;

            % Create parameterforpeakenvelopeLabel
            app.parameterforpeakenvelopeLabel = uilabel(app.UIFigure);
            app.parameterforpeakenvelopeLabel.Position = [31 690 169 40];
            app.parameterforpeakenvelopeLabel.Text = 'parameter for "peak" envelope';

            % Create parameterforrmsenvelopeLabel
            app.parameterforrmsenvelopeLabel = uilabel(app.UIFigure);
            app.parameterforrmsenvelopeLabel.Position = [274 690 169 40];
            app.parameterforrmsenvelopeLabel.Text = 'parameter for "rms" envelope';

            % Create numberofaveragesDropDownLabel
            app.numberofaveragesDropDownLabel = uilabel(app.UIFigure);
            app.numberofaveragesDropDownLabel.HorizontalAlignment = 'right';
            app.numberofaveragesDropDownLabel.Position = [404 264 112 22];
            app.numberofaveragesDropDownLabel.Text = 'number of averages';

            % Create numberofaveragesDropDown
            app.numberofaveragesDropDown = uidropdown(app.UIFigure);
            app.numberofaveragesDropDown.Items = {'1', '2', '4', '5', '10', '20', '25', '50'};
            app.numberofaveragesDropDown.ItemsData = {'1', '2', '4', '5', '10', '20', '25', '50'};
            app.numberofaveragesDropDown.ValueChangedFcn = createCallbackFcn(app, @numberofaveragesDropDownValueChanged, true);
            app.numberofaveragesDropDown.Position = [531 264 100 22];
            app.numberofaveragesDropDown.Value = '10';

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

            % 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