Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Sliding Average...

Subject: Sliding Average...

From: Jeff Chang

Date: 2 Mar, 2002 23:49:26

Message: 1 of 5

I have about 200 sample of data and I would like to do a sliding average
that has the following equation:

v(x) = 1/(2w) * integral( v(x+s), s, -w, w)

Let w=5, and my "v" is following. Any hint? Thanks

v = [
  -19.3226
   -9.6244
  -12.9935
  -18.1143
  -10.2077
  -20.0648
  -15.1647
  -26.8062
  -29.6549
  -28.7824
  -45.9569
  -42.5600
  -34.1243
  -34.1460
  -40.3392
  -35.4074
  -42.6896
  -37.9118
  -36.1736
  -52.7753
  -34.9688
  -46.1975
  -51.9871
  -53.9275
  -44.8036
  -86.2719
  -63.1777
  -94.8795
  -96.7384
  -95.3163
 -105.6131
 -106.7652
  -69.2027
 -115.8485
  -46.9569
  -48.7307
  -64.9493
  -51.9624
  -78.2873
  -59.2995
  -71.9357
  -73.8701
  -54.5893
  -58.6826
  -60.9532
  -58.9130
  -57.0154
  -61.3514
  -60.0011
  -63.5041
  -58.5688
  -85.0214
  -61.0588
  -59.2794
  -88.2601
  -62.2670
  -67.4306
  -78.0824
  -71.6144
  -73.6477
  -79.1284
 -119.1761
 -120.4079
  -85.3370
 -128.9118
  -80.2018
  -88.1262
 -137.8103
 -139.1467
 -147.8504
  -81.3284
  -73.8779
  -74.3736
  -86.7915
  -82.8683
  -74.2880
  -84.0318
  -86.0432
 -100.3159
  -77.3279
  -83.9245
  -85.6280
  -91.3204
  -80.5998
 -109.2471
 -115.3906
 -106.6151
  -92.0482
 -137.3400
 -112.4414
  -94.0856
  -96.3754
 -120.7759
 -105.6595
 -131.9410
 -105.7931
 -116.0302
  -99.2430
  -85.9910
 -146.1293
 -122.9166
  -96.2863
 -108.9542
 -105.7629
 -110.9460
 -108.7697
 -104.2662
 -110.5805
 -112.7226
 -123.1096
 -114.5650
 -111.5323
 -116.1920
 -136.3711
 -109.5839
 -107.9488
 -126.6724
 -112.9646
 -115.7318
 -118.6013
 -125.1629
 -133.8633
 -139.0532
 -139.6156
 -122.6142
 -129.4178
 -121.2913
 -125.4783
 -126.8083
 -129.2445
 -141.0724
 -143.9918
 -136.6367
 -150.7817
 -144.7754
 -139.5840
 -143.3107
 -123.6834
 -136.9615
 -132.3972
 -153.5231
 -121.2382
 -133.2464
 -138.9549
 -135.2419
 -137.4144
 -142.5602
 -143.3816
 -136.1362
 -145.7073
 -135.8910
 -146.1412
 -142.7636
 -127.7440
 -143.0150
 -152.4818
 -161.0198
 -159.3609
 -158.4433]

Subject: Sliding Average...

From: John D'Errico

Date: 3 Mar, 2002 07:56:26

Message: 2 of 5

In article <u83aqoda8ag74b@corp.supernews.com>, "Jeff Chang"
<pchang@chartermi.net> wrote:

> I have about 200 sample of data and I would like to do a sliding average
> that has the following equation:
>
> v(x) = 1/(2w) * integral( v(x+s), s, -w, w)
>
> Let w=5, and my "v" is following. Any hint? Thanks
>
> v = [
> -19.3226 .....

A hint?

Well, since you did not define how you wanted to
approximate the integral, I'll presume the trapezoidal
rule.

My hint is to use filter. You can figure out the
appropriate coefficients by looking at what the
trapezoidal rule coefficients would be for your
problem. (Hey, you only asked for a hint.)

Why do I suggest trapezoidal rule? Certainly the
same solution would work had I suggested you use
Simpson's rule. The answer is that you have data.
In the presence of noise, I'd suggest that a
Simpson's rule will amplify that noise more so
than a trapezoidal rule, so the extra accuracy
may be lost in the amplified noise. Obviously
this depends on the signal to noise ratio in your
problem, but since you are trying to do smoothing,
you also obviously have too much noise in your data.

I might have responded by suggesting integrating
a spline fit, but this will also be a noise
amplifier, and it would take some work on your
part or on my part, and its Sunday morning and
I don't want to think too hard right now. (Ok,
it can be done with filter if you only want to
use a C1 cubic interpolant. A true interpolating
spline would probably not work with filter.)

Finally, you might ask whether the use of rectangle
rule for the integration is even less of an amplifier
of noise. The answer is the variance of a trapezoidal
rule estimator would be marginally higher (this
depends on the size of w) but the higher accuracy
may warrant its use.

Hope this helps,
John D'Errico

--

Subject: Sliding Average...

From: Peter Boettcher

Date: 4 Mar, 2002 09:44:07

Message: 3 of 5

"Jeff Chang" <pchang@chartermi.net> writes:

> I have about 200 sample of data and I would like to do a sliding average
> that has the following equation:
>
> v(x) = 1/(2w) * integral( v(x+s), s, -w, w)
>
> Let w=5, and my "v" is following. Any hint? Thanks
>
> v = [
> -19.3226
> -9.6244
> -12.9935
> [snip]

Do you mean a summation? If v is discrete, an integral is tough.
Assuming you mean a sliding average something like y(x) = sum(v(x+k), k=0:5),
just use

y = filter(ones(1,w)/w, 1, v)

Does this give you what you're looking for?


--
Peter Boettcher <boettcher@ll.mit.edu>
MIT Lincoln Laboratory
MATLAB FAQ: http://www.mit.edu/~pwb/cssm/

Subject: Sliding Average...

From: Jeff Chang

Date: 5 Mar, 2002 12:56:18

Message: 4 of 5

I just know how to do the following case (4-point averager), where the
output just depend on the pass input.
x = [1 3 2 4 5 4 3 2]
y[n] = ( x[n] + x[n-1] + x[n-2] + x[n-3] ) / (4)

Solution [1]:
y = filter( ones(1,4)/4, x )


But I think the equation I provided was a bit different (correct me if I'm
wrong). Do you think this equation y(n) = 1/(2w) * integral( v(n+s), s, -w,
w), has the following equivalent representation?

For w = 3,
y[n] = ( x[n+3] + x[n+2] + x[n+1] + x[n] + x[n-1] + x[n-2] + x[n-3] ) /
(2*3+1) ???


If I were used the approach in solution [1], my output will be shifted a
little bit, where the peaks and nulls of the signal will be shifted to the
right.

Thanks



"Peter Boettcher" <boettcher@ll.mit.edu> wrote in message
news:tr7kosmlzc.fsf@coyote.llan.ll.mit.edu...
> "Jeff Chang" <pchang@chartermi.net> writes:
>
> > I have about 200 sample of data and I would like to do a sliding average
> > that has the following equation:
> >
> > v(x) = 1/(2w) * integral( v(x+s), s, -w, w)
> >
> > Let w=5, and my "v" is following. Any hint? Thanks
> >
> > v = [
> > -19.3226
> > -9.6244
> > -12.9935
> > [snip]
>
> Do you mean a summation? If v is discrete, an integral is tough.
> Assuming you mean a sliding average something like y(x) = sum(v(x+k),
k=0:5),
> just use
>
> y = filter(ones(1,w)/w, 1, v)
>
> Does this give you what you're looking for?
>
>
> --
> Peter Boettcher <boettcher@ll.mit.edu>
> MIT Lincoln Laboratory
> MATLAB FAQ: http://www.mit.edu/~pwb/cssm/

Subject: Sliding Average...

From: Peter Boettcher

Date: 5 Mar, 2002 13:44:44

Message: 5 of 5

"Jeff Chang" <pchang@chartermi.net> writes:

> I just know how to do the following case (4-point averager), where the
> output just depend on the pass input.
> x = [1 3 2 4 5 4 3 2]
> y[n] = ( x[n] + x[n-1] + x[n-2] + x[n-3] ) / (4)
>
> Solution [1]:
> y = filter( ones(1,4)/4, x )

Right.

> But I think the equation I provided was a bit different (correct me if I'm
> wrong). Do you think this equation y(n) = 1/(2w) * integral( v(n+s), s, -w,
> w), has the following equivalent representation?
>
> For w = 3,
> y[n] = ( x[n+3] + x[n+2] + x[n+1] + x[n] + x[n-1] + x[n-2] + x[n-3] ) /
> (2*3+1) ???

I still claim that if v is a vector, the integral of v(n) doesn't make
any sense. From a signal processing point of view, the closest
discrete version is probably what you have written. If you are really
trying to compute the integral of a continuous function using a
discrete approximation, you probably want something like a trapezoid
approximation. Assuming you just want the sliding average of a
vector, use the equation you typed above.

> If I were used the approach in solution [1], my output will be shifted a
> little bit, where the peaks and nulls of the signal will be shifted to the
> right.

Well, yes. The filter is non-causal, so you shift it so that it is
causal and you can use [1], then shift it back afterward. After you
do the filter, just say something like: ynew = y(w:end). Am I missing
something?

-PB


>
> "Peter Boettcher" <boettcher@ll.mit.edu> wrote in message
> news:tr7kosmlzc.fsf@coyote.llan.ll.mit.edu...
>> "Jeff Chang" <pchang@chartermi.net> writes:
>>
>> > I have about 200 sample of data and I would like to do a sliding average
>> > that has the following equation:
>> >
>> > v(x) = 1/(2w) * integral( v(x+s), s, -w, w)
>> >
>> > Let w=5, and my "v" is following. Any hint? Thanks
>> >
>> > v = [
>> > -19.3226
>> > -9.6244
>> > -12.9935
>> > [snip]
>>
>> Do you mean a summation? If v is discrete, an integral is tough.
>> Assuming you mean a sliding average something like y(x) = sum(v(x+k),
> k=0:5),
>> just use
>>
>> y = filter(ones(1,w)/w, 1, v)
>>
>> Does this give you what you're looking for?

--
Peter Boettcher <boettcher@ll.mit.edu>
MIT Lincoln Laboratory
MATLAB FAQ: http://www.mit.edu/~pwb/cssm/

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us