Skip to content

Commit

Permalink
Merge branch 'testing' into nextRelease
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankReiser committed Nov 26, 2023
2 parents 86824f4 + 73b858d commit 14f8534
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.15)

project(
ReiserRT_ChirpingPhasor
VERSION 1.1.5
VERSION 2.0.1
DESCRIPTION "ReiserRT Complex Chirping Phasor Tone Generator" )

# Set up compiler requirements
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ add_library( ${PROJECT_NAME} SHARED "" )

# We are dependent on ReiserRT_FlyingPhasor shared library package.
# Ensure we specify the Major.Minor version we want.
find_package( ReiserRT_FlyingPhasor 2.3 CONFIG REQUIRED )
find_package( ReiserRT_FlyingPhasor 3.0 CONFIG REQUIRED )
if ( ReiserRT_FlyingPhasor_FOUND )
message( STATUS "Found ReiserRT_FlyingPhasor!" )
endif()
Expand Down
2 changes: 1 addition & 1 deletion src/ChirpingPhasorToneGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void ChirpingPhasorToneGenerator::reset( double accel, double omegaZero, double
{
// Our `phasor` represents our output, the next sample to be retrieved. This
// is simply initialized with a magnitude of 1.0 at starting phase `phi`.
phasor = std::polar(1.0, phi );
phasor = std::polar( 1.0, phi );

// We store `accelOver2` as it is needed within the `modifyAccel` operation.
// It is also used to initialize our `rate` attribute below.
Expand Down
10 changes: 9 additions & 1 deletion src/ChirpingPhasorToneGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ namespace ReiserRT
* @return Returns the average angular velocity between the next two, yet to be retrieved, samples.
*/
inline FlyingPhasorPrecisionType getOmegaBar() const {
return std::arg( rate.peakNextSample() );
return std::arg( rate.peekNextSample() );
}

/**
Expand All @@ -121,6 +121,14 @@ namespace ReiserRT
*/
void modifyAccel( double newAccel=0 );

/**
* @brief Peek Next Sample
*
* This operation exists for uses cases, where querying the current phase of an instance is necessary
* without 'working' the machine. The phasor state remains unchanged.
*/
inline const FlyingPhasorElementType & peekNextSample() const { return phasor; }

private:
/**
* @brief The Normalize Operation.
Expand Down
10 changes: 10 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
add_executable( testInitialization "" )
target_sources( testInitialization PRIVATE testInitialization.cpp)
target_include_directories( testInitialization PUBLIC ../src ../testUtilities )
target_link_libraries( testInitialization ReiserRT_ChirpingPhasor TestUtilities )
target_compile_options( testInitialization PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/W4 /WX>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wall -Wextra -Wpedantic -Werror>
)
add_test( NAME runInitializationTest COMMAND $<TARGET_FILE:testInitialization> )

add_executable( chirpPurityTest "" )
target_sources( chirpPurityTest PRIVATE chirpPurityTest.cpp)
target_include_directories( chirpPurityTest PUBLIC ../src ../testUtilities )
Expand Down
182 changes: 182 additions & 0 deletions tests/testInitialization.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/**
* @file testInitialization.cpp
* @brief Initializes ChirpingPhasor Generator and Performs Basic Tests
*
* @authors Frank Reiser
* @date Initiated on Nov 25, 2023
*/

#include "ChirpingPhasorToneGenerator.h"

#include "MiscTestUtilities.h"

#include <iostream>
#include <memory>

using namespace ReiserRT::Signal;

int main()
{
int retCode = 0;

std::cout << "Initialization Testing of TSG Flying Phasor Tone Generator" << std::endl;

// For maximum view of significant digits for diagnostic purposes.
std::cout << std::scientific;
std::cout.precision(17);

do
{
// A small buffer for storing two elements
std::unique_ptr< FlyingPhasorElementType[] > pElementBuf{new FlyingPhasorElementType[2] };

// Test Default Construction
{
// Default construction does not lead to a very useful chirp generator as it is stuck at zero.
ChirpingPhasorToneGenerator chirpingPhasorToneGenerator{};

// It's sampleCounter should be zero
auto sampleCount = chirpingPhasorToneGenerator.getSampleCount();
if ( 0 != sampleCount )
{
std::cout << "Sample Count should be zero and is " << sampleCount << std::endl;
retCode = 1;
break;
}

// Fetch 2 samples.
chirpingPhasorToneGenerator.getSamples( pElementBuf.get(), 2 );

// It's sampleCounter should be two
sampleCount = chirpingPhasorToneGenerator.getSampleCount();
if ( 2 != sampleCount )
{
std::cout << "Sample Count should be two and is " << sampleCount << std::endl;
retCode = 2;
break;
}

// The first sample should have a mag of one and a phase of zero.
auto phase = std::arg( pElementBuf[0] );
if ( !inTolerance( phase, 0.0, 1e-12 ) )
{
std::cout << "First Sample Phase: " << phase << " out of Tolerance! Should be: "
<< 0.0 << std::endl;
retCode = 3;
break;
}
auto mag = std::abs( pElementBuf[0] );
if ( !inTolerance( mag, 1.0, 1e-12 ) )
{
std::cout << "First Sample Magnitude: " << mag << " out of Tolerance! Should be: "
<< 1.0 << std::endl;
retCode = 4;
break;
}

// The second sample should also have a mag of one and a phase of zero (pure DC).
phase = std::arg( pElementBuf[1] );
if ( !inTolerance( phase, 0.0, 1e-12 ) )
{
std::cout << "Second Sample Phase: " << phase << " out of Tolerance! Should be: "
<< 0.0 << std::endl;
retCode = 5;
break;
}
mag = std::abs( pElementBuf[1] );
if ( !inTolerance( mag, 1.0, 1e-12 ) )
{
std::cout << "Second Sample Magnitude: " << mag << " out of Tolerance! Should be: "
<< 1.0 << std::endl;
retCode = 6;
break;
}

// Attempt a reset to something else and test again.
const auto accel = M_PI / 1024;
const auto omegaZero = M_PI / 512;
const auto phi = M_PI / 256;
chirpingPhasorToneGenerator.reset( accel, omegaZero, phi );

// It's sampleCounter should be zero
sampleCount = chirpingPhasorToneGenerator.getSampleCount();
if ( 0 != sampleCount )
{
std::cout << "Sample Count should be zero and is " << sampleCount << std::endl;
retCode = 7;
break;
}

// Peek at the next sample
const auto peek = chirpingPhasorToneGenerator.peekNextSample();

// The first sample should have a mag of one and a phase of 1.0.
phase = std::arg( peek );
if ( !inTolerance( phase, phi, 1e-12 ) )
{
std::cout << "Peek First Sample Phase: " << phase << " out of Tolerance! Should be: "
<< 1.0 << std::endl;
retCode = 8;
break;
}
mag = std::abs( peek );
if ( !inTolerance( mag, 1.0, 1e-12 ) )
{
std::cout << "Peek First Sample Magnitude: " << mag << " out of Tolerance! Should be: "
<< 1.0 << std::endl;
retCode = 9;
break;
}

// Fetch two samples
chirpingPhasorToneGenerator.getSamples( pElementBuf.get(), 2 );

// The first sample should equal the peeked sample
if ( peek != pElementBuf[0] )
{
std::cout << "First Sample not equal to peeked at sample." << std::endl;
retCode = 10;
break;
}

// The second sample should be advanced by phi plus omegaZero plus accel over two
// and a mag of one
phase = std::arg( pElementBuf[1] );
auto expectedPhase = phi + omegaZero + accel / 2.0;
if ( !inTolerance( phase, expectedPhase, 1e-12 ) )
{
std::cout << "Second Sample Phase: " << phase << " out of Tolerance! Should be: "
<< expectedPhase << std::endl;
retCode = 11;
break;
}
mag = std::abs( pElementBuf[1] );
if ( !inTolerance( mag, 1.0, 1e-12 ) )
{
std::cout << "Second Sample Magnitude: " << mag << " out of Tolerance! Should be: "
<< 1.0 << std::endl;
retCode = 12;
break;
}

// Test Getting Single Samples. We will reset again with the same parameters
chirpingPhasorToneGenerator.reset( accel, omegaZero, phi );
for ( size_t i = 0; 2 != i; ++i )
{
const auto sample = chirpingPhasorToneGenerator.getSample();
if ( sample != pElementBuf[i] )
{
std::cout << "Get Single Sample failed at index " << i << ". Expected " << pElementBuf[i]
<< ", obtained " << sample << std::endl;
retCode = 13;
break;
}
}
if ( retCode ) break;
}
} while (false);

exit( retCode );
return retCode;
}

0 comments on commit 14f8534

Please sign in to comment.