classdef DynUkM_app3 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                       matlab.ui.Figure
        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
        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_lab3
            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];
        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
        function Calculate2ButtonPushed(app, event)

        end

        % Callback function
        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.XGrid = 'on';
            app.UIAxes.YGrid = 'on';
            app.UIAxes.Box = 'on';
            app.UIAxes.ButtonDownFcn = createCallbackFcn(app, @UIAxesButtonDown, true);
            app.UIAxes.Position = [220 560 1200 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.XGrid = 'on';
            app.UIAxes2.YGrid = 'on';
            app.UIAxes2.Box = 'on';
            app.UIAxes2.ButtonDownFcn = createCallbackFcn(app, @UIAxesButtonDown, true);
            app.UIAxes2.Position = [220 360 1200 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 1200 180];

            % 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 = [1347 543 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 = [1415 539 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 = [48 254 100 22];
            app.CalculateButton.Text = 'Calculate';

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

            % 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