# What do you get applying FFT to Music?

I am trying to take a section of a song (approx 1.5 seconds) 44100 sample rate equals sample size of 65536 which is 2**16.

I want to find the frequency of the piano note that is played in the window of time.

I have implemented the four1 function - real data starting at array pos 1 and I data at 2 ...

four1 uses are rewritten fortran function so the array does not start at index 0.

My I data is all 0.0.

I have verified that input data looks good ... small sample: Idx is index into data array

Idx 22023   R -1876.000000   I 0.000000
Idx 22025   R -1720.000000   I 0.000000
Idx 22027   R -1453.000000   I 0.000000
Idx 22029   R -1203.000000   I 0.000000
Idx 22031   R -1017.000000   I 0.000000
Idx 22033   R -799.000000   I 0.000000
Idx 22035   R -386.000000   I 0.000000
Idx 22037   R 75.000000   I 0.000000
Idx 22039   R 243.000000   I 0.000000
Idx 22041   R 327.000000   I 0.000000
Idx 22043   R 759.000000   I 0.000000
Idx 22045   R 1238.000000   I 0.000000
Idx 22047   R 1317.000000   I 0.000000
Idx 22049   R 1335.000000   I 0.000000
Idx 22051   R 1639.000000   I 0.000000


Upon return -- the array contains the R and I of the freq component.

Looking at the first half (real frequency half) -- I get a range of different R and I values but when I calculate the Magnitude (srq of sum sqrs) --- the magnitudes are almost constant the complete real half of the freq range

Sample of output -- I normalized the Magnitude to the Max Magnitude found

Idx 13841   R -605090688.000000   I -74311704.000000    M 0.958672
Idx 13843   R -605982336.000000   I -74471496.000000    M 0.960094
Idx 13845   R -605715712.000000   I -74230088.000000    M 0.959632
Idx 13847   R -605639744.000000   I -74081136.000000    M 0.959485
Idx 13849   R -605420992.000000   I -74552360.000000    M 0.959234
Idx 13851   R -606319744.000000   I -73444392.000000    M 0.960425
Idx 13853   R -605728512.000000   I -73858336.000000    M 0.959581
Idx 13855   R -605823424.000000   I -73527352.000000    M 0.959666
Idx 13857   R -605311424.000000   I -73650440.000000    M 0.958890
Idx 13859   R -606479360.000000   I -73952432.000000    M 0.960771
Idx 13861   R -606198336.000000   I -74041552.000000    M 0.960349
Idx 13863   R -605836544.000000   I -74178936.000000    M 0.959811
Idx 13865   R -605548864.000000   I -73529936.000000    M 0.959238
Idx 13867   R -605573312.000000   I -73070880.000000    M 0.959189


There is little to no change in Magnitude across freq bins.

Should I not be able to detect the frequency of that piano note played in that time window?

Am I misusing the FFT?

• Many parts of this question are vague. When you say "I calculate the Magnitude (srq of sum sqrs)" you should instead put a precise mathematical representation of what you did. Try this and maybe someone can help you. – DanielSank Sep 28 '14 at 5:08
• I figured that some one who could help would understand the short hand for square root of the sum of the squares -- Standard for FFT. I don't have an issue with the FFT implementation but understanding why the results don't make sense. I have since reduced my sample size down to 1024 instead of 65536 and I'm starting to get results that make sense. I'm assuming right now that the same size was large and the data was changing dynamically enough that the FFT distribution was almost constant. By reducing the sample size down to 1024 --- the data is not as dynamic thus providing a result. – JHinkle Sep 28 '14 at 5:43
• @JHinkle what "someone who could help" understands is that you're being vague. You can't ask "what am I doing wrong?" and then summarize all the parts where you might be doing something wrong with a few words instead of providing code. – hobbs Sep 28 '14 at 6:07
• I implemented a moving 1024 window and I am getting great results. Thanks – JHinkle Sep 28 '14 at 6:08

Yes, you should be able to see the component of the piano note. But there are quite a few pitfalls on the way. I would suggest you experiment a little to get a 'feel' on how fft works first. Use GNU Octave or Matlab to do simple experiments, and then you can plug in your real data, and see if it works. For example, in Octave I can easily generate a sine wave of 440 Hz and do the FFT:

t = linspace(0, 2*pi, 44100);
s = sin(440.0 * t)              % This is the 440 Hz tone
f = abs(fft(s))                 % Take the fft (magnitude)
plot(f)
xlim([0,1000])                  % Show only the 1000 first bins


Then you can read your samples and do the same. At least, this way you exclude insecurity about the fft function you're using. You could also save the generated sine, and feed it to your four1 function, and check if the result is correct.

I think that you don't get the relation between the window size and results ... that's why you were surprised from the results....
SamplingFrequency/WindowsSize gives you the distance(in Hz) between points in the frequency spectrum.
WindowsSize/SamplingFrequency gives you the distance(in seconds) between each calculated spectrum in time.

So for 44100hz and window size 65536 you have spectrum where values are spaced on 0.6hz. That's why they have similar values... After that when you changed window size to 1024, you have spectrum where values are spaced on 43hz, so difference is noticeable