How do I compare a current discrete value of a signal with a previous discrete value?
3 次查看(过去 30 天)
显示 更早的评论
Hi, How do I compare the current discrete value with a previous discrete value? It would be nice to see a simple matlab coded example, if possible. Is there a way to do that in simulink? Thanks!
0 个评论
回答(4 个)
Ameer Hamza
2018-5-27
Here is an example
x = [1 4 2 6 2 4];
y = diff(x)
y =
3 -2 4 -4 2
diff() will subtract every value with the previous value. If the value given by diff() is greater then 0, then it means that the current value is larger than the previous value, similarly, a negative value means current value is smaller then previous.
0 个评论
Image Analyst
2018-5-27
To compare elements of a signal with the elements immediately prior to them use diff(), as Ameer showed. To compare a whole signal to a later whole signal, then you'll have to save your original signal so it can be subtracted later:
oldSignal = signal; % Save a copy of the current signal.
% Now the signal changes somehow... Now check the difference:
differenceSignal = signal - oldSignal;
That's assuming that the "comparison" you want to do is a simple subtraction and not something else, like exclusive OR or spectral change or whatever.
0 个评论
kat001
2018-5-27
1 个评论
Image Analyst
2018-5-27
Yes, I gave you an idea. The code I gave will work for that case. Basically save your current value as the "old" value, then when you get a new value in subtract the old value from it and redefine the old value as the current value.
TAB
2018-5-27
编辑:TAB
2018-5-27
In Simulink you can use Unit delay block to extract previous step value and Relational Operator to compare.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 General Applications 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!