tfdmdv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*---------------------------------------------------------------------------*\
  2. FILE........: tfdmdv.c
  3. AUTHOR......: David Rowe
  4. DATE CREATED: April 16 2012
  5. Tests for the C version of the FDMDV modem. This program outputs a
  6. file of Octave vectors that are loaded and automatically tested
  7. against the Octave version of the modem by the Octave script
  8. tfmddv.m
  9. \*---------------------------------------------------------------------------*/
  10. /*
  11. Copyright (C) 2012 David Rowe
  12. All rights reserved.
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU Lesser General Public License version 2, as
  15. published by the Free Software Foundation. This program is
  16. distributed in the hope that it will be useful, but WITHOUT ANY
  17. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  19. License for more details.
  20. You should have received a copy of the GNU Lesser General Public License
  21. along with this program; if not, see <http://www.gnu.org/licenses/>.
  22. */
  23. #include <assert.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <math.h>
  28. #include "fdmdv_internal.h"
  29. #include "fdmdv.h"
  30. #include "octave.h"
  31. #define FRAMES 25
  32. #define CHANNEL_BUF_SIZE (10*M)
  33. int main(int argc, char *argv[])
  34. {
  35. struct FDMDV *fdmdv;
  36. int tx_bits[FDMDV_BITS_PER_FRAME];
  37. COMP tx_symbols[NC+1];
  38. COMP tx_baseband[NC+1][M];
  39. COMP tx_fdm[M];
  40. float channel[CHANNEL_BUF_SIZE];
  41. int channel_count;
  42. COMP rx_fdm[M+M/P];
  43. float foff_coarse;
  44. int nin, next_nin;
  45. COMP rx_fdm_fcorr[M+M/P];
  46. COMP rx_baseband[NC+1][M+M/P];
  47. COMP rx_filt[NC+1][P+1];
  48. float rx_timing;
  49. float env[NT*P];
  50. COMP rx_symbols[NC+1];
  51. int rx_bits[FDMDV_BITS_PER_FRAME];
  52. float foff_fine;
  53. int sync_bit;
  54. int fest_state;
  55. int tx_bits_log[FDMDV_BITS_PER_FRAME*FRAMES];
  56. COMP tx_symbols_log[(NC+1)*FRAMES];
  57. COMP tx_baseband_log[(NC+1)][M*FRAMES];
  58. COMP tx_fdm_log[M*FRAMES];
  59. COMP pilot_baseband1_log[NPILOTBASEBAND*FRAMES];
  60. COMP pilot_baseband2_log[NPILOTBASEBAND*FRAMES];
  61. COMP pilot_lpf1_log[NPILOTLPF*FRAMES];
  62. COMP pilot_lpf2_log[NPILOTLPF*FRAMES];
  63. COMP S1_log[MPILOTFFT*FRAMES];
  64. COMP S2_log[MPILOTFFT*FRAMES];
  65. float foff_coarse_log[FRAMES];
  66. float foff_log[FRAMES];
  67. COMP rx_baseband_log[(NC+1)][(M+M/P)*FRAMES];
  68. int rx_baseband_log_col_index;
  69. COMP rx_filt_log[NC+1][(P+1)*FRAMES];
  70. int rx_filt_log_col_index;
  71. float env_log[NT*P*FRAMES];
  72. float rx_timing_log[FRAMES];
  73. COMP rx_symbols_log[NC+1][FRAMES];
  74. float sig_est_log[NC+1][FRAMES];
  75. float noise_est_log[NC+1][FRAMES];
  76. int rx_bits_log[FDMDV_BITS_PER_FRAME*FRAMES];
  77. float foff_fine_log[FRAMES];
  78. int sync_bit_log[FRAMES];
  79. int coarse_fine_log[FRAMES];
  80. int nin_log[FRAMES];
  81. FILE *fout;
  82. int f,c,i,j;
  83. fdmdv = fdmdv_create();
  84. next_nin = M;
  85. channel_count = 0;
  86. rx_baseband_log_col_index = 0;
  87. rx_filt_log_col_index = 0;
  88. printf("sizeof FDMDV states: %d bytes\n", sizeof(struct FDMDV));
  89. for(f=0; f<FRAMES; f++) {
  90. /* --------------------------------------------------------*\
  91. Modulator
  92. \*---------------------------------------------------------*/
  93. fdmdv_get_test_bits(fdmdv, tx_bits);
  94. bits_to_dqpsk_symbols(tx_symbols, fdmdv->prev_tx_symbols, tx_bits, &fdmdv->tx_pilot_bit);
  95. memcpy(fdmdv->prev_tx_symbols, tx_symbols, sizeof(COMP)*(NC+1));
  96. tx_filter(tx_baseband, tx_symbols, fdmdv->tx_filter_memory);
  97. fdm_upconvert(tx_fdm, tx_baseband, fdmdv->phase_tx, fdmdv->freq);
  98. /* --------------------------------------------------------*\
  99. Channel
  100. \*---------------------------------------------------------*/
  101. nin = next_nin;
  102. /*
  103. if (f == 2)
  104. nin = 120;
  105. if (f == 3)
  106. nin = 200;
  107. if ((f !=2) && (f != 3))
  108. nin = M;
  109. */
  110. /* add M tx samples to end of buffer */
  111. assert((channel_count + M) < CHANNEL_BUF_SIZE);
  112. for(i=0; i<M; i++)
  113. channel[channel_count+i] = tx_fdm[i].real;
  114. channel_count += M;
  115. /* take nin samples from start of buffer */
  116. for(i=0; i<nin; i++) {
  117. rx_fdm[i].real = channel[i];
  118. rx_fdm[i].imag = 0;
  119. }
  120. /* shift buffer back */
  121. for(i=0,j=nin; j<channel_count; i++,j++)
  122. channel[i] = channel[j];
  123. channel_count -= nin;
  124. /* --------------------------------------------------------*\
  125. Demodulator
  126. \*---------------------------------------------------------*/
  127. /* freq offset estimation and correction */
  128. foff_coarse = rx_est_freq_offset(fdmdv, rx_fdm, nin);
  129. if (fdmdv->coarse_fine == COARSE)
  130. fdmdv->foff = foff_coarse;
  131. fdmdv_freq_shift(rx_fdm_fcorr, rx_fdm, fdmdv->foff, &fdmdv->foff_rect, &fdmdv->foff_phase_rect, nin);
  132. /* baseband processing */
  133. fdm_downconvert(rx_baseband, rx_fdm_fcorr, fdmdv->phase_rx, fdmdv->freq, nin);
  134. rx_filter(rx_filt, rx_baseband, fdmdv->rx_filter_memory, nin);
  135. rx_timing = rx_est_timing(rx_symbols, rx_filt, rx_baseband, fdmdv->rx_filter_mem_timing, env, fdmdv->rx_baseband_mem_timing, nin);
  136. foff_fine = qpsk_to_bits(rx_bits, &sync_bit, fdmdv->phase_difference, fdmdv->prev_rx_symbols, rx_symbols);
  137. snr_update(fdmdv->sig_est, fdmdv->noise_est, fdmdv->phase_difference);
  138. memcpy(fdmdv->prev_rx_symbols, rx_symbols, sizeof(COMP)*(NC+1));
  139. next_nin = M;
  140. if (rx_timing > 2*M/P)
  141. next_nin += M/P;
  142. if (rx_timing < 0)
  143. next_nin -= M/P;
  144. fdmdv->coarse_fine = freq_state(sync_bit, &fdmdv->fest_state);
  145. fdmdv->foff -= TRACK_COEFF*foff_fine;
  146. /* --------------------------------------------------------*\
  147. Log each vector
  148. \*---------------------------------------------------------*/
  149. memcpy(&tx_bits_log[FDMDV_BITS_PER_FRAME*f], tx_bits, sizeof(int)*FDMDV_BITS_PER_FRAME);
  150. memcpy(&tx_symbols_log[(NC+1)*f], tx_symbols, sizeof(COMP)*(NC+1));
  151. for(c=0; c<NC+1; c++)
  152. for(i=0; i<M; i++)
  153. tx_baseband_log[c][f*M+i] = tx_baseband[c][i];
  154. memcpy(&tx_fdm_log[M*f], tx_fdm, sizeof(COMP)*M);
  155. /* freq offset estimation */
  156. memcpy(&pilot_baseband1_log[f*NPILOTBASEBAND], fdmdv->pilot_baseband1, sizeof(COMP)*NPILOTBASEBAND);
  157. memcpy(&pilot_baseband2_log[f*NPILOTBASEBAND], fdmdv->pilot_baseband2, sizeof(COMP)*NPILOTBASEBAND);
  158. memcpy(&pilot_lpf1_log[f*NPILOTLPF], fdmdv->pilot_lpf1, sizeof(COMP)*NPILOTLPF);
  159. memcpy(&pilot_lpf2_log[f*NPILOTLPF], fdmdv->pilot_lpf2, sizeof(COMP)*NPILOTLPF);
  160. memcpy(&S1_log[f*MPILOTFFT], fdmdv->S1, sizeof(COMP)*MPILOTFFT);
  161. memcpy(&S2_log[f*MPILOTFFT], fdmdv->S2, sizeof(COMP)*MPILOTFFT);
  162. foff_coarse_log[f] = foff_coarse;
  163. foff_log[f] = fdmdv->foff;
  164. /* rx down conversion */
  165. for(c=0; c<NC+1; c++) {
  166. for(i=0; i<nin; i++)
  167. rx_baseband_log[c][rx_baseband_log_col_index + i] = rx_baseband[c][i];
  168. }
  169. rx_baseband_log_col_index += nin;
  170. /* rx filtering */
  171. for(c=0; c<NC+1; c++) {
  172. for(i=0; i<(P*nin)/M; i++)
  173. rx_filt_log[c][rx_filt_log_col_index + i] = rx_filt[c][i];
  174. }
  175. rx_filt_log_col_index += (P*nin)/M;
  176. /* timing estimation */
  177. memcpy(&env_log[NT*P*f], env, sizeof(float)*NT*P);
  178. rx_timing_log[f] = rx_timing;
  179. nin_log[f] = nin;
  180. for(c=0; c<NC+1; c++)
  181. rx_symbols_log[c][f] = rx_symbols[c];
  182. /* qpsk_to_bits() */
  183. memcpy(&rx_bits_log[FDMDV_BITS_PER_FRAME*f], rx_bits, sizeof(int)*FDMDV_BITS_PER_FRAME);
  184. for(c=0; c<NC+1; c++) {
  185. sig_est_log[c][f] = fdmdv->sig_est[c];
  186. noise_est_log[c][f] = fdmdv->noise_est[c];
  187. }
  188. foff_fine_log[f] = foff_fine;
  189. sync_bit_log[f] = sync_bit;
  190. coarse_fine_log[f] = fdmdv->coarse_fine;
  191. }
  192. /*---------------------------------------------------------*\
  193. Dump logs to Octave file for evaluation
  194. by tfdmdv.m Octave script
  195. \*---------------------------------------------------------*/
  196. fout = fopen("tfdmdv_out.txt","wt");
  197. assert(fout != NULL);
  198. fprintf(fout, "# Created by tfdmdv.c\n");
  199. octave_save_int(fout, "tx_bits_log_c", tx_bits_log, 1, FDMDV_BITS_PER_FRAME*FRAMES);
  200. octave_save_complex(fout, "tx_symbols_log_c", tx_symbols_log, 1, (NC+1)*FRAMES, (NC+1)*FRAMES);
  201. octave_save_complex(fout, "tx_baseband_log_c", (COMP*)tx_baseband_log, (NC+1), M*FRAMES, M*FRAMES);
  202. octave_save_complex(fout, "tx_fdm_log_c", (COMP*)tx_fdm_log, 1, M*FRAMES, M*FRAMES);
  203. octave_save_complex(fout, "pilot_lut_c", (COMP*)fdmdv->pilot_lut, 1, NPILOT_LUT, NPILOT_LUT);
  204. octave_save_complex(fout, "pilot_baseband1_log_c", pilot_baseband1_log, 1, NPILOTBASEBAND*FRAMES, NPILOTBASEBAND*FRAMES);
  205. octave_save_complex(fout, "pilot_baseband2_log_c", pilot_baseband2_log, 1, NPILOTBASEBAND*FRAMES, NPILOTBASEBAND*FRAMES);
  206. octave_save_complex(fout, "pilot_lpf1_log_c", pilot_lpf1_log, 1, NPILOTLPF*FRAMES, NPILOTLPF*FRAMES);
  207. octave_save_complex(fout, "pilot_lpf2_log_c", pilot_lpf2_log, 1, NPILOTLPF*FRAMES, NPILOTLPF*FRAMES);
  208. octave_save_complex(fout, "S1_log_c", S1_log, 1, MPILOTFFT*FRAMES, MPILOTFFT*FRAMES);
  209. octave_save_complex(fout, "S2_log_c", S2_log, 1, MPILOTFFT*FRAMES, MPILOTFFT*FRAMES);
  210. octave_save_float(fout, "foff_log_c", foff_log, 1, FRAMES, FRAMES);
  211. octave_save_float(fout, "foff_coarse_log_c", foff_coarse_log, 1, FRAMES, FRAMES);
  212. octave_save_complex(fout, "rx_baseband_log_c", (COMP*)rx_baseband_log, (NC+1), rx_baseband_log_col_index, (M+M/P)*FRAMES);
  213. octave_save_complex(fout, "rx_filt_log_c", (COMP*)rx_filt_log, (NC+1), rx_filt_log_col_index, (P+1)*FRAMES);
  214. octave_save_float(fout, "env_log_c", env_log, 1, NT*P*FRAMES, NT*P*FRAMES);
  215. octave_save_float(fout, "rx_timing_log_c", rx_timing_log, 1, FRAMES, FRAMES);
  216. octave_save_complex(fout, "rx_symbols_log_c", (COMP*)rx_symbols_log, (NC+1), FRAMES, FRAMES);
  217. octave_save_float(fout, "sig_est_log_c", (float*)sig_est_log, (NC+1), FRAMES, FRAMES);
  218. octave_save_float(fout, "noise_est_log_c", (float*)noise_est_log, (NC+1), FRAMES, FRAMES);
  219. octave_save_int(fout, "rx_bits_log_c", rx_bits_log, 1, FDMDV_BITS_PER_FRAME*FRAMES);
  220. octave_save_float(fout, "foff_fine_log_c", foff_fine_log, 1, FRAMES, FRAMES);
  221. octave_save_int(fout, "sync_bit_log_c", sync_bit_log, 1, FRAMES);
  222. octave_save_int(fout, "coarse_fine_log_c", coarse_fine_log, 1, FRAMES);
  223. octave_save_int(fout, "nin_log_c", nin_log, 1, FRAMES);
  224. fclose(fout);
  225. fdmdv_destroy(fdmdv);
  226. return 0;
  227. }