function out = RX_PN(in,dBc, cutoff, floor, Fs)
 % =========================================================================
 % Book title: Digital Front-End Compensation for Emerging Wireless Systems
 % Authors: Fran鏾is Horlin (ULB), Andr� Bourdoux (IMEC)
 % Editor: John Wiley & Sons, Ltd
 % Date: 2008
 % =========================================================================
 %
 % DESCRIPTION
 % This function applies phase noise to the signal stream(s). Each row
 % contains one stream. The same phase noise is applied to each stream as
 % if they are working with the same local oscillator. The phase noise has
 % a PSD defined by four values: integrated phase noise, cutoff frequency, a
 % phase noise floor and the sample rate. At the cutoff frequency, the PSD
 % starts a -20dB/dec roll-off
 %
 % INPUTS
 % - in [length x Ns]: input signal where each row is a signal
 % from a different antenna.
 % - dBc [1]: integrated phase noise
 % - cutoff [1]: cutoff frequency of the PSD
 % - floor [1]: phase noise floor
 % - Fs [1]: sample rate
 %
 % OUTPUTS
 % - out [length x Ns]: Ns streams of output signals
 [M N] = size(in);
 ltx = 2^(fix(log2(M-0.5))+2); % ltx will always be at least 2xM
 if cutoff/(Fs/ltx)<16,
 ltx = 16*Fs/cutoff;
 ltx = 2^(fix(log2(ltx-0.5))+1);
 end
 PhaseNoise = FreqSynthLorenzian_new(dBc, cutoff, floor, Fs, ltx).';
 Nphn = length(PhaseNoise);
 if Nphn<M
 error(['Phase noise vector must be longer than ' num2str(Nsamples)])
 end
 Nstart = fix(rand(1,1)*(Nphn-M)); % random staring point
 PhaseNoise = PhaseNoise(Nstart:Nstart+M-1);
 % Add phase noise to data
 out = zeros(size(in));
 for k=1:N
 out(:,k) = in(:,k).*PhaseNoise.';
 end
 %%%%%%%%%%%%%
 function y = FreqSynthLorenzian_new( K, B, p2, Fs, ltx);
 % - lo: time domain vector, possible time domain representation of the
 % oscillator amplitude.
 % - K: total noise power in dBc
 % - B: 3dB bandwidth
 % - p2: noise plateau level in dBc/Hz, to be set at #20dB above the noise
 % floor.
 % - ltx: length of the signal lo vector to return
 % The negative frequencies of the BB equivalent LO spectrum are the
 % complex conjugate of the positive frequencies (as a result, phi(t) is
 % real). The reason for this is not the need for the phi(t) or lo(t) to be
 % real, but it is coming from the analogy to FM or PM modulations that do
 % create such 抯ymetric� frequency responses.
 % - PHI(f), hence LO(f), should not be generated as a spectrum with a wanted
 % shape, but as a PSD (representation for a random process) with a wanted
 % mask.
 global d
 Ns = ltx; % Number of samples for ifft calculation
 df = Fs/Ns; % frequency resolution
 k = [0:1:Ns/2-1, -Ns/2:1:-1]; % frequency index range
 % frequency range: f = k * df
 p2o = p2;
 p2 = 10^(p2/10); % in V^2/Hz
 p2 = p2*df; % in V^2/df
 Ko = K;
 K = 10^(K/10); % in V^2
 B = B/df; % 3-dB bandwidth index
 % Low frequency part is defined by Lorenzian function
 SSBmask = sqrt( K*B/pi./([1:Ns/2].^2+B^2) );
 % High frequency part is defined by the noise floor of the system p2
 SSBmask = max(SSBmask, sqrt(p2)*ones(size(SSBmask)));
 %-- Phase noise PHI(f, f>0) is first generated as a wide band signal
 PHI = sqrt(0.5) * abs( randn(1,Ns/2) + j*randn(1,Ns/2) );
 PHI = PHI .* exp(j*2*pi*rand(1,Ns/2));
 %-- Phase noise PHI(f, f>0) is shaped according to the wanted mask
 PHI = PHI .* SSBmask;
 %-- Phase noise PHI(f) is then generated from PHI(f, f>0)
 % (no phase noise on the carrier)
 PHI = [0 PHI(1:Ns/2-1) conj(PHI(Ns/2:-1:1))];
 NoisePower = 10*log10( sum(abs(PHI).^2) );
 if (0)
 f = k(2:Ns/2)*df;
 PHI_f = 20*log10(abs(PHI(2:Ns/2))/sqrt(df));
 SSBmask_f = 20*log10(SSBmask(1:Ns/2-1)/sqrt(df));
 % normalisation to get PHI(f) in dBc/Hz, and not dBc/df
 figure(20),semilogx(f, PHI_f, 'b-','linewidth',1);
 hold on; grid on; zoom on;
 semilogx(f, SSBmask_f, 'r-','linewidth',2);
 axis([f(1) f(end) p2o-10 SSBmask_f(1)+10]);
 end;
 %-- Correction for the integrated phase noise power:
 PHI = PHI * 10^((Ko-NoisePower)/20);
 %-- Phase noise phi(t) in the time domain
 phi = ifft(PHI,Ns)*Ns;
 %-- Local oscillator signal lo(t) in the time domain
 lo = exp(j*phi);
 %-- Local oscillator signal LO(f) in the frequency domain
 if (0)
 LO = fft(lo,Ns)/Ns;
 f = k(2:Ns/2)*df;
 LO_f = 20*log10(abs(LO(2:Ns/2))/sqrt(df));
 SSBmask_f = 20*log10(SSBmask(1:Ns/2-1)/sqrt(df));
 figure(21);semilogx(f, LO_f, 'k-','linewidth',1);
 hold on; grid on; zoom on;
 semilogx(f, SSBmask_f, 'r-','linewidth',2);
 axis([f(1) f(end) p2o-10 SSBmask_f(1)+10]);
 end;
 %-- Prepare lo(t) for efficient use in dbbm_fe
 y = lo(1,1:ltx*fix(Ns/ltx));
 y = reshape(y,ltx,fix(Ns/ltx));










