function freqresp(src, r)
%FREQRESP  Updates @specdata objects.
%  Author(s):  
%  Copyright 1986-2002 The MathWorks, Inc.
%  $Revision: 1.1.6.1 $ $Date: 2004/12/26 21:43:30 $
 
%% Get new data from the @timeseries object.
[data,Ts] = tsToRegular(src);

%% Look for visible+cleared responses in response array
if isempty(r.Data.Response) && strcmp(r.View.Visible,'on')
  % Get frequency response data from the fft of the time series data  
  pspec = abs(fft(detrend(data,0))).^2;
  M = mean(pspec);
  for k=1:length(M)
      if M(k)>eps
         pspec(:,k) = 2*pspec(:,k)/M(k)*std(data(:,k))^2;
      end
  end
    
  % Find the cumulative sum is this is a cumulative spectrum
  if strcmp(r.Parent.Cumulative,'on')
      pspec = cumsum(pspec)/size(pspec,1);
      set(r.Data,'Accumulated','on')
  else
      set(r.Data,'Accumulated','off')
  end
  
  % Define the freq vector
  N = length(pspec);
  freq = linspace(0,1,N)'/Ts;
  r.Data.FreqUnits = sprintf('cyc/%s',src.Timeseries.TimeInfo.Units(1:end-1));
  
  % Find the conversion factor from the @timeseries time units to
  % the view units
  plottimeunits = sprintf('%ss',r.Parent.AxesGrid.XUnits(5:end));
  freq = freq/tsunitconv(plottimeunits,src.Timeseries.TimeInfo.Units);
    
  % Nan out masked columns
  if ~isempty(src.Mask) && length(src.Mask)==size(pspec,2)
      pspec(:,src.Mask) = NaN*ones([size(pspec,1) sum(src.Mask)]);
  end
  % Update the data with the power spec
  set(r.Data,'Response',pspec(1:ceil(N/2),:),'Frequency',freq(1:ceil(N/2)),...
        'SoftFocus',false);
  % Update the focus
  ffocus = [freq(1) freq(ceil(N/2))];
  if ffocus(2)-ffocus(1)>eps  
      set(r.Data,'Focus',ffocus)
  else
      set(r.Data,'Focus',[ffocus(1) - 1e-6, ffocus(1) + 1e-6])
  end
end

%% The response name should match the time series name
r.Name = src.Timeseries.Name;
