classdef BodePlotData
    %BodePlotData is class which contains data for representation of
    %frequency responses  
    
    properties
        Amplitude;  % Amplitude frequency characteristic - y-axis [-]
        Phase;      % Phase frequency characteristic - y-axis [°]
        Frequency;  % Frequency axis - x-axis [Hz]
        Length;     % Length of axes
    end
    
    methods
        % Constructor
        function obj = BodePlotData(Amplitude, Phase,Frequency)
            if nargin == 0
                obj.Amplitude = zeros(1,0);
                obj.Phase = zeros(1,0);
                obj.Frequency = zeros(1,0);
                obj.Length = 0;
            elseif nargin == 3
                if (length(Amplitude) ~= length(Phase)) || (length(Amplitude) ~= length(Frequency))
                    error('Wrong length of input vectors')
                end
                obj.Amplitude = Amplitude;
                obj.Phase = Phase;
                obj.Frequency = Frequency;                
                obj.Length = length(Amplitude);
            else
                error('Wrong number of input variables')
            end
        end
        % Method for show Bode diagram
        function handle = Show(obj, Prop1, ValProp1, Prop2, ValProp2, Prop3, ValProp3)
            if nargin == 1
                handle = figure;
                subplot(2,1,1);
                semilogx(obj.Frequency,20*log10(obj.Amplitude))
                hold on
                grid on
                xlabel('f[Hz]')
                ylabel('Amplitude [dB]')
                subplot(2,1,2);
                semilogx(obj.Frequency,obj.Phase)
                hold on
                grid on
                xlabel('f[Hz]')
                ylabel('Phase [°]')
                
            elseif (mod(nargin-1,2) == 0) 
                iFrequecy = obj.Frequency;
                iFrequencyLabel = 'f [Hz]';
                iPhase = obj.Phase;
                iPhaseLabel = 'Phase [°]';
                NewFigure = true;
                
                NumProperties = (nargin-1)/2;
                Prop = cell(NumProperties,1);
                ValProp = cell(NumProperties,1);
                
                if nargin >= 3
                    Prop{1} = Prop1;
                    ValProp{1} = ValProp1;
                end
                if nargin >= 5
                    Prop{2} = Prop2;
                    ValProp{2} = ValProp2;
                end
                if nargin >= 7
                    Prop{3} = Prop3;                            
                    ValProp{3} = ValProp3;
                end
                if nargin >= 8
                    error('Wrong number of input variables')
                end
                                
                for i = 1:NumProperties
                    
                    if strcmp(Prop{i},'FreqUnit')
                        if strcmp(ValProp{i},'rad/s')
                            iFrequecy = iFrequecy*2*pi;
                            iFrequencyLabel = '\omega [rad/s]';
                        elseif strcmp(ValProp{i},'Hz')
                        else
                            error('Wrong value of FreqUnit property')
                        end
                    end
                    
                    if strcmp(Prop{i},'PhaseUnit')
                        if strcmp(ValProp{i},'rad')
                            iPhase = iPhase*pi/180;
                            iPhaseLabel = 'Phase [rad]';
                        elseif strcmp(ValProp{i},'deg')
                        else
                            error('Wrong value of PhaseUnit property')
                        end
                    end
                    
                    if strcmp(Prop{i},'HandleFigure')
                        NewFigure = false;
                        handle = figure(ValProp{i});
                    end
                    
                end
                
                if NewFigure
                    handle = figure;
                end
                
                subplot(2,1,1);
                semilogx(iFrequecy,20*log10(obj.Amplitude))
                hold on
                grid on
                xlabel(iFrequencyLabel)
                ylabel('Amplitude [dB]')
                subplot(2,1,2);
                semilogx(iFrequecy,iPhase)
                hold on
                grid on
                xlabel(iFrequencyLabel)
                ylabel(iPhaseLabel)
            else
                error('Wrong number of input variables')
            end
        end

    end
    
    methods(Static)
      % Set legend
      function SetLegend(hFigure, sLegend)
         figure(hFigure);
         subplot(2,1,1)
         legend(sLegend);
         subplot(2,1,2)
         legend(sLegend);
      end
    end
    
    methods(Static)
      % Set title
      function SetTitle(hFigure, sTitle)
         figure(hFigure);
         subplot(2,1,1)
         title(sTitle)
      end
    end
    
end

