1 | function [LongTrainingStart,ValidStart, Corr] = warplab_findLongTrainingStart(RxData,LongTrainingSymbols_up2,AGC_Set_Address) |
---|
2 | |
---|
3 | % The long training symbols are used for timing synchronization |
---|
4 | |
---|
5 | % The long training symbols (LongTrainingSymbols_up2) consist of two long |
---|
6 | % symbols with a 64 sample cyclic prefix. |
---|
7 | |
---|
8 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
9 | % Step 0: Correlate with reference signal |
---|
10 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
11 | % The long training symbols arrive within the 672 samples after the AGC |
---|
12 | % sets the gains. Hence, to find the long training symbols we will only |
---|
13 | % consider the 672 samples after the AGC sets the gains. |
---|
14 | RxData_Window = RxData(AGC_Set_Address+1:AGC_Set_Address+672); |
---|
15 | |
---|
16 | % Correlate the received signal (RxData_Window) with the long training |
---|
17 | % symbols (LongTrainingSymbols_up2). Beacause of the symmetry of the |
---|
18 | % LongTrainingSymbols_up2 signal we can use the conv() function to perform |
---|
19 | % the correlation. |
---|
20 | |
---|
21 | Conv = conv(RxData_Window,LongTrainingSymbols_up2); |
---|
22 | Corr = abs(Conv); |
---|
23 | |
---|
24 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
25 | % Step 1: Find the two peaks in the Corr vector. These peaks indicate the |
---|
26 | % end of a long symbol. The long training sysmbols consist of two long |
---|
27 | % symbols |
---|
28 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
29 | [CorrSorted, CorrSortedIndices] = sort(Corr); |
---|
30 | CorrSortedIndices=CorrSortedIndices(end:-1:1); |
---|
31 | peak_1_index = CorrSortedIndices(1); |
---|
32 | peak_2_index = CorrSortedIndices(2); |
---|
33 | % The two peaks must be 64x2=128 samples away. One peak's correlation (for |
---|
34 | % example peak A) may be much higher or wider than the other one, resulting |
---|
35 | % in two or more values around peak A being larger than peak B. In that |
---|
36 | % case variables peak_1_index and peak_2_index are only a few samples away, |
---|
37 | % instead of 128 samples away. The following if statement checks that the two peak |
---|
38 | % indexes peak_1_index and peak_2_index are not corresponding to the same |
---|
39 | % peak. IF so it tries to correct and checks again, it corrects again and |
---|
40 | % if the third check fails then it gives up. |
---|
41 | if(128 ~= abs(peak_2_index-peak_1_index)) |
---|
42 | peak_2_index = CorrSortedIndices(3); |
---|
43 | end |
---|
44 | if(128 ~= abs(peak_2_index-peak_1_index)) |
---|
45 | peak_2_index = CorrSortedIndices(4); |
---|
46 | end |
---|
47 | if(128 ~= abs(peak_2_index-peak_1_index)) |
---|
48 | peak_2_index = CorrSortedIndices(5); |
---|
49 | end |
---|
50 | ValidStart = 1; |
---|
51 | if(128 ~= abs(peak_2_index-peak_1_index)) |
---|
52 | disp('Invalid correlation values for timing synchronization !'); |
---|
53 | peak1 = peak_1_index; |
---|
54 | peak2 = peak_2_index; |
---|
55 | peakdiff = peak_2_index-peak_1_index; |
---|
56 | ValidStart = 0; |
---|
57 | end |
---|
58 | |
---|
59 | if(peak_1_index > peak_2_index) |
---|
60 | var = peak_1_index; |
---|
61 | peak_1_index = peak_2_index; |
---|
62 | peak_2_index = var; |
---|
63 | end |
---|
64 | |
---|
65 | % Store Address where the first long symbol in the long training symbols |
---|
66 | % starts |
---|
67 | FirstLongSymbol_Start = peak_1_index - 128; |
---|
68 | % Long training symbols have a 64 sample cyclic prefix hence long training |
---|
69 | % symbols start 64 samples before the first long symbol starts |
---|
70 | LongTrainingStart = FirstLongSymbol_Start - 64; |
---|
71 | % Take in count the AGC_Set_Address offset |
---|
72 | LongTrainingStart = LongTrainingStart + AGC_Set_Address; |
---|
73 | |
---|
74 | |
---|
75 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
76 | % Plotting for debugging purposes |
---|
77 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
78 | % figure |
---|
79 | % subplot(2,2,1) |
---|
80 | % plot(real(Conv)) |
---|
81 | % subplot(2,2,2) |
---|
82 | % plot(imag(Conv)) |
---|
83 | % subplot(2,2,3) |
---|
84 | % plot(Corr) |
---|
85 | % subplot(2,2,4) |
---|
86 | % plot(angle(Conv)) |
---|
87 | |
---|