kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Radio device interface with sample rate conversion |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 3 | * Written by Thomas Tsou <tom@tsou.cc> |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 4 | * |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 5 | * Copyright 2011, 2012, 2013 Free Software Foundation, Inc. |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 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 <radioInterface.h> |
| 23 | #include <Logger.h> |
| 24 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 25 | #include "Resampler.h" |
| 26 | |
| 27 | extern "C" { |
| 28 | #include "convert.h" |
| 29 | } |
| 30 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 31 | /* Resampling parameters for 100 MHz clocking */ |
| 32 | #define RESAMP_INRATE 52 |
| 33 | #define RESAMP_OUTRATE 75 |
Thomas Tsou | 0e44ab3 | 2013-09-17 20:12:26 -0400 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | * Resampling filter bandwidth scaling factor |
| 37 | * This narrows the filter cutoff relative to the output bandwidth |
| 38 | * of the polyphase resampler. At 4 samples-per-symbol using the |
| 39 | * 2 pulse Laurent GMSK approximation gives us below 0.5 degrees |
| 40 | * RMS phase error at the resampler output. |
| 41 | */ |
| 42 | #define RESAMP_TX4_FILTER 0.45 |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 43 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 44 | #define INCHUNK (RESAMP_INRATE * 4) |
| 45 | #define OUTCHUNK (RESAMP_OUTRATE * 4) |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 46 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 47 | static Resampler *upsampler = NULL; |
| 48 | static Resampler *dnsampler = NULL; |
| 49 | short *convertRecvBuffer = NULL; |
| 50 | short *convertSendBuffer = NULL; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 51 | |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 52 | RadioInterfaceResamp::RadioInterfaceResamp(RadioDevice *wRadio, |
| 53 | int wReceiveOffset, |
| 54 | int wSPS, |
| 55 | GSM::Time wStartTime) |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 56 | : RadioInterface(wRadio, wReceiveOffset, wSPS, wStartTime), |
| 57 | innerSendBuffer(NULL), outerSendBuffer(NULL), |
| 58 | innerRecvBuffer(NULL), outerRecvBuffer(NULL) |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 59 | { |
| 60 | } |
| 61 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 62 | RadioInterfaceResamp::~RadioInterfaceResamp() |
| 63 | { |
| 64 | close(); |
| 65 | } |
| 66 | |
| 67 | void RadioInterfaceResamp::close() |
| 68 | { |
| 69 | RadioInterface::close(); |
| 70 | |
| 71 | delete innerSendBuffer; |
| 72 | delete outerSendBuffer; |
| 73 | delete innerRecvBuffer; |
| 74 | delete outerRecvBuffer; |
| 75 | |
| 76 | delete upsampler; |
| 77 | delete dnsampler; |
| 78 | |
| 79 | innerSendBuffer = NULL; |
| 80 | outerSendBuffer = NULL; |
| 81 | innerRecvBuffer = NULL; |
| 82 | outerRecvBuffer = NULL; |
| 83 | |
| 84 | upsampler = NULL; |
| 85 | dnsampler = NULL; |
| 86 | } |
| 87 | |
| 88 | /* Initialize I/O specific objects */ |
| 89 | bool RadioInterfaceResamp::init() |
| 90 | { |
| 91 | float cutoff = 1.0f; |
| 92 | |
| 93 | close(); |
| 94 | |
Thomas Tsou | 0e44ab3 | 2013-09-17 20:12:26 -0400 | [diff] [blame] | 95 | if (mSPSTx == 4) |
| 96 | cutoff = RESAMP_TX4_FILTER; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 97 | |
| 98 | dnsampler = new Resampler(RESAMP_INRATE, RESAMP_OUTRATE); |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame^] | 99 | if (!dnsampler->init()) { |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 100 | LOG(ALERT) << "Rx resampler failed to initialize"; |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | upsampler = new Resampler(RESAMP_OUTRATE, RESAMP_INRATE); |
| 105 | if (!upsampler->init(cutoff)) { |
| 106 | LOG(ALERT) << "Tx resampler failed to initialize"; |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Allocate high and low rate buffers. The high rate receive |
| 112 | * buffer and low rate transmit vectors feed into the resampler |
| 113 | * and requires headroom equivalent to the filter length. Low |
| 114 | * rate buffers are allocated in the main radio interface code. |
| 115 | */ |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame^] | 116 | innerSendBuffer = new signalVector(INCHUNK * 20, upsampler->len()); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 117 | outerSendBuffer = new signalVector(OUTCHUNK * 20); |
| 118 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame^] | 119 | outerRecvBuffer = new signalVector(OUTCHUNK * 2, dnsampler->len()); |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 120 | innerRecvBuffer = new signalVector(INCHUNK * 20); |
| 121 | |
| 122 | convertSendBuffer = new short[OUTCHUNK * 2 * 20]; |
| 123 | convertRecvBuffer = new short[OUTCHUNK * 2 * 2]; |
| 124 | |
| 125 | sendBuffer = innerSendBuffer; |
| 126 | recvBuffer = innerRecvBuffer; |
| 127 | |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | /* Receive a timestamped chunk from the device */ |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 132 | void RadioInterfaceResamp::pullBuffer() |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 133 | { |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 134 | bool local_underrun; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 135 | int rc, num_recv; |
| 136 | int inner_len = INCHUNK; |
| 137 | int outer_len = OUTCHUNK; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 138 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 139 | /* Outer buffer access size is fixed */ |
| 140 | num_recv = mRadio->readSamples(convertRecvBuffer, |
| 141 | outer_len, |
| 142 | &overrun, |
| 143 | readTimestamp, |
| 144 | &local_underrun); |
| 145 | if (num_recv != outer_len) { |
| 146 | LOG(ALERT) << "Receive error " << num_recv; |
| 147 | return; |
| 148 | } |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 149 | |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 150 | convert_short_float((float *) outerRecvBuffer->begin(), |
| 151 | convertRecvBuffer, 2 * outer_len); |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 152 | |
| 153 | underrun |= local_underrun; |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 154 | readTimestamp += (TIMESTAMP) num_recv; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 155 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 156 | /* Write to the end of the inner receive buffer */ |
| 157 | rc = dnsampler->rotate((float *) outerRecvBuffer->begin(), outer_len, |
| 158 | (float *) (innerRecvBuffer->begin() + recvCursor), |
| 159 | inner_len); |
| 160 | if (rc < 0) { |
| 161 | LOG(ALERT) << "Sample rate upsampling error"; |
| 162 | } |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 163 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 164 | recvCursor += inner_len; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 167 | /* Send a timestamped chunk to the device */ |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 168 | void RadioInterfaceResamp::pushBuffer() |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 169 | { |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 170 | int rc, chunks, num_sent; |
| 171 | int inner_len, outer_len; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 172 | |
| 173 | if (sendCursor < INCHUNK) |
| 174 | return; |
| 175 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 176 | chunks = sendCursor / INCHUNK; |
| 177 | if (chunks > 8) |
| 178 | chunks = 8; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 179 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 180 | inner_len = chunks * INCHUNK; |
| 181 | outer_len = chunks * OUTCHUNK; |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 182 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 183 | /* Always send from the beginning of the buffer */ |
| 184 | rc = upsampler->rotate((float *) innerSendBuffer->begin(), inner_len, |
| 185 | (float *) outerSendBuffer->begin(), outer_len); |
| 186 | if (rc < 0) { |
| 187 | LOG(ALERT) << "Sample rate downsampling error"; |
| 188 | } |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 189 | |
Thomas Tsou | 9471d76 | 2013-08-20 21:24:24 -0400 | [diff] [blame] | 190 | convert_float_short(convertSendBuffer, |
| 191 | (float *) outerSendBuffer->begin(), |
| 192 | powerScaling, 2 * outer_len); |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 193 | |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 194 | num_sent = mRadio->writeSamples(convertSendBuffer, |
| 195 | outer_len, |
| 196 | &underrun, |
| 197 | writeTimestamp); |
| 198 | if (num_sent != outer_len) { |
| 199 | LOG(ALERT) << "Transmit error " << num_sent; |
| 200 | } |
| 201 | |
| 202 | /* Shift remaining samples to beginning of buffer */ |
| 203 | memmove(innerSendBuffer->begin(), |
| 204 | innerSendBuffer->begin() + inner_len, |
| 205 | (sendCursor - inner_len) * 2 * sizeof(float)); |
| 206 | |
| 207 | writeTimestamp += outer_len; |
| 208 | sendCursor -= inner_len; |
| 209 | assert(sendCursor >= 0); |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 210 | } |