Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Schmitt Trigger Block with Uncertainty Support and Optional Interpolation #476

Merged
merged 3 commits into from
Dec 5, 2024

Conversation

RalphSteinhagen
Copy link
Member

This PR introduces a new digitial SchmittTrigger block with configurable threshold and offset, as well as sub-sample edge timing interpolation options.

Features

  • Edge Detection: detects rising and falling edges based on user-defined threshold and offset values.
  • Uncertainty Support: Propagates uncertainties and provides precise edge timing with associated errors.
  • Interpolation Methods: Supports multiple methods for sub-sample precision:
    • NO_INTERPOLATION: basic detection without interpolation.
    • BASIC_LINEAR_INTERPOLATION: simple linear interpolation between adjacent samples.
    • LINEAR_INTERPOLATION: linear regression over the samples within the threshold window.
    • POLYNOMIAL_INTERPOLATION: (TODO) Placeholder for future implementation using Savitzky–Golay filters.
  • Trigger Generation: Generates customizable triggers (trigger_name_rising_edge, trigger_name_falling_edge)

image

FIXME and TODOs

  • FIXME: In the processBulk method, there's a commented-out section related to input sample buffering and history. This needs to be revisited to ensure proper operation, especially when using interpolation methods that require a history buffer. The error seems to be unrelated to the underlying algorithm and probably also chosen block implementation (tbc.). To be followed up in the GR4 if that is the case.

  • TODO: The POLYNOMIAL_INTERPOLATION method has not yet been implemented and requires the pending Tensor<T> and SVD implementations. This will be addressed in a future update.

Copy link
Member

@wirew0rm wirew0rm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to look into it but got stuck trying to get it to compile and it was not clear to me what the right direction to fix this would be.

But overall looks very nice, I suspect this was something that was changed last minute and missed a few parts of the code.

EDIT: after writing this I gave it one more quick try with more understanding and got it working. I also added fixup commits for the code formatting.

requires(std::is_arithmetic_v<T> or (UncertainValueLike<T> && std::is_arithmetic_v<meta::fundamental_base_value_type_t<T>>))
struct SchmittTrigger {
using value_t = meta::fundamental_base_value_type_t<T>;
using EdgeType = std::conditional_t<std::is_floating_point_v<value_t>, T, float>; // float being used for integer types
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not clear to me and seems like it causes inconsistent types:

T -> EdgeType

  • double -> double
  • int -> float` <= special case
  • UncertainValue<double> -> UncertainValue<double>
  • UncertainValue<int> -> ``float` !!! inconsistency? would have to be handled explicitly in the rest of the code which it looks like it isn't.

// edge timing variables
EdgeDetection lastEdge = EdgeDetection::NONE;
EdgeType lastEdgeIdx{1}; /// relative index [-EdgeType|_MAX,1[ w.r.t. current sample of the last detected edge
EdgeType lastEdgeOffset{0}; /// relative sub-sample offset [0, 1[ w.r.t. current sample of the last detected edge
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would mean that for integer datatype the position could only be 0 or 1?

Maybe we need an input and an output datatype?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is that -- normally it's:

  • float -> float
  • double -> double
  • UncertainValue<float> -> UncertainValue<float>
  • UncertainValue<double> -> UncertainValue<double>

The integer types are a bit special because while the vertical values are discrete, the time-offsets aren't, thus my choice to default to float or UncertainValue<float> for them. Was also temporarily contemplating keeping them all as float or UncertainValue<float> but this would complicate the downstream logic probably.

My goal was first to have a viable working solution and to see how it works out in the field.

@RalphSteinhagen RalphSteinhagen force-pushed the schmitt_trigger branch 4 times, most recently from 0043518 to 91f9731 Compare December 4, 2024 14:58
@RalphSteinhagen
Copy link
Member Author

fixed tag indexing problem and upgraded unit-tests to cover some more edge cases:

image

@RalphSteinhagen RalphSteinhagen marked this pull request as ready for review December 4, 2024 14:59
@RalphSteinhagen RalphSteinhagen force-pushed the schmitt_trigger branch 2 times, most recently from 14d6234 to f319c57 Compare December 4, 2024 15:08
Also:
* fixed '<=>' operator for when comparing with non-UncertainValue<T>
* fixed up-conversion warning in UncertainValue.hpp
* upgraded UncertainValue fmt-formatter to support number format specifiers

Signed-off-by: rstein <[email protected]>
Signed-off-by: Ralph J. Steinhagen <[email protected]>
Signed-off-by: Ralph J. Steinhagen <[email protected]>
implements a SchmittTrigger block with edge detection and optional interpolation

Signed-off-by: rstein <[email protected]>
Signed-off-by: Ralph J. Steinhagen <[email protected]>
Copy link

sonarqubecloud bot commented Dec 4, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
54.5% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

Copy link
Member

@wirew0rm wirew0rm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really nice, thanks for the nice implementation, documentation, tests and issue follow-up. 👍

@wirew0rm wirew0rm merged commit c8fe166 into main Dec 5, 2024
11 of 14 checks passed
@wirew0rm wirew0rm deleted the schmitt_trigger branch December 5, 2024 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants