×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Are you an
Engineering professional?
Join Eng-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Spencer moving averages2

## Spencer moving averages

(OP)
I've just been idly implementing moving averages in Octave, and came across Spencer's moving averages
spencer15=[-3, -6, -5, 3, 21, 46, 67, 74, 67, 46, 21, 3, -5, -6, -3];
spencer21=[-1 -3 -5 -5 -2 6 18 33 47 57 60 57 47 33 18 6 -2 -5 -5 -3 -1];

[ms,z]= filter(spencer21,sum(spencer21),cet);

Just recently we had a question from someone who wanted to use some fancy pants method for determining optima, which seemed quite similar.

Wolfram has this to say

http://mathworld.wolfram.com/SpencersFormula.html

But I have not been able to find a hand wavy description of what exactly spencer was hoping to achieve. FWIW on real data spencer15 seems about equivalent to a simple moving average with period 7.

Does anybody have a handwavy description?

Cheers

Greg Locock

New here? Try reading these, they might help FAQ731-376: Eng-Tips.com Forum Policies http://eng-tips.com/market.cfm?

### RE: Spencer moving averages

There's a little bit of hand-waviness here:

http://interstat.statjournals.net/YEAR/2002/articl...

But it's not well explained, and I'm not sure if I follow their notation correctly. But in the discussion on page 6, they mention

"It deserves to note that Anderson (1971) is pioneer in linking the degree of the polynomial assumed to estimate the trend with the number of terms operating in the moving average formula. In other words (mine), he linked the number of terms (n) used in the average to a polynomial of order (n? n-1?) used to fit the data. Might be interesting to read the original paper to figure out what this really means.

They note that Anderson used a least-squares method to find the best fit coefficients for the polynomial. The coefficient matrix they then give looks somewhat like the sequence you show for Spencer's formula, if I'm parsing their notation correctly.

The Wikipedia article for moving averages notes that the French version of the article gives spectral response formula for the filtering done by various moving average weighting schemes; that would be interesting to look at (and compare Spencer's formula to). I'm not sure how to formulate the problem, nor do I hablo Frankish.

With a nod and one last hand-wave, good luck Greg.

### RE: Spencer moving averages

I thought I replied yesterday, but...

Page 2 of http://www.stat.berkeley.edu/~aditya/Site/Statisti... says that these equations are specifically for leaving polynomial functions "untouched"

TTFN
FAQ731-376: Eng-Tips.com Forum Policies

Need help writing a question or understanding a reply? forum1529: Translation Assistance for Engineers

Of course I can. I can do anything. I can do absolutely anything. I'm an expert!

### RE: Spencer moving averages

(OP)
Thanks, that last link was just the ticket, it leaves low order polynomials (N<4) 'untouched', that is it is essentially a low pass filter. I'll have a play with it and see what its frequency response is like.

Cheers

Greg Locock

New here? Try reading these, they might help FAQ731-376: Eng-Tips.com Forum Policies http://eng-tips.com/market.cfm?

### RE: Spencer moving averages

Would be interested in any results you get for freq. response Greg.

### RE: Spencer moving averages

(OP)
Here's the frequency response for a 1024 sample length

So the Spencer filter shape does a good job of preserving the low order amplitudes and getting rid of the high frequency stuff.

It's certainly a lot better than a simple moving average, with a faster roll off, and suppressed side lobes.

Cheers

Greg Locock

New here? Try reading these, they might help FAQ731-376: Eng-Tips.com Forum Policies http://eng-tips.com/market.cfm?

### RE: Spencer moving averages

Thanks for that, Greg.

I may not be reading it right (you plotted output vs. frequency, or is it versus period?), or I'm just too tired and fuzzy about the term "rolloff" - you say faster rolloff for the Spencer filter, but it looks like (comparing Spencer 21 to a 21-pt. moving average, which is what I think the two plot lines mean, i.e. same number of terms used) that the plain jane method rolls off more quickly, just that it has more sidelobe "fuzziness" at higher frequencies? I like the idea of the smoother rolloff (lack of sidelobes, if you will) of the Spencer function, it would seem a better averaging technique in that light, as it would tend to smooth noisy data more like how we humans would fair a line through such.

### RE: Spencer moving averages

Just realized, this would be interesting to people doing FFT analysis, i.e. using Hamming windows...the wiki article for those shows the spectral response of various windowing schemes (all of which show a lot of sidelobe fuzz), and there is some discussion of it going on in thread248-370129: What is a "Humming Window" (Digital Image or Speach Processing ?) (though the OP keeps talking about Humming windows...)

### RE: Spencer moving averages

(OP)
It is plotted versus bin number, ie frequency.

The roll off is the gradient at around a gain of 0.5, as you say the 21 MA has a steeper rolloff, but there again it has virtually 0 passband (the level bit with a gain of 1)

You could use a spencer shaped curve instead of Hanning, but people have been looking at this stuff for centuries, the known window shapes already give you a wide range of tradeoffs to choose from. I haven't seen an anti leakage window with negative values before, that does seem rather unhelpful.

Cheers

Greg Locock

New here? Try reading these, they might help FAQ731-376: Eng-Tips.com Forum Policies http://eng-tips.com/market.cfm?

### RE: Spencer moving averages

I would guess that you should be comparing the 21-pt to 21-pt, which shows a higher bandwidth for the Spencer, which probably translates into less phase error.

TTFN
FAQ731-376: Eng-Tips.com Forum Policies

Need help writing a question or understanding a reply? forum1529: Translation Assistance for Engineers

Of course I can. I can do anything. I can do absolutely anything. I'm an expert!

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Close Box

# Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!