prev_tx_symbols = ones(Nc+1,1);
prev_rx_symbols = ones(Nc+1,1);
foff_phase_rect = 1;
-track = 0;
+coarse_fine = 0;
fest_state = 0;
% Octave outputs we want to collect for comparison to C version
rx_symbols_log = [];
rx_bits_log = [];
sync_bit_log = [];
-track_log = [];
+coarse_fine_log = [];
for f=1:frames
[pilot prev_pilot pilot_lut_index prev_pilot_lut_index] = get_pilot(pilot_lut_index, prev_pilot_lut_index, M);
[foff_coarse S1 S2] = rx_est_freq_offset(rx_fdm, pilot, prev_pilot, M);
- if track == 0
+ if coarse_fine == 0
foff = foff_coarse;
end
- foff_log = [foff_log foff];
foff_coarse_log = [foff_coarse_log foff_coarse];
pilot_baseband1_log = [pilot_baseband1_log pilot_baseband1];
rx_bits_log = [rx_bits_log rx_bits];
foff_fine_log = [foff_fine_log foff_fine];
sync_bit_log = [sync_bit_log sync_bit];
+ foff -= 0.5*foff_fine;
+ foff_log = [foff_log foff];
% freq est state machine
- [track fest_state] = freq_state(sync_bit, fest_state);
- track_log = [track_log track];
+ [coarse_fine fest_state] = freq_state(sync_bit, fest_state);
+ coarse_fine_log = [coarse_fine_log coarse_fine];
end
% Compare to the output from the C version
plot_sig_and_error(9, 212, foff_fine_log, foff_fine_log - foff_fine_log_c, 'Fine Freq Offset' )
plot_sig_and_error(10, 211, foff_log, foff_log - foff_log_c, 'Freq Offset' )
-plot_sig_and_error(10, 212, track_log, track_log - track_log_c, 'Freq Track' )
+plot_sig_and_error(10, 212, coarse_fine_log, coarse_fine_log - coarse_fine_log_c, 'Freq Est Coarse(0) Fine(1)', [1 frames -0.5 1.5] )
c=15;
plot_sig_and_error(11, 211, real(rx_baseband_log(c,:)), real(rx_baseband_log(c,:) - rx_baseband_log_c(c,:)), 'Rx baseband real' )
plot_sig_and_error(12, 212, imag(rx_filt_log(c,:)), imag(rx_filt_log(c,:) - rx_filt_log_c(c,:)), 'Rx filt imag' )
plot_sig_and_error(13, 211, env_log, env_log - env_log_c, 'env' )
-plot_sig_and_error(13, 212, real(rx_symbols_log(c,:)), real(rx_symbols_log(c,:) - rx_symbols_log_c(c,:)), 'rx symbols' )
+stem_sig_and_error(13, 212, real(rx_symbols_log(c,:)), real(rx_symbols_log(c,:) - rx_symbols_log_c(c,:)), 'rx symbols' )
st=10*28;
en = 12*28;
check(rx_symbols_log, rx_symbols_log_c, 'rx_symbols');
check(rx_bits_log, rx_bits_log_c, 'rx bits');
check(sync_bit_log, sync_bit_log_c, 'sync bit');
+check(coarse_fine_log, coarse_fine_log_c, 'coarse_fine');
printf("\npasses: %d fails: %d\n", passes, fails);
f->pilot_lpf1[i].imag = f->pilot_lpf2[i].imag = 0.0;
}
+ f->foff = 0.0;
f->foff_rect.real = 1.0;
f->foff_rect.imag = 0.0;
f->foff_phase_rect.real = 1.0;
f->foff_phase_rect.imag = 0.0;
f->fest_state = 0;
- f->track = 0;
+ f->coarse_fine = COARSE;
return f;
}
int freq_state(int sync_bit, int *state)
{
- int next_state, track;
+ int next_state, coarse_fine;
/* acquire state, look for 6 symbol 010101 sequence from sync bit */
*state = next_state;
if (*state >= 6)
- track = 1;
+ coarse_fine = FINE;
else
- track = 0;
+ coarse_fine = COARSE;
- return track;
+ return coarse_fine;
}
/*---------------------------------------------------------------------------*\
void fdmdv_demod(struct FDMDV *fdmdv, int rx_bits[], int *sync_bit, float rx_fdm[], int *nin)
{
- float foff;
+ float foff_coarse, foff_fine;
COMP rx_fdm_fcorr[M+M/P];
COMP rx_baseband[NC+1][M+M/P];
COMP rx_filt[NC+1][P+1];
/* freq offset estimation and correction */
- foff = rx_est_freq_offset(fdmdv, rx_fdm, *nin);
- freq_shift(rx_fdm_fcorr, rx_fdm, foff, &fdmdv->foff_rect, &fdmdv->foff_phase_rect, *nin);
+ foff_coarse = rx_est_freq_offset(fdmdv, rx_fdm, *nin);
+ if (fdmdv->coarse_fine == COARSE)
+ fdmdv->foff = foff_coarse;
+ freq_shift(rx_fdm_fcorr, rx_fdm, fdmdv->foff, &fdmdv->foff_rect, &fdmdv->foff_phase_rect, *nin);
/* baseband processing */
fdm_downconvert(rx_baseband, rx_fdm_fcorr, fdmdv->phase_rx, fdmdv->freq, *nin);
rx_filter(rx_filt, rx_baseband, fdmdv->rx_filter_memory, *nin);
rx_timing = rx_est_timing(rx_symbols, rx_filt, rx_baseband, fdmdv->rx_filter_mem_timing, env, fdmdv->rx_baseband_mem_timing, *nin);
- ferr = qpsk_to_bits(rx_bits, sync_bit, fdmdv->prev_rx_symbols, rx_symbols);
+ foff_fine = qpsk_to_bits(rx_bits, sync_bit, fdmdv->prev_rx_symbols, rx_symbols);
memcpy(fdmdv->prev_rx_symbols, rx_symbols, sizeof(COMP)*(NC+1));
+
+ fdmdv->coarse_fine = freq_state(*sync_bit, &fdmdv->fest_state);
+ fdmdv->foff -= 0.5*foff_fine;
}
#define NPILOTLPF (4*M) /* number of samples we DFT pilot over, pilot est window */
#define MPILOTFFT 256
+/* freq offset sestimation states */
+
+#define COARSE 0
+#define FINE 1
+
/*---------------------------------------------------------------------------*\
STRUCT for States
COMP S1[MPILOTFFT];
COMP S2[MPILOTFFT];
+ float foff;
COMP foff_rect;
COMP foff_phase_rect;
COMP prev_rx_symbols[NC+1];
int fest_state;
- int track;
+ int coarse_fine;
};
/*---------------------------------------------------------------------------*\
COMP tx_fdm[M];
float rx_fdm[M+M/P];
float foff_coarse;
- float foff;
int nin;
COMP rx_fdm_fcorr[M+M/P];
COMP rx_baseband[NC+1][M+M/P];
int rx_bits_log[FDMDV_BITS_PER_FRAME*FRAMES];
float foff_fine_log[FRAMES];
int sync_bit_log[FRAMES];
- int track_log[FRAMES];
+ int coarse_fine_log[FRAMES];
FILE *fout;
int f,c,i;
/* freq offset estimation and correction */
foff_coarse = rx_est_freq_offset(fdmdv, rx_fdm, nin);
- if (fdmdv->track == 0)
- foff = foff_coarse;
- freq_shift(rx_fdm_fcorr, rx_fdm, foff, &fdmdv->foff_rect, &fdmdv->foff_phase_rect, nin);
+ if (fdmdv->coarse_fine == COARSE)
+ fdmdv->foff = foff_coarse;
+ freq_shift(rx_fdm_fcorr, rx_fdm, fdmdv->foff, &fdmdv->foff_rect, &fdmdv->foff_phase_rect, nin);
/* baseband processing */
rx_timing = rx_est_timing(rx_symbols, rx_filt, rx_baseband, fdmdv->rx_filter_mem_timing, env, fdmdv->rx_baseband_mem_timing, nin);
foff_fine = qpsk_to_bits(rx_bits, &sync_bit, fdmdv->prev_rx_symbols, rx_symbols);
memcpy(fdmdv->prev_rx_symbols, rx_symbols, sizeof(COMP)*(NC+1));
- fdmdv->track = freq_state(sync_bit, &fdmdv->fest_state);
-
+ fdmdv->coarse_fine = freq_state(sync_bit, &fdmdv->fest_state);
+ fdmdv->foff -= 0.5*foff_fine;
+
/* --------------------------------------------------------*\
Log each vector
\*---------------------------------------------------------*/
memcpy(&S1_log[f*MPILOTFFT], fdmdv->S1, sizeof(COMP)*MPILOTFFT);
memcpy(&S2_log[f*MPILOTFFT], fdmdv->S2, sizeof(COMP)*MPILOTFFT);
foff_coarse_log[f] = foff_coarse;
- foff_log[f] = foff;
+ foff_log[f] = fdmdv->foff;
/* rx down conversion */
foff_fine_log[f] = foff_fine;
sync_bit_log[f] = sync_bit;
- track_log[f] = fdmdv->track;
+ coarse_fine_log[f] = fdmdv->coarse_fine;
}
octave_save_int(fout, "rx_bits_log_c", rx_bits_log, 1, FDMDV_BITS_PER_FRAME*FRAMES);
octave_save_float(fout, "foff_fine_log_c", foff_fine_log, 1, FRAMES);
octave_save_int(fout, "sync_bit_log_c", sync_bit_log, 1, FRAMES);
- octave_save_int(fout, "track_log_c", track_log, 1, FRAMES);
+ octave_save_int(fout, "coarse_fine_log_c", coarse_fine_log, 1, FRAMES);
fclose(fout);
codec2_destroy(fdmdv);