00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <stdio.h>
00025 #include <it/types.h>
00026 #include <it/io.h>
00027 #include <it/vec.h>
00028 #include <it/distance.h>
00029 #include <it/convcode.h>
00030 #include <it/random.h>
00031 #include <it/channel.h>
00032 #include <it/source.h>
00033 #include <it/source_func.h>
00034 #include <it/mat.h>
00035 #include <it/math.h>
00036
00037
00038 #undef NONRECURSIVE_1_3_CODE
00039 #undef NONRECURSIVE_2_3_CODE
00040 #define RECURSIVESYS_1_2_CODE
00041
00042 mat bspk_metrics (it_convolutional_code_t * cc, vec r)
00043 {
00044 int i, j, m;
00045 int N = cc->n;
00046 int L = vec_length (r) / N;
00047 mat metrics;
00048 int n_labels = cc->n_labels;
00049 double d;
00050
00051
00052 #define bit(x, n) (((x) >> (n)) & 1)
00053
00054 metrics = mat_new (n_labels, L);
00055
00056 for (i = 0; i < L; i++)
00057 for (j = 0; j < n_labels; j++) {
00058 metrics[j][i] = 0;
00059 for (m = 0; m < N; m++) {
00060 d = (r[i * N + m] - (bit (j, N - 1 - m) ? 1 : -1));
00061 metrics[j][i] -= d * d;
00062 }
00063 }
00064
00065 return (metrics);
00066 }
00067
00068 int main (void)
00069 {
00070 bvec input;
00071 bvec output;
00072 vec modulated;
00073 vec received;
00074 it_convolutional_code_t *cc;
00075 imat generator;
00076 int feedback;
00077 mat metrics;
00078
00079
00080
00081
00082
00083 input = source_binary (10, 0.5);
00084 it_printf ("input = $b\n", input);
00085
00086
00087 #ifdef NONRECURSIVE_1_3_CODE
00088 generator = imat_new (1, 3);
00089 generator[0][0] = 0133;
00090 generator[0][1] = 0165;
00091 generator[0][2] = 0171;
00092 feedback = 0;
00093 #endif
00094
00095 #ifdef NONRECURSIVE_2_3_CODE
00096 generator = imat_new (2, 3);
00097 generator[0][0] = 023;
00098 generator[0][1] = 035;
00099 generator[0][2] = 0;
00100
00101 generator[1][0] = 0;
00102 generator[1][1] = 05;
00103 generator[1][2] = 013;
00104 feedback = 0;
00105 #endif
00106
00107 #ifdef RECURSIVESYS_1_2_CODE
00108 generator = imat_new (1, 2);
00109 generator[0][0] = 033;
00110 generator[0][1] = 037;
00111 feedback = 037;
00112 #endif
00113
00114 cc = it_convolutional_code_new (generator, feedback);
00115
00116
00117 output = it_cc_encode (cc, input);
00118 it_printf ("output = $b\n", output);
00119
00120
00121 modulated = modulate_bpsk (output);
00122 it_printf ("modulated = $v\n", modulated);
00123
00124
00125 received = channel_awgn (modulated, 1.5);
00126 it_printf ("received = $v\n", received);
00127
00128
00129 metrics = bspk_metrics (cc, received);
00130
00131
00132 it_printf ("decoding\n");
00133 bvec_delete (output);
00134 output = it_viterbi_decode (cc, metrics);
00135 it_printf ("decoded = $b\n", output);
00136 it_printf ("input = $b\n", input);
00137
00138 mat_delete (metrics);
00139 vec_delete (received);
00140 vec_delete (modulated);
00141 bvec_delete (output);
00142 it_delete (cc);
00143 bvec_delete (input);
00144 imat_delete (generator);
00145
00146 return (0);
00147 }