From 4eab096cdeecc9ec8478599bbb684c0d57d170c9 Mon Sep 17 00:00:00 2001 From: Erik Strand <erik.strand@cba.mit.edu> Date: Wed, 8 May 2019 19:09:11 -0400 Subject: [PATCH] Sample points in C++ --- CMakeLists.txt | 1 - main.cpp | 33 +++++++++++++++++++++++++++++---- plotter.py | 9 +++++++++ vector.h | 18 ------------------ 4 files changed, 38 insertions(+), 23 deletions(-) create mode 100644 plotter.py delete mode 100644 vector.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e235272..0a21944 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,6 @@ include(cmake/shared_settings.cmake) add_executable(compressed_sensing main.cpp - vector.h ) target_link_libraries(compressed_sensing shared_settings Eigen3::Eigen) target_compile_features(compressed_sensing PUBLIC cxx_std_17) diff --git a/main.cpp b/main.cpp index 99fb5d7..3534690 100644 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,36 @@ +#include <Eigen/Core> #include <iostream> -#include "vector.h" -using namespace cs; +using Scalar = double; +using Vector = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>; +using Matrix = Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>; + +void python_print(char const* name, Vector const& x) { + std::cout << name << " = np.array(["; + std::cout << x[0]; + for (uint32_t i = 1; i < x.size(); ++i) { + std::cout << ", " << x[i]; + } + std::cout << "])\n"; +} //-------------------------------------------------------------------------------------------------- int main() { - Vector<double> x = Vector<double>::Random(5); - std::cout << x << '\n'; + //Vector x = Vector::Random(5); + //std::cout << x << '\n'; + + Scalar pi = 3.1415926535897932384626433832795; + + Scalar f1 = 697; + Scalar f2 = 1209; + + Scalar sample_period = 0.01; + uint32_t n_samples = 10; + Vector sample_times = (sample_period / n_samples) * Vector::LinSpaced(n_samples, 0, n_samples); + python_print("sample_times", sample_times); + Vector sample_rads = 2 * pi * sample_times; + Vector sample_values = sin((f1 * sample_rads).array()) + sin((f2 * sample_rads).array()); + python_print("sample_values", sample_values); + return 0; } diff --git a/plotter.py b/plotter.py new file mode 100644 index 0000000..0b4575e --- /dev/null +++ b/plotter.py @@ -0,0 +1,9 @@ +import numpy as np +import matplotlib.pyplot as plt + +if __name__ == "__main__": + sample_times = np.array([0, 0.00111111, 0.00222222, 0.00333333, 0.00444444, 0.00555556, 0.00666667, 0.00777778, 0.00888889, 0.01]) + sample_values = np.array([0, -0.155307, -1.22423, 1.08309, 1.2909, -1.69749, -0.428405, 1.04634, -0.0577232, 0.348445]) + plt.plot(sample_times, sample_values) + plt.savefig("fig_a.png") + plt.close() diff --git a/vector.h b/vector.h deleted file mode 100644 index cf7c07e..0000000 --- a/vector.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef FUNSIM_VECTOR_H -#define FUNSIM_VECTOR_H - -#include <Eigen/Core> - -namespace cs { - -//-------------------------------------------------------------------------------------------------- -template <typename T> -using Vector = Eigen::Matrix<T, Eigen::Dynamic, 1>; - -//-------------------------------------------------------------------------------------------------- -template <typename T> -using Matrix = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>; - -} - -#endif -- GitLab