The personal website of Scott W Harden
June 20th, 2010

Smoothing Window Data Averaging in Python - Moving Triangle Tecnique

⚠️ SEE UPDATED POST: Signal Filtering in Python

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[0]]*(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()
Markdown source code last modified on January 18th, 2021
---
title: Smoothing Window Data Averaging in Python - Moving Triangle Tecnique
date: 2010-06-20 22:12:03
tags: python, old
---

# Smoothing Window Data Averaging in Python - Moving Triangle Tecnique

> **⚠️ SEE UPDATED POST:** [**Signal Filtering in Python**](https://swharden.com/blog/2020-09-23-signal-filtering-in-python/)

__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!

<div class="text-center">

[![](moving-triangle-python-data-smoothing_thumb.jpg)](moving-triangle-python-data-smoothing.png)

</div>

```python
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[0]]*(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()

```

June 19th, 2010

Simple Python Spectrograph with PyGame

While thinking of ways to improve my QRSS VD high-definitions spectrograph software, I often wish I had a better way to display large spectrographs. Currently I'm using PIL (the Python Imaging Library) with TK and it's slow as heck. I looked into the PyGame project, and it seems to be designed with speed in mind. I whipped-up this quick demo, and it's a simple case audio spectrograph which takes in audio from your sound card and graphs it time vs. frequency. This method is far superior to the method I was using previously to display the data, because while QRSS VD can only update the entire GUI (500px by 8,000 px) every 3 seconds, early tests with PyGame suggests it can do it about 20 times a second (wow!). With less time/CPU going into the GUI, the program can be more responsivle and my software can be less of a drain.

import pygame
import numpy
import threading
import pyaudio
import scipy
import scipy.fftpack
import scipy.io.wavfile
import wave
rate = 12000  # try 5000 for HD data, 48000 for realtime
soundcard = 2
windowWidth = 500
fftsize = 512
currentCol = 0
scooter = []
overlap = 5  # 1 for raw, realtime - 8 or 16 for high-definition


def graphFFT(pcm):
    global currentCol, data
    ffty = scipy.fftpack.fft(pcm)  # convert WAV to FFT
    ffty = abs(ffty[0:len(ffty)/2])/500  # FFT is mirror-imaged
    # ffty=(scipy.log(ffty))*30-50 # if you want uniform data
    print "MIN:t%stMAX:t%s" % (min(ffty), max(ffty))
    for i in range(len(ffty)):
        if ffty[i] < 0:
            ffty[i] = 0
        if ffty[i] > 255:
            ffty[i] = 255
    scooter.append(ffty)
    if len(scooter) < 6:
        return
    scooter.pop(0)
    ffty = (scooter[0]+scooter[1]*2+scooter[2]*3+scooter[3]*2+scooter[4])/9
    data = numpy.roll(data, -1, 0)
    data[-1] = ffty[::-1]
    currentCol += 1
    if currentCol == windowWidth:
        currentCol = 0


def record():
    p = pyaudio.PyAudio()
    inStream = p.open(format=pyaudio.paInt16, channels=1, rate=rate,
                      input_device_index=soundcard, input=True)
    linear = [0]*fftsize
    while True:
        linear = linear[fftsize/overlap:]
        pcm = numpy.fromstring(inStream.read(
            fftsize/overlap), dtype=numpy.int16)
        linear = numpy.append(linear, pcm)
        graphFFT(linear)


pal = [(max((x-128)*2, 0), x, min(x*2, 255)) for x in xrange(256)]
print max(pal), min(pal)
data = numpy.array(numpy.zeros((windowWidth, fftsize/2)), dtype=int)
# data=Numeric.array(data) # for older PyGame that requires Numeric
pygame.init()  # crank up PyGame
pygame.display.set_caption("Simple Spectrograph")
screen = pygame.display.set_mode((windowWidth, fftsize/2))
world = pygame.Surface((windowWidth, fftsize/2), depth=8)  # MAIN SURFACE
world.set_palette(pal)
t_rec = threading.Thread(target=record)  # make thread for record()
t_rec.daemon = True  # daemon mode forces thread to quit with program
t_rec.start()  # launch thread
clk = pygame.time.Clock()
while 1:
    for event in pygame.event.get():  # check if we need to exit
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    pygame.surfarray.blit_array(world, data)  # place data in window
    screen.blit(world, (0, 0))
    pygame.display.flip()  # RENDER WINDOW
    clk.tick(30)  # limit to 30FPS
Markdown source code last modified on January 18th, 2021
---
title: Simple Python Spectrograph with PyGame
date: 2010-06-19 21:53:25
tags: python, old
---

# Simple Python Spectrograph with PyGame

<b style="font-size: inherit;">While thinking of ways to improve my QRSS VD high-definitions spectrograph software,</b><span style="font-size: inherit;"> I often wish I had a better way to display large spectrographs. Currently I'm using PIL (the Python Imaging Library) with TK and it's slow as heck. I looked into the </span><a href="http://www.pygame.org" style="font-size: inherit;">PyGame</a><span style="font-size: inherit;"> project, and it seems to be designed with speed in mind. I whipped-up this quick demo, and it's a simple case audio spectrograph which takes in audio from your sound card and graphs it time vs. frequency. This method is far superior to the method I was using previously to display the data, because while QRSS VD can only update the entire GUI (500px by 8,000 px) every 3 seconds, early tests with PyGame suggests it can do it about 20 times a second (wow!). With less time/CPU going into the GUI, the program can be more responsivle and my software can be less of a drain.</span>

<div class="text-center img-border">

[![](simple-spectrograph_thumb.jpg)](simple-spectrograph.png)

</div>

</div>

```python
import pygame
import numpy
import threading
import pyaudio
import scipy
import scipy.fftpack
import scipy.io.wavfile
import wave
rate = 12000  # try 5000 for HD data, 48000 for realtime
soundcard = 2
windowWidth = 500
fftsize = 512
currentCol = 0
scooter = []
overlap = 5  # 1 for raw, realtime - 8 or 16 for high-definition


def graphFFT(pcm):
    global currentCol, data
    ffty = scipy.fftpack.fft(pcm)  # convert WAV to FFT
    ffty = abs(ffty[0:len(ffty)/2])/500  # FFT is mirror-imaged
    # ffty=(scipy.log(ffty))*30-50 # if you want uniform data
    print "MIN:t%stMAX:t%s" % (min(ffty), max(ffty))
    for i in range(len(ffty)):
        if ffty[i] < 0:
            ffty[i] = 0
        if ffty[i] > 255:
            ffty[i] = 255
    scooter.append(ffty)
    if len(scooter) < 6:
        return
    scooter.pop(0)
    ffty = (scooter[0]+scooter[1]*2+scooter[2]*3+scooter[3]*2+scooter[4])/9
    data = numpy.roll(data, -1, 0)
    data[-1] = ffty[::-1]
    currentCol += 1
    if currentCol == windowWidth:
        currentCol = 0


def record():
    p = pyaudio.PyAudio()
    inStream = p.open(format=pyaudio.paInt16, channels=1, rate=rate,
                      input_device_index=soundcard, input=True)
    linear = [0]*fftsize
    while True:
        linear = linear[fftsize/overlap:]
        pcm = numpy.fromstring(inStream.read(
            fftsize/overlap), dtype=numpy.int16)
        linear = numpy.append(linear, pcm)
        graphFFT(linear)


pal = [(max((x-128)*2, 0), x, min(x*2, 255)) for x in xrange(256)]
print max(pal), min(pal)
data = numpy.array(numpy.zeros((windowWidth, fftsize/2)), dtype=int)
# data=Numeric.array(data) # for older PyGame that requires Numeric
pygame.init()  # crank up PyGame
pygame.display.set_caption("Simple Spectrograph")
screen = pygame.display.set_mode((windowWidth, fftsize/2))
world = pygame.Surface((windowWidth, fftsize/2), depth=8)  # MAIN SURFACE
world.set_palette(pal)
t_rec = threading.Thread(target=record)  # make thread for record()
t_rec.daemon = True  # daemon mode forces thread to quit with program
t_rec.start()  # launch thread
clk = pygame.time.Clock()
while 1:
    for event in pygame.event.get():  # check if we need to exit
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    pygame.surfarray.blit_array(world, data)  # place data in window
    screen.blit(world, (0, 0))
    pygame.display.flip()  # RENDER WINDOW
    clk.tick(30)  # limit to 30FPS
```

June 15th, 2010

Simple-Case PyGame Example

I'm starting to investigate PyGame as an alternative to PIL and K for my QRSS VD spectrograph project. This sample code makes a box bounce around a window.

import pygame, sys
pygame.init() #load pygame modules
size = width, height = 320, 240 #size of window
speed = [2, 2] #speed and direction
screen = pygame.display.set_mode(size) #make window
s=pygame.Surface((100,50)) #create surface 100px by 50px
s.fill((33,66,99)) #color the surface blue
r=s.get_rect() #get the rectangle bounds for the surface
clock=pygame.time.Clock() #make a clock
while 1: #infinite loop
        clock.tick(30) #limit framerate to 30 FPS
        for event in pygame.event.get(): #if something clicked
                if event.type == pygame.QUIT: #if EXIT clicked
                        sys.exit() #close cleanly
        r=r.move(speed) #move the box by the "speed" coordinates
        #if we hit a  wall, change direction
        if r.left < 0 or r.right > width: speed[0] = -speed[0]
        if r.top < 0 or r.bottom > height: speed[1] = -speed[1]
        screen.fill((0,0,0)) #make redraw background black
        screen.blit(s,r) #render the surface into the rectangle
        pygame.display.flip() #update the screen
Markdown source code last modified on January 18th, 2021
---
title: Simple-Case PyGame Example
date: 2010-06-15 08:29:03
tags: python, old
---

# Simple-Case PyGame Example

__I'm starting to investigate PyGame__ as an alternative to PIL and K for my QRSS VD spectrograph project. This sample code makes a box bounce around a window.

<div class="text-center img-border">

[![](example_pygame_thumb.jpg)](example_pygame.png)

</div>

```python
import pygame, sys
pygame.init() #load pygame modules
size = width, height = 320, 240 #size of window
speed = [2, 2] #speed and direction
screen = pygame.display.set_mode(size) #make window
s=pygame.Surface((100,50)) #create surface 100px by 50px
s.fill((33,66,99)) #color the surface blue
r=s.get_rect() #get the rectangle bounds for the surface
clock=pygame.time.Clock() #make a clock
while 1: #infinite loop
        clock.tick(30) #limit framerate to 30 FPS
        for event in pygame.event.get(): #if something clicked
                if event.type == pygame.QUIT: #if EXIT clicked
                        sys.exit() #close cleanly
        r=r.move(speed) #move the box by the "speed" coordinates
        #if we hit a  wall, change direction
        if r.left < 0 or r.right > width: speed[0] = -speed[0]
        if r.top < 0 or r.bottom > height: speed[1] = -speed[1]
        screen.fill((0,0,0)) #make redraw background black
        screen.blit(s,r) #render the surface into the rectangle
        pygame.display.flip() #update the screen
```
June 9th, 2010

Minimalist Radio Receiver

Now that my minimalist QRSS transmitter is mostly functional, I'm shifting gears toward building a minimalist receiver. These are some early tests, but I'm amazed I managed to hack something together that actually works! Once it's finished I'll post schematics. For now, here are some photos. This receiver is based upon an SA602 and although there *IS* an op-amp on the board, I actually bypassed it completely! The SA602 seems to put out enough juice to make my PC microphone jack happy, and those cheap op-amps are noisy anyway, so awesome! Go minimalism!

Here's the output from 7.040 MHz. Conditions are pretty bad right now, and I'm at my apartment using my crazy indoor antenna

Markdown source code last modified on January 18th, 2021
---
title: Minimalist Radio Receiver
date: 2010-06-09 23:42:00
tags: qrss, amateur radio, old
---

# Minimalist Radio Receiver

__Now that my minimalist QRSS transmitter is mostly functional, I'm shifting gears toward building a minimalist receiver.__ These are some early tests, but I'm amazed I managed to hack something together that actually works! Once it's finished I'll post schematics. For now, here are some photos. This receiver is based upon an SA602 and although there \*IS\* an op-amp on the board, I actually bypassed it completely! The SA602 seems to put out enough juice to make my PC microphone jack happy, and those cheap op-amps are noisy anyway, so awesome! Go minimalism!

<div class="text-center img-border img-medium">

[![](DSCN0832_thumb.jpg)](DSCN0832.jpg)

[![](DSCN0833_thumb.jpg)](DSCN0833.jpg)

</div>

__Here's the output from 7.040 MHz.__ Conditions are pretty bad right now, and I'm at my apartment using my crazy indoor antenna

<div class="text-center img-border img-medium">

[![](recvbig_thumb.jpg)](recvbig.jpg)

</div>

June 7th, 2010

QRSS VD Image Assembler

This minimal Python script will convert a directory filled with tiny image captures such as this into gorgeous montages as seen below! I whipped-up this script tonight because I wanted to assess the regularity of my transmitter's embarrassing drift. I hope you find it useful.

import os
from PIL import Image

x1,y1,x2,y2=[0,0,800,534] #crop from (x,y) 0,0 to 800x534
squish=10 #how much to squish it horizontally

### LOAD LIST OF FILES ###
workwith=[]
for fname in os.listdir('./'):
    if ".jpg" in fname and not "assembled" in fname:
        workwith.append(fname)
workwith.sort()

### MAKE NEW IMAGE ###
im=Image.new("RGB",(x2*len(workwith),y2))
for i in range(len(workwith)):
    print "Loading",workwith[i]
    im2=Image.open(workwith[i])
    im2=im2.crop((x1,y1,x2,y2))
    im.paste(im2,(i*x2,0))
print "saving BIG image"
im.save("assembled.jpg")
print "saving SQUISHED image"
im=im.resize((im.size[0]/10,im.size[1]),Image.ANTIALIAS)
im.save("assembled-squished.jpg")
print "DONE"

Script to download every image linked to from a webpage:

import urllib2
import os

suckFrom="http://w1bw.org/grabber/archive/2010-06-08/"

f=urllib2.urlopen(suckFrom)
s=f.read().split("'")
f.close()
download=[]

for line in s:
    if ".jpg" in line and not line in download and not "thumb" in line:
        download.append(line)

for url in download:
    fname = url.split("/")[-1].replace(":","-")
    if fname in os.listdir('./'):
        print "I already downloaded",fname
    else:
        print "downloading",fname
        output=open(fname,'wb')
        output.write(urllib2.urlopen(url).read())
        output.close()
Markdown source code last modified on January 18th, 2021
---
title: QRSS VD Image Assembler
date: 2010-06-07 23:20:18
tags: qrss, old, python
---

# QRSS VD Image Assembler

This minimal Python script will convert a directory filled with tiny image captures such as [this](http://www.swharden.com/blog/images/mass-W1BW_2jpg.jpg) into gorgeous montages as seen below! I whipped-up this script tonight because I wanted to assess the regularity of my transmitter's embarrassing drift. I hope you find it useful.

<div class="text-center img-border">

[![](assembled-squished_thumb.jpg)](assembled-squished.jpg)

</div>

```python
import os
from PIL import Image

x1,y1,x2,y2=[0,0,800,534] #crop from (x,y) 0,0 to 800x534
squish=10 #how much to squish it horizontally

### LOAD LIST OF FILES ###
workwith=[]
for fname in os.listdir('./'):
    if ".jpg" in fname and not "assembled" in fname:
        workwith.append(fname)
workwith.sort()

### MAKE NEW IMAGE ###
im=Image.new("RGB",(x2*len(workwith),y2))
for i in range(len(workwith)):
    print "Loading",workwith[i]
    im2=Image.open(workwith[i])
    im2=im2.crop((x1,y1,x2,y2))
    im.paste(im2,(i*x2,0))
print "saving BIG image"
im.save("assembled.jpg")
print "saving SQUISHED image"
im=im.resize((im.size[0]/10,im.size[1]),Image.ANTIALIAS)
im.save("assembled-squished.jpg")
print "DONE"
```

__Script to download every image linked to from a webpage__:

```python
import urllib2
import os

suckFrom="http://w1bw.org/grabber/archive/2010-06-08/"

f=urllib2.urlopen(suckFrom)
s=f.read().split("'")
f.close()
download=[]

for line in s:
    if ".jpg" in line and not line in download and not "thumb" in line:
        download.append(line)

for url in download:
    fname = url.split("/")[-1].replace(":","-")
    if fname in os.listdir('./'):
        print "I already downloaded",fname
    else:
        print "downloading",fname
        output=open(fname,'wb')
        output.write(urllib2.urlopen(url).read())
        output.close()
```

Pages