-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ support interpolated pulse shape (#31)
close #26
- Loading branch information
Showing
5 changed files
with
77 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using MessagePack; | ||
|
||
namespace Qynit.PulseGen.Server; | ||
|
||
[MessagePackObject] | ||
public sealed record InterpolatedShapeInfo( | ||
[property: Key(0)] double[] X, | ||
[property: Key(1)] double[] Y) : ShapeInfo | ||
{ | ||
private InterpolatedPulseShape? _pulseShape; | ||
public override IPulseShape GetPulseShape() | ||
{ | ||
return _pulseShape ??= InterpolatedPulseShape.CreateFromXY(X, Y); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System.Numerics; | ||
|
||
using BitFaster.Caching.Lru; | ||
|
||
using CommunityToolkit.Diagnostics; | ||
|
||
using MathNet.Numerics; | ||
using MathNet.Numerics.Interpolation; | ||
|
||
namespace Qynit.PulseGen; | ||
|
||
public sealed record InterpolatedPulseShape(IInterpolation Interpolation) : IPulseShape | ||
{ | ||
public IqPair<T> SampleAt<T>(T x) where T : unmanaged, IFloatingPointIeee754<T> | ||
{ | ||
var half = T.CreateChecked(0.5); | ||
return (x >= -half && x <= half) | ||
? T.CreateChecked(Interpolation.Interpolate(double.CreateChecked(x))) | ||
: T.Zero; | ||
} | ||
|
||
public static InterpolatedPulseShape CreateFromXY(IReadOnlyList<double> x, IReadOnlyList<double> y) | ||
{ | ||
if (x.Count != y.Count) | ||
{ | ||
ThrowHelper.ThrowArgumentException("x and y must have the same length."); | ||
} | ||
var key = (new ValueArray<double>(x), new ValueArray<double>(y)); | ||
return Cache.GetOrAdd(key, k => | ||
{ | ||
var interpolation = Interpolate.RationalWithoutPoles(x, y); | ||
return new InterpolatedPulseShape(interpolation); | ||
}); | ||
} | ||
|
||
private readonly record struct ValueArray<T> | ||
{ | ||
public T[] Data { get; init; } | ||
public ValueArray(IEnumerable<T> values) | ||
{ | ||
Data = values.ToArray(); | ||
} | ||
public bool Equals(ValueArray<T> other) | ||
{ | ||
return Data.SequenceEqual(other.Data); | ||
} | ||
public override int GetHashCode() | ||
{ | ||
var hash = new HashCode(); | ||
foreach (var item in Data) | ||
{ | ||
hash.Add(item); | ||
} | ||
return hash.ToHashCode(); | ||
} | ||
} | ||
|
||
private static readonly FastConcurrentLru<(ValueArray<double>, ValueArray<double>), InterpolatedPulseShape> Cache = new(666); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters