# Calculate slope and plot it

A sensor is transmitting data serially. I can plot the data using Arduino and Processing. Now, I want to plot the slope of the waveform as well, i.e., I want two plots - waveform and its slope in the same window or may be in two windows together.

That sounds like a Processing problem rather than an Arduino problem. It would make little sense to do hard maths on an Arduino when you also have a PC available.

...R

Yes, I can understand. But in some cases, we are not left with much options.

Anyway, plotting is not important now, just some idea to find slope. Say, duration of getting sensor data is 10 sec. and sampling time is 4 msec.

Some idea might help.

DishaCU:
But in some cases, we are not left with much options.

From the very limited amount you have told us so far it seems to me that the option of doing it in Processing is readily available.

...R

Just rearrange y=mx+b to solve for m.

Actually, I need to find the peak of a waveform in the maximum slope region. For this, sample-to-sample slopes are to be found, then max slope region is to be defined. In that max slope region the point, where the slope becomes zero or becomes negative, corresponds to the peak.

If the peak is successfully detected, then the time at which that peak occurred is to be determined. That is all.

The sensor data is read for 10 sec, and I need to display the time of the peak whenever it occurs.

Please note: peak corresponding the highest amplitude might not always be the peak in the maximum slope region, so I could not use amplitude threshold to find peak.

aarg:
Just rearrange y=mx+b to solve for m.

In order to find m, I need these x values; this is exactly where I am getting confused. Y values are the sensor data, being sampled at 4 msec. But, how to detect at which time they are occurring. I tried to use millis() function, but could not get it worked properly.

DishaCU:
Please note: peak corresponding the highest amplitude might not always be the peak in the maximum slope region, so I could not use amplitude threshold to find peak.

Peak amplitude can't possibly have the maximum slope. Not for a continuous function.

But if you simply having trouble with getting time values, why didn't you just ask for it, instead of trotting out all this slope stuff? Post your code, and you will get some help with that.

May I rephrase your goal in one sentence? You want to differentiate a signal, and identify the points of peak amplitude of the result. Right?

What about sign? Do you count the negative peaks?

aarg:
May I rephrase your goal in one sentence? You want to differentiate a signal, and identify the points of peak amplitude of the result. Right?

I’m still waiting to learn why he does not do that within Processing where it would be much easier?

…R

aarg:
May I rephrase your goal in one sentence? You want to differentiate a signal, and identify the points of peak amplitude of the result. Right?

No, not the the peak amplitudes of the result; rather I want to identify the locations of the points in the result, where the slope becomes zero or negative from positive.

I attached a snap here. The top plot is the original signal and the bottom one is the 1st derivative plot. I encircled the peaks in red. The peaks occur at x=145 and x = 395. Corresponding slope values in the bottom plot is -120 and -248 respectively.

The objective is to identify these two locations with the help of the slope values only as I described above.

Hope, this helps.

Take any pair of readings. The difference in x divided by the difference in y is the slope. This is pre-algebra. m=y2-y1 / x2-X1

The slope is never going to be zero. Or almost never. All you can do is sense when it crosses zero. Given that you are looking for peaks, what you really want is when the slope changes from positive to negative.

But if you are looking for peaks, why not just look for peaks? Why all this “slope” business?

``````boolean goingUp;

void loop() {

if(goingUp) {
// we have found a peak value. Do something with it.
}
goingUp = false;
}
Thank you all 