function tremfreqplot(group,deriv)
% tremfreqplot(group,deriv) plots each of the tremor 
%   series for the specified group in the frequency domain
%   using the derivative of the signal indicated (0, 1 or 2).
% If no deriv is specified, it plots the raw displacement data
%   (without any filtering).
% group='p' for Parkinsonian
%      ='n' for Normal (control) group
%      ='m' for Mystery group
%      ='a' for the artificial data group
% example tremplot('p',0)

%   Copyright 2003
%   Centre for Nonlinear Dynamics in Physiology and Medicine

if (group=='p'),
  nsubj=3;
  tgroup='Parkinson';
end
if (group=='n'),
  nsubj=3; 
  tgroup = 'Control';
end
if (group=='m'),
  nsubj=3; 
  tgroup = 'Mystery';
end
if (group=='a') 
  nsubj=4; 
  tgroup = 'Artificial';
end

tt = (0:5999)/200;
offset=0;
maxpow = 0;

clf
for (i=1:nsubj)
  eval(['load trem' group int2str(i) ';']);
  x = eval(['trem' group int2str(i)]);
  if (nargin > 1)
    x = tremfilt(x,deriv);
    ttext = '(Filtered)';
    if (deriv==0),
      vtext = ' displacement ';
    end
    if (deriv==1),
      vtext = ' velocity ';
    end
    if (deriv==2),
      vtext = ' acceleration ';
    end
  else
    deriv=0;
    ttext='(Raw)';
    vtext=' displacement ';
  end;
  x=x-mean(x);
  pxx = abs(fft(x));
  fx = 200*(0:(length(x)-1))/length(x);
  if (max(abs(pxx)) > maxpow),
    maxpow = max(abs(pxx));
  end
%  bottom(i)=abs(min(pxx))*1.1;
%  offset=offset+(max(pxx)*1.1);
%  if(i>1) offset=offset+bottom(i-1); end;
%  plot(fx,pxx-offset)
  subplot(nsubj,1,i)
  plot(fx,pxx)
  if (group == 'a'),
    legend(['Dataset ',num2str(i)])
  else
    legend(['Subject ',num2str(i)])
  end
  if (i == nsubj),
     xlabel('Frequency (Hz)')
  end
  if (deriv == 0)
    ytext='Power (from disp.)';
  elseif (deriv == 1)
    ytext='Power (from vel.)';
  elseif (deriv == 2)
    ytext='Power (from accel.)';
  end;
  ylabel(ytext)
  if (i == 1),
    title([tgroup,vtext,'data ',ttext])
  end
  hold on
end

for (i=1:nsubj),
  subplot(nsubj,1,i)
  set(gca,'Ylim',1.1*maxpow*[0 1],'Xlim',[0 25])
end

hold off





