2

this question is related to : DSP : audio processing : squart or log to leverage fft?

in which I was lost about the right algorithm to choose.

Now,

Goal :

I want to get all the frequencies of my signal, that I get from an audio file.

Context:

I use numpy, and scikits.audiolab. I made a lot of reading on the dsp subject, went to dspguru.com as well, read papers and nice blogs over the net.

The code I use is this one :

import numpy as np
from scikits.audiolab import Sndfile


f = Sndfile('first.ogg', 'r')

# Sndfile instances can be queried for the audio file meta-data
fs = f.samplerate
nc = f.channels
enc = f.encoding

print(fs,nc,enc)
# Reading is straightfoward
data = f.read_frames(10)
print(data)
print(np.fft.rfft(data))

I am new to DSP.

My question

I would like to be able to separate all the frequencies of a signal to compare different signals. I use numpy.fft.rfft on the array of sound; But now, this operation alone is not enough. So, what is the best solution to get all the magnitudes of frequencies correctly ?

I saw that multiplying the resulting values get the complex numbers off and transform the whole as a real number.

Now what please ? Is that it ?

if you need me to clarify anything, just ask.

Thanks a lot !

Community
  • 1
  • 1
Larry
  • 1,735
  • 1
  • 18
  • 46
  • possible duplicate of [DSP : audio processing : squart or log to leverage fft?](http://stackoverflow.com/questions/20057831/dsp-audio-processing-squart-or-log-to-leverage-fft) – Paul R Nov 19 '13 at 21:06
  • No it is not : my other question was related to the best equation to take log or sqrt. this one is related to the whole extraction of magnitude as i understand it is two different things. – Larry Nov 20 '13 at 06:48

3 Answers3

2

Mathematically Fourier Transform returns complex values as it is transform with the function *exp(-i*omega*t). So the PC gives you spectrum as a complex number corresponding to the cosine and sine transforms. In order to get the amplitude you just need to take the absolute value: np.abs(spectrum). In order to get the power spectrum square the absolute value. Complex representation is valuable as you can get not only amplitude, but also phase of the frequencies - that may be useful in DSP as well.

Sleepyhead
  • 1,009
  • 1
  • 10
  • 27
  • +1 - The only problem is that I cannot give both of Sleepyhead and Bjorn the right answer as they complete each other! Thanks a million – Larry Nov 20 '13 at 06:55
2
  • You say "I want to get all the frequencies of my signal, that I get from an audio file." but what you really want is the magnitude of the frequencies.

  • In your code, it looks like (I don't know python) you only read the first 10 samples. Assuming your file is mono, that's fine, but you probably want to look at a larger set of samples, say 1024 samples. Once you do that, of course, you'll want to repeat on the next set of N samples. You may or may not want to overlap the sets of samples, and you may want to apply a window function, but what you've done here is a good start.

  • What sleepyhead says is true. The output of the fft is complex. To find the magnitude of a given frequency, you need to find the length or absolute value of the complex number, which is simply sqrt( r^2 + i^2 ).

Bjorn Roche
  • 11,279
  • 6
  • 36
  • 58
0

If I got it right, you want walk over all data(sound) and capture amplitude, for this make a "while" over the data capturing at each time 1024 samples

data = f.read_frames(1024)

while data != '':
    print(data)
    print(np.fft.rfft(data))
    data = f.read_frames(1024)
ederwander
  • 3,410
  • 1
  • 18
  • 23