classdef Electricalsignal < Signal
    %ELECTRICALSIGNAL Summary of this class goes here
    %   Detailed explanation goes here

    properties
        fs
    end

    methods
        function obj = Electricalsignal(signal, options)
            %ELECTRICALSIGNAL Construct an instance of this class
            %   Detailed explanation goes here
            arguments
                signal
                options.fs
                options.logbook
            end

            obj = obj@Signal(signal);

            fn = fieldnames(options);

            for l = 1:numel(fn)
                obj.(fn{l}) = options.(fn{l});
            end

        end

        function pow = power(obj)

            % Power of an electrical signal
            R = 50;
            pow = mean(abs(obj.signal).^2) / R; % Power in watts = V^2 / R
            pow = 10*log10(pow) + 30; % Power in dB +30 = dBm

        end

        function cspr = cspr(obj)

            carrier_power_dbm = pow2db( abs(mean(obj.signal)).^2 )+30; % dB -> +30 -> dBm
            signal_power_dbm  = obj.power;

            carr_lin = abs(mean(obj.signal)).^2;
            sign_lin = mean(abs(obj.signal).^2);

            cspr_lin = pow2db(carr_lin/sign_lin);

            cspr =carrier_power_dbm-signal_power_dbm;

        end

        function obj = normalize(obj,options)

            arguments
                obj Electricalsignal
                options.mode normalization_mode = normalization_mode.rms
            end

            switch options.mode

                case normalization_mode.milliwatt
                    
                    curpow = sqrt(mean(obj.signal.^2)/50); %watt
                    tgtpow = sqrt(1e-3); %watt -> wir wollen milliwatt

                    scling = tgtpow/curpow;

                    obj.signal = obj.signal*scling;

                case normalization_mode.rms
                    
                    obj.signal = obj.signal / sqrt(mean(obj.signal.^2));

                case normalization_mode.oneone

                    obj.signal = obj.signal - min(obj.signal);
                    obj.signal = obj.signal/max(abs(obj.signal));
                    obj.signal = (2*obj.signal) - 1;

            end

        end

        function obj = setPower(obj,outputpower, mode)

            arguments
                obj Electricalsignal
                outputpower
                mode power_notation
            end
            
           switch mode
               case power_notation.W

               case power_notation.mW
                    outputpower_w = outputpower/1000; %mW to Watt
               case power_notation.dBm
                    outputpower_w = 10^((outputpower-30)/10); % dBm to Watt
               case power_notation.dBW
                   outputpower_w = 10^((outputpower)/10); % dBm to Watt
           end
            
            curpow = sqrt(mean(obj.signal.^2)/50); %watt mit 50 ohm ref
            tgtpow = sqrt(outputpower_w); %watt -> wir wollen milliwatt

            scling = tgtpow/curpow;

            obj.signal = obj.signal*scling;

        end



    end
end

