Long time ago (really long time ago) I was studying the Fourier Transform (http://en.wikipedia.org/wiki/Fourier_transform) in the faculty, and even I used the FFT (Fast Fourier Transform, http://en.wikipedia.org/wiki/Fast_Fourier_transform) algorythm in an application written in C for digital image processing. Since then I have not been worried about the FFT.
These days returned my interest for the FFT so, with the idea of learning audio programming techniques, I wanted to code a tuner or a spectral power analyzer with my Linux box (it means it should be a JACK client).
First of all you have to look what FFT algorithm could be used, GPL licenced, and googling a little bit I realise that FFTW (http://www.fftw.org/) is the election:
FFTW is a C subroutine library for computing the discrete Fourier transform (DFT) in one or more dimensions, of arbitrary input size, and of both real and complex data (as well as of even/odd data, i.e. the discrete cosine/sine transforms or DCT/DST). We believe that FFTW, which is free software, should become the FFT library of choice for most applications.
I was compiling few examples and became familiar with the calculations of the FFT. What I want to compute is the power spectral range of an audio signal, so the data will be real samples (one dimension) coming from my microphone; and the FFT output will be complex values, and both the real part and the imaginary part are important in order to compute the power spectrum of a signal.
fftPlan = fftwf_plan_dft_r2c_1d(fftSize, fftIn, fftOut, FFTW_MEASURE); //r2c_1d: real to complex, one dimension
Searching if there are open source tuners, preferably CLI and JACK compatible I found two projects that are precisely using the FFTW library:
- tuneit: A simple command-line instrument tuner for Linux (http://delysid.org/tuneit.html, Mario Lang). Both ALSA/JACK compatible
- sndfile-spectrogram: Generate a spectrogram as a PNG file from a given sound file (http://www.mega-nerd.com/libsndfile/tools/, Erik de Castro Lopo)
And also it was useful for my own code the project capture_client:
- capture_client: (https://github.com/jackaudio/example-clients/blob/master/capture_client.c, Paul Davis, Jack O’Quin)
I have divided the problem into two parts:
- First of all I capture the signal coming from the microphone, and as a result I have two files: a wav file, and a text file where I write a sufficient number of samples.
- Second: I open the sample file, apply a Hanning window to the samples for smoothing, compute the FFT, compute the power spectral range of the signal, and I write an output file ready to be processed with the graphics utility gnuplot (http://www.gnuplot.info/).
I never worked with gnuplot, the Matlab equivalent in Linux world. Running the gnuplot demo that quickly shows all the gnuplot possibilities, I was really impressed. In this case to draw the graphic is very easy:
$ gnuplot
gnuplot> plot “data_440_trumpet_output.txt”
The sample that I show was recorded with the 80′s mini keyboard Casio SK-8, choosing the trumpet sound. Here is the result:
Casio SK-8 440Hz (A) trumpet, 44100 fps by joanillo
Now I want to compare the results of my calculations with the ones obtained with Audacity and Ardour, just to verify that the calculations were correct:
| My calculations and Gnuplot | Audacity | Ardour |
![]() |
![]() |
![]() |
Perfect! I got it!











