clear all;
close all;
clc;
tmp = csvread('.\20111126\tek0000CH1.csv',15,0);
t01 = tmp(:,1);
a01 = tmp(:,2)- mean(tmp(:,2));
dt = t01(2)- t01(1);
Fs = 1/dt;
f0=180e6;      % Linear chirp starting frequency,Hz
f1=210e6;      % Linear chirp ending frequency,Hz
fc=(f0+f1)/2;
BW=f1-f0;      % Linear chirp bandwidth,Hz  
LO=195e6;
[B A] = butter(8,BW/Fs*1.1);
a01b = a01.*exp(-j*2*pi*LO*t01);
a01b = filtfilt(B,A,a01b);
figure;plot(t01,real(a01b));

pulseDuration=1e-6;          %linear chirp duration,sec
alpha=pi*BW/pulseDuration;    %Chirp rate
wc=2*pi*fc;
w0=2*pi*f0;
n = pulseDuration/dt + 1;           % number of time samples
t=0:dt:(n-1)*dt;                    % time array for data acquisition
t = t';
ref=cos(w0*t+alpha*(t.^2));         % waveform
ref = ref.*tukeywin(n,0.01);        % amplitude tapering
refw = ref.*chebwin(size(ref,1),100);   % windowed waveform
refwb = refw.*exp(-j*2*pi*LO*t);
refwb = filtfilt(B,A,refwb);
s01 = xcorr(refwb,a01b);
figure;plot(20*log10(abs(s01)/max(abs(s01))));
% b01 = a01(275000:285000);
% tb = 0:dt:(length(b01)-1)*dt;
% tb = tb';
% b01b = b01.*exp(-j*2*pi*LO*tb);
% b01b = filtfilt(B,A,b01b);
% s02 = xcorr(refwb,b01b);
% figure;plot(20*log10(abs(s02)/max(abs(s02))));
% grid
% s03 = xcorr(refwb,refwb);
% figure;plot(20*log10(abs(s03)/max(abs(s03))));
% grid