function f=PlotFft(n1,n2,t1,t2);
%   This function plots the fft of the difference of a  test case with the 
%       base case, it also plots the fft of each data set.
%
% INPUT
%   n1 = Name of the file containing data from base case simulation 
%           exported from ADS with the first line containing text deleted
%   n2 = Name of the file containing data from test case simulation 
%           also exported from ADS 
%   t1 = string to be displayed as title on first plot produced
%   t2 = string to be displayed as title on second plot
%
% OUTPUT
%   f = the three lengths that are associated with the three peaks
%       The first f value is the length of the Base Case
%       The second f value is the length of the Test Case
%       The third f value is the max peak of the difference plot
%       The fourth f value indicates the first peak of the difference plot
%           and should correspond to the distance to the 'break'.
%
%   Author: Jeremy Pruitt
%   Last Modified: Oct. 23, 2002


startup  % File provided by You Chung Chung to make lines bolder

% Load data sets from names given
data1=load(n1); 
data2=load(n2);

% Find Average and subtract from original data
dt1=mean(data1(:,2));
dt2=mean(data2(:,2));

dat1=data1(:,2)-dt1;
dat2=data2(:,2)-dt2;

% Determine length index of graph
er=3.2;         % Epsilon relative Defined by Msub parameters
d=.000762;      % 30 mils (H)
W=.001821;      % 71.678 mils (W)
epsilonE=(er+1)/2 + ((er-1)/2)*(1/sqrt(1+12*d/W)); % Epsilon effective
c= 2.998e8;                     % Speed of Light
u= c/sqrt(epsilonE);            % Velocity of propagation
deltaF=data1(2,1)-data1(1,1);   % Frequency step distance
Lmax= u/(4*deltaF);             % maximum length of cable
Lin=Lmax*39.37;                 % Convert to inches
Ltemp=Lin*2;                    % Expand to fit 1024 point fft
distance=0:(Lin*2)/(1024-1):Lin*2; % Calculate step and adjust

% Compute 1024 Fourier Transforms
four1=fft(dat1,1024);     % Base Case
four2=fft(dat2,1024);     % Test Case
diff=dat1-dat2;     % difference between two cases
dfm=mean(diff);

four=fft(diff,1024);

% Compute Magnitudes of Fourier Transforms
magFour1=abs(four1);
magFour2=abs(four2);
magFour=abs(four);

% Find Peak locations(i.e. length of cable)
nd=1;             % Noise Displacement to compensate for noise at zero inches
[peak1,loc1]=max(magFour1(nd:end/2));
loc1=loc1-1;
[peak2,loc2]=max(magFour2(nd:end/2));
loc2=loc2-1;
[peak,loc]=max(magFour(1:end/2));
%loc=loc;
fstPeak=peak/2;         % Initialze minimum peak value
i=1;                   % first index to check for first peak
fstLoc=i;               % Stores the location of the first peak

% Find first peak
while((fstPeak==peak/2)&(i<length(magFour)))              % Look for first value larger than min peak value
    if(fstPeak<magFour(i))  
        while(magFour(i)>fstPeak)   %  Go up the slope until you hit the peak    
            fstPeak=magFour(i);
            i=i+1; 
        end
        fstLoc=i-1;                 % Assign index to acces distance vector
        fstPeak=magFour(i-1);       % Record the peak value to plot later  
    end
    i=i+1;
end
     
f=[distance(loc1+nd) distance(loc2+nd) distance(loc) distance(fstLoc)];

% Set graph size
minx=0;     
maxx=Lin/5;   % Only look at half the data
miny=0;     % Magnitudes are all positive
maxy1=40;   % These are changed depending on how close we want to zoom in
maxy2=30;

% Plot Of Raw Data
figure(1)
subplot(2,1,1), plot(data1(:,1),data1(:,2),data2(:,1),data2(:,2),'r')
title('Orignal Waveforms')
legend('Base Case','Test Case',0);
subplot(2,1,2), plot(data1(:,1),diff,'k')
title('Difference between Waveforms')
xlabel('Frequency')

% Plot Results of FFTs
figure(2)
subplot(2,1,1), plot(distance,magFour1,'b',distance,magFour2,'r',distance(loc1+nd),peak1,'go',distance(loc2+nd),peak2,'c*');
axis([minx maxx miny maxy1 ]);
title(t1);
ylabel('Magnitude of FFT');
%xlabel('Distance in Inches');
%legend('Base Case','Test Case',['Base Case is ', num2str(f(1)) ' in.'],['Test Case is ', num2str(f(2)) ' in.']  );
%legend('Base Case','Test Case',['d_{BC}= ', num2str(f(1)) '"'],['d_{TC}= ', num2str(f(2)) '"']  );
legend('BC','TC',['d_{BC}= ', num2str(f(1)) '"'],['d_{TC}= ', num2str(f(2)) '"'],0);

% Second Plot
%figure(2)
subplot(2,1,2), plot(distance,magFour,'k',distance(fstLoc),fstPeak,'o',distance(loc),peak,'x');
axis([minx maxx miny maxy2]);
title(['FFT of the difference: ', t2]);
ylabel('Magnitude of FFT');
xlabel('Distance in Inches');
%legend( 'FFT',['The first peak is ', num2str(distance(fstLoc)),'" from the FDR'],['The max peak is ', num2str(distance(loc)),'" from the FDR']);
legend( 'FFT',['d_{FP}= ', num2str(distance(fstLoc)),'"'],['d_{MP}= ', num2str(distance(loc)),'"'],0);
