%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  FileName:            mfRxTimeslotSyn.m
%  Description:         通过PSC滑动相关获得时隙同步
%  Reference:           3GPP TS 25.211, 5.3.3.5 Synchronisation Channel (SCH)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Parameter List:       
%       Output Parameter
%           search_flag     相关峰是否明显,0表示未搜索到相关峰，1表示搜索到相关峰
%           timeslot_star	时隙开始的点数
%           mod_PSC_corr    主同步码相关结果，用于绘制相关峰图像          
%       Input Parameter
%           input_data      输入数据
%           sample_rate     采样率
%           corr_length     滑动相关长度
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  History
%    1. Date:           2017-12-1
%       Author:         david.lee
%       Version:        1.0 
%       Modification:   初稿
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [search_flag, timeslot_star, mod_PSC_corr] = CDMA_RxTimeslotSyn(input_data,sample_rate,corr_length)   

%% 功能实现
%生成主同步码
a=[1, 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, 1];
PSC=(1+1)*[a, a, a, -a, -a, a, -a, -a, a, a, a, -a, a, -a, a, a];
PSC_data=256*sample_rate;
PSC_sample=zeros(1,PSC_data);
for code_num=1:256
    for sample_num=1:sample_rate
        PSC_sample((code_num-1)*sample_rate+sample_num)=PSC(code_num);
    end
end

%主同步码相关
PSC_correlation=zeros(1,corr_length);
for data_num=1:corr_length;
    for n=1:PSC_data
        PSC_correlation(data_num)=PSC_correlation(data_num)+input_data(data_num+n-1)*PSC_sample(n);
    end
end
mod_PSC_corr=abs(PSC_correlation);

%判断相关峰是否明显
timeslot_star=find(max(mod_PSC_corr)==mod_PSC_corr);

if max(mod_PSC_corr) > 2*(sum(mod_PSC_corr)-max(mod_PSC_corr))/(corr_length-1)
    search_flag = 1;
else
    search_flag = 0;
end
end
