function [signal_detected,start_of_data,H] = FrameSynch(input,Th)
%=================================%
%load in preambles for cross-correlation
%=================================%
load Receiver/short_time %time domain of one short preamble symbol
load Receiver/flip_conj_short_preamble %flipped and complex conjugated
xsp = short_time(1:16);
load Receiver/long_time %one symbol of long preamble in time domain
load Receiver/flip_conj_long_preamble %above flipped and complex conjugated
%=================================%
%take input and place into blocks of L; L=16 for short preamble
%perform cross-correlation
%look for correlation values above threshold
%=================================%
L=16;n=1;short_ind=zeros(1,10);AGC=0;
short_corr = zeros(1,length(input));
for i = 1: length(input)-2*L; %L=sample length that is repeated
b1 = input(i:i+L-1); %sliding window
P = xsp' * b1; %cross correlation with stored preamble
Ps = P' * P; %get magnitude
R2 = (xsp' * xsp); %power of stored preamble for normalization
Rs = R2 * R2; %square term so proportional to Ps
short_corr(i) = Ps / Rs; %normalize with respect to preamble power
%Apply Automatic Gain Control to faded signals: see below
% if AGC == 1
% short_corr(i) = short_corr(i) * pow_correction;
% end
%find peaks above threshold
if short_corr(i) > Th && n == 1;
short_ind(n) = i;
n = n+1;
elseif short_corr(i) > Th && n < 11 && short_ind(n-1)+ 16 == i
short_ind(n) = i;
n = n+1;
% if n == 5
% %Automatic Gain Control: Boost faded signals
% r_pow =(input(i-64:i)' * input(i-64:i))/64;
% p_pow =(short_time' * short_time)/64;
% if p_pow > r_pow
% AGC=1;
% pow_correction = (p_pow/r_pow);
% end
% end
end
end
%figure;plot(short_corr(400:700)); %plot cross-correlation
%=================================%
%16 samples after last peak starts the long preamble
%=================================%
if n == 11
start_of_data = short_ind(10) + 16 +160;
signal_detected=1;
else
signal_detected=0;H=0;start_of_data=0;
return
end
%=================================%
%Estimate Channel
%=================================%
% L=16; %length of channel set by standard to 16
LP=input(start_of_data -160:start_of_data-1);
LP1=LP(33:96); %1st Long Preamble
LP2=LP(97:160); %2nd Long Preabmle
% % y(n) = h(n)*xlp(n) + noise;
% % y= Xh + noise in vector notation
% c = long_time;r = [long_time(1);long_time(64:-1:64-L+2)]; %column wins
% X = toeplitz(c,r);
% h1 = X\LP1;
% h2 = X\LP2;
% h=0.5*(h1+h2); %channel impulse response
% figure;plot(h .* conj(h));
% H=fft(h,64); %Frequency response of channel
%
% %=================================%
% %plot channel impulse response
% %=================================%
%
% Y1 = fft(LP1);
% Y2 = fft(LP2);
% X = fft(long_time);
% a = find(round(abs(X))==1);
% X1 = X(a);
% H = (Y1(a)+Y2(a))./(2*X1);
% h1 =ifft(H);
% figure;plot(h1 .* conj(h1));
% % a=1;
xLP=long_time; % Long Preamble
L=16; % choose L>16 to account for uncertainty in the estimated beginning of the
preamble
X=toeplitz(xLP, [xLP(1); xLP(64:-1:64-L+2)]);
Xinv=inv(X'*X)*X';
h1=Xinv*LP1;
h2=Xinv*LP2;
h=0.5*(h1+h2);
H=fft(h,64);
%figure;plot(abs(H));xlabel('Frequency');ylabel('Magnitude')