classdef DUM_app2 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                       matlab.ui.Figure
        timeintervalDropDown           matlab.ui.control.DropDown
        timeintervalDropDownLabel      matlab.ui.control.Label
        indicatorsButtonGroup          matlab.ui.container.ButtonGroup
        formfactorButton               matlab.ui.control.ToggleButton
        crestfactorButton              matlab.ui.control.ToggleButton
        kurtosisButton                 matlab.ui.control.ToggleButton
        skewnessButton                 matlab.ui.control.ToggleButton
        ordmomentButton                matlab.ui.control.ToggleButton
        rmsButton                      matlab.ui.control.ToggleButton
        standarddevButton              matlab.ui.control.ToggleButton
        varianceButton                 matlab.ui.control.ToggleButton
        maximumButton                  matlab.ui.control.ToggleButton
        meanButton                     matlab.ui.control.ToggleButton
        showtrendButton                matlab.ui.control.Button
        CalculateButton                matlab.ui.control.Button
        timerangeEditField             matlab.ui.control.NumericEditField
        timerangeEditFieldLabel        matlab.ui.control.Label
        RstdbEditField                 matlab.ui.control.NumericEditField
        RstdbEditFieldLabel            matlab.ui.control.Label
        RpdbEditField                  matlab.ui.control.NumericEditField
        RpdbEditFieldLabel             matlab.ui.control.Label
        FilterparametersTextArea       matlab.ui.control.TextArea
        FilterparametersTextAreaLabel  matlab.ui.control.Label
        FsEditField                    matlab.ui.control.NumericEditField
        FsEditFieldLabel               matlab.ui.control.Label
        FpSpinner                      matlab.ui.control.Spinner
        FpSpinnerLabel                 matlab.ui.control.Label
        NSpinner                       matlab.ui.control.Spinner
        NSpinnerLabel                  matlab.ui.control.Label
        UIAxes4                        matlab.ui.control.UIAxes
        UIAxes3                        matlab.ui.control.UIAxes
        UIAxes2                        matlab.ui.control.UIAxes
        UIAxes                         matlab.ui.control.UIAxes
    end

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

        % Callback function: CalculateButton, UIAxes, UIAxes2, UIAxes3
        function UIAxesButtonDown(app, event)
            N = app.NSpinner.Value;
            Fp = app.FpSpinner.Value;
            Rp = app.RpdbEditField.Value;
            Rst = app.RstdbEditField.Value;
            Fs = app.FsEditField.Value;
            tr = app.timerangeEditField.Value;

            eqnum = firceqrip(N,Fp/(Fs/2),[Rp db2mag(-Rst)],'passedge'); % eqnum = vec of coeffs
            param=fvtool(eqnum,'Fs',Fs,'Color','White'); % Visualize filter
            W=get(param);
            W.CurrentAxes.XLabel.Parent.Children.YData;
            
            lowpassFilt = dsp.LowpassFilter('DesignForMinimumOrder',false, ...
 	        'FilterOrder',N,'PassbandFrequency',Fp,'SampleRate',Fs,...
 	        'PassbandRipple',Rp, 'StopbandAttenuation',Rst);

            load Dane_lab2
            t=yt(:,1);
            z=yt(:,2);
            z_filt = lowpassFilt(z);
            tk=t(size(t,1)); %czas trwania pomiaru
            Y1=abs(fft(z))/((tk*Fs)/2);  
            Y2=abs(fft(z_filt))/((tk*Fs)/2);
            f=0:1/tk:Fs/2-1/tk;
            
            plot(app.UIAxes, t(1:Fs*tr),z(1:Fs*tr), t(1:Fs*tr),z_filt(1:Fs*tr), 'r' )
            app.UIAxes.XLim = [0 tr];
            plot(app.UIAxes2, linspace(0,Fs/2,2^13),W.CurrentAxes.XLabel.Parent.Children.YData )
            app.UIAxes2.XLim = [0 Fp*2];
            app.UIAxes2.YLim = [-100 10];
            plot(app.UIAxes3, f,Y1(1:(tk*Fs)/2), f,Y2(1:(tk*Fs)/2), 'r' )
            app.UIAxes3.XLim = [0 Fp*2];

            save('xxxxxxxxxx.mat','t','z','z_filt')

        end

        % Value changed function: NSpinner
        function NSpinnerValueChanged(app, event)
            N = app.NSpinner.Value;
            
        end

        % Value changed function: FpSpinner
        function FpSpinnerValueChanged(app, event)
            Fp = app.FpSpinner.Value;
            
        end

        % Value changed function: FsEditField
        function FsEditFieldValueChanged(app, event)
            Fs = app.FsEditField.Value;
            
        end

        % Value changed function: RpdbEditField
        function RpdbEditFieldValueChanged(app, event)
            Rp = app.RpdbEditField.Value;
            
        end

        % Value changed function: RstdbEditField
        function RstdbEditFieldValueChanged(app, event)
            Rst = app.RstdbEditField.Value;
            
        end

        % Value changed function: timerangeEditField
        function timerangeEditFieldValueChanged(app, event)
            tr = app.timerangeEditField.Value;
            
        end

        % Callback function: UIAxes4, indicatorsButtonGroup, showtrendButton
        function Calculate2ButtonPushed(app, event)
                        
            ti_ling_value = app.timeintervalDropDown.Value;
            if ti_ling_value == '1'
                ti=1;
            elseif ti_ling_value == '2'
                ti=2;
            elseif ti_ling_value == '4'
                ti=4;
            elseif ti_ling_value == '5'
                ti=5;
            elseif ti_ling_value == '10'
                ti=10;
            end

            load xxxxxxxxxx
            Fs = app.FsEditField.Value;
            tk=t(size(t,1));
            z_B=buffer(z,Fs*ti);
            z_B_filt=buffer(z_filt,Fs*ti);
            if app.meanButton.Value == true
            trend=mean(z_B);
            trend_filt=mean(z_B_filt);
            elseif app.maximumButton.Value == true
            trend=max(abs(z_B));
            trend_filt=max(z_B_filt);
            elseif app.varianceButton.Value == true
            trend=var(z_B);
            trend_filt=var(z_B_filt);
            elseif app.standarddevButton.Value == true
            trend=std(z_B);
            trend_filt=std(z_B_filt);
            elseif app.rmsButton.Value == true
            trend=rms(z_B);
            trend_filt=rms(z_B_filt);
            elseif app.ordmomentButton.Value == true
            trend=moment(z_B,3);
            trend_filt=moment(z_B_filt,3);
            elseif app.skewnessButton.Value == true
            trend=skewness(z_B);
            trend_filt=skewness(z_B_filt);
            elseif app.kurtosisButton.Value == true
            trend=kurtosis(z_B);
            trend_filt=kurtosis(z_B_filt);
            elseif app.crestfactorButton.Value == true
            trend=max(abs(z_B))./rms(z_B);
            trend_filt=max(abs(z_B_filt))./rms(z_B_filt);
            elseif app.formfactorButton.Value == true
            trend=rms(z_B)./mean(abs(z_B));
            trend_filt=rms(z_B_filt)./mean(abs(z_B_filt));
            end
            plot(app.UIAxes4, t,z, linspace(0,tk,tk/ti),trend, '.', linspace(0,tk,tk/ti),trend_filt, '.' )
            app.UIAxes4.XLim = [0 tk];
        end

        % Value changed function: timeintervalDropDown
        function timeintervalDropDownValueChanged(app, event)
            ti_ling_value = app.timeintervalDropDown.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, 'vibration')
            xlabel(app.UIAxes, 'time, s')
            ylabel(app.UIAxes, 'y')
            zlabel(app.UIAxes, 'Z')
            app.UIAxes.PlotBoxAspectRatio = [4.25675675675676 1 1];
            app.UIAxes.XGrid = 'on';
            app.UIAxes.YGrid = 'on';
            app.UIAxes.Box = 'on';
            app.UIAxes.ButtonDownFcn = createCallbackFcn(app, @UIAxesButtonDown, true);
            app.UIAxes.Position = [220 560 600 180];

            % Create UIAxes2
            app.UIAxes2 = uiaxes(app.UIFigure);
            title(app.UIAxes2, 'filter response characteristics')
            xlabel(app.UIAxes2, 'frequency, Hz')
            ylabel(app.UIAxes2, 'magnitude (dB)')
            zlabel(app.UIAxes2, 'Z')
            app.UIAxes2.PlotBoxAspectRatio = [4 1 1];
            app.UIAxes2.XGrid = 'on';
            app.UIAxes2.YGrid = 'on';
            app.UIAxes2.Box = 'on';
            app.UIAxes2.ButtonDownFcn = createCallbackFcn(app, @UIAxesButtonDown, true);
            app.UIAxes2.Position = [220 360 600 180];

            % Create UIAxes3
            app.UIAxes3 = uiaxes(app.UIFigure);
            title(app.UIAxes3, 'vibration spectrum')
            xlabel(app.UIAxes3, 'frequency, Hz')
            ylabel(app.UIAxes3, 'A')
            zlabel(app.UIAxes3, 'Z')
            app.UIAxes3.XGrid = 'on';
            app.UIAxes3.YGrid = 'on';
            app.UIAxes3.Box = 'on';
            app.UIAxes3.ButtonDownFcn = createCallbackFcn(app, @UIAxesButtonDown, true);
            app.UIAxes3.Position = [220 160 600 180];

            % Create UIAxes4
            app.UIAxes4 = uiaxes(app.UIFigure);
            title(app.UIAxes4, 'trend')
            xlabel(app.UIAxes4, 'time, s')
            ylabel(app.UIAxes4, 'trend')
            zlabel(app.UIAxes4, 'Z')
            app.UIAxes4.ButtonDownFcn = createCallbackFcn(app, @Calculate2ButtonPushed, true);
            app.UIAxes4.Position = [881 352 605 374];

            % Create NSpinnerLabel
            app.NSpinnerLabel = uilabel(app.UIFigure);
            app.NSpinnerLabel.HorizontalAlignment = 'right';
            app.NSpinnerLabel.Position = [30 704 19 22];
            app.NSpinnerLabel.Text = 'N';

            % Create NSpinner
            app.NSpinner = uispinner(app.UIFigure);
            app.NSpinner.Limits = [0 10000];
            app.NSpinner.ValueChangedFcn = createCallbackFcn(app, @NSpinnerValueChanged, true);
            app.NSpinner.Position = [63 698 68 40];
            app.NSpinner.Value = 100;

            % Create FpSpinnerLabel
            app.FpSpinnerLabel = uilabel(app.UIFigure);
            app.FpSpinnerLabel.HorizontalAlignment = 'right';
            app.FpSpinnerLabel.Position = [30 644 21 22];
            app.FpSpinnerLabel.Text = 'Fp';

            % Create FpSpinner
            app.FpSpinner = uispinner(app.UIFigure);
            app.FpSpinner.Limits = [0 48000];
            app.FpSpinner.ValueChangedFcn = createCallbackFcn(app, @FpSpinnerValueChanged, true);
            app.FpSpinner.Position = [64 638 68 40];
            app.FpSpinner.Value = 3000;

            % Create FsEditFieldLabel
            app.FsEditFieldLabel = uilabel(app.UIFigure);
            app.FsEditFieldLabel.HorizontalAlignment = 'right';
            app.FsEditFieldLabel.Position = [30 480 25 22];
            app.FsEditFieldLabel.Text = 'Fs';

            % Create FsEditField
            app.FsEditField = uieditfield(app.UIFigure, 'numeric');
            app.FsEditField.Limits = [0 48000];
            app.FsEditField.ValueChangedFcn = createCallbackFcn(app, @FsEditFieldValueChanged, true);
            app.FsEditField.Position = [82 476 50 30];
            app.FsEditField.Value = 48000;

            % Create FilterparametersTextAreaLabel
            app.FilterparametersTextAreaLabel = uilabel(app.UIFigure);
            app.FilterparametersTextAreaLabel.HorizontalAlignment = 'right';
            app.FilterparametersTextAreaLabel.Position = [10 429 96 22];
            app.FilterparametersTextAreaLabel.Text = 'Filter parameters';

            % Create FilterparametersTextArea
            app.FilterparametersTextArea = uitextarea(app.UIFigure);
            app.FilterparametersTextArea.Position = [10 339 165 91];
            app.FilterparametersTextArea.Value = {'N - Filter Order'; 'Fp - Passband Frequency'; 'Rp - Passband Ripple'; 'Rst - Stopband Attenuation'; 'Fs - SampleRate'};

            % Create RpdbEditFieldLabel
            app.RpdbEditFieldLabel = uilabel(app.UIFigure);
            app.RpdbEditFieldLabel.HorizontalAlignment = 'right';
            app.RpdbEditFieldLabel.Position = [30 580 46 22];
            app.RpdbEditFieldLabel.Text = 'Rp (db)';

            % Create RpdbEditField
            app.RpdbEditField = uieditfield(app.UIFigure, 'numeric');
            app.RpdbEditField.Limits = [0.01 0.1];
            app.RpdbEditField.ValueChangedFcn = createCallbackFcn(app, @RpdbEditFieldValueChanged, true);
            app.RpdbEditField.Position = [82 576 50 30];
            app.RpdbEditField.Value = 0.02;

            % Create RstdbEditFieldLabel
            app.RstdbEditFieldLabel = uilabel(app.UIFigure);
            app.RstdbEditFieldLabel.HorizontalAlignment = 'right';
            app.RstdbEditFieldLabel.Position = [30 529 48 22];
            app.RstdbEditFieldLabel.Text = 'Rst (db)';

            % Create RstdbEditField
            app.RstdbEditField = uieditfield(app.UIFigure, 'numeric');
            app.RstdbEditField.Limits = [0 80];
            app.RstdbEditField.ValueChangedFcn = createCallbackFcn(app, @RstdbEditFieldValueChanged, true);
            app.RstdbEditField.Position = [84 525 47 30];
            app.RstdbEditField.Value = 40;

            % Create timerangeEditFieldLabel
            app.timerangeEditFieldLabel = uilabel(app.UIFigure);
            app.timerangeEditFieldLabel.HorizontalAlignment = 'right';
            app.timerangeEditFieldLabel.Position = [702 551 62 22];
            app.timerangeEditFieldLabel.Text = 'time range';

            % Create timerangeEditField
            app.timerangeEditField = uieditfield(app.UIFigure, 'numeric');
            app.timerangeEditField.Limits = [0.1 100];
            app.timerangeEditField.ValueChangedFcn = createCallbackFcn(app, @timerangeEditFieldValueChanged, true);
            app.timerangeEditField.Position = [770 547 50 30];
            app.timerangeEditField.Value = 1;

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

            % Create showtrendButton
            app.showtrendButton = uibutton(app.UIFigure, 'push');
            app.showtrendButton.ButtonPushedFcn = createCallbackFcn(app, @Calculate2ButtonPushed, true);
            app.showtrendButton.BackgroundColor = [0 1 1];
            app.showtrendButton.Position = [1134 84 100 22];
            app.showtrendButton.Text = 'show trend';

            % Create indicatorsButtonGroup
            app.indicatorsButtonGroup = uibuttongroup(app.UIFigure);
            app.indicatorsButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @Calculate2ButtonPushed, true);
            app.indicatorsButtonGroup.Title = 'indicators';
            app.indicatorsButtonGroup.Position = [881 92 123 248];

            % Create meanButton
            app.meanButton = uitogglebutton(app.indicatorsButtonGroup);
            app.meanButton.Text = 'mean';
            app.meanButton.Position = [11 195 100 22];
            app.meanButton.Value = true;

            % Create maximumButton
            app.maximumButton = uitogglebutton(app.indicatorsButtonGroup);
            app.maximumButton.Text = 'maximum';
            app.maximumButton.Position = [11 174 100 22];

            % Create varianceButton
            app.varianceButton = uitogglebutton(app.indicatorsButtonGroup);
            app.varianceButton.Text = 'variance';
            app.varianceButton.Position = [11 153 100 22];

            % Create standarddevButton
            app.standarddevButton = uitogglebutton(app.indicatorsButtonGroup);
            app.standarddevButton.Text = 'standard dev';
            app.standarddevButton.Position = [12 132 100 22];

            % Create rmsButton
            app.rmsButton = uitogglebutton(app.indicatorsButtonGroup);
            app.rmsButton.Text = 'rms';
            app.rmsButton.Position = [11 111 100 22];

            % Create ordmomentButton
            app.ordmomentButton = uitogglebutton(app.indicatorsButtonGroup);
            app.ordmomentButton.Text = '3ord moment';
            app.ordmomentButton.Position = [11 90 100 22];

            % Create skewnessButton
            app.skewnessButton = uitogglebutton(app.indicatorsButtonGroup);
            app.skewnessButton.Text = 'skewness';
            app.skewnessButton.Position = [11 69 100 22];

            % Create kurtosisButton
            app.kurtosisButton = uitogglebutton(app.indicatorsButtonGroup);
            app.kurtosisButton.Text = 'kurtosis';
            app.kurtosisButton.Position = [11 49 100 22];

            % Create crestfactorButton
            app.crestfactorButton = uitogglebutton(app.indicatorsButtonGroup);
            app.crestfactorButton.Text = 'crest factor';
            app.crestfactorButton.Position = [11 28 100 22];

            % Create formfactorButton
            app.formfactorButton = uitogglebutton(app.indicatorsButtonGroup);
            app.formfactorButton.Text = 'form factor';
            app.formfactorButton.Position = [12 7 100 22];

            % Create timeintervalDropDownLabel
            app.timeintervalDropDownLabel = uilabel(app.UIFigure);
            app.timeintervalDropDownLabel.HorizontalAlignment = 'right';
            app.timeintervalDropDownLabel.Position = [1026 287 70 22];
            app.timeintervalDropDownLabel.Text = 'time interval';

            % Create timeintervalDropDown
            app.timeintervalDropDown = uidropdown(app.UIFigure);
            app.timeintervalDropDown.Items = {'1s', '2s', '4s', '5s', '10s'};
            app.timeintervalDropDown.ItemsData = {'1', '2', '4', '5', '10'};
            app.timeintervalDropDown.ValueChangedFcn = createCallbackFcn(app, @timeintervalDropDownValueChanged, true);
            app.timeintervalDropDown.Position = [1111 287 67 22];
            app.timeintervalDropDown.Value = '1';

            % 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_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