Agora  1.2.0
Agora project
radio_lib.h
Go to the documentation of this file.
1 
5 #ifndef RADIO_LIB_H_
6 #define RADIO_LIB_H_
7 
8 #include <atomic>
9 #include <complex>
10 #include <cstdint>
11 #include <cstdlib>
12 #include <memory>
13 #include <vector>
14 
15 #include "SoapySDR/Device.hpp"
16 #include "armadillo"
17 #include "config.h"
18 #include "memory_manage.h"
19 #include "radio.h"
20 
21 class RadioConfig {
22  public:
23  RadioConfig(Config* cfg, Radio::RadioType radio_type);
24  ~RadioConfig();
25 
26  bool RadioStart();
27  void RadioStop();
28  void ReadSensors();
29  int RadioTx(size_t radio_id, const void* const* buffs, Radio::TxFlags flags,
30  long long& tx_time);
31  int RadioTx(size_t radio_id,
32  const std::vector<std::vector<std::complex<int16_t>>>& tx_data,
33  Radio::TxFlags flags, long long& tx_time_ns);
34 
35  int RadioRx(size_t radio_id,
36  std::vector<std::vector<std::complex<int16_t>>>& rx_data,
37  size_t rx_size, Radio::RxFlags& out_flags, long long& rx_time_ns);
38 
39  int RadioRx(size_t radio_id,
40  std::vector<std::vector<std::complex<int16_t>>*>& rx_buffs,
41  size_t rx_size, Radio::RxFlags& out_flags, long long& rx_time_ns);
42 
43  int RadioRx(size_t radio_id, std::vector<void*>& rx_locs, size_t rx_size,
44  Radio::RxFlags& out_flags, long long& rx_time_ns);
45 
46  bool DoCalib() const { return calib_; }
47  void Go();
48  arma::cx_float* GetCalibUl() { return init_calib_ul_processed_; }
49  arma::cx_float* GetCalibDl() { return init_calib_dl_processed_; }
50 
51  // Thread functions
52  void InitBsRadio(size_t radio_id);
53  void ConfigureBsRadio(size_t radio_id);
54 
55  private:
56  long long SyncArrayTime();
57 
58  void CalibrateSampleOffset();
59  bool CalibrateSampleOffsetUplink(size_t max_attempts);
60  bool CalibrateSampleOffsetDownlink(size_t max_attempts);
61 
62  /* Transmit from each array antenna to ref antenna,
63  * return the received signal vector at the ref antenna
64  */
65  auto TxArrayToRef(const std::vector<std::complex<int16_t>>& tx_vec);
66  /* Transmit from ref antenna to the rest of the array
67  * return the received signal vector at the array
68  */
69  auto TxRefToArray(const std::vector<std::complex<int16_t>>& tx_vec);
70  bool FindTimeOffset(
71  const std::vector<std::vector<std::complex<int16_t>>>& rx_mat,
72  std::vector<int>& offset);
73  bool InitialCalib();
74  void AdjustDelays(const std::vector<int>& ch0_offsets);
75  static void DciqMinimize(Radio* target_dev, Radio* ref_dev, int direction,
76  size_t channel, double rx_center_tone,
77  double tx_center_tone);
78  static void SetIqBalance(Radio* dev, int direction, size_t channel, int gcorr,
79  int iqcorr);
80  static void AdjustCalibrationGains(std::vector<Radio*>& rx_devs,
81  Radio* tx_dev, size_t channel,
82  double fft_bin, bool plot = false);
83  void DciqCalibrationProc(size_t channel);
85  std::vector<SoapySDR::Device*> hubs_;
86  std::vector<std::unique_ptr<Radio>> radios_;
87  arma::cx_float* init_calib_ul_processed_;
88  arma::cx_float* init_calib_dl_processed_;
91  size_t radio_num_;
92  size_t antenna_num_;
93  bool calib_;
95 
96  std::atomic<size_t> num_radios_initialized_;
97  std::atomic<size_t> num_radios_configured_;
98 };
99 #endif // RADIO_LIB_H_
RadioConfig::cfg_
Config * cfg_
Definition: radio_lib.h:84
RadioConfig::AdjustCalibrationGains
static void AdjustCalibrationGains(std::vector< Radio * > &rx_devs, Radio *tx_dev, size_t channel, double fft_bin, bool plot=false)
Definition: radio_calibrate_analog.cc:123
radio.h
Defination file for the Radio class.
RadioConfig::RadioRx
int RadioRx(size_t radio_id, std::vector< std::vector< std::complex< int16_t >>> &rx_data, size_t rx_size, Radio::RxFlags &out_flags, long long &rx_time_ns)
Definition: radio_lib.cc:326
RadioConfig::CalibrateSampleOffsetDownlink
bool CalibrateSampleOffsetDownlink(size_t max_attempts)
Definition: radio_calibrate_digital.cc:395
Table::Calloc
void Calloc(size_t dim1, size_t dim2, Agora_memory::Alignment_t alignment)
Definition: memory_manage.h:45
RadioConfig::init_calib_dl_processed_
arma::cx_float * init_calib_dl_processed_
Definition: radio_lib.h:88
RadioConfig::num_radios_configured_
std::atomic< size_t > num_radios_configured_
Definition: radio_lib.h:97
Config::SampsPerSymbol
size_t SampsPerSymbol() const
Definition: config.h:234
fmt::v8::printf
auto printf(const S &fmt, const T &... args) -> int
Definition: printf.h:631
matplotlibcpp::plot
bool plot(const std::vector< Numeric > &x, const std::vector< Numeric > &y, const std::map< std::string, std::string > &keywords)
Definition: matplotlibcpp.h:442
RadioConfig::ReadSensors
void ReadSensors()
Definition: radio_lib.cc:344
FreeBuffer1d
static void FreeBuffer1d(T **buffer)
Definition: memory_manage.h:116
Config::RadioId
const std::vector< std::string > & RadioId() const
Definition: config.h:356
RadioConfig::CalibrateSampleOffset
void CalibrateSampleOffset()
Definition: radio_calibrate_digital.cc:341
RadioConfig::calib_meas_num_
size_t calib_meas_num_
Definition: radio_lib.h:94
Config::HubId
const std::vector< std::string > & HubId() const
Definition: config.h:359
RadioConfig::num_radios_initialized_
std::atomic< size_t > num_radios_initialized_
Definition: radio_lib.h:96
RadioConfig::init_calib_ul_
Table< arma::cx_float > init_calib_ul_
Definition: radio_lib.h:89
memory_manage.h
RadioConfig::InitialCalib
bool InitialCalib()
Definition: radio_calibrate_digital.cc:438
kHubMissingWaitMs
static constexpr size_t kHubMissingWaitMs
Definition: radio_lib.cc:15
Config::BsAntNum
size_t BsAntNum() const
Definition: config.h:35
RadioConfig::Go
void Go()
Definition: radio_lib.cc:293
kRecordCalibrationMats
static constexpr bool kRecordCalibrationMats
Definition: symbols.h:203
Config::RxGainB
double RxGainB() const
Definition: config.h:67
spdlog::level::info
@ info
Definition: common.h:215
AGORA_LOG_ERROR
#define AGORA_LOG_ERROR(...)
Definition: logger.h:39
RadioConfig::antenna_num_
size_t antenna_num_
Definition: radio_lib.h:92
Config::RefRadio
size_t RefRadio(size_t id) const
Definition: config.h:101
flags
list flags
Definition: .ycm_extra_conf.py:39
Config::Frame
const FrameStats & Frame() const
Definition: config.h:340
RadioConfig::CalibrateSampleOffsetUplink
bool CalibrateSampleOffsetUplink(size_t max_attempts)
Definition: radio_calibrate_digital.cc:349
kSoapyMakeMaxAttempts
static constexpr size_t kSoapyMakeMaxAttempts
Definition: radio_lib.cc:14
RadioConfig::GetCalibUl
arma::cx_float * GetCalibUl()
Definition: radio_lib.h:48
AGORA_LOG_TRACE
#define AGORA_LOG_TRACE(...)
Definition: logger.h:92
RadioConfig::FindTimeOffset
bool FindTimeOffset(const std::vector< std::vector< std::complex< int16_t >>> &rx_mat, std::vector< int > &offset)
Definition: radio_calibrate_digital.cc:210
AllocBuffer1d
static void AllocBuffer1d(T **buffer, U dim, Agora_memory::Alignment_t alignment, int init_zero)
Definition: memory_manage.h:105
mean
static double mean(const std::vector< double > in_vec)
Definition: timer.h:103
Table< arma::cx_float >
radio_lib.h
Declaration file for the RadioConfig class.
RadioConfig::init_calib_ul_processed_
arma::cx_float * init_calib_ul_processed_
Definition: radio_lib.h:87
RadioConfig::RadioStart
bool RadioStart()
Definition: radio_lib.cc:177
Config::OfdmDataNum
size_t OfdmDataNum() const
Definition: config.h:47
RadioConfig::InitBsRadio
void InitBsRadio(size_t radio_id)
Definition: radio_lib.cc:157
Radio::Close
virtual void Close()=0
Definition: radio.cc:35
Radio
Definition: radio.h:15
Radio::TxFlags
TxFlags
Definition: radio.h:20
RadioConfig
Definition: radio_lib.h:21
RadioConfig::radios_
std::vector< std::unique_ptr< Radio > > radios_
Definition: radio_lib.h:86
kPrintCalibrationMats
static constexpr bool kPrintCalibrationMats
Definition: radio_lib.cc:13
Radio::RadioType
RadioType
Definition: radio.h:17
Table::Free
void Free()
Definition: memory_manage.h:84
RadioConfig::RadioConfig
RadioConfig(Config *cfg, Radio::RadioType radio_type)
Definition: radio_lib.cc:17
Radio::Create
static std::unique_ptr< Radio > Create(RadioType type)
Definition: radio.cc:11
i
for i
Definition: generate_data.m:107
nlohmann::json_v3_11_1NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON::detail::void
j template void())
Definition: json.hpp:4744
Config::ImbalanceCalEn
bool ImbalanceCalEn() const
Definition: config.h:107
Catch::cout
std::ostream & cout()
RadioConfig::RadioStop
void RadioStop()
Definition: radio_lib.cc:350
u
Plot Rx waveform for u
Definition: inspect_single_frame.m:108
Agora_memory::Alignment_t::kAlign64
@ kAlign64
rx_mat
Rx data rx_mat
Definition: generate_data.m:115
kUseUHD
static constexpr bool kUseUHD
Definition: symbols.h:162
Config::Channel
std::string Channel() const
Definition: config.h:113
RadioConfig::GetCalibDl
arma::cx_float * GetCalibDl()
Definition: radio_lib.h:49
RadioConfig::~RadioConfig
~RadioConfig()
Definition: radio_lib.cc:400
RadioConfig::init_calib_dl_
Table< arma::cx_float > init_calib_dl_
Definition: radio_lib.h:90
Config::InitCalibRepeat
size_t InitCalibRepeat() const
Definition: config.h:96
RadioConfig::hubs_
std::vector< SoapySDR::Device * > hubs_
Definition: radio_lib.h:85
RadioConfig::RadioTx
int RadioTx(size_t radio_id, const void *const *buffs, Radio::TxFlags flags, long long &tx_time)
Definition: radio_lib.cc:306
Radio::RxFlags
RxFlags
Definition: radio.h:19
RadioConfig::AdjustDelays
void AdjustDelays(const std::vector< int > &ch0_offsets)
Definition: radio_calibrate_digital.cc:251
RadioConfig::SetIqBalance
static void SetIqBalance(Radio *dev, int direction, size_t channel, int gcorr, int iqcorr)
Definition: radio_calibrate_analog.cc:181
RadioConfig::DciqMinimize
static void DciqMinimize(Radio *target_dev, Radio *ref_dev, int direction, size_t channel, double rx_center_tone, double tx_center_tone)
Definition: radio_calibrate_analog.cc:192
Utils::StrToChannels
static std::vector< size_t > StrToChannels(const std::string &channel)
Definition: utils.cc:207
FrameStats::NumDLSyms
size_t NumDLSyms() const
Definition: framestats.cc:83
Config::CellId
const std::vector< size_t > & CellId() const
Definition: config.h:368
Utils::SaveMat
static void SaveMat(const arma::cx_fmat &c, const std::string &filename, const std::string &ss, const bool append)
Definition: utils.cc:428
RadioConfig::calib_
bool calib_
Definition: radio_lib.h:93
Config::HwFramer
bool HwFramer() const
Definition: config.h:53
Config
Definition: config.h:26
AGORA_LOG_INFO
#define AGORA_LOG_INFO(...)
Definition: logger.h:62
Config::SampleCalEn
bool SampleCalEn() const
Definition: config.h:106
RadioConfig::TxArrayToRef
auto TxArrayToRef(const std::vector< std::complex< int16_t >> &tx_vec)
Definition: radio_calibrate_digital.cc:24
zeros
N_SC, NUM_UE, NUM_UE zeros()
tx_data
end Generate data if GENERATE_DATA tx_data
Definition: generate_data.m:38
AGORA_LOG_WARN
#define AGORA_LOG_WARN(...)
Definition: logger.h:53
RadioConfig::ConfigureBsRadio
void ConfigureBsRadio(size_t radio_id)
Definition: radio_lib.cc:164
RadioConfig::TxRefToArray
auto TxRefToArray(const std::vector< std::complex< int16_t >> &tx_vec)
Definition: radio_calibrate_digital.cc:118
to_string
std::string to_string() const
Definition: eth_common.h:64
RadioConfig::radio_num_
size_t radio_num_
Definition: radio_lib.h:91
Config::NumCells
size_t NumCells() const
Definition: config.h:94
config.h
Declaration file for the configuration class which importants json configuration values into class va...
rx_data
case QPSK rx_data
Definition: generate_data.m:160
Radio::Activate
virtual void Activate(Radio::ActivationTypes type=Radio::ActivationTypes::kActivate, long long act_time_ns=0, size_t samples=0)=0
Definition: radio.cc:62
Radio::Deactivate
virtual void Deactivate()=0
Definition: radio.cc:71
RadioConfig::SyncArrayTime
long long SyncArrayTime()
Definition: radio_lib.cc:364
RadioConfig::DoCalib
bool DoCalib() const
Definition: radio_lib.h:46
Config::RxGainA
double RxGainA() const
Definition: config.h:65
Utils::PrintMat
static void PrintMat(const arma::cx_fmat &c, const std::string &ss)
Definition: utils.cc:452
Config::NumRadios
size_t NumRadios() const
Definition: config.h:95
Config::TxGainB
double TxGainB() const
Definition: config.h:66
Config::TxGainA
double TxGainA() const
Definition: config.h:64
Config::BfAntNum
size_t BfAntNum() const
Definition: config.h:39
RadioConfig::DciqCalibrationProc
void DciqCalibrationProc(size_t channel)
Definition: radio_calibrate_analog.cc:330