function [s,t]=spec2sig(p,f);

% SPEC2SIG convert a power spectrum to a signal
%
% [s t]=spec2sig(p,f);
%
% s     signal
% t     corresponding time or length
% p     power spectrum
% f     corresponding frequency or wavenumber

p=p(:)'; f=f(:)';

fN=max(f);                   % Nyquist frequency or wavenumber
np=length(p);                % length of power spectrum
nfft=(np-1)*2;               % lenght of fft

p=p*fN;
p(1)=p(1)*2;

mft=sqrt(p*nfft);            % magnitude of fourier transform
mft=[mft mft((np-1):-1:2)];

pft=(rand(1,np)-.5)*2*pi;    % phase of fourier transform
pft(1)=0; pft(np)=0;         % H(-f) = H(f)* for real h(t)
pft=[pft -pft((np-1):-1:2)]; % (Press et al, p491)

ft=mft.*exp(pft*sqrt(-1));   % fourier transform
s=real(ifft(ft));            % synthetic signal
t=(0:(length(s)-1))/fN/2;    % corresponding time or length
