function [x]=bandpower(varargin)
 %y=bandpower(x):Gives the average power of the signal
 %y=bandpower(x,f,[lower upper]):Gives the average power of the signal in the band of the frequencies given by the limits
 %
  if(length(varargin)==1)
   x=avg_pwr([varargin{1}]);
  elseif(length(varargin)==2)
     error("not enough inputs");
  elseif(length(varargin)==3)
      x=pwr_freq(varargin{:});
  else
     error("error");
  end
end

function pwr=avg_pwr(x)
   pwr=(x*x')/length(x);
end

function pw=pwr_freq(x,fs,limits)
    [p]=periodogram(x);  
    N=length(x)
    lower_limit=limits(1);
    upper_limit=limits(2);
    lower_limits_n=floor(lower_limit*N/fs);
    upper_limits_n=floor(upper_limit*N/fs);
    pw=2*sum(p(max([1,lower_limits_n]):upper_limits_n))/N;
end




