This matlab function is to calculate the average power of a
This matlab function is to calculate the average power of a signal over a given time
 Project: Lab #2
 2
 range. The form of the function is:
 function P = Find_power(t,x,a,b)
 where the time signal is defined by the two matlab vectors t and x, and the time
 interval is defined by parameters a and b. The output, P, is the power contained in the
 signal for the time range over which it is defined. It should be written so that if the
 parameters a and b are omitted, then the full range of the input signal is used. Also if
 only a exists but b is omitted, take b to be the end of the time vector t.
 Your function should check the input arguments to make sure they make sense. If an
 error occurs, your code should print that there is an error and output the NULL value
 for P. For example, if the time and sample vectors are not the same length, this should
 cause an error. If the value for b<a then those values should be swapped before
 proceeding with the energy calculation and the program should NOT result in an error.
 If a is less than the first time value, it should be set to the first time value. If b is
 greater than the last time value, it should be set to the last time value.
 This function should operate properly whether the x vector is purely real or whether it
 has an imaginary component.
Solution
Solution:
Although matlab does not support concept of default arguments still we can process you problem upto some extent using number of argument. i.e you can\'t call function here like , f(t,x,,b) . this will definately throw error.
still the code has structure like :
----------------
function P = Find_power(t,x,a,b )
 
 
 len1 = length(t);
 len2 = length(x);
 
 if len1 ~= len2
     error(\'length of time and signal vector must be same\')
 end
 
 
 if nargin == 2
 P = (norm(x)^2)/length(x);
 end
 
 if nargin== 3
     b = a;
     if b>t(len1)
         P=\'NULL\'
     end
     if b<t(len1)
 sam_size = abs(t(len1)-b);
    len = floor((sam_size*len1)/t(len1));
     P = (norm(x)^2)/len;
     end
 end
 
 if nargin ==4
    sam_size = abs(a-b);
    len = floor((sam_size*len1)/t(len1));
     P = (norm(x)^2)/len;
 end
 
 end
-----------


