MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# How can I find the average pressure before a sharp dip?

Asked by David on 30 Jun 2012

Hello,

I have a pressure vs. time plot as shown. There is obviously quite a bit of noise in the signal. How could I find the average pressure (~30 psi) before the sharp dip? The only code I wrote so far is to plot the image here. It seems that standard deviation or the first derivative test could be used, but I have not found any examples similar to mine.

Thank you.

## Products

Answer by the cyclist on 30 Jun 2012
Edited by the cyclist on 30 Jun 2012

Is the drop always as markedly different from the level of noise? And does it happen in one big drop down? If so, then you could use

```>> [biggestDrop idx] = min(diff(pressure)); % Index will be to the point just before drop
```

to find the drop, and then

```>> meanBeforeDrop = mean(pressure(idx-9:idx)); % Averaging the prior ten points
```

If not, of course the problem will be more challenging. If that's the case, maybe post am image that is something akin to your worst-case scenario for identification.

## 1 Comment

David on 30 Jun 2012

Yes, the drop is always that pronounced. Thank you very much for your help!

Answer by Star Strider on 30 Jun 2012

It seems to me you first need to find the sharp dip. Although your data seem to be noisy, the magnitude of the sharp dip is sufficiently large to be noticable. There are a few ways of going about this in my experience. This stragegy is 'brute force' rather than in any way elegant, but it should do what you want.

First, you could try:

```datadif = diff([0; data]);
```

then search for the minimum using 'find'. It should correspond to the minimum, at least in this section of your data, so

```mindif = find(datadif <= min(datadif));
```

should find its index. (The initial zero in the 'diff' argument corrects for the index offset 'diff' creates and makes the 'datadif' indices comparable to those in 'data'.) An alternative is 'findpeaks' with two output arguments, the second being the indices of the peaks. You will need to play with the options to find the peak just before the sharp drop, rather than everything 'findpeaks' might consider a peak.

As for the average just before the sharp drop, my suggestion would be a linear regression, perhaps 'polyfit' followed by 'polyval' on a range of data just preceding the discontinuity. (Other regression function options will provide you with confidence limits on the parameter estimates and the fit, so you will have the approximate mean as well as the 95% confidence intervals, from which you can calculate the std, since the 95% c.i. are ±1.96*std.) The average just preceding the sharp drop would be given by 'polyfit' with the x-value corresponding to the index just preceding it. The linear regression assumes the errors are normally distributed.

There may be other, better options, but this is the sort of strategy I've successfully used in the past on similar problems. It should work.

## 1 Comment

David on 30 Jun 2012

Ok, I appreciate it. I was trying to use 'findpeaks' earlier, but it's proving rather difficult to get the options just right.