in reply to Music and FFT

Note: FFT only works with a sample count that is a power of 2, for example, 512.

And it assumes the waveform is periodic, i.e. after sample 511 (in the case of 512 samples) comes sample 0. That most likely implies a level jump, which will cause inclusion of a lot of harmonics that are not actually in the original sound. Think: sawtooth, for an original gentle slope, which includes all harmonics(even+odd) at an amplitude level 1/n. There are ways to reduce that effect, for example doubling the sample count while mirroring the original samples backwards, turning the sawtooth into a triangle (odd harmonics only, amplitude 1/nē, which is a lot better). Even more advanced systems can make using of windowing functions, adding original samples from the left and right, to the side, which gently fade in/out.

Replies are listed 'Best First'.
Re^2: Music and FFT
by llancet (Friar) on Oct 21, 2010 at 03:19 UTC
    There's also another tough issue: what I'm going to hack is a piano music. As piano has rich harmonics even you press only one key, I need to distinguish fundamental frequency from harmonics.
    Any clue to do that? Maybe only through my ear and brain?
      Harmonics always have frequencies that are multiples of the base frequency. So, if in a result you have a lot of frequencies, you can simply ignore those that are multiples of another frequency which is also present, and strong. Note that after FFT, those frequencies will be approximated, and thus, not exact multiples... But if you have enough sample points, the resolution shoul become fine enough.

      Also, a piano has several strings per key, which are tuned in almost the same frequency... which produces the phasing effect you generally hear. The ribbles on strings, especially the lower strings, have a similar effect.

      Start experimenting with it. You'll probably get familiar with the results from the FFT, soon enough. Note that if you edit the values from the FFT data, you can convert it back to sound, and hear if your approximation is close enough.

      I need to distinguish fundamental frequency from harmonics. Any clue to do that?

      If you are recording the music you are analysing, yourself. Record directly into a non-lossy format (like.wav) and use as high a sample rate as your software allows. Many recorders can create .wav files with much higher sample rates than the stand 44,100 hz. Some will even allow you to specify any rate you type in, so FFT friendly powers of 2 are possible.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.