2019-01-04 08:45:22 +00:00
|
|
|
// Copyright (c) 2019, Vincent SAMY
|
|
|
|
// All rights reserved.
|
|
|
|
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
2019-10-10 02:31:00 +00:00
|
|
|
|
|
|
|
// 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
2019-01-04 08:45:22 +00:00
|
|
|
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
2019-10-10 02:31:00 +00:00
|
|
|
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
2019-01-04 08:45:22 +00:00
|
|
|
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2019-10-10 02:31:00 +00:00
|
|
|
// The views and conclusions contained in the software and documentation are those
|
|
|
|
// of the authors and should not be interpreted as representing official policies,
|
|
|
|
// either expressed or implied, of the FreeBSD Project.
|
|
|
|
|
2018-10-26 07:18:08 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-01-11 10:07:11 +00:00
|
|
|
#include "gsl/gsl_assert.h"
|
2018-12-14 11:30:44 +00:00
|
|
|
#include "type_checks.h"
|
|
|
|
#include "typedefs.h"
|
2019-01-11 10:07:11 +00:00
|
|
|
#include <limits>
|
2018-10-26 07:18:08 +00:00
|
|
|
|
2019-01-04 08:45:22 +00:00
|
|
|
namespace difi {
|
2018-10-26 07:18:08 +00:00
|
|
|
|
2019-01-04 08:15:03 +00:00
|
|
|
/*! \brief Transform an analog signal to a discrete signal and vice versa.
|
|
|
|
*
|
|
|
|
* \see https://en.wikipedia.org/wiki/Bilinear_transform
|
|
|
|
* \tparam T Floating (complex) types.
|
|
|
|
*/
|
2018-10-26 07:18:08 +00:00
|
|
|
template <typename T>
|
|
|
|
struct BilinearTransform {
|
2019-01-04 08:15:03 +00:00
|
|
|
using SubType = internal::complex_sub_type_t<T>; /*!< Sub-type of the complex if T is complex, T otherwise */
|
2018-10-26 07:18:08 +00:00
|
|
|
static_assert(std::is_floating_point<SubType>::value, "This struct can only accept floating point types (real and complex).");
|
|
|
|
|
2019-01-04 08:15:03 +00:00
|
|
|
/*! \brief Transformation from analog to discrete.
|
|
|
|
* \param fs Sampling frequency.
|
|
|
|
* \param sPlanePole Analog data.
|
|
|
|
* \param[out] zPlanePole Resulting discrete data.
|
|
|
|
*/
|
2018-10-26 07:18:08 +00:00
|
|
|
static void SToZ(SubType fs, const T& sPlanePole, T& zPlanePole);
|
2019-01-04 08:15:03 +00:00
|
|
|
/*! \brief Transformation from analog to discrete.
|
|
|
|
* \param fs Sampling frequency.
|
|
|
|
* \param sPlanePole Analog signal.
|
|
|
|
* \param[out] zPlanePole Resulting discrete signal.
|
|
|
|
*/
|
|
|
|
static void SToZ(SubType fs, const vectX_t<T>& sPlanePoles, Eigen::Ref<vectX_t<T>>& zPlanePoles); // Can be optimized maybe
|
|
|
|
/*! \brief Transformation from discrete to analog.
|
|
|
|
* \param fs Sampling frequency.
|
|
|
|
* \param zPlanePole Discrete data.
|
|
|
|
* \param[out] sPlanePole Resulting analog data.
|
|
|
|
*/
|
2018-10-26 07:18:08 +00:00
|
|
|
static void ZToS(SubType fs, const T& zPlanePole, T& sPlanePole);
|
2019-01-04 08:15:03 +00:00
|
|
|
/*! \brief Transformation from discrete to analog.
|
|
|
|
* \param fs Sampling frequency.
|
|
|
|
* \param zPlanePole Discrete signal.
|
|
|
|
* \param[out] sPlanePole Resulting analog signal.
|
|
|
|
*/
|
|
|
|
static void ZToS(SubType fs, const vectX_t<T>& zPlanePoles, Eigen::Ref<vectX_t<T>>& sPlanePoles); // Can be optimized maybe
|
2018-10-26 07:18:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void BilinearTransform<T>::SToZ(SubType fs, const T& sPlanePole, T& zPlanePole)
|
|
|
|
{
|
2019-01-11 10:07:11 +00:00
|
|
|
Expects(std::abs(2 * fs - sPlanePole) > std::numeric_limits<SubType>::epsilon()); // Divide-by-zero otherwise
|
2018-10-26 07:18:08 +00:00
|
|
|
T scalePole = sPlanePole / (2 * fs);
|
|
|
|
zPlanePole = (T(1) + scalePole) / (T(1) - scalePole);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2018-12-17 08:16:01 +00:00
|
|
|
void BilinearTransform<T>::SToZ(SubType fs, const vectX_t<T>& sPlanePoles, Eigen::Ref<vectX_t<T>>& zPlanePoles)
|
2018-10-26 07:18:08 +00:00
|
|
|
{
|
2019-01-11 10:07:11 +00:00
|
|
|
Expects(sPlanePoles.size() == zPlanePoles.size());
|
2018-12-17 05:48:44 +00:00
|
|
|
for (Eigen::Index k = 0; k < sPlanePoles.size(); ++k)
|
|
|
|
SToZ(fs, sPlanePoles(k), zPlanePoles(k));
|
2018-10-26 07:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void BilinearTransform<T>::ZToS(SubType fs, const T& zPlanePole, T& sPlanePole)
|
|
|
|
{
|
2019-01-11 10:07:11 +00:00
|
|
|
Expects(std::abs(T(1) + zPlanePole) > std::numeric_limits<SubType>::epsilon()); // Divide-by-zero otherwise
|
2018-10-26 07:18:08 +00:00
|
|
|
T invPole = T(1) / zPlanePole;
|
|
|
|
sPlanePole = 2 * fs * (T(1) - invPole) / (T(1) + invPole);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2018-12-17 08:16:01 +00:00
|
|
|
void BilinearTransform<T>::ZToS(SubType fs, const vectX_t<T>& zPlanePoles, Eigen::Ref<vectX_t<T>>& sPlanePoles)
|
2018-10-26 07:18:08 +00:00
|
|
|
{
|
2019-01-11 10:07:11 +00:00
|
|
|
Expects(sPlanePoles.size() == zPlanePoles.size());
|
2018-12-17 05:48:44 +00:00
|
|
|
for (Eigen::Index k = 0; k < sPlanePoles.size(); ++k)
|
|
|
|
ZToS(fs, zPlanePoles(k), sPlanePoles(k));
|
2018-10-26 07:18:08 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 08:45:22 +00:00
|
|
|
} // namespace difi
|