2017-11-17 13:47:50 +00:00
|
|
|
|
/*!
|
|
|
|
|
* \file matio_test.cc
|
|
|
|
|
* \brief This file implements tests for the matio library
|
|
|
|
|
* in long arrays.
|
|
|
|
|
* \author Carles Fernandez-Prades, 2017. cfernandez(at)cttc.es
|
|
|
|
|
*
|
|
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
|
* -----------------------------------------------------------------------------
|
2017-11-17 13:47:50 +00:00
|
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
|
* GNSS-SDR is a Global Navigation Satellite System software-defined receiver.
|
2017-11-17 13:47:50 +00:00
|
|
|
|
* This file is part of GNSS-SDR.
|
|
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
|
* Copyright (C) 2010-2020 (see AUTHORS file for a list of contributors)
|
2020-02-08 00:20:02 +00:00
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2017-11-17 13:47:50 +00:00
|
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
|
* -----------------------------------------------------------------------------
|
2017-11-17 13:47:50 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2021-01-16 13:37:19 +00:00
|
|
|
|
#include "gnss_sdr_filesystem.h"
|
2017-11-17 13:47:50 +00:00
|
|
|
|
#include <gnuradio/gr_complex.h>
|
2018-02-23 23:13:35 +00:00
|
|
|
|
#include <gtest/gtest.h>
|
2018-12-09 21:00:09 +00:00
|
|
|
|
#include <matio.h>
|
2019-07-14 12:09:12 +00:00
|
|
|
|
#include <array>
|
2019-07-14 00:28:28 +00:00
|
|
|
|
|
2017-11-17 13:47:50 +00:00
|
|
|
|
TEST(MatioTest, WriteAndReadDoubles)
|
|
|
|
|
{
|
|
|
|
|
// Write a .mat file
|
|
|
|
|
mat_t *matfp;
|
|
|
|
|
matvar_t *matvar;
|
|
|
|
|
std::string filename = "./test.mat";
|
2018-12-03 09:05:47 +00:00
|
|
|
|
matfp = Mat_CreateVer(filename.c_str(), nullptr, MAT_FT_MAT73);
|
|
|
|
|
ASSERT_FALSE(reinterpret_cast<long *>(matfp) == nullptr) << "Error creating .mat file";
|
2017-11-17 13:47:50 +00:00
|
|
|
|
|
2019-07-14 12:09:12 +00:00
|
|
|
|
std::array<double, 10> x{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
|
|
|
|
std::array<size_t, 2> dims{10, 1};
|
|
|
|
|
matvar = Mat_VarCreate("x", MAT_C_DOUBLE, MAT_T_DOUBLE, 2, dims.data(), x.data(), 0);
|
2018-12-03 09:05:47 +00:00
|
|
|
|
ASSERT_FALSE(reinterpret_cast<long *>(matvar) == nullptr) << "Error creating variable for ’x’";
|
2017-11-17 13:47:50 +00:00
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
|
Mat_VarWrite(matfp, matvar, MAT_COMPRESSION_ZLIB); // or MAT_COMPRESSION_NONE
|
2017-11-17 13:47:50 +00:00
|
|
|
|
Mat_VarFree(matvar);
|
|
|
|
|
|
|
|
|
|
Mat_Close(matfp);
|
|
|
|
|
|
|
|
|
|
// Read a .mat file
|
|
|
|
|
mat_t *matfp_read;
|
|
|
|
|
matvar_t *matvar_read;
|
|
|
|
|
|
|
|
|
|
matfp_read = Mat_Open(filename.c_str(), MAT_ACC_RDONLY);
|
2018-12-03 09:05:47 +00:00
|
|
|
|
ASSERT_FALSE(reinterpret_cast<long *>(matfp_read) == nullptr) << "Error reading .mat file";
|
2017-11-17 13:47:50 +00:00
|
|
|
|
|
|
|
|
|
matvar_read = Mat_VarReadInfo(matfp_read, "x");
|
2018-12-03 09:05:47 +00:00
|
|
|
|
ASSERT_FALSE(reinterpret_cast<long *>(matvar_read) == nullptr) << "Error reading variable in .mat file";
|
2017-11-17 13:47:50 +00:00
|
|
|
|
|
|
|
|
|
matvar_read = Mat_VarRead(matfp_read, "x");
|
2018-12-03 15:25:11 +00:00
|
|
|
|
auto *x_read = reinterpret_cast<double *>(matvar_read->data);
|
2017-11-17 13:47:50 +00:00
|
|
|
|
Mat_Close(matfp_read);
|
2017-11-18 09:05:14 +00:00
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
|
for (int i = 0; i < 10; i++)
|
2017-11-17 13:47:50 +00:00
|
|
|
|
{
|
|
|
|
|
EXPECT_DOUBLE_EQ(x[i], x_read[i]);
|
|
|
|
|
}
|
2017-11-18 09:05:14 +00:00
|
|
|
|
Mat_VarFree(matvar_read);
|
2019-07-14 00:28:28 +00:00
|
|
|
|
errorlib::error_code ec;
|
|
|
|
|
ASSERT_EQ(fs::remove(fs::path(filename), ec), true);
|
2017-11-17 13:47:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TEST(MatioTest, WriteAndReadGrComplex)
|
|
|
|
|
{
|
|
|
|
|
// Write a .mat file
|
|
|
|
|
mat_t *matfp;
|
2017-11-18 09:05:14 +00:00
|
|
|
|
matvar_t *matvar1;
|
|
|
|
|
std::string filename = "./test3.mat";
|
2018-12-03 09:05:47 +00:00
|
|
|
|
matfp = Mat_CreateVer(filename.c_str(), nullptr, MAT_FT_MAT73);
|
|
|
|
|
ASSERT_FALSE(reinterpret_cast<long *>(matfp) == nullptr) << "Error creating .mat file";
|
2017-11-17 13:47:50 +00:00
|
|
|
|
|
2019-07-14 12:09:12 +00:00
|
|
|
|
const std::vector<gr_complex> x_v = {{1, 10}, {2, 9}, {3, 8}, {4, 7}, {5, 6}, {6, -5}, {7, -4}, {8, 3}, {9, 2}, {10, 1}};
|
2017-11-17 20:54:02 +00:00
|
|
|
|
const unsigned int size = x_v.size();
|
2019-07-14 12:09:12 +00:00
|
|
|
|
std::array<float, 10> x_real{};
|
|
|
|
|
std::array<float, 10> x_imag{};
|
2017-11-17 20:20:56 +00:00
|
|
|
|
unsigned int i = 0;
|
2018-12-03 17:03:25 +00:00
|
|
|
|
for (auto it : x_v)
|
2017-11-17 13:47:50 +00:00
|
|
|
|
{
|
2018-12-03 17:03:25 +00:00
|
|
|
|
x_real[i] = it.real();
|
|
|
|
|
x_imag[i] = it.imag();
|
2017-11-17 20:20:56 +00:00
|
|
|
|
i++;
|
2017-11-17 13:47:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-14 12:09:12 +00:00
|
|
|
|
struct mat_complex_split_t x = {x_real.data(), x_imag.data()};
|
|
|
|
|
std::array<size_t, 2> dims{static_cast<size_t>(size), 1};
|
|
|
|
|
matvar1 = Mat_VarCreate("x", MAT_C_SINGLE, MAT_T_SINGLE, 2, dims.data(), &x, MAT_F_COMPLEX);
|
2018-12-03 09:05:47 +00:00
|
|
|
|
ASSERT_FALSE(reinterpret_cast<long *>(matvar1) == nullptr) << "Error creating variable for ’x’";
|
2017-11-17 13:47:50 +00:00
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
|
std::vector<gr_complex> x2 = {{1.1, -10}, {2, -9}, {3, -8}, {4, -7}, {5, 6}, {6, -5}, {7, -4}, {8, 3}, {9, 2}, {10, 1}};
|
2017-11-18 09:05:14 +00:00
|
|
|
|
const unsigned int size_y = x2.size();
|
2019-07-14 12:09:12 +00:00
|
|
|
|
std::array<float, 10> y_real{};
|
|
|
|
|
std::array<float, 10> y_imag{};
|
2017-11-17 22:17:08 +00:00
|
|
|
|
i = 0;
|
2018-12-03 17:03:25 +00:00
|
|
|
|
for (auto it : x2)
|
2017-11-17 22:17:08 +00:00
|
|
|
|
{
|
2018-12-03 17:03:25 +00:00
|
|
|
|
y_real[i] = it.real();
|
|
|
|
|
y_imag[i] = it.imag();
|
2017-11-17 22:17:08 +00:00
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-14 12:09:12 +00:00
|
|
|
|
struct mat_complex_split_t y = {y_real.data(), y_imag.data()};
|
|
|
|
|
std::array<size_t, 2> dims_y{static_cast<size_t>(size_y), 1};
|
2017-11-18 09:05:14 +00:00
|
|
|
|
matvar_t *matvar2;
|
2019-07-14 12:09:12 +00:00
|
|
|
|
matvar2 = Mat_VarCreate("y", MAT_C_SINGLE, MAT_T_SINGLE, 2, dims_y.data(), &y, MAT_F_COMPLEX);
|
2018-12-03 09:05:47 +00:00
|
|
|
|
ASSERT_FALSE(reinterpret_cast<long *>(matvar2) == nullptr) << "Error creating variable for ’y’";
|
2017-11-17 22:17:08 +00:00
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
|
Mat_VarWrite(matfp, matvar1, MAT_COMPRESSION_ZLIB); // or MAT_COMPRESSION_NONE
|
|
|
|
|
Mat_VarWrite(matfp, matvar2, MAT_COMPRESSION_ZLIB); // or MAT_COMPRESSION_NONE
|
2017-11-18 09:10:23 +00:00
|
|
|
|
Mat_VarFree(matvar1);
|
2017-11-18 09:05:14 +00:00
|
|
|
|
Mat_VarFree(matvar2);
|
2017-11-17 13:47:50 +00:00
|
|
|
|
|
|
|
|
|
Mat_Close(matfp);
|
|
|
|
|
|
|
|
|
|
// Read a .mat file
|
|
|
|
|
mat_t *matfp_read;
|
|
|
|
|
matvar_t *matvar_read;
|
|
|
|
|
|
|
|
|
|
matfp_read = Mat_Open(filename.c_str(), MAT_ACC_RDONLY);
|
2018-12-03 09:05:47 +00:00
|
|
|
|
ASSERT_FALSE(reinterpret_cast<long *>(matfp_read) == nullptr) << "Error reading .mat file";
|
2017-11-17 13:47:50 +00:00
|
|
|
|
|
|
|
|
|
matvar_read = Mat_VarReadInfo(matfp_read, "x");
|
2018-12-03 09:05:47 +00:00
|
|
|
|
ASSERT_FALSE(reinterpret_cast<long *>(matvar_read) == nullptr) << "Error reading variable in .mat file";
|
2017-11-17 13:47:50 +00:00
|
|
|
|
|
|
|
|
|
matvar_read = Mat_VarRead(matfp_read, "x");
|
2018-12-03 15:25:11 +00:00
|
|
|
|
auto *x_read_st = reinterpret_cast<mat_complex_split_t *>(matvar_read->data);
|
|
|
|
|
auto *x_read_real = reinterpret_cast<float *>(x_read_st->Re);
|
|
|
|
|
auto *x_read_imag = reinterpret_cast<float *>(x_read_st->Im);
|
2017-11-17 13:47:50 +00:00
|
|
|
|
std::vector<gr_complex> x_v_read;
|
2024-09-25 18:08:02 +00:00
|
|
|
|
x_v_read.reserve(size);
|
2020-02-26 17:16:04 +00:00
|
|
|
|
for (unsigned int k = 0; k < size; k++)
|
2017-11-17 13:47:50 +00:00
|
|
|
|
{
|
2020-02-26 17:16:04 +00:00
|
|
|
|
x_v_read.emplace_back(x_read_real[k], x_read_imag[k]);
|
2017-11-17 13:47:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Mat_Close(matfp_read);
|
2017-11-17 20:54:02 +00:00
|
|
|
|
Mat_VarFree(matvar_read);
|
2017-11-17 13:47:50 +00:00
|
|
|
|
|
2020-02-26 17:16:04 +00:00
|
|
|
|
for (unsigned int k = 0; k < size; k++)
|
2017-11-17 13:47:50 +00:00
|
|
|
|
{
|
2020-02-26 17:16:04 +00:00
|
|
|
|
EXPECT_FLOAT_EQ(x_v[k].real(), x_v_read[k].real());
|
|
|
|
|
EXPECT_FLOAT_EQ(x_v[k].imag(), x_v_read[k].imag());
|
2017-11-17 13:47:50 +00:00
|
|
|
|
}
|
2019-07-14 00:28:28 +00:00
|
|
|
|
errorlib::error_code ec;
|
|
|
|
|
ASSERT_EQ(fs::remove(fs::path(filename), ec), true);
|
2017-11-17 13:47:50 +00:00
|
|
|
|
}
|