This repository was archived by the owner on May 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathhilbert.cpp
More file actions
207 lines (180 loc) · 6.62 KB
/
hilbert.cpp
File metadata and controls
207 lines (180 loc) · 6.62 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//============================================================================
//
// This file is part of GPSTk, the GPS Toolkit.
//
// The GPSTk is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 3.0 of the License, or
// any later version.
//
// The GPSTk 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with GPSTk; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
//
// Copyright 2004, The University of Texas at Austin
//
//============================================================================
//============================================================================
//
//This software developed by Applied Research Laboratories at the University of
//Texas at Austin, under contract to an agency or agencies within the U.S.
//Department of Defense. The U.S. Government retains all rights to use,
//duplicate, distribute, disclose, or release this software.
//
//Pursuant to DoD Directive 523024
//
// DISTRIBUTION STATEMENT A: This software has been approved for public
// release, distribution is unlimited.
//
//=============================================================================
/*
Program to perform Hilbert transform on samples from output file generated by SiGe SE4110L. The SiGe chip outputs real 2-bit samples. The GPSCreations GPS1A board ships with a program "ogusb-lit60.exe" that outputs the samples in char format to a file. This program reads from such a file, performs the Hilbert transform, and outputs I and Q samples in an IQStream used by the GPStk.
The effective sampling rate is cut in half because N points of real data generate N/2 points of complex data. The input sampling rate from the GPS1A is 16.368 MHz, so the output from this program will be 8.184 MHz.
Requires the FFTW library.
GCC: (not added to Jamfile yet, since this links to FFTW)
g++ -c -o hilbert.o -O -I. -I/.../gpstk/dev/apps/swrx -I/.../gpstk/dev/src hilbert.cpp
g++ -o hilbert hilbert.o /.../gpstk/dev/apps/swrx/simlib.a /.../gpstk/dev/src/libgpstk.a -lm -lstdc++ -lfftw3 -lm
*/
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <math.h>
#include <complex>
#include <fftw3.h>
#include <vector>
#include "CommandOption.hpp"
#include "CommandOptionParser.hpp"
#include "BasicFramework.hpp"
#include "IQStream.hpp"
using namespace gpstk;
using namespace std;
using namespace gpstk::StringUtils;
int main(int argc, char *argv[])
{
CommandOptionWithAnyArg
inputOpt('i', "input", "Location of input data file.",true);
CommandOptionNoArg
helpOption('h', "help", "Print usage. Repeat for more info. ");
CommandOptionWithAnyArg
timeOpt('t', "number-samples", "Number of C/A periods to input.");
CommandOptionWithAnyArg
quantizationOpt('q', "quantization",
"What type of IQ stream; 1, 2 or f. The default is 2.");
string appDesc("Performs hilbert transform on GPS1A data.");
CommandOptionParser cop(appDesc);
cop.parseOptions(argc, argv);
if (helpOption.getCount() || cop.hasErrors())
{
if (cop.hasErrors() && helpOption.getCount()==0)
{
cop.dumpErrors(cout);
cout << "Use -h for help." << endl;
}
else
{
cop.displayUsage(cout);
}
exit(0);
}
string f;
f = inputOpt.getValue()[0].c_str();
std::ifstream in;
in.open(f.data(), std::ios::in | std::ios::binary);
if(!in)
{ cerr << "Could not open input file, exiting..." << endl;
return 0;}
IQStream *output;
char quantization='2';
if (quantizationOpt.getCount())
quantization = quantizationOpt.getValue()[0][0];
switch (quantization)
{
case '1': output = new IQ1Stream(); break;
case '2': output = new IQ2Stream(); break;
case 'f': output = new IQFloatStream(); break;
default: output = new IQ2Stream(); break;
}
int numPeriods;
if(timeOpt.getCount())
{numPeriods=asInt(timeOpt.getValue().front());}
// This is not currently implemented, right now we just read to
// the end of the input file.
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
int N = 64; // Default number of samples per transform.
int N2 = N/2;
char c[N];
int count = 0;
double *REAL;
fftw_complex *IQ;
fftw_complex *X;
fftw_complex *X1;
fftw_plan p,pb;
using std::basic_ios; // Set up IQStream
output->copyfmt(std::cout);
output->clear(std::cout.rdstate());
output->basic_ios<char>::rdbuf(std::cout.rdbuf());
output->filename = "<stdout>";
REAL = (double*) malloc(sizeof(double) * N + 1); // input
IQ = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N2); // final output
X = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N); // intermediate
X1 = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N2);
p = fftw_plan_dft_r2c_1d(N, REAL, X, FFTW_MEASURE); //FFT plan
pb = fftw_plan_dft_1d(N2, X1, IQ, FFTW_BACKWARD, FFTW_MEASURE);
/*
// Sloppy way to get samples from middle of file.
char throwAway;
for(int i = 0; i < 16368 * 2000; i++)
{
throwAway = fgetc(in);
}
*/
int size; // in bytes
if(in.is_open())
{
in.seekg(0,ios::end);
size = in.tellg();
in.seekg(0, ios::beg);
}
while(size > 0)
{
// Read data into input array.
in.read(c,N);
size -= N;
for(int i=0 ; i < N ; i++)
{
REAL[i] = c[i];
}
// Execute FFT.
fftw_execute(p);
// Create new Hilbert components.
for(int i=0; i < ( N/2 - 1 ) ; i++)
{
X1[i][0] = X[i][0];
X1[i][1] = X[i][1];
}
// Execute Backward FFT.
fftw_execute(pb);
for(int i=0 ; i < N2 ; i++)
{
IQ[i][0] = ( IQ[i][0] / N2 ) ;
IQ[i][1] = ( IQ[i][1] / N2 ) ;
}
// Output I and Q samples to IQStream.
complex<float> o[N2];
for(int i=0; i < (N2); i++)
{
o[i] = complex<float>(IQ[i][0],IQ[i][1]);
*output << o[i];
}
count+=N;
}//while
fftw_destroy_plan(p);fftw_destroy_plan(pb);
fftw_free(REAL); fftw_free(IQ); fftw_free(X); fftw_free(X1);
return 0;
}