緩衝區只會釋放所有的值直到緩衝區關閉,這對於實時圖不是很有用。 您必須將值分成不重疊的窗口 - 從給定的觸發器開始,並在掃描條件完成時關閉 - 一個完整掃描週期的窗口。 不幸的是,窗口在啓動時仍會給我們值,所以我們將不得不跳過觸發器觸發前所有的值。
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,我已經把更多的細節放在主帖子 –