kurtis.heimerl | 8aea56e | 2011-11-26 03:18:30 +0000 | [diff] [blame] | 1 | /* |
| 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 | * |
| 7 | * This program is free software: you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU Affero General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * See the COPYING file in the main directory for details. |
| 20 | */ |
| 21 | |
| 22 | #include "radioVector.h" |
| 23 | |
Thomas Tsou | a2fe91a | 2013-11-13 22:48:11 -0500 | [diff] [blame] | 24 | radioVector::radioVector(GSM::Time &time, size_t size, |
| 25 | size_t start, size_t chans) |
| 26 | : vectors(chans), mTime(time) |
kurtis.heimerl | 8aea56e | 2011-11-26 03:18:30 +0000 | [diff] [blame] | 27 | { |
Thomas Tsou | a2fe91a | 2013-11-13 22:48:11 -0500 | [diff] [blame] | 28 | for (size_t i = 0; i < vectors.size(); i++) |
| 29 | vectors[i] = new signalVector(size, start); |
kurtis.heimerl | 8aea56e | 2011-11-26 03:18:30 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Thomas Tsou | a2fe91a | 2013-11-13 22:48:11 -0500 | [diff] [blame] | 32 | radioVector::radioVector(GSM::Time& wTime, signalVector *vector) |
| 33 | : vectors(1), mTime(wTime) |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 34 | { |
Thomas Tsou | a2fe91a | 2013-11-13 22:48:11 -0500 | [diff] [blame] | 35 | vectors[0] = vector; |
Thomas Tsou | e0fa2bf | 2013-11-09 02:46:29 -0500 | [diff] [blame] | 36 | } |
| 37 | |
Thomas Tsou | a2fe91a | 2013-11-13 22:48:11 -0500 | [diff] [blame] | 38 | radioVector::~radioVector() |
Thomas Tsou | d0f3ca3 | 2013-11-09 22:05:23 -0500 | [diff] [blame] | 39 | { |
Thomas Tsou | a2fe91a | 2013-11-13 22:48:11 -0500 | [diff] [blame] | 40 | for (size_t i = 0; i < vectors.size(); i++) |
| 41 | delete vectors[i]; |
Thomas Tsou | d0f3ca3 | 2013-11-09 22:05:23 -0500 | [diff] [blame] | 42 | } |
| 43 | |
kurtis.heimerl | 0628613 | 2011-11-26 03:18:43 +0000 | [diff] [blame] | 44 | GSM::Time radioVector::getTime() const |
kurtis.heimerl | 8aea56e | 2011-11-26 03:18:30 +0000 | [diff] [blame] | 45 | { |
| 46 | return mTime; |
| 47 | } |
| 48 | |
| 49 | void radioVector::setTime(const GSM::Time& wTime) |
| 50 | { |
| 51 | mTime = wTime; |
| 52 | } |
| 53 | |
| 54 | bool radioVector::operator>(const radioVector& other) const |
| 55 | { |
| 56 | return mTime > other.mTime; |
| 57 | } |
| 58 | |
Thomas Tsou | 1882099 | 2013-11-14 18:04:10 -0500 | [diff] [blame] | 59 | signalVector *radioVector::getVector(size_t chan) const |
Thomas Tsou | a2fe91a | 2013-11-13 22:48:11 -0500 | [diff] [blame] | 60 | { |
| 61 | if (chan >= vectors.size()) |
| 62 | return NULL; |
| 63 | |
| 64 | return vectors[chan]; |
| 65 | } |
| 66 | |
| 67 | bool radioVector::setVector(signalVector *vector, size_t chan) |
| 68 | { |
| 69 | if (chan >= vectors.size()) |
| 70 | return false; |
| 71 | |
| 72 | vectors[chan] = vector; |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
Thomas Tsou | a0179e3 | 2013-11-14 15:52:04 -0500 | [diff] [blame] | 77 | noiseVector::noiseVector(size_t size) |
| 78 | : std::vector<float>(size), itr(0) |
Thomas Tsou | fa3a787 | 2013-10-17 21:23:34 -0400 | [diff] [blame] | 79 | { |
Thomas Tsou | fa3a787 | 2013-10-17 21:23:34 -0400 | [diff] [blame] | 80 | } |
| 81 | |
Thomas Tsou | 1882099 | 2013-11-14 18:04:10 -0500 | [diff] [blame] | 82 | float noiseVector::avg() const |
Thomas Tsou | fa3a787 | 2013-10-17 21:23:34 -0400 | [diff] [blame] | 83 | { |
| 84 | float val = 0.0; |
| 85 | |
Thomas Tsou | 3f32ab5 | 2013-11-15 16:32:54 -0500 | [diff] [blame] | 86 | for (size_t i = 0; i < size(); i++) |
Thomas Tsou | fa3a787 | 2013-10-17 21:23:34 -0400 | [diff] [blame] | 87 | val += (*this)[i]; |
| 88 | |
| 89 | return val / (float) size(); |
| 90 | } |
| 91 | |
| 92 | bool noiseVector::insert(float val) |
| 93 | { |
| 94 | if (!size()) |
| 95 | return false; |
| 96 | |
Thomas Tsou | a0179e3 | 2013-11-14 15:52:04 -0500 | [diff] [blame] | 97 | if (itr >= this->size()) |
| 98 | itr = 0; |
Thomas Tsou | fa3a787 | 2013-10-17 21:23:34 -0400 | [diff] [blame] | 99 | |
Thomas Tsou | a0179e3 | 2013-11-14 15:52:04 -0500 | [diff] [blame] | 100 | (*this)[itr++] = val; |
Thomas Tsou | fa3a787 | 2013-10-17 21:23:34 -0400 | [diff] [blame] | 101 | |
| 102 | return true; |
| 103 | } |
| 104 | |
kurtis.heimerl | 8aea56e | 2011-11-26 03:18:30 +0000 | [diff] [blame] | 105 | GSM::Time VectorQueue::nextTime() const |
| 106 | { |
| 107 | GSM::Time retVal; |
| 108 | mLock.lock(); |
| 109 | |
| 110 | while (mQ.size()==0) |
| 111 | mWriteSignal.wait(mLock); |
| 112 | |
kurtis.heimerl | 0628613 | 2011-11-26 03:18:43 +0000 | [diff] [blame] | 113 | retVal = mQ.top()->getTime(); |
kurtis.heimerl | 8aea56e | 2011-11-26 03:18:30 +0000 | [diff] [blame] | 114 | mLock.unlock(); |
| 115 | |
| 116 | return retVal; |
| 117 | } |
| 118 | |
| 119 | radioVector* VectorQueue::getStaleBurst(const GSM::Time& targTime) |
| 120 | { |
| 121 | mLock.lock(); |
| 122 | if ((mQ.size()==0)) { |
| 123 | mLock.unlock(); |
| 124 | return NULL; |
| 125 | } |
| 126 | |
kurtis.heimerl | 0628613 | 2011-11-26 03:18:43 +0000 | [diff] [blame] | 127 | if (mQ.top()->getTime() < targTime) { |
kurtis.heimerl | 8aea56e | 2011-11-26 03:18:30 +0000 | [diff] [blame] | 128 | radioVector* retVal = mQ.top(); |
| 129 | mQ.pop(); |
| 130 | mLock.unlock(); |
| 131 | return retVal; |
| 132 | } |
| 133 | mLock.unlock(); |
| 134 | |
| 135 | return NULL; |
| 136 | } |
| 137 | |
| 138 | radioVector* VectorQueue::getCurrentBurst(const GSM::Time& targTime) |
| 139 | { |
| 140 | mLock.lock(); |
| 141 | if ((mQ.size()==0)) { |
| 142 | mLock.unlock(); |
| 143 | return NULL; |
| 144 | } |
| 145 | |
kurtis.heimerl | 0628613 | 2011-11-26 03:18:43 +0000 | [diff] [blame] | 146 | if (mQ.top()->getTime() == targTime) { |
kurtis.heimerl | 8aea56e | 2011-11-26 03:18:30 +0000 | [diff] [blame] | 147 | radioVector* retVal = mQ.top(); |
| 148 | mQ.pop(); |
| 149 | mLock.unlock(); |
| 150 | return retVal; |
| 151 | } |
| 152 | mLock.unlock(); |
| 153 | |
| 154 | return NULL; |
| 155 | } |