3
$\begingroup$

I have written a C code for a radix FFT transform over 1024 samples. But my main goal is taking audio files, converting them to raw pcm sample values, and then doing FFT on the whole audio file. My goal is to make a spectogram with bars, that show the amount of frequency present in an audio file.

The problem I have is that I get an arbitrary number of samples from audio files, depending on duration. (ie. 10k, 50k, 100k). How can I use my 1024 sample FFT function to transform all the samples? I have no idea how many samples the file is going to have beforehand. Is this a completely wrong approach to this?

$\endgroup$

2 Answers 2

4
$\begingroup$

Is this a completely wrong approach to this?

Yes.

and then doing FFT on the whole audio file.

There is really no reason to do this. You end up with a frequency resolution that's way more than you would ever need. For 3 minute long file, your frequency resolution would be about 0.005 Hz.

How can I use my 1024 sample FFT function to transform all the samples?

If you are only interested in the average power over the whole file, you can simply use "Welch's method": chop your file up into frames with an overlap of 50%, apply a window, apply FFT, calculate the magnitude squared and average over all frames,

1024 is a little short of that. I would recommend 8192 or 16384 to get good enough resolution in the bass frequencies. If you want it on a log frequency scale, you can integrate over octaves or third octaves, which would be "standard" way of doing it.

I have written a C code for a radix FFT transform over 1024 samples

Why? There plenty of excellent libraries out there that are highly optimized for this job.

$\endgroup$
1
  • $\begingroup$ Thank you kindly, I was really starting to run out of ideas. I will look into the method you mentioned. I've written the C code as a prototype function to look up to (and to understand the algorithm better), since this will be later transformed into hardware VHDL code. $\endgroup$
    – math101
    Nov 22, 2021 at 19:17
3
$\begingroup$

Hilmar has presented a quite nice answer here which, in my opinion is a very good way to approach your problem.

Nevertheless, if you already have an FFT algorithm it should be trivial to use it with an arbitrary number of samples. All you have to do is make sure the number is a power of two (if I am not mistaken though there exist algorithms that allow the use of FFT with arrays that are not a power of two long). If your signal does not contain a number of samples that is a power of two you could always zero-pad to make it so.

I definitely do not suggest that this is the best way to go and I strongly believe that Hilmar's approach is (at least) more appropriate here. Despite that, zero-padding and using a "generic" FFT algorithm is also an option (not optimal in many ways, in my opinion).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.