% test pulsecompressor class...

close all
clearvars
clc

%

% Set up example constants ...

% Define parameters of the LFM pulse.
sampleRate = 32e5;              % Define sample rate of baseband signal (Hz)
pulseWidth = 13.4375e-6;        % Define pulse width (seconds)
% PriTimeSec = 133e-6;% Define pulse repetition interval (seconds)
% priLength=PriTimeSec*sampleRate;

% Calculate the PRI time based on the number of samples per PRI.
priLength = 433;
PriTimeSec = priLength/sampleRate;

bandWidth = 2e6;               % Bandwidth of the pulse (Hz)
IPhase = 0;                     % Initial phase in degrees of IComponent
QPhase = 90;                    % Initial phase in degrees of QComponent
noiseAmp = 2;
InterfScale= 10;
ourNoiseStdDev = 1;

%

% Create Pulse compressor class...

LrPc = PulseCompressor(waveformTypes.LfmUpChirp,bandWidth,pulseWidth,sampleRate,(PriTimeSec*sampleRate),-40,6);
Fig1=figure("Name","Replica");
plot(real(LrPc.Replica));

%



%

% Generate something to pulse compress...
% tVector = 0:1/sampleRate:pulseWidth; % Vector to generate ideal pulse
% IComponent = chirp(tVector,-bandWidth/2,tVector(end), bandWidth/2,'linear',IPhase);
% QComponent = chirp(tVector,-bandWidth/2,tVector(end), bandWidth/2,'linear',QPhase);
% IQData = IComponent + 1i*QComponent;
tVector = -pulseWidth/2:1/sampleRate:pulseWidth/2; % Vector to generate ideal pulse

%                 IComponent = chirp(tVector,-bandWidth/2,tVector(end), bandWidth/2,'linear',IPhase);
%                 QComponent = chirp(tVector,-bandWidth/2,tVector(end), bandWidth/2,'linear',QPhase);
LFM = exp(1i*pi*(bandWidth/pulseWidth)*(tVector.^2));
figure("Name","Ideal LFM I & Q");
plot(imag(LFM),"r");
hold;
plot(real(LFM),"b"); 
IQData=LFM;


%


% Normalize amplitude.
scale = max(max(abs(real(IQData))), max(abs(imag(IQData))));
idealPulse = IQData / scale;

% Pad the pulse with zeros for the off cycle of the pulse.

offPulse = 0:1/sampleRate:((PriTimeSec-pulseWidth)/2);
idealSpacedPulse = [offPulse idealPulse offPulse];
idealSpacedPulse = idealSpacedPulse(1,1:priLength);

figure("Name","pure test waveform")
plot(real(idealSpacedPulse),"r");
hold;
plot(imag(idealSpacedPulse),"b");

%
% Just for fun, lets look at the spectrum of this signal...
%


freq=((-sampleRate/2):sampleRate/length(idealSpacedPulse):sampleRate/2)/1e6;
fdomain=fftshift(fft(idealSpacedPulse));
figure("Name","Spectrum");
plot(freq(1:end-1),20*log10(abs(fdomain)));
xlabel("MHz")

%

% pulse compress this signal
PcResult = LrPc.compress(idealSpacedPulse);

figure("Name","pure test waveform")
maxValue = max(abs(PcResult));
% plot(20*log10(abs(PcResult)/maxValue));
plot((abs(PcResult)/maxValue));

%

idealNoise = ourNoiseStdDev.*randn(size(idealSpacedPulse));
interfNoise = zeros(size(idealNoise));
pulseWithBkgNoise = idealSpacedPulse + idealNoise;

%
%
% Lets look at the resulting spectrum....
%

freq=((-sampleRate/2):sampleRate/length(pulseWithBkgNoise):sampleRate/2)/1e6;
fdomain=fftshift(fft(pulseWithBkgNoise));
figure("Name","Spectrum");
plot(freq(1:end-1),abs(fdomain));
xlabel("MHz")

%


PcResult = LrPc.compress(pulseWithBkgNoise);
f1 = figure;
fl.Name='PC Result';
maxValue = max(abs(PcResult));
plot(20*log10(abs(PcResult)/maxValue));
