classdef DynUkM_app2b < 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
        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
        CalculateButton                matlab.ui.control.Button
        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
        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)
            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=y';
            N = app.NSpinner.Value;
            Fp = app.FpSpinner.Value;
            Rp = app.RpdbEditField.Value;
            Rst = app.RstdbEditField.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);


            y_filt = lowpassFilt(y);
            Y1=abs(fft(y))/((tk*fs)/2);  
            Y2=abs(fft(y_filt))/((tk*fs)/2);
            f=0:1/tk:fs/2-1/tk;

            
            plot(app.UIAxes, t,y, t,y_filt, 'r' )
            plot(app.UIAxes2, linspace(0,fs/2,2^13),W.CurrentAxes.XLabel.Parent.Children.YData )
            app.UIAxes2.YLim = [-100 10];
            plot(app.UIAxes3, f,Y1(1:(tk*fs)/2), f,Y2(1:(tk*fs)/2), 'r' )
        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: RpdbEditField
        function RpdbEditFieldValueChanged(app, event)
            Rp = app.RpdbEditField.Value;
            
        end

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

        % Callback function
        function timerangeEditFieldValueChanged(app, event)
            
        end

        % Callback function
        function Calculate2ButtonPushed(app, event)

        end

        % Callback function
        function timeintervalDropDownValueChanged(app, event)
       
        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 = [669 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 = [669 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 = [669 160 600 180];

            % Create NSpinnerLabel
            app.NSpinnerLabel = uilabel(app.UIFigure);
            app.NSpinnerLabel.HorizontalAlignment = 'right';
            app.NSpinnerLabel.Position = [479 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 = [512 698 68 40];
            app.NSpinner.Value = 100;

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

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

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

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

            % Create RpdbEditFieldLabel
            app.RpdbEditFieldLabel = uilabel(app.UIFigure);
            app.RpdbEditFieldLabel.HorizontalAlignment = 'right';
            app.RpdbEditFieldLabel.Position = [479 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 = [531 576 50 30];
            app.RpdbEditField.Value = 0.02;

            % Create RstdbEditFieldLabel
            app.RstdbEditFieldLabel = uilabel(app.UIFigure);
            app.RstdbEditFieldLabel.HorizontalAlignment = 'right';
            app.RstdbEditFieldLabel.Position = [479 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 = [533 525 47 30];
            app.RstdbEditField.Value = 40;

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

            % 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.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.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.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.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.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.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.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.Position = [325 522 50 30];
            app.sampling_freqEditField.Value = 200;

            % 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.5;

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

            % 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