缓冲区只会释放所有的值直到缓冲区关闭,这对于实时图不是很有用。 您必须将值分成不重叠的窗口 - 从给定的触发器开始,并在扫描条件完成时关闭 - 一个完整扫描周期的窗口。 不幸的是,窗口在启动时仍会给我们值,所以我们将不得不跳过触发器触发前所有的值。
static IObservable<IObservable<T>> TriggeredSweep<T>(
this IObservable<T> source,
Func<T, bool> triggerCondition,
Func<T, bool> sweepEnd
)
{
source = source.Publish().RefCount();
return source.Window(() => source.Where(triggerCondition).Sample(source.Where(sweepEnd)))
.Select(s => s.SkipWhile(v => !triggerCondition(v)));
}
测试了这一点,最好的办法是在其上这个的前提是非常示波器型号:
double period = 1000/0.5; //0.5 Hz
int cycles = 4; //cycles to display
int quantization = 100; //cycles to display
int amplitude = 10; //signal peak
int range = quantization * cycles; //full range
//Sine wave generator for n cycles
//makes tuple of (t, sin(t))
var source = Observable.Interval(TimeSpan.FromMilliseconds(period/range))
.Select(s => s % (range + 1))
.Select(s => Tuple.Create(s, amplitude * Math.Sin((double)s/((double)range/(double)cycles) * 2 * Math.PI)));
source.TriggeredSweep(
value => value.Item2 > 5, //Trigger when Signal value > 5
value => value.Item1/quantization >= cycles //end sweep when all cycles are done
)
.Subscribe(window =>
{
Console.Clear(); //Clear CRO Monitor
window.Subscribe(value =>
{
//Set (x, y)
Console.CursorLeft = (int)((double)value.Item1/range * (Console.WindowWidth - 1));
Console.CursorTop = (int)((amplitude - value.Item2)/(2 * amplitude) * (Console.WindowHeight - 1));
//draw
Console.Write("x");
});
});
//prevent close
Console.ReadLine();
输出:
xxx xxxx xxx xxxx
xx x x x xx x x x
x x xx x x x xx x
xx x x x xx x x x
x x xx x x x xx x
x x x x x x x x
x x x x xx x x x
x x x x x x x
x x x x x x x
x x x x x x x
x x xx x x x xx
x x x x x x x
x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
xx x x x xx x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x xx x x x xx
x x x x x x x x
x xx x x x xx x x
x x x xx x x x xx
x xx x x x xx x x
x x x xx x x x xx
xxxx xxx xxxx xxx
x x x x
我希望这些代码可能是有用的使用Rx测试简单的信号处理功能。 :)
我想你需要解释一些关于你的问题/要求。 “阈”? “集”? etc .. – Enigmativity
我有一个IObservable流从一个COM端口 - 我从巴特代斯梅特代码示例获得的serialData –
对不起,Enigmativity,我已经把更多的细节放在主帖子 –