A really brief introduction to audio signal processing in Julia

Nov 03, 2014

Introduction

If you are into scientific computing, you probably have already heard about Julia, the magical language that aims to be (almost) as fast as C and as easy as MATLAB and Python to write. I have been playing with Julia for more than a year now, and I really like it and recommend checking it out.

The objective of this post is not to teach you Julia. Hopefully, everything used here is so similar to MATLAB or Python you will not need any explanation on what is happening :) The main idea here is to show the basic tools we already have available in DSP.jl (disclaimer: I am one of the developers!) and give you something to play with.

Setup

To be able to run the code in this post (also distributed as an IJulia notebook), you will need to install Julia (of course!) and the following packages:

  • DSP.jl: signal processing functions (filtering, spectrograms/periodograms, window functions...).
  • WAV.jl: loading/writing WAV files.
  • PyPlot.jl: a Julia interface to the Matplotlib plotting library. This is actually a Python library, but there is very little overhead (for examples, arrays are passed to it without making copies).
  • IJulia.jl: where the magic happens :) IJulia notebooks work exactly like IPython notebooks. In fact, they all share the same infrastructure (more on this here).

Installing packages in Julia is easy. Just run the following commands in the Julia REPL:

Pkg.init() # only needed if this is the first time you are dealing with packages
Pkg.update()
Pkg.add("PackageName")

where "PackageName" is the name of the package you want to install (without the .jl suffix).

If you want to follow along using the notebook, you can download it from this GitHub repository.

Loading, visualizing, and processing an audio file

using DSP, WAV, PyPlot # "using" makes the listed modules available for the
                       # user, like "import" in other languages

# Loading and plotting an audio signal
s, fs = wavread("test.wav")

plot(0:1/fs:(length(s)-1)/fs, s)
xlabel("Time [s]")
# PyPlot includes a specgram function, but let's use the native
# implementation from DSP.jl. The function below extracts a
# spectrogram with standard parameters for speech (25ms Hanning windows
# and 10ms overlap), then plots it and returns it .

function plot_spectrogram(s, fs)
    S = spectrogram(s[:,1], convert(Int, 25e-3*fs),
                    convert(Int, 10e-3*fs); window=hanning)
    t = time(S)
    f = freq(S)
    imshow(flipud(log10(power(S))), extent=[first(t), last(t),
             fs*first(f), fs*last(f)], aspect="auto")
    S
end
plot_spectrogram(s, fs)
# Now let's bandpass the signal to simulate telephone bandwidth
# and plot its spectrogram again
responsetype = Bandpass(300, 3400; fs=fs)
prototype = Butterworth(8)
telephone_filter = digitalfilter(responsetype, prototype)

# Let's take a look at the filter response now
ω = 0:0.01:pi # variables can have Unicode names!
              # This is typed in the notebook as \omega + tab.
H = freqz(telephone_filter, ω)

plot(fs/2*ω/pi, 20*log10(abs(H)))
xlabel("Frequency [Hz]")
ylabel("Gain [dB]")

# Filtering our signal with the filter
sf = filt(telephone_filter, s)
# Just to be sure the filter has done the right thing, let's see
# the spectrogram of the filtered signal
plot_spectrogram(sf, fs)
# IJulia still does not support playing audio, so we include
# some code to do it.
include("AudioDisplay.jl")

using AudioDisplay

audioplayer(s, fs)
audioplayer(sf, fs)

(Just a sidenote: the AudioIO.jl module has more advanced functions for loading and playing audio, but in this example I wanted to use the notebook for all multimedia rendering.)

Where to go from here?

I know this is all really basic, but Julia has much more to offer. There are currently 429 packages (and counting) available to install via the package manager and much more in development on GitHub. Package development has been especially strong in the statistics/machine learning and optimization domains.

If you want to learn more about the language, check the official documentation and the learning resources at the website. There is also a very active mailing list for users.

I sincerely hope you find Julia interesting and worth of your time!

This entry was tagged as development