% 
%  FUNCTION X = LOWPEAK (M)
%
%  DESCRIPTION
%    Returns the time signal X with minimized peakfactor
%    given a frequency magnitude response M. The algorithm is
%    extracted from Schroeder et al (1970).
%  
%  ARGUMENT
%    M  - frequency magnitude response containing the frequency
%         response in equidistant bins which correspond to frequency
%         0 to the Nyquist frequency (best to make the length of M
%         a power of 2). The algorithm calculates a frequency phase 
%         response that< together with M, yields a low-peak time signal. 
%    
%  RETURN VALUE
%    X  - Time signal with minimized peak factor 
%  
%  EXAMPLE
%    >> sweep = lowpeak (ones(1,512));
% 
%  .. Hofware Inc...
% 

function Signal = lowpeak (Magnitude)

Nbin       = length (Magnitude);

TotalPower = sum (Magnitude.^2); 
NormFactor = 1.0/TotalPower;

TwoPi      = 2*pi;  
Factor     = 0.0;
Phi        = 0.0;
Power      = 0.0;
Spectrum   = zeros (1, Nbin);

for j=1:Nbin 

  Spectrum(j) = Magnitude(j) * exp (i*Phi);
  Power = Power + NormFactor*Magnitude(j).^2;
  Phi   = Phi - TwoPi*Power;     

end;

Spectrum = [Spectrum -conj([Spectrum(1) Spectrum((Nbin):-1:2)])];
Signal   = imag(ifft(Spectrum)); 
