# A Working Signal Frequency Example?

I've been attempting to work on signal transformation and filtering for the past few months. It's a bit difficult, but I've been making progress.

I was wondering if anyone had any simple examples to show loading in a wave file, editing the volume and frequencies of that file, and re-outputting it to a file? I've already gotten up to volume, but I just can't edit the frequencies.

I thought that by using FFT, I would gain direct access to the frequencies of the sound file. Was that incorrect? I recall looking at frequency bins and such, but I don't see how that ties into wave files.

I know it might sound really complex, and I don't want to ask for a lot, but could someone post or link to an example? It would really help me out.

I'm using Python, with Windows 64-bit. I'm already using Scipy and NumPy for FFT and wave file input / output.

I want to basically be able to low-pass or high-pass filter a sound by code, as well as be able to read the frequency data in each sample (i.e. see how high- or low-pitched the sample is).

• Editing volume is easy, as you just scale the raw time-domain values appropriately. Frequency-domain processing is a more complex topic and will require a little study. You're right in that you can use FFTs to decompose a time-domain file into its frequency content, but direct manipulation of the spectrum values is not usually what you want to do. – Jason R Aug 7 '12 at 18:41
• @SolarLune: But how do you want to read and write the frequency data? What do you want to do with it? – endolith Aug 8 '12 at 4:24
• @SolarLune: As endolith explained, you need to have a clearer goal of what you want to accomplish. "Influencing frequency data" isn't descriptive. It's analogous to saying "I want to learn how to influence a car's engine." To do what? Make it more powerful? Quieter? More fuel-efficient? – Jason R Aug 8 '12 at 18:46
• @SolarLune: in the context of an audio signal, amplitude has a clear definition, so there is only one way of altering the amplitude/volume of the signal. But it's unclear what you mean by "manipulating the frequency". Do you want to change the fundamental frequency/pitch (eg: make a piano recording sound as if it has been played higher or lower on the keyboard)? The timbre (make the sound brigther or duller)? Note that the Fourier transform is rarely the right way of doing these things. – pichenettes Aug 9 '12 at 9:00
• Define "alter the frequency of a sound". What is "the" frequency of a sound? – pichenettes Aug 10 '12 at 15:09

As you have observed, instantaneous signal amplitude is an easy to understand, one-dimensional quantity, whose averaged absolute value is loosely associated with perceived loudness, and which can be easily altered by multiplying your signal by a constant.

Frequency is a very different affair. If we lived in a world where the audio signals were made of sustained pure tones, it would make sense to ask "what is the frequency of this sound?". But things are more complicated:

• Stationary speech or music signals can be described as a sum of pure tones. Thus, it makes more sense to ask "what is the signal amplitude at this particular frequency?". This is why we look at spectra - graphs showing signal amplitude as a function of frequency. Note that frequency is not the plotted quantity, it is the $x$ axis!
• Speech or music signals are not stationary: some components of the signal decay over time or are modulated, some appears... Thus, it makes even more sense to ask "what is the signal amplitude at this particular frequency and particular time frame?". This is why we commonly see for audio analysis a representation called "spectrogram" - showing the measured signal energy as a function of time and frequency (again, note that frequency is the axis, not the measured quantity).
• In one of your comments you say "as well as be able to read the frequency data in each sample". Frequency is a rate of change, and to observe a change, we need a collection of samples - it makes no sense at all to do any frequency analysis on a single sample, and if you use Fourier analysis (which is just one of the ways of estimating a sum of sine waves from a signal), you'll need more and more samples to get a more and more precise resolution on the frequency axis. But remember that sounds change over time, and that performing a Fourier Transform discards timing information (the magnitude spectrum of two simultaneous 100 Hz and 300 Hz tones is the same as the spectrum of a 100 Hz tone followed in time by a 300 Hz tone). Thus, if you care about the temporal position of events, you will need to perform your analysis on slices of the signal, computing a FFT on overlapping blocks of the input signal - this is called the short-term Fourier transform (STFT). You'll have to deal with resolution problems here - the smaller your windows, the more accuracy you have on the time axis, the less accuracy you have on the frequency axis. The bigger the analysis windows, the less accuracy you have on the time axis, the more accuracy you have on the frequency axis. Again, and again, note that frequency is not what you estimate - your STFT is just answering the question "What is the signal energy in this particular frequency range and this particular time frame?".

I hope these will make you realize that there's no such thing as "the frequency" of an audio signal. Frequency is an axis, "where we look at things", not a measured quantity, "what we measure".

Note that there are some one-dimensional quantities homogeneous to frequencies (and thus expressed in Hz), that can be used to characterize stationary (stable) sounds. For generic audio or music, these quantities will vary over time:

• Fundamental frequency, is the inverse of the period of the sound. It relates to the perceived musical pitch (note) of the sound. This quantity is undefined for aperiodic sounds. Pitch-shifting algorithms will alter this quantity, to make a recording sound as if it was played/sung higher or lower on the musical scale.
• Roll-off frequency, is the frequency under which a given fraction (say 95%) of the energy of a sound is contained. This quantity can be affected by low-pass / high-pass filtering, to make a recording sound "brighter" or "duller". It is one of the many dimensions of the perceived timbre of sound.

Maybe some of these are the quantities you really care about?

One final word about the FFT - it is the hammer in a signal processing version of "if all you have is a hammer, everything looks like a nail". It is an extremely useful tool, but it is not the best fit for many audio signal processing/analysis problems, and more often than not it is used only as an intermediary processing or computational step. In particular, for some of the questions you might be interested in:

• Estimating the fundamental frequency might an involve a FFT, but the spectrum is not the right thing to look at, because of the "missing f0" phenomenon (a signal might be perceived as being a 263Hz C3 note, without having energy at this frequency). Don't make the mistake of trying to estimate the musical pitch of a signal by looking at the peak in its spectrum.
• Computing a FFT, messing with the numbers, and performing the inverse FFT is a poor way of filtering a signal. This should be done with digital filters, available in scipy.signal.lfilter. You'll find formulae to compute IIR filter coefficients for a variety of musically useful second-order filters here.