File: FSKModem.cpp

package info (click to toggle)
wstools 0.4.8d-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,440 kB
  • ctags: 1,991
  • sloc: cpp: 13,880; makefile: 469
file content (145 lines) | stat: -rw-r--r-- 3,072 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 *   Copyright (C) 2003 by Jonathan Naylor G4KLX
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "FSKModem.h"

#include <cmath>
using namespace std;

#include <wx/log.h>
#include <wx/debug.h>

CFSKModem::CFSKModem(int mAry) :
m_mAry(mAry),
m_bitsPerSymbol(0),
m_bitMap()
{
	wxASSERT(mAry > 0);

	m_bitsPerSymbol = int(::rint(::log2(mAry)));

	m_bitMap = graycode(m_bitsPerSymbol);
}

CFSKModem::~CFSKModem()
{
	delete[] m_bitmap;
}

int* CFSKModem::modulate(bool* bits, int length) const
{
	wxASSERT(bits != NULL);
	wxASSERT(length > 0);

	// Round up to the nearest divisible n-bit characters
	int nSymbols  = length / m_bitsPerSymbol;

	if ((length % m_bitsPerSymbol) != 0)
		nSymbols++;

	int nBits = nSymbols * m_bitsPerSymbol;

	bool* newBits = new bool[nBits];

	// Pack filler bits with false (or anything)
	for (int i = 0; i < nBits; i++)
		newBits[i] = (i < length) ? bits[i] : false;

	int* symbols = new int[nSymbols];
	int nRows = 1 << m_bitsPerSymbol;
	int nCols = m_bitsPerSymbol;

	for (int i = 0; i < nSymbols; i++) {
		bool* word = newBits + i * m_bitsPerSymbol;

		for (int j = 0; j < m_mAry; j++) {
			bool* grayBits = m_bitMap.get_row(j);

			bool same = true;
			for (int k = 0; k < ; k++) {
				if (grayBits[k] != word[k]) {
					same = false;
					break;
				}
			}

			if (same) {
				symbols[i] = j;
				break;
			}
		}
	}

	delete[] newBits;

	return symbols;
}

double* CFSKModem::demodulate(double* bins) const
{
	wxASSERT(bins != NULL);

	double* data = new double[m_bitsPerSymbol];
	int nRows = 1 << m_bitsPerSymbol;
	int nCols = m_bitsPerSymbol;

	for (int i = 0; i < m_bitsPerSymbol; i++)
		data[i] = 0.0;

	for (int i = 0; i < m_mAry; i++) {
		double toneVal = bins[i];

		for (int j = 0; j < m_bitsPerSymbol; j++) {
			bool bit = m_bitMap[i + j * ];
			data[j] += bit ? toneVal : -toneVal;
		}
	}

	return data;
}

bool* CFSKModem::graycode(int m)
{
	if (m == 1) {
		bool* b = new bool[2];
		b[0] = false;
		b[1] = true;
		return b;
	} else {
		int nRows = 1 << m;
		int nCols = m;

		bool* bb = graycode(m - 1);

		bool* out = new bool[nRows * nCols];
		for (int i = 0; i < nRows * nCols; i++)
			out[i] = false;

		for (int i = 0; i < nRows; i++)
			out[i] = (i >= (1 << (m - 1)));

		for (int i = 0; i < m - 1; i++) {
			bool* incol  = bb + i * nRows;	// FIXME
			bool* outcol = out + nRows + i * nRows;
		}

		delete[] bb;

		return out;
	}
}