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_psk.c
25 //
26 
27 // create a psk (phase-shift keying) modem object
MODEM(_create_psk)28 MODEM() MODEM(_create_psk)(unsigned int _bits_per_symbol)
29 {
30     MODEM() q = (MODEM()) malloc( sizeof(struct MODEM(_s)) );
31 
32     switch (_bits_per_symbol) {
33     case 1: q->scheme = LIQUID_MODEM_PSK2;   break;
34     case 2: q->scheme = LIQUID_MODEM_PSK4;   break;
35     case 3: q->scheme = LIQUID_MODEM_PSK8;   break;
36     case 4: q->scheme = LIQUID_MODEM_PSK16;  break;
37     case 5: q->scheme = LIQUID_MODEM_PSK32;  break;
38     case 6: q->scheme = LIQUID_MODEM_PSK64;  break;
39     case 7: q->scheme = LIQUID_MODEM_PSK128; break;
40     case 8: q->scheme = LIQUID_MODEM_PSK256; break;
41     default:
42         fprintf(stderr,"error: modem_create_psk(), cannot support PSK with m > 8\n");
43         exit(1);
44     }
45 
46     // initialize basic modem structure
47     MODEM(_init)(q, _bits_per_symbol);
48 
49     // compute alpha
50     q->data.psk.alpha = M_PI/(T)(q->M);
51 
52     // initialize demodulation array reference
53     unsigned int k;
54     for (k=0; k<(q->m); k++)
55         q->ref[k] = (1<<k) * q->data.psk.alpha;
56 
57     // compute phase offset (half of phase difference between symbols)
58     q->data.psk.d_phi = M_PI*(1.0f - 1.0f/(T)(q->M));
59 
60     // set modulation/demodulation functions
61     q->modulate_func = &MODEM(_modulate_psk);
62     q->demodulate_func = &MODEM(_demodulate_psk);
63 
64     // initialize symbol map
65     q->symbol_map = (TC*)malloc(q->M*sizeof(TC));
66     MODEM(_init_map)(q);
67     q->modulate_using_map = 1;
68 
69     // initialize soft-demodulation look-up table
70     if (q->m >= 3)
71         MODEM(_demodsoft_gentab)(q, 2);
72 
73     // reset and return
74     MODEM(_reset)(q);
75     return q;
76 }
77 
78 // modulate PSK
MODEM(_modulate_psk)79 void MODEM(_modulate_psk)(MODEM()      _q,
80                           unsigned int _sym_in,
81                           TC *         _y)
82 {
83     // 'encode' input symbol (actually gray decoding)
84     _sym_in = gray_decode(_sym_in);
85 
86     // compute output sample
87     *_y = liquid_cexpjf(_sym_in * 2 * _q->data.psk.alpha );
88 }
89 
90 // demodulate PSK
MODEM(_demodulate_psk)91 void MODEM(_demodulate_psk)(MODEM()        _q,
92                             TC             _x,
93                             unsigned int * _sym_out)
94 {
95     // compute angle and subtract phase offset, ensuring phase is in [-pi,pi)
96     T theta = cargf(_x);
97     theta -= _q->data.psk.d_phi;
98     if (theta < -M_PI)
99         theta += 2*M_PI;
100 
101     // demodulate on linearly-spaced array
102     unsigned int s;             // demodulated symbol
103     T demod_phase_error;        // demodulation phase error
104     MODEM(_demodulate_linear_array_ref)(theta, _q->m, _q->ref, &s, &demod_phase_error);
105 
106     // 'decode' output symbol (actually gray encoding)
107     *_sym_out = gray_encode(s);
108 
109     // re-modulate symbol and store state
110     MODEM(_modulate_psk)(_q, *_sym_out, &_q->x_hat);
111     _q->r = _x;
112 }
113 
114