function S=magspec(X,N)
% magspec - get magnitude spectra of columns of x (v. 2)
%
% magspec :: [[N,L]] -> [[M,L]] :- M=dftbins(N).
%
% Version 2 corrects error in V. 1 - the wrong bands were being
% doubled up in the power reckoning. This version now guarantees
% that sum(magspec(x).^2) = sum(x.^2).

if nargin<2, N=size(X,1); end

bins=1+floor(N/2);
Y=fft(X,N);
S=abs(Y(1:bins,:))*sqrt(2/N); % sqrt of double of total power per band
singles=1:ceil(N/2):bins;
S(singles,:)=S(singles,:)/sqrt(2); % undo double counting of singletons

