None worked. Had to implement it.
#include <iostream>
#include <iomanip>
#include <numbers>
#include <vector>
#include <cmath>
// Normalize to [0,2PI):
double phaseNorm(double x)
{
x = fmod(x, 2*std::numbers::pi);
if (x < 0)
x += 2*std::numbers::pi;
return x;
};
// unwrap phase [-PI,PI]
std::vector<double> phaseUnwrap(std::vector<double> in)
{
// Normalize to [0,2PI):
std::transform(in.begin(),in.end(),in.begin(),[&](double d){ return phaseNorm(d); });
// unwrap iteration
for(size_t i = 0; i < in.size()-1; ++i)
{
int n2PiJump = in[i] / (2*std::numbers::pi);
in[i+1] += n2PiJump * 2*std::numbers::pi;
if(in[i]-in[i+1] > std::numbers::pi)
{
in[i+1] += 2*std::numbers::pi;
}
}
return in;
}
int main() {
// create phase vector
int n = 3;
std::vector<double> phase;
for(int h = 0; h < 3; ++h)
{
for(int i = n; i > -n; --i)
{
phase.push_back(-i*std::numbers::pi/n);
}
}
// print phase vector
std::cout << std::setw(25) << "Input vector: ";
for(auto& p : phase)
{
std::cout << std::setw(8) << p << " ";
}
std::cout << std::endl;
// normalize phase vector
std::cout << std::setw(25) << "Normalized vector: ";
for(auto& p : phase)
{
p = phaseNorm(p);
std::cout << std::setw(8) << p << " ";
}
std::cout << std::endl;
// unwrap phase vector
std::cout << std::setw(25) << "Unwraped norm. vector: ";
std::vector<double> phaseUnwraped = phaseUnwrap(phase);
for(auto& p : phaseUnwraped)
{
std::cout << std::setw(8) << p << " ";
}
std::cout << std::endl;
return 0;
}
Input vector: -3.14159 -2.0944 -1.0472 0 1.0472 2.0944 -3.14159 -2.0944 -1.0472 0 1.0472 2.0944 -3.14159 -2.0944 -1.0472 0 1.0472 2.0944
Normalized vector: 3.14159 4.18879 5.23599 0 1.0472 2.0944 3.14159 4.18879 5.23599 0 1.0472 2.0944 3.14159 4.18879 5.23599 0 1.0472 2.0944
Unwraped norm. vector: 3.14159 4.18879 5.23599 6.28319 7.33038 8.37758 9.42478 10.472 11.5192 12.5664 13.6136 14.6608 15.708 16.7552 17.8024 18.8496 19.8968 20.944
To compile don't forget the c++20 flag -std=c++20
due to std::numbers::pi
or use M_PI
as included in <math.h>
.