How do I compare a current discrete value of a signal with a previous discrete value?

조회 수: 3 (최근 30일)
kat001
kat001 2018년 5월 27일
편집: TAB 2018년 5월 27일
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!

답변 (4개)

Ameer Hamza
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.

Image Analyst
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.

kat001
kat001 2018년 5월 27일
well, this is a real-time simulation case. If the value from the previous steo (the discrete signal which i mentioned earlier) is greater than current step value, then some position movement will take place towards a positive direction. If the previous value is less than current value, then the position will move towards a negative direction.
In either case, the output position will affect the input values, i.e. the discrete signal and the loop continues. So, thats pretty much how it's look like. Any idea?
  댓글 수: 1
Image Analyst
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
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.

카테고리

Help CenterFile Exchange에서 General Applications에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by