1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| clc; close all; clear all;
PN_INDEX1 = 8 ; PN_INDEX2 = 10; SAMP_RATE = 8.184e6; CODE_RATE = 2.046e6; NH_CODE_LEN = 20; SOURCE_DATA_LEN = 300; PN_CODE_LEN = 2046; PN_SAMP_LEN = (SAMP_RATE/CODE_RATE) * PN_CODE_LEN * NH_CODE_LEN * SOURCE_DATA_LEN;
source_data = rand(1, SOURCE_DATA_LEN); source_data(source_data < 0.5) = -1; source_data(source_data >= 0.5) = 1; source_data = repelem(source_data, 1, NH_CODE_LEN);
nh_code = [0 0 0 0 0 1 0 0 1 1 0 1 0 1 0 0 1 1 1 0]; nh_code = repmat(nh_code, 1, SOURCE_DATA_LEN); nh_code(nh_code == 0) = -1;
nh_mod = source_data .* nh_code;
nh_mod = repelem(nh_mod, 1, (SAMP_RATE/CODE_RATE)*PN_CODE_LEN);
[pn_code, pn_code_sample] = PNCode_Gen(PN_CODE_LEN, PN_SAMP_LEN, CODE_RATE, SAMP_RATE, PN_INDEX1);
pn_mod = nh_mod .* pn_code_sample;
AMP = 1; ROM_DEPTH = 4096; CARRIER_FREQ = CODE_RATE; FREQ_CTRL_WORLD = CARRIER_FREQ * 2^32/ SAMP_RATE;
rom_addr = 0: 1/ROM_DEPTH: 1-1/ROM_DEPTH; carrier_wave_cos = AMP*cos(2*pi*rom_addr); carrier_wave_sin = AMP*sin(2*pi*rom_addr);
bpsk_mod = zeros(1,PN_SAMP_LEN); rom_index = 1; phase_accumulator = 0;
for i=1:PN_SAMP_LEN if(pn_mod(i) == -1) bpsk_mod(i) = -carrier_wave_cos(rom_index); else bpsk_mod(i) = carrier_wave_cos(rom_index); end
phase_accumulator = phase_accumulator + FREQ_CTRL_WORLD; if(phase_accumulator > 2^32) phase_accumulator = phase_accumulator - 2^32; end
rom_index = round(phase_accumulator/2^20); if(rom_index == 0) rom_index = 1; end end
figure(1); plot(1:1024, bpsk_mod(1:1024), 'r', 1:1024, pn_mod(1:1024),'b'); axis([0, 1024, -2, 2]); legend('bpsk mod', 'pn mod'); title("BPSK modulation");
DATA_OFFSET = 1020; data_in = bpsk_mod(DATA_OFFSET: end);
SAMP_PER_PN = round((SAMP_RATE/CODE_RATE)*PN_CODE_LEN); HALF_CODE_WIDTH = round((SAMP_RATE/CODE_RATE)/2); PN_SEARCH_RANGE = round(SAMP_PER_PN/HALF_CODE_WIDTH);
[pn_code, pn_code_sample] = PNCode_Gen(PN_CODE_LEN, SAMP_PER_PN, CODE_RATE, SAMP_RATE, PN_INDEX1); pn_pre = [pn_code_sample(HALF_CODE_WIDTH+1:end), pn_code_sample(1:HALF_CODE_WIDTH)]; pn_middle = pn_code_sample; pn_post =[pn_code_sample(SAMP_PER_PN-HALF_CODE_WIDTH+1:end), pn_code_sample(1:SAMP_PER_PN-HALF_CODE_WIDTH)];
sin_carrier = zeros(1, SAMP_PER_PN); cos_carrier = zeros(1, SAMP_PER_PN); rom_index = 1; phase_accumulator = 0;
power_pre = zeros(1, PN_SEARCH_RANGE); power_middle = zeros(1, PN_SEARCH_RANGE); power_post = zeros(1, PN_SEARCH_RANGE);
for i=1:SAMP_PER_PN sin_carrier(i) = carrier_wave_sin(rom_index); cos_carrier(i) = carrier_wave_cos(rom_index); phase_accumulator = phase_accumulator + FREQ_CTRL_WORLD; if(phase_accumulator > 2^32) phase_accumulator = phase_accumulator - 2^32; end
rom_index = round(phase_accumulator/2^20); if(rom_index == 0) rom_index = 1; end end
signal = data_in(1: SAMP_PER_PN); sample_offset = 0;
for code_index = 1: PN_SEARCH_RANGE i_data = signal .* cos_carrier; q_data = signal .* sin_carrier; temp_pre_i = sum(i_data .* pn_pre); temp_middle_i = sum(i_data .* pn_middle); temp_post_i = sum(i_data .* pn_post); temp_pre_q = sum(q_data .* pn_pre); temp_middle_q = sum(q_data .* pn_middle); temp_post_q = sum(q_data .* pn_post);
power_pre(code_index) = temp_pre_i^2 + temp_pre_q^2; power_middle(code_index) = temp_middle_i^2 + temp_middle_q^2; power_post(code_index) = temp_post_i^2 + temp_post_q^2;
sample_offset = sample_offset + SAMP_PER_PN; signal = data_in(sample_offset+1: sample_offset+SAMP_PER_PN); pn_pre = [pn_pre(HALF_CODE_WIDTH+1:end), pn_pre(1:HALF_CODE_WIDTH)]; pn_middle = [pn_middle(HALF_CODE_WIDTH+1:end), pn_middle(1:HALF_CODE_WIDTH)]; pn_post = [pn_post(HALF_CODE_WIDTH+1:end), pn_post(1:HALF_CODE_WIDTH)];
end figure(2); plot(power_pre, 'r'); hold on; plot(power_middle, 'g'); hold on; plot(power_post, 'b'); title("pn code capture");
|