Updated FSF address in all files. Fixes ticket:51
[debian/gnuradio] / ezdop / src / host / hunter / src / sample.cc
1 /*
2  Copyright 2006 Johnathan Corgan.
3  
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License version 2
6  as published by the Free Software Foundation.
7  
8  This software is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with GNU Radio; see the file COPYING.  If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street,
16  Boston, MA 02110-1301, USA.
17 */
18
19 // Application level includes
20 #include "sample.h"
21 #include "util.h"
22
23 // wxWidgets includes
24 #include <wx/log.h>
25
26 Sample::Sample()
27 {
28     m_time = 0;
29     m_valid = false;
30     m_location = Spherical(0.0, 0.0);
31     m_heading = 0.0;
32     m_speed = 0.0;
33     m_in_phase = 0.0;
34     m_quadrature = 0.0;
35     m_phase = 0.0;
36     m_strength = 0.0;
37     m_volume = 0.0;
38     m_rate = 0;
39     m_filtering = 1;
40     m_error = 0.0;
41     m_ierror = 0.0;
42     m_qerror = 0.0;
43 }
44
45 Sample::Sample(const Sample &sample)
46 {
47     m_time = sample.m_time;
48     m_valid = sample.m_valid;
49     m_location = sample.m_location;
50     m_heading = sample.m_heading;
51     m_speed = sample.m_speed;
52     m_in_phase = sample.m_in_phase;
53     m_quadrature = sample.m_quadrature;
54     m_phase = sample.m_phase;
55     m_strength = sample.m_strength;
56     m_volume = sample.m_volume;
57     m_rate = sample.m_rate;
58     m_filtering = sample.m_filtering;
59     m_error = sample.m_error;
60     m_ierror = sample.m_ierror;
61     m_qerror = sample.m_qerror;
62 }
63
64 Sample::Sample(wxString &line)
65 {
66     char valid;
67     double lat, lon;
68
69     sscanf((char *)line.c_str(), "%c %i %lf %lf %f %f %f %f %f %f %f %f %f %f %i %i",
70         &valid,
71         &m_time,
72         &lat,
73         &lon,
74         &m_heading,
75         &m_speed,
76         &m_volume,
77         &m_strength,
78         &m_in_phase,
79         &m_quadrature,
80         &m_phase,
81         &m_error,
82         &m_ierror,
83         &m_qerror,
84         &m_rate,
85         &m_filtering);    
86
87     if (valid == 'V') 
88         m_valid = true;
89     else
90         m_valid = false;
91
92     m_location = Spherical(lat, lon);
93 }
94
95 void Sample::Dump(char *str, int len)
96 {
97     //                 vld tim  lat     lon     hdg   speed vol   stren inphs quad  phase error ierr  qerr  rt flt
98     snprintf(str, len, "%s %10i %10.5lf %10.5lf %5.1f %6.2f %7.5f %7.5f %9.6f %9.6f %9.6f %7.2f %9.6f %9.6f %i %i",
99                     m_valid ? "V":"I",
100                     m_time,
101                     m_location.Latitude(),
102                     m_location.Longitude(),
103                     m_heading,
104                     m_speed,
105                     m_volume,
106                     m_strength,
107                     m_in_phase,
108                     m_quadrature,
109                     m_phase,
110                     m_error,
111                     m_ierror,
112                     m_qerror,
113                     m_rate,
114                     m_filtering);    
115 }
116
117 void Sample::CalcError(const Spherical &location, float &angle, float &ierror, float &qerror) const
118 {
119     float actual_bearing = bearing(m_location, location);
120     float sample_relative_bearing = degree_normalize(to_degrees(m_phase));
121     float sample_absolute_bearing = degree_normalize(sample_relative_bearing+m_heading);
122     angle = sample_absolute_bearing-actual_bearing;
123     
124     if (angle < -180.0)
125         angle += 360;
126     if (angle > 180.0)
127         angle -= 360;
128
129     ierror = 0.0;
130     qerror = 0.0;
131
132     // Rotate I, Q by actual bearing
133     float i_act = cos(to_radians(-actual_bearing));
134     float q_act = sin(to_radians(-actual_bearing));
135     ierror = m_in_phase*i_act - m_quadrature*q_act;
136     qerror = m_quadrature*i_act + m_in_phase*q_act;
137
138
139 }