~ubuntu-branches/ubuntu/quantal/linpsk/quantal

« back to all changes in this revision

Viewing changes to linpsk/qpskdemodulator.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bruce Walker
  • Date: 2002-02-06 11:43:38 UTC
  • Revision ID: james.westby@ubuntu.com-20020206114338-xqmjmhh01lpjm0g4
Tags: upstream-0.6.2
ImportĀ upstreamĀ versionĀ 0.6.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          qpskdemodulator.cpp  -  description
 
3
                             -------------------
 
4
    begin                : Sat Jun 2 2001
 
5
    copyright            : (C) 2001 by Volker Schroer
 
6
    email                : dl1ksv@gmx.de
 
7
 ***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *                                                                         *
 
11
 *   This program is free software; you can redistribute it and/or modify  *
 
12
 *   it under the terms of the GNU General Public License as published by  *
 
13
 *   the Free Software Foundation; either version 2 of the License, or     *
 
14
 *   (at your option) any later version.                                   *
 
15
 *    based on the work of  Moe Wheatly, AE4JY                             *  
 
16
 ***************************************************************************/
 
17
 
 
18
#include "qpskdemodulator.h"
 
19
 
 
20
// phase wraparound correction table for viterbi decoder
 
21
const double AngleTbl1[4] = { M_PI_3_2, 0.0, M_PI/2.0, M_PI};
 
22
const double AngleTbl2[4] = { M_PI_3_2, PI2, M_PI/2.0, M_PI};
 
23
 
 
24
 
 
25
 
 
26
QPskDemodulator::QPskDemodulator():CPskDemodulator()
 
27
{
 
28
int i;
 
29
        for( i=0; i<21; i++)
 
30
                ViterbiDecode( M_PI_3_2);       // init the Viterbi decoder
 
31
ave1=0.0;
 
32
ave2=0.0;
 
33
 
 
34
}
 
35
QPskDemodulator::~QPskDemodulator()
 
36
{
 
37
}
 
38
void QPskDemodulator::DecodeSymbol(double_complex newsamp)
 
39
 
 
40
{
 
41
 
 
42
bool bit;
 
43
//unsigned char ch = 0;
 
44
 
 
45
char ch =0;
 
46
bool GotChar = false;
 
47
double angle;
 
48
 
 
49
 
 
50
        m_I1 = m_I0;    //form the multi delayed symbol samples
 
51
        m_Q1 = m_Q0;
 
52
        m_I0 = newsamp.real();
 
53
        m_Q0 = newsamp.imag();
 
54
// calculates difference angle for QPSK, BPSK, and IQPSK decoding
 
55
//create vector whose angle is the difference angle by multiplying the
 
56
// current smaple by the complex conjugate of the previous sample.
 
57
//swap I and Q axis to keep away from  the +/-Pi discontinuity and
 
58
//  add Pi to make make range from 0 to 2Pi.
 
59
// 180 deg phase changes center at Pi/4
 
60
// 0 deg phase changes center at 3Pi/2
 
61
// +90 deg phase changes center at 2Pi or 0
 
62
// -90 deg phase changes center at Pi
 
63
 
 
64
                angle = M_PI + atan2( (m_I1*m_I0 + m_Q1*m_Q0),
 
65
                                                                (m_I1*m_Q0 - m_I0*m_Q1));        //QPSK or BPSK
 
66
 
 
67
        CalcQuality(angle);
 
68
        
 
69
        bit = ViterbiDecode( angle);
 
70
                        
 
71
        if( (bit==0) && m_LastBitZero ) //if character delimiter
 
72
        {
 
73
                if(m_BitAcc != 0 )
 
74
                {
 
75
                        m_BitAcc >>= 2;                         //get rid of last zero and one
 
76
                        m_BitAcc &= 0x07FF;
 
77
                        ch = m_VaricodeDecTbl[m_BitAcc];
 
78
                        m_BitAcc = 0;
 
79
                        GotChar = true;
 
80
                }
 
81
        }
 
82
        else
 
83
        {
 
84
                m_BitAcc <<= 1;
 
85
                m_BitAcc |= bit;
 
86
                if(bit==0)
 
87
                        m_LastBitZero = true;
 
88
                else
 
89
                        m_LastBitZero = false;
 
90
        }
 
91
        if(GotChar && (ch!=0) )
 
92
                emit newSymbol(ch);
 
93
        if (bit)
 
94
                {
 
95
                m_OffCount=0;
 
96
                if (m_OnCount++ >20)
 
97
                        emit setFastSquelch(true);
 
98
 
 
99
                }
 
100
        else
 
101
                {
 
102
                m_OnCount=0;
 
103
                if (m_OffCount++ > 20)
 
104
                        emit setFastSquelch(false);                     
 
105
                }       
 
106
GotChar = false;
 
107
 
 
108
}
 
109
 
 
110
///////////////////////////////////////////////////////////////////////
 
111
// Soft-decision Viterbi decoder function.
 
112
///////////////////////////////////////////////////////////////////////
 
113
bool QPskDemodulator::ViterbiDecode( double newangle)
 
114
{
 
115
double pathdist[32];
 
116
double min;
 
117
int bitestimates[32];
 
118
int ones;
 
119
int i;
 
120
const double* pAngleTbl;
 
121
        min = 1.0e100;          // make sure can find a minimum value
 
122
        //alternative soft decision method using angle differences
 
123
        {
 
124
                if( newangle >= PI2/2 )                 //deal with ambiguity at +/- 2PI
 
125
                        pAngleTbl = AngleTbl2;          // by using two different tables
 
126
                else
 
127
                        pAngleTbl = AngleTbl1;
 
128
                for(i = 0; i < 32; i++)         // calculate all possible distances
 
129
                {                                                       //lsb of 'i' is newest bit estimate
 
130
                        pathdist[i] = m_SurvivorStates[i / 2].Pathdistance +
 
131
                                        fabs(newangle - pAngleTbl[ ConvolutionCodeTable[i] ]);
 
132
                        if(pathdist[i] < min)   // keep track of minimum distance
 
133
                                min = pathdist[i];
 
134
                        // shift in newest bit estimates
 
135
                        bitestimates[i] = ((m_SurvivorStates[i / 2].BitEstimates) << 1) + (i & 1);
 
136
                }
 
137
        }
 
138
        for(i = 0; i < 16; i++) //compare path lengths with the same end state
 
139
                                                        // and keep only the smallest path in m_SurvivorStates[].
 
140
        {
 
141
                if(pathdist[i] < pathdist[16 + i])
 
142
                {
 
143
                        m_SurvivorStates[i].Pathdistance = pathdist[i] - min;
 
144
                        m_SurvivorStates[i].BitEstimates = bitestimates[i];
 
145
                }
 
146
                else
 
147
                {
 
148
                        m_SurvivorStates[i].Pathdistance = pathdist[16 + i] - min;
 
149
                        m_SurvivorStates[i].BitEstimates = bitestimates[16 + i];
 
150
                }
 
151
        }
 
152
        ones = 0;
 
153
        for(i = 0; i < 16; i++)         // find if more ones than zeros at bit 20 position
 
154
                ones += (m_SurvivorStates[i].BitEstimates&(1L << 20));
 
155
        if( ones == (8L << 20 ) )
 
156
                return ( rand() & 0x1000 );     //if a tie then guess
 
157
        else
 
158
                return(ones > (8L << 20) );     //else return most likely bit value
 
159
}
 
160
 
 
161
//////////////////////////////////////////////////////////////////////
 
162
// Calculate signal quality based on the statistics of the phase
 
163
//      difference angle.  The more dispersion of the "0" and "180" degree
 
164
//  phase shifts, the worse the signal quality.  This information is used
 
165
//  to activate the squelch control.  If 20 consecutive "180" degree shifts
 
166
//  occur, the squelch is forced on, and if 20 consecutive "0" degree
 
167
//  shifts occur, the squelch is forced off quickly.
 
168
//////////////////////////////////////////////////////////////////////
 
169
 
 
170
void QPskDemodulator::CalcQuality(  double angle )
 
171
{
 
172
 
 
173
double temp;
 
174
 
 
175
ave2=ave1;
 
176
ave1=m_DevAve;
 
177
angle = angle - M_PI_2;
 
178
if (angle < 0.0)
 
179
        angle = angle + PI2;
 
180
 
 
181
temp = fmod(angle,M_PI_2);
 
182
 
 
183
if (temp > M_PI)
 
184
        temp=temp - M_PI;
 
185
 
 
186
                        m_QFreqError = temp;
 
187
                        temp = fabs(temp);
 
188
                        if (temp > M_PI_4)
 
189
                                        temp= M_PI_4;
 
190
 
 
191
                m_DevAve =0.47 * ave1 + 0.46 * ave2 + 0.03 *temp;
 
192
 
 
193
 
 
194
 
 
195
                temp=100.-250.0*m_DevAve;
 
196
                emit setSquelchValue((int)temp);
 
197
}