### Smoothing Window Data Averaging in Python – Moving Triangle Tecnique

Warning: This post is several years old and the author has marked it as poor quality (compared to more recent posts). It has been left intact for historical reasons, but but its content (and code) may be inaccurate or poorly written.

While I wrote a pervious post on linear data smoothing with python, those scripts were never fully polished. Fred (KJ4LFJ) asked me about this today and I felt bad I had nothing to send him. While I might add that the script below isn’t polished, at least it’s clean. I’ve been using this method for all of my smoothing recently. Funny enough, none of my code was clean enough to copy and paste, so I wrote this from scratch tonight. It’s a function to take a list in (any size) and smooth it with a triangle window (of any size, given by “degree”) and return the smoothed data with or without flanking copies of data to make it the identical length as before. The script also graphs the original data vs. smoothed traces of varying degrees. The output is below. I hope it helps whoever wants it!

```import numpy
import pylab

def smoothTriangle(data,degree,dropVals=False):
"""performs moving triangle smoothing with a variable degree."""
"""note that if dropVals is False, output length will be identical
to input length, but with copies of data at the flanking regions"""
triangle=numpy.array(range(degree)+[degree]+range(degree)[::-1])+1
smoothed=[]
for i in range(degree,len(data)-degree*2):
point=data[i:i+len(triangle)]*triangle
smoothed.append(sum(point)/sum(triangle))
if dropVals: return smoothed
smoothed=[smoothed]*(degree+degree/2)+smoothed
while len(smoothed)<len(data):smoothed.append(smoothed[-1])
return smoothed

### CREATE SOME DATA ###
data=numpy.random.random(100) #make 100 random numbers from 0-1
data=numpy.array(data*100,dtype=int) #make them integers from 1 to 100
for i in range(100):
data[i]=data[i]+i**((150-i)/80.0) #give it a funny trend

### GRAPH ORIGINAL/SMOOTHED DATA ###
pylab.plot(data,"k.-",label="original data",alpha=.3)
pylab.plot(smoothTriangle(data,3),"-",label="smoothed d=3")
pylab.plot(smoothTriangle(data,5),"-",label="smoothed d=5")
pylab.plot(smoothTriangle(data,10),"-",label="smoothed d=10")
pylab.title("Moving Triangle Smoothing")
pylab.grid(alpha=.3)
pylab.axis([20,80,50,300])
pylab.legend()
pylab.show()
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.7976356,"math_prob":0.90583813,"size":2290,"snap":"2020-10-2020-16","text_gpt3_token_len":566,"char_repetition_ratio":0.13648294,"word_repetition_ratio":0.0,"special_character_ratio":0.26069868,"punctuation_ratio":0.14686824,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9765629,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-04-05T00:01:59Z\",\"WARC-Record-ID\":\"<urn:uuid:6fb29949-4488-4848-b612-2d8e1c7a86cf>\",\"Content-Length\":\"27741\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7059e387-0677-46f3-9281-6443a4a19efd>\",\"WARC-Concurrent-To\":\"<urn:uuid:52ad0524-cbf0-4a5b-aa0c-ba18c27a22d9>\",\"WARC-IP-Address\":\"35.209.239.148\",\"WARC-Target-URI\":\"https://www.swharden.com/wp/2010-06-20-smoothing-window-data-averaging-in-python-moving-triangle-tecnique/\",\"WARC-Payload-Digest\":\"sha1:36SHXEGIMAFUOSTW2GWDKIXZ3LEO54YC\",\"WARC-Block-Digest\":\"sha1:6LJCDQI3U2TZRJAJQ7O7J7PHPBLM72SX\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-16/CC-MAIN-2020-16_segments_1585370526982.53_warc_CC-MAIN-20200404231315-20200405021315-00523.warc.gz\"}"}