# How to filter and downsample before FFT?

I would like to perform STFT on musical signal with defined sampling rate (44100Hz). I would like to get FFT result for smaller range of frequencies than are maximally available (not 22050Hz but for 4000Hz) so the result will have better frequency resolution. So I think I should downsample it, but how can you downsample to the frequency that is not in integer relation to the input frequency (you cannot leave the kth sample)? Do you do some kind of interpolation to get values of new samples? How does it affect the signal?

I also know I should filter out the frequencies higher than 8000Hz otherwise the signal would get aliased. Is there some kind of filter revelant for usage in STFT?

All I know about filters is that to get better (more sharp) filter you need more coefficents and so it will take more time to calculate. I know there are IIR filters and FIR filters. I read IIR filters can be unstable but should I care about it if I use a ready implementation (and I assume it's done right)?

Is it better to filter the whole signal (whole audio file) at once? If I get the infite response from the IIR filter and I filter the whole signal at once will the energy be most smudged at the end part of the signal?

I also read filters can pose some kind of delay on the frequencies and it differs for different frequencies. How do you analyse and compensate this phenomenon?

Edit: I found that you can get FIR with linear delay and that now they are used more often than IIR. But there's still a delay, what does it mean? That the frequency events (like musical notes) will occur later in sample number time? What will be this delay?

Edit2: When I want to downsample to sampling rate of 8000Hz I have to filter so there won't be any frequencies over 4000Hz. Is this practically possible, because I looked at different FIR filters characteristics and they just seem to greatly damp the stopband frequencies not to eliminate them? If I will have just a bit of the high frequencies in the signal, will the signal look ok when downsampled (aliasing won't be noticeable)?

• If you use matlab, just use resample function. Oct 5, 2012 at 18:19
• Thanks for the tip. I think that maybe I should have used Matlab, but now I use C++ and SPUC library. Oct 5, 2012 at 18:29
• A shorter FFT on the same signal won't give you better frequency resolution. If fact, without perfect interpolation, the resolution will usually be slightly worse. Oct 6, 2012 at 0:22
• What do you mean by shorter FFT? FFT with smaller window? I want to use the same window for 8000Hz as I would for 44100Hz so I think it will have better frequency resolution, won't it? Oct 6, 2012 at 10:13
• Frequency resoution is determined by the length of the window in seconds, not the sampling rate or number of samples. Use longer windows if you want better frequency resolution, and a lower sampling rate if you don't care about higher frequencies Oct 9, 2012 at 3:34

The process of changing the sampling rate by an arbitrary fraction $\frac{n}{k}$ is called resampling. It is basically just interpolating and decimating at the same time.

It sounds like you are trying to go from 44100 Hz to 4000 Hz. If so, that requires a sampling rate change of $\frac{40}{441}$, since $44100 * \frac{40}{441} = 4000$. As a practical matter you don't want to do really large sample rate changes (like decimating by 441) all at once because it is very difficult to implement filters that can do that. We thus try to break it down a bit into smaller steps.

If you factor the interpolation rate, 40, we see that it is 2 * 2 * 2 * 5. We can likewise factor the decimation rate to 3 * 3 * 7 * 7. One simple way to do the resampling, then, would be to do it in four steps like so:

$44100 Hz * \frac{2}{3} * \frac{2}{3} * \frac{2}{7} * \frac{5}{7}$

If you are trying to minimize the computational load you could rearrange the order of the resampling to reduce the sample rate as quickly as possible. That would make the later resampling steps less computationally intensive because they don't have to work on as many samples.

$44100 Hz * \frac{2}{7} * \frac{2}{3} * \frac{2}{3} * \frac{5}{7}$

You could also combine the two $\frac{2}{3}$ resamples because their product, $\frac{4}{9}$, does not have any large numbers. That leaves us with the following-

$44100 Hz * \frac{2}{7} * \frac{4}{9} * \frac{5}{7}$

• You use the word filter for the component that will perform the interpolation, right? You have to use normal filter to filter the frequencies before? Or there is some kind of filter that does it both? Oct 6, 2012 at 10:45
• Also it was hard for me to understand with fraction downsampling but I made an example and I think I understand it now: Signal sampled with rate 8: 0;1;2;3;4;5;6;7 will get: 0.25;1.5;2.75;4.25;5,5;6.25 when downsampled to rate 6. Oct 6, 2012 at 16:46
• A filter can implement both the frequency filtering and the resampling at the same time. No, your downsampling to 6 samples example is not quite right. If one of the "8" samples has a period of one time unit, then one of the "6" samples should have a period of $\frac{8}{6}$ time units, so the sequence would be 0, 1.33, 2.67, 4, 5.33, 6.67. Oct 6, 2012 at 19:22
• How do you project such a filter? I found I can design FIR filters with firls in octave but you only can specify frequencies there, no new sampling rate. Oct 6, 2012 at 20:16
• I couldn't begin to do the topic justice in a comment. I highly suggest getting a DSP book that covers decimation, interpolation, and resampling, and studying it. Oct 7, 2012 at 2:34