function [score,weights] = pca(rates)

% Performs PC Analysis on the firing rates of each spike group.
% Input:  rates = firing rates matrix (rates stacked as columns)
% Output: score = PC Scores (# of bins x m)

C = cov(rates);                                                             % Calculate the covariance matrix of the firing rates
[pc,eigs,weights] = pcacov(C);                                              % Perform PC analysis on the cov matrix
pc = repmat(sign(mean(pc)),size(pc,1),1).*pc;                               % Make the components positive
score = rates*pc;                                                           % Calculate the PC scores

disp('The PCA eigenvalues are:')
disp(eigs)
disp('The PCA weights (in %) are:')
disp(weights);

%     [pc,score,eigs] = princomp(rate);
%     p = repmat(sign(mean(pc)),length(score),1);
%     score = p.*score + repmat(mean(rate)*pc,length(score),1);