HART  0.2.0
High level Audio Regression and Testing
Loading...
Searching...
No Matches
PolarityPreserved< SampleType > Class Template Reference

Checks whether the output signal preserves the polarity of the input signal. More...

#include <hart_polarity_preserved.hpp>

Inheritance diagram for PolarityPreserved< SampleType >:

Public Member Functions

 PolarityPreserved (double minimumSignedCorrelation=0.5, double maxLagSeconds=0.01, SilencePolicy silencePolicy=SilencePolicy::strict)
 Creates a polarity matcher with a minimum signed correlation threshold.
 
void prepare (double sampleRateHz, size_t numInputChannels, size_t numOutputChannels, size_t) override
 Prepare for processing It is guaranteed that all subsequent process() calls will be in line with the arguments received in this callback. This callback is guaranteed to be called after canOperatePerBlock() If any of the values supplied by this callback are not supported by the matcher, it is expected to act as if the match has failed when match() gets called.
 
bool canOperatePerBlock () const override
 Tells the host if it can operate on a block-by-block basis.
 
void reset () override
 Resets the matcher to its initial state.
 
bool supportsChannelLayout (size_t numInputChannels, size_t numOutputChannels) const override
 Tells the host whether this Matcher is capable of operating on audio with a specific number of channels.
 
bool match (AnalysisContext< SampleType > context) override
 Tells the host if the piece of audio satisfies Matcher's condition or not.
 
MatcherFailureDetails getFailureDetails () const override
 Returns a description of why the match has failed.
 
void represent (std::ostream &stream) const override
 Makes a text representation of this Matcher for test failure outputs.
 
- Public Member Functions inherited from Matcher< SampleType, PolarityPreserved< SampleType > >
virtual std::unique_ptr< MatcherBase< SampleType > > copy () const override
 Returns a smart pointer with a copy of this object.
 
virtual std::unique_ptr< MatcherBase< SampleType > > move () override
 Returns a smart pointer with a moved instance of this object.
 
PolarityPreserved< SampleType > & atChannels (std::initializer_list< size_t > channelsToMatch) &
 Makes this matcher check only specific channels, and ignore the rest.
 
PolarityPreserved< SampleType > && atChannels (std::initializer_list< size_t > channelsToMatch) &&
 Makes this matcher check only specific channels, and ignore the rest.
 
PolarityPreserved< SampleType > & atChannel (size_t channelToMatch) &
 Makes this matcher check only one specific channel, and ignore the rest.
 
PolarityPreserved< SampleType > && atChannel (size_t channelToMatch) &&
 Makes this matcher check only one specific channel, and ignore the rest.
 
PolarityPreserved< SampleType > & atAllChannels () &
 Makes this matcher check all channels.
 
PolarityPreserved< SampleType > && atAllChannels () &&
 Makes this matcher check all channels.
 
PolarityPreserved< SampleType > & atAllChannelsExcept (std::initializer_list< size_t > channelsToSkip) &
 Makes this matcher check only specific channels, and ignore the rest.
 
PolarityPreserved< SampleType > && atAllChannelsExcept (std::initializer_list< size_t > channelsToSkip) &&
 Makes this matcher check only specific channels, and ignore the rest.
 
- Public Member Functions inherited from MatcherBase< SampleType >
virtual ~MatcherBase ()=default
 
 HART_DEPRECATED ("This method is deprecated. Switch to the one that uses AnalysisContext instead.") virtual bool match(const AudioBuffer< SampleType > &inputAudio
 Tells the host if the piece of audio satisfies Matcher's condition or not.
 
return match (analysisContext)
 
virtual bool supportsSampleRate (double) const
 Tells whether this Matcher supports given sample rate.
 
void prepareWithActiveChannels (double sampleRateHz, size_t numInputChannels, size_t numOutputChannels, size_t maxBlockSizeFrames)
 Prepare for processing, meant to be called by a host.
 
void representWithActiveChannels (std::ostream &stream) const
 Makes a text representation of this Matcher with optional "atChannels" appendix.
 

Additional Inherited Members

- Public Attributes inherited from MatcherBase< SampleType >
const AudioBuffer< SampleType > & observedOutputAudio
 
- Protected Member Functions inherited from MatcherBase< SampleType >
bool appliesToChannel (size_t channel)
 Indicates whether this matcher should check a specific channel.
 
ChannelFlags getChannelFlags () const
 Returns flags for the channels that the matcher should apply to.
 
- Protected Attributes inherited from MatcherBase< SampleType >
ChannelFlags m_channelsToMatch {true}
 

Detailed Description

template<typename SampleType>
class hart::PolarityPreserved< SampleType >

Checks whether the output signal preserves the polarity of the input signal.

Uses normalized cross-correlation in the time domain to compare input and output audio, while searching for the best match within a configurable lag range. Correlation is calculated independently for every applicable channel using the formula:

\[ \frac{\sum_n x[n]\,y[n+k]} {\sqrt{\left(\sum_n x[n]^2\right)\left(\sum_n y[n+k]^2\right)}} \]

(sum (x[n] * y[n+k]) / sqrt (sum (x[n]^2) * sum (y[n+k]^2))),

where x is input signal and y is observed output signal.

The lag with the highest absolute correlation is used to compensate for latency, and the signed correlation at this lag is then checked against a minimum signed correlation threshold.

For multi-channel audio, all applicable channels must preserve polarity. If at least one applicable channel exceeds the negative threshold, the match fails.

This matcher is useful for detecting accidental polarity inversions while remaining robust to latency and gain differences.

Notes:

  • Gain differences do not affect the result due to normalization.
  • Constant DC offset may bias the signed correlation.
  • Heavy nonlinear processing may reduce confidence in polarity detection.
  • Unlike hart::CorrelationAbove, the sign of correlation is preserved.
    Template Parameters
    SampleTypeFloating point sample type, typically float or double

Definition at line 50 of file hart_polarity_preserved.hpp.

Constructor & Destructor Documentation

◆ PolarityPreserved()

template<typename SampleType >
PolarityPreserved ( double  minimumSignedCorrelation = 0.5,
double  maxLagSeconds = 0.01,
SilencePolicy  silencePolicy = SilencePolicy::strict 
)
inline

Creates a polarity matcher with a minimum signed correlation threshold.

The matcher scans lags in the range [-maxLagSeconds, +maxLagSeconds] and finds the lag with the strongest absolute normalized cross-correlation.

Polarity is considered preserved only if the signed correlation at the best lag is greater than or equal to minimumSignedCorrelation.

Lower values make polarity detection more tolerant to distortion, noise, or other waveform changes, while higher values require a cleaner match.

Parameters
minimumSignedCorrelationMinimum required correlation between input and the output signal. If the observed correlation is in (minimumSignedCorrelation, minimumSignedCorrelation) range, the matcher will fail due to the signals being weakly correlated. If correlation is in [-1, -minimumSignedCorrelation] range, the phase is considered flipped. If it falls into [minimumSignedCorrelation, 1] range, it is considered preserved, and this is where the matcher will pass.
maxLagSecondsMaximum absolute lag to search in seconds
silencePolicyDefines how channels with silence (zeros or almost zeros) are handled. Available options are:
See also
hart::SilencePolicy

Definition at line 80 of file hart_polarity_preserved.hpp.

Member Function Documentation

◆ prepare()

template<typename SampleType >
void prepare ( double  sampleRateHz,
size_t  numInputChannels,
size_t  numOutputChannels,
size_t  maxBlockSizeFrames 
)
inlineoverridevirtual

Prepare for processing It is guaranteed that all subsequent process() calls will be in line with the arguments received in this callback. This callback is guaranteed to be called after canOperatePerBlock() If any of the values supplied by this callback are not supported by the matcher, it is expected to act as if the match has failed when match() gets called.

Parameters
sampleRateHzsample rate at which the audio should be interpreted
numInputChannelsNumber of channels in the input (reference) audio
numOutputChannelsNumber of channels in the output (observed) audio
maxBlockSizeFramesMaximum block size in frames (samples)

Implements MatcherBase< SampleType >.

Definition at line 100 of file hart_polarity_preserved.hpp.

◆ canOperatePerBlock()

template<typename SampleType >
bool canOperatePerBlock ( ) const
inlineoverridevirtual

Tells the host if it can operate on a block-by-block basis.

Some types of conditions absolutely require having a full piece of audio to produce an appropriate response. For example, hart::PeaksAt matcher. Those types of matchers will return false on this callback. Matcher is guaranteed to receive a full piece of audio if this callback has returned false. Otherwise, it may receive audio either block-by-block basis, or still get a full piece of audio, if the host decides to do so.

Implements MatcherBase< SampleType >.

Definition at line 112 of file hart_polarity_preserved.hpp.

◆ reset()

template<typename SampleType >
void reset ( )
inlineoverridevirtual

Resets the matcher to its initial state.

Reimplemented from MatcherBase< SampleType >.

Definition at line 117 of file hart_polarity_preserved.hpp.

◆ supportsChannelLayout()

template<typename SampleType >
bool supportsChannelLayout ( size_t  ,
size_t   
) const
inlineoverridevirtual

Tells the host whether this Matcher is capable of operating on audio with a specific number of channels.

It is guaranteed that the matcher will not receive unsupported number of channels in match(). This method is guaranteed to be called at least once before prepare()

Parameters
numInputChannelsNumber of channels in input (reference) buffer that will need to be processed
numOutputChannelsNumber of channels in output (observed) buffer that will need to be processed
Returns
true if signal is capable of processing audio with requested number of channels, false otherwise

Reimplemented from MatcherBase< SampleType >.

Definition at line 126 of file hart_polarity_preserved.hpp.

◆ match()

template<typename SampleType >
bool match ( AnalysisContext< SampleType >  analysisContext)
inlineoverridevirtual

Tells the host if the piece of audio satisfies Matcher's condition or not.

It is guaranteed to be called only after prepare(), or not be called at all. It is guaranteed to be handed a pair of AudioBuffers in line with values set by the last prepare() call. If canOperatePerBlock() has returned false, this callback is guaranteed to be handed a full piece of audio to check. Otherwise, it may still get a full piece of audio, or get data on a block-by-block basis.

Parameters
analysisContextCarries everything matcher should know about the input and output audio.
Returns
true if the audio satisfies the Matcher's condition, false otherwise

Reimplemented from MatcherBase< SampleType >.

Definition at line 131 of file hart_polarity_preserved.hpp.

◆ getFailureDetails()

template<typename SampleType >
MatcherFailureDetails getFailureDetails ( ) const
inlineoverridevirtual

Returns a description of why the match has failed.

It is guaranteed to be called strictly after calling match(), or not called at all

Note
This method is a callback for the test host, so you probably don't need to call it yourself ever. If you're making a custom matcher, use it to communicate the data with test host.
Return values
MatcherFailureDetails::frameIndex of frame at which the match has failed
MatcherFailureDetails::channelIndex of channel at which the failure was detected
MatcherFailureDetails::descriptionReadable description of why the match has failed. Do not include the value of observed frame value or its timing in the description, as well as any of values printed by represent(), as all of this will be added to the output anyway. Also, query hart::CLIConfig for number of displayed decimal places, whenever applicable.
See also
MatcherFailureDetails

Implements MatcherBase< SampleType >.

Definition at line 260 of file hart_polarity_preserved.hpp.

◆ represent()

template<typename SampleType >
void represent ( std::ostream &  stream) const
inlineoverridevirtual

Makes a text representation of this Matcher for test failure outputs.

It is strongly encouraged to follow python's repr() conventions for returned text - basically, put something like "MyClass(value1, value2)" (with no quotes) into the stream whenever possible, or "<Readable info in angled brackets>" otherwise. Also, use built-in stream manipulators like hart::dbPrecision wherever applicable. Use HART_DEFINE_GENERIC_REPRESENT() to get a basic implementation for this method.

Parameters
[out]streamOutput stream to write to

Implements MatcherBase< SampleType >.

Definition at line 285 of file hart_polarity_preserved.hpp.


The documentation for this class was generated from the following file: