Tutorial: Animate your spectrogram!

A couple of people have expressed interest, so here are a little tutorial describing how I created the spectrogram animation that you can find here: http://www.michw.com/2014/02/spectrogram-animation/

You should know a few things:

1) I’m not an AE expert, you guys!

2) There are a lot of ways to accomplish what I’m about to explain; this is just one way, and it may not even be the best way.

3) If anything isn’t clear, then please let me know so I can try to fix it!

4) Many thanks to Christian Petropolis, who showed me how to do this in the first place!

What you’ll need

First, let’s start with ingredients, the things you’re going to need right from the get-go to be able to start this tutorial:

– A spectrogram! Here’s mine. Nothing special – just a standard Matlab-generated spectrogram in png format. Probably a jpeg or tif or even a Photoshop file would be just fine as well. Any image that you can import to After Effects.

– Your audio file! Again, mine has is just a wav file generated by Matlab. Make sure it’s the audio that matches your spectrogram!

– Adobe After Effects (sorry – I know it’s expensive – I managed to get a good deal on it the Adobe Creative Cloud through my University bookstore, if you’re a student or teacher you can probably find a way to get it relatively inexpensively)

Let’s get down to business

1) Open After Effects! Select File -> New -> New Project.

2) Select File -> Import -> File…

Then select the both the spectrogram image file and the accompanying audio file

3) Select both files from the Project tab and drag them onto the “Composition” icon:



4) A window will open; select “Single Composition”, and set the duration at approximately the length of your audio file (don’t worry, you can change this later if it’s not right)



5) In the Composition tab, right-click and rename the layers if you like:



6) Select the image layer (in my case, it’s called “Spectro”). Use Shortcut keys (cmd-C/ctrl-C and cmd-V/ctrl-V) or the Edit pull-down menu to make two duplicates.

7) Rename the two new layers to “GlowLayer” and “DarkLayer” (or whatever you like, that’s what I’m going to call them). Make sure “GlowLayer” is on top.


8) Click on the “GlowLayer” to make it active, select the “rectangle tool” icon, then draw a thin rectangle on the spectrogram. This will be the glowing bar that slides across as you play the sound.



9) With the “GlowLayer” still selected, Click the pulldown menu Effect -> Stylize -> Glow. The Glow effect controls should pop up in their own tab. Here’s how I set it up, but do whatever works for your image. The only things I changed were Glow Threshold (22%) and Glow Intensity (1.8).


10) Select Layer (pulldown) -> New -> Shape Layer, and drag that new layer to just below “DarkLayer”. I renamed my new Shape layer “darkRectangle”.


11) With “darkRectangle” layer selected, click the “Rectangle tool” button. Draw a Rectangle on your spectrogram like this (see image). Click the “Fill Options” and choose “solid color” and ~60% opacity, then click  “Fill color”, and choose a medium gray.


12) Select “DarkLayer”, choose the “Rectangle tool”, and draw another rectangle the same way as in step 11. The difference is that this time, because of the layer type, it’s going to make a mask, not a rectangle.

13) Click the check box to “Invert mask”. You should see the dark colored rectangle show through at this point.


14) Now for the fun part. Make sure the “scrubber” is at time = zero. The scrubber is the little yellow triangle on the timeline that lets you move forward and backward in time.

15) Click the triangle icon beside the Mask layer under “DarkLayer” to see more options.

16) Click the little clock icon beside “Mask Path”, and you should see your first “keyframe” show up (yellow diamond). If you’re unfamiliar with After Effects, you should know that it’s *all about keyframes*. Seriously.



17)  Drag your keyframe scrubber to the end of your audio segment.  The audio segment is shown by a green bar on my timeline.

18) Click on the mask layer to activate it, if it’s not already active (my mask layer is called “Mask 1” in the image above). Click the right arrow on your keyboard until the left side of the mask rectangle is lined up with the right edge of the spectrogram image. (hint: use shift+arrow to move in larger increments). Now if you move your scrubber back and forth you should see the “shadow” move across your spectrogram.


19) Now we’re going to animate our “GlowLayer” in much the same way. Select the GlowLayer, and again open up “Mask 1” options.

20) Move the scrubber to zero time again. Add a Mask Path keyframe.

21) Move the scrubber to the end of the audio segment.

22) Make sure “Mask 1” under the “GlowLayer” is selected, and move the glow bar over using the right arrow key. Stop when the right side of the rectangle mask is lined up with the right edge of the spectrogram.

23) Congratulations! You’ve animated your spectrogram. Now if you’d like to preview it, you can use the RAM preview. Just hit the little triangle on the far right of the preview panel, and there you go. It’ll run through it quickly once to render the video and audio, then you can behold your masterpiece. A little warning: if you’re computer is a bit slow, this preview can be jumpy. But once you export it, it should be just fine.


24) Exporting! File -> Export -> add to Render queue. Choose render settings and output location and click “Render” button. (Defaults are probably a decent place to start if you’re not sure).

Doodle videos, heck yeah!

My latest project is figuring out how to make a doodle video. Or a speed drawing animation. I don’t actually know what they’re called, but you’ll get the idea if you check out the videos below. I started off attempting to use my iphone. Setting the iphone up to record was… awkward:

completely professional.

I did my video this way, posted it to youtube, and got a bunch of feedback from my awesome Facebook and Twitter friends. I tried to take that feedback into account as best I could and eventually moved on to my Nikon D5100 (dSLR).  I even set it up on an actual tripod (Thanks, Andrew Shao!).


Recording on my SLR was pretty easy too, once I got it adjusted. I did have some issues with focusing – not that it was hard, I just kept forgetting to check. oops. Editing for the SLR video was done in iMovie, and the audio was recorded in Hindenburg Journalist. Here’s a 30-second sample from this experiment. I hope that you agree that it has improved, at least marginally, from my first attempt!

Next, I decided to try doing the video using Camtasia. Luckily they have a free trial, so I gave that a whirl. Camtasia is for screen recording and video editing, and was really easy to jump into and start using right away. I did the drawings in Adobe Photoshop using my Wacom Intuos4 tablet/stylus. Here’s the 30-second sample using the “all-digital” method:

Dear reader, if you have been patient and kind enough to actually read all this, and more importantly, watch the two videos, please feel free to give me feedback in the comments (below) or in email or on Twitter or Facebook!

Learning HTML, CSS, and PHP

I can’t tell you how often I find myself wishing I could design my own web pages.  Or even tweak things like WordPress templates.  It’s a skill that I’ve wanted to work on for a long time now.

I actually started digging into this almost a year ago, when my sister and her husband visited, although things have been busy and I have abandoned it for some time now. Christian is really into web programming, and is also quite patient when it comes to sharing his knowledge. He suggested checking out a couple of books to get started:

1.  CSS: The missing manual

2.  PHP and MySQL for Dynamic Web Sites

3.  Javascript and jquery

I’ve started with the first two, just working through the examples.  I don’t think of myself as a total tech dummy, but I have to admit – I have very little knowledge of the inner workings of websites.  It’s actually a lot of fun to go through these books and see what’s going on in the background – so complicated, but so logical.

A couple of other things to mention:  Although it would be pretty awesome, I can’t actually afford the Adobe Design and Web premium (even though it’s on massive discount for students – I will have to get it before I graduate!)  In the meantime, I’m happy to explore what my options are in the land of open source.  I found what seems to be a great alternative to Dreamweaver – it’s called Aptana, and it’s totally free!  It also directly links up to Firefox’s Firebug debugger, which is handy.  Of course, never having used Dreamweaver, it’s tough for me to give a fair comparison.  But it’s working just fine for me now.

FFMPEG: png to mpeg

I have been lucky enough to have a chance to do some of my more basic data processing in Python (with numpy/scipy/matplotlib). One of the things I’ve been working on is the effect of interference between the direct path and surface bounce arrivals from a fin whale that is near the surface. Hopefully I’ll have a post dedicated to that problem in particular, but for the time being, I’ve been trying to figure out the best way to animate a series of plots output each time the code goes through a loop.

There are a few options for doing this. From what I can gather, it’s not something that is easily done within Python (someone correct me if that’s wrong!). So instead, I’m creating a series of image files (.png’s), and converting to video outside Python. I looked at a couple of options and settled on ffmpeg, which seemed really simple.

I grabbed it from fink:

fink install ffmpeg

All of my image files were saved in a single folder, with names like 001.png, 002.png, … 198.png. Converting them to video format:

ffmpeg -i %03d.png -vb 1024K video.mpg

The -i flag is for input file(s) and -vb is video bitrate. Here’s the output:

Brushes experiment

Oh, hello, procrastination!  Wonderful to see you again.  I have two exams tomorrow, yet still I felt the need to share this video with you guys.  I made this more than a year ago, and was sure I’d already posted it, but I guess not.  It’s actually just an iPad Brushes app animation of a drawing I did of my friend Val and his daughter, Gracie (ah, Brushes, the one iPad app I really do miss). I swiped some music from the life aquatic soundtrack, and put it together in iMovie. Hooray for technology, and for not learning about pre-formed nitrate and radio-carbon dating for a few minutes. Now. Back to the books.

Experiment with Brushes from Michelle Wray on Vimeo.

Emacs encryption

I finally figured out how to encrypt an emacs file – ooooh.  Now I can put all of my secrets into org-mode, and no one will ever know, mua-ha-ha!

Emacs encryption can be done using EasyPG, which comes with Emacs2.3.  So the first thing I did was upgrade to Emacs2.3 from 2.2 using Fink.  A quick aside:  I mistakenly installed the x11 version and kept thinking I had totally broken something because I was impatient when I didn’t immediately see emacs pop up in the terminal.  I also installed GnuPG from fink.

After that it’s easy – just add a .gpg extension onto the file, so something like myorgfile.org.gpg.  When you open it in Emacs, it recognizes that it’s really an org file, and when you go to save it, you just need to give a password to re-open it.  Don’t forget that password!! Here’s what it looks like:

And that’s it.

Python and plankton

Wow, it has been a long time since I actually played around with Python.  My brain has melted into Matlab-only mode, and I am having a tough time remembering how to do the simplest things…

So here is the latest.  Jaci was telling me about what she was working on in Python.  She has a comma delimited file containing data about different types of plankton, and the plankton are listed by numbers.  Then she has a different text file containing information on what the numbers mean.  She wants to link the list of data to the list of id numbers and plankton names.  It is entirely possible I mis-interpreted her problem, but it was fun to work this one out anyway – had to remember about lists and tuples and dictionaries and slicing, and also about how to read in a file line by line… good thing I still have some of my old scripts so I can copy and paste things that I forget!

Here are my fake data files:

critters.txt, which contains the actual data in comma-delimited format.  the first column is the ID number, and the second column is the shoe size.  Because different types of plankton wear different shoe sizes:


idnumbers.txt, which contains the ID numbers in the first column, and the names that go with that plankton in the second column.  (guys, I am not a biologist.  Just sayin.):


And here is the python script, lookuptest.py:


# a quick lookup table: may not be the best way to do this. I’m new at this stuff!

critterfile = ‘critters.txt’
idfile = ‘idnumbers.txt’

# preparing variables
datalist = [] # actual data – id numbers
datavalue = [] # actual data – data value (shoe size in my file)
dataid = [] # dictionary – data id number
critterlookup = {} # dictionary – critter names

infile1 = open(critterfile)
infile1.readline() # skip header line, could instead save column names.
lines = infile1.readlines()
for line in lines:
splitup = line[:-1].split(‘,’) # ignore newline character, split on com\

infile2 = open(idfile)
lines2 = infile2.readlines()
for line in lines2:
splitup2 = line[:-1].split(‘,’)
critterlookup[splitup2[0]] = splitup2[1]

for cdex in datalist:

It may not be the most efficient way to do it, but it seems to work! 🙂 Oh, and if you’re forgetful like me, it’s a good idea to hang on to every script or function you write, even the unfinished ones, because they can be super helpful later on.

Antelope and Python

Now that I’m (somewhat) on my way to getting ObsPy up and running, it sort of makes sense to figure out if I could realistically use it to do my work. You know, not just to play around and make pictures. Right now my data is pretty much all in a couple of Datascope databases, and I access it using Matlab. But a bit of Google searching led me to the Antelope Users Group on Github, where they have user-contributed software designed for Antelope and Datascope.  And from what I can tell, there’s a Python interface somewhere in there.

Here are the steps for getting the Antelope Users Group contributed code:

1.  Download and unzip the contributed code.  It will look something like this:


2. Create a directory for the Antelope contrib files, and copy them here:


3. Navigate to the directory with the Python stuff:

cd $ANTELOPE/src/contrib/data/python/datascope/

4. Compile:

make include
make install

I haven’t tried it yet, but hopefully will have a chance soon…

Data visualization and statistics

While flipping through Flipboard* on the iPad last night, I stumbled across this very cool site, Flowingdata.com.  It was created by Nathan Yau, a PhD candidate in statistics at UCLA.  From his website:

FlowingData explores how designers, statisticians, and computer scientists are using data to understand ourselves better – mainly through data visualization.

There are lots of neat ideas and and examples on his website.  I knew I liked it when I saw this post:

Why learning Code for Data is Worthwhile

* Flipboard is also totally amazing in its own right. If you have an iPad, and haven’t downloaded that app yet, do it!

LaTex and Inkscape

In about a month and a half I’m going to be presenting a poster at the DCL/DE workshop in Oregon.  Since I don’t currently have a copy of Illustrator, I’m trying to decide what to use instead.  I downloaded Inkscape onto my imac, and started playing around.  The first thing I did was try to make a small annotated sketch.  It was all going fairly smoothly until I tried to add in symbols… and then I remembered how I ran into this problem last time I played with Inkscape.  So it was pretty much the same thing.

Here’s what I did:

1.  Install Inkscape from their website:  inkscape.org.  This part was straightforward.
2.  Download Textext, unzip the folder.  It’s found here: http://pav.iki.fi/software/textext/
3.  Copy the files textext.py and textext.inx to the inkscape extensions folder:

cp *text* ~/.config/inkscape/extensions/

4.  Then get Pstoedit:

fink install pstoedit

5.  In Inkscape, go to Extensions -> Render -> Textext,  and fill the Tex code into the GUI that pops up.


There is an error message that comes up saying:

textext.py:55: DeprecationWarning: the md5 module is deprecated; use hashlib instead
import os, sys, tempfile, traceback, glob, re, md5, copy

I recall getting a message something like this last time I installed Textext. It is annoying, but doesn’t seem to be a huge problem, and the extension still works.