rom_index = round(phase_accumulator/2^20); if(rom_index ==0) rom_index =1; end end
% plot the bpsk result figure(1); plot(1:1024, bpsk_mod(1:1024), 'r', 1:1024, 1024*source_data(1:1024),'b'); legend('bpsk mod', 'source data'); title("BPSK modulation");
%% bpsk demodulation using pll % the demodulation steps % 1. iq demod, using the iq demodulation to get the iq data, the receiver lo_freq is coming for nco % 2. low pass the demod signal to filter out high frequency(using mean filter ) % 3. phase detect, calculate the phase error and generate the frequency error % 4. loop filter parameter calculate % 5. drive the nco to generate local carrier frequency rom_index =1; % rom index mean_cal_index =1; i_data=0; q_data=0; % i/q demod data i_acc =0; q_acc =0; % i/q accumulate value i_mean=1; q_mean=1; % mean value of i/q samples p_dot =1; p_cross=1; % dot/cross value of 2 vectors delta_phi =0; % phase error freq_err =0; % freq error phase_accumulator =0; % clear the phase accumulator freq_err_out = zeros(1,SAMP_LEN); demod_i = zeros(1,SAMP_LEN); demod_q = zeros(1,SAMP_LEN); freq_err_control_word =0; freq_k1_acc_t =0; freq_k1_acc =0; freq_k2 =0; loop_out =0; loop_out_t=0;
for i=1:SAMP_LEN % mixing i_data = bpsk_mod(i)*carrier_wave_cos(rom_index); q_data = -bpsk_mod(i)*carrier_wave_sin(rom_index);
%% prepare mean filter(calculate MEAN_FILTER_LEN samples) % accumulate the samples i_acc = i_acc + i_data; q_acc = q_acc + q_data; if (mean_cal_index == MEAN_FILTER_LEN) i_mean_t = i_mean; q_mean_t = q_mean; % calculate mean value i_mean = i_acc/MEAN_FILTER_LEN; q_mean = q_acc/MEAN_FILTER_LEN; % clean accumulator i_acc =0; q_acc =0;
mean_cal_index =0; end mean_cal_index = mean_cal_index + 1;
%% NCO control using loop filter out value phase_accumulator = phase_accumulator + RX_FREQ_CTRL_WORD + freq_err_control_word; 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