1 /*
2 * Copyright (c) 2007 - 2015 Joseph Gaeddert
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23 //
24 // modem_qpsk.c
25 //
26
27 #include <stdio.h>
28 #include <assert.h>
29 #include <math.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "liquid.internal.h"
34
35 // create a qpsk (quaternary phase-shift keying) modem object
MODEM(_create_qpsk)36 MODEM() MODEM(_create_qpsk)()
37 {
38 MODEM() q = (MODEM()) malloc( sizeof(struct MODEM(_s)) );
39 q->scheme = LIQUID_MODEM_QPSK;
40
41 MODEM(_init)(q, 2);
42
43 q->modulate_func = &MODEM(_modulate_qpsk);
44 q->demodulate_func = &MODEM(_demodulate_qpsk);
45
46 // reset and return
47 MODEM(_reset)(q);
48 return q;
49 }
50
51
52 // modulate QPSK
MODEM(_modulate_qpsk)53 void MODEM(_modulate_qpsk)(MODEM() _q,
54 unsigned int _sym_in,
55 TC * _y)
56 {
57 // compute output sample directly from input
58 *_y = (_sym_in & 0x01 ? -M_SQRT1_2 : M_SQRT1_2) +
59 (_sym_in & 0x02 ? -M_SQRT1_2 : M_SQRT1_2)*_Complex_I;
60 }
61
62 // demodulate QPSK
MODEM(_demodulate_qpsk)63 void MODEM(_demodulate_qpsk)(MODEM() _q,
64 TC _x,
65 unsigned int * _sym_out)
66 {
67 // slice directly to output symbol
68 *_sym_out = (crealf(_x) > 0 ? 0 : 1) +
69 (cimagf(_x) > 0 ? 0 : 2);
70
71 // re-modulate symbol and store state
72 MODEM(_modulate_qpsk)(_q, *_sym_out, &_q->x_hat);
73 _q->r = _x;
74 }
75
76 // demodulate QPSK (soft)
MODEM(_demodulate_soft_qpsk)77 void MODEM(_demodulate_soft_qpsk)(MODEM() _q,
78 TC _x,
79 unsigned int * _s,
80 unsigned char * _soft_bits)
81 {
82 // gamma = 1/(2*sigma^2), approximate for constellation size
83 T gamma = 5.8f;
84
85 // approximate log-likelihood ratios
86 T LLR;
87 int soft_bit;
88
89 // compute soft value for first bit
90 LLR = -2.0f * cimagf(_x) * gamma;
91 soft_bit = LLR*16 + 127;
92 if (soft_bit > 255) soft_bit = 255;
93 if (soft_bit < 0) soft_bit = 0;
94 _soft_bits[0] = (unsigned char) ( soft_bit );
95
96 // compute soft value for first bit
97 LLR = -2.0f * crealf(_x) * gamma;
98 soft_bit = LLR*16 + 127;
99 if (soft_bit > 255) soft_bit = 255;
100 if (soft_bit < 0) soft_bit = 0;
101 _soft_bits[1] = (unsigned char) ( soft_bit );
102
103 // re-modulate symbol and store state
104 *_s = (crealf(_x) > 0 ? 0 : 1) +
105 (cimagf(_x) > 0 ? 0 : 2);
106 MODEM(_modulate_qpsk)(_q, *_s, &_q->x_hat);
107 _q->r = _x;
108 }
109
110