blob: ad40a1176fddab105da5d8c16d782973b064defd [file] [log] [blame]
kurtis.heimerl8aea56e2011-11-26 03:18:30 +00001/*
2 * Written by Thomas Tsou <ttsou@vt.edu>
3 * Based on code by Harvind S Samra <hssamra@kestrelsp.com>
4 *
5 * Copyright 2011 Free Software Foundation, Inc.
6 *
Pau Espin Pedrol21d03d32019-07-22 12:05:52 +02007 * SPDX-License-Identifier: AGPL-3.0+
8 *
kurtis.heimerl8aea56e2011-11-26 03:18:30 +00009 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * See the COPYING file in the main directory for details.
22 */
23
24#include "radioVector.h"
25
Thomas Tsoua2fe91a2013-11-13 22:48:11 -050026radioVector::radioVector(GSM::Time &time, size_t size,
27 size_t start, size_t chans)
28 : vectors(chans), mTime(time)
kurtis.heimerl8aea56e2011-11-26 03:18:30 +000029{
Thomas Tsoua2fe91a2013-11-13 22:48:11 -050030 for (size_t i = 0; i < vectors.size(); i++)
31 vectors[i] = new signalVector(size, start);
kurtis.heimerl8aea56e2011-11-26 03:18:30 +000032}
33
Thomas Tsoua2fe91a2013-11-13 22:48:11 -050034radioVector::radioVector(GSM::Time& wTime, signalVector *vector)
35 : vectors(1), mTime(wTime)
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -050036{
Thomas Tsoua2fe91a2013-11-13 22:48:11 -050037 vectors[0] = vector;
Thomas Tsoue0fa2bf2013-11-09 02:46:29 -050038}
39
Thomas Tsoua2fe91a2013-11-13 22:48:11 -050040radioVector::~radioVector()
Thomas Tsoud0f3ca32013-11-09 22:05:23 -050041{
Thomas Tsoua2fe91a2013-11-13 22:48:11 -050042 for (size_t i = 0; i < vectors.size(); i++)
43 delete vectors[i];
Thomas Tsoud0f3ca32013-11-09 22:05:23 -050044}
45
kurtis.heimerl06286132011-11-26 03:18:43 +000046GSM::Time radioVector::getTime() const
kurtis.heimerl8aea56e2011-11-26 03:18:30 +000047{
48 return mTime;
49}
50
51void radioVector::setTime(const GSM::Time& wTime)
52{
53 mTime = wTime;
54}
55
56bool radioVector::operator>(const radioVector& other) const
57{
58 return mTime > other.mTime;
59}
60
Thomas Tsou18820992013-11-14 18:04:10 -050061signalVector *radioVector::getVector(size_t chan) const
Thomas Tsoua2fe91a2013-11-13 22:48:11 -050062{
63 if (chan >= vectors.size())
64 return NULL;
65
66 return vectors[chan];
67}
68
69bool radioVector::setVector(signalVector *vector, size_t chan)
70{
71 if (chan >= vectors.size())
72 return false;
73
74 vectors[chan] = vector;
75
76 return true;
77}
78
Thomas Tsoua0179e32013-11-14 15:52:04 -050079noiseVector::noiseVector(size_t size)
80 : std::vector<float>(size), itr(0)
Thomas Tsoufa3a7872013-10-17 21:23:34 -040081{
Thomas Tsoufa3a7872013-10-17 21:23:34 -040082}
83
Thomas Tsou18820992013-11-14 18:04:10 -050084float noiseVector::avg() const
Thomas Tsoufa3a7872013-10-17 21:23:34 -040085{
86 float val = 0.0;
87
Thomas Tsou3f32ab52013-11-15 16:32:54 -050088 for (size_t i = 0; i < size(); i++)
Thomas Tsoufa3a7872013-10-17 21:23:34 -040089 val += (*this)[i];
90
91 return val / (float) size();
92}
93
94bool noiseVector::insert(float val)
95{
96 if (!size())
97 return false;
98
Thomas Tsoua0179e32013-11-14 15:52:04 -050099 if (itr >= this->size())
100 itr = 0;
Thomas Tsoufa3a7872013-10-17 21:23:34 -0400101
Thomas Tsoua0179e32013-11-14 15:52:04 -0500102 (*this)[itr++] = val;
Thomas Tsoufa3a7872013-10-17 21:23:34 -0400103
104 return true;
105}
106
kurtis.heimerl8aea56e2011-11-26 03:18:30 +0000107GSM::Time VectorQueue::nextTime() const
108{
109 GSM::Time retVal;
110 mLock.lock();
111
112 while (mQ.size()==0)
113 mWriteSignal.wait(mLock);
114
kurtis.heimerl06286132011-11-26 03:18:43 +0000115 retVal = mQ.top()->getTime();
kurtis.heimerl8aea56e2011-11-26 03:18:30 +0000116 mLock.unlock();
117
118 return retVal;
119}
120
121radioVector* VectorQueue::getStaleBurst(const GSM::Time& targTime)
122{
123 mLock.lock();
124 if ((mQ.size()==0)) {
125 mLock.unlock();
126 return NULL;
127 }
128
kurtis.heimerl06286132011-11-26 03:18:43 +0000129 if (mQ.top()->getTime() < targTime) {
kurtis.heimerl8aea56e2011-11-26 03:18:30 +0000130 radioVector* retVal = mQ.top();
131 mQ.pop();
132 mLock.unlock();
133 return retVal;
134 }
135 mLock.unlock();
136
137 return NULL;
138}
139
140radioVector* VectorQueue::getCurrentBurst(const GSM::Time& targTime)
141{
142 mLock.lock();
143 if ((mQ.size()==0)) {
144 mLock.unlock();
145 return NULL;
146 }
147
kurtis.heimerl06286132011-11-26 03:18:43 +0000148 if (mQ.top()->getTime() == targTime) {
kurtis.heimerl8aea56e2011-11-26 03:18:30 +0000149 radioVector* retVal = mQ.top();
150 mQ.pop();
151 mLock.unlock();
152 return retVal;
153 }
154 mLock.unlock();
155
156 return NULL;
157}