[ACCEPTED]-Downsampling and applying a lowpass filter to digital audio-downsampling
Read on FIR and IIR filters. These are the 8 filters that use a coefficent array.
If you 7 do a google search on "FIR or IIR filter 6 designer" you will find lots of software 5 and online-applets that does the hard job 4 (getting the coefficients) for you.
This 3 page here ( http://www-users.cs.york.ac.uk/~fisher/mkfilter/ ) lets you enter the parameters 2 of your filter and will spit out ready to 1 use C-Code...
You're right in that you need apply lowpass 24 filtering on your signal. Any signal over 23 5500 Hz will be present in your downsampled 22 signal but 'aliased' as another frequency 21 so you'll have to remove those before downsampling.
It's 20 a good idea to do the filtering with floats. There 19 are fixed point filter algorithms too but 18 those generally have quality tradeoffs to 17 work. If you've got floats then use them!
Using 16 DFT's for filtering is generally overkill 15 and it makes things more complicated because 14 dft's are not a contiuous process but work 13 on buffers.
Digital filters generally come 12 in two tastes. FIR and IIR. The're generally 11 the same idea but IIF filters use feedback 10 loops to achieve a steeper response with 9 far less coefficients. This might be a good 8 idea for downsampling because you need a 7 very steep filter slope there.
Downsampling 6 is sort of a special case. Because you're 5 going to throw away 3 out of 4 samples there's 4 no need to calculate them. There is a special 3 class of filters for this called polyphase 2 filters.
Try googling for polyphase IIR or 1 polyphase FIR for more information.
Notice (in additions to the other comments) that 13 the simple-easy-intuitive approach "downsample by a factor of 4 by replacing each group of 4 consecutive samples by the average value", is 12 not optimal but is nevertheless not wrong, nor 11 practically nor conceptually. Because the 10 averaging amounts precisely to a low pass 9 filter (a rectangular window, which corresponds 8 to a sinc in frequency). What would be conceptually 7 wrong is to just downsample by taking one 6 of each 4 samples: that would definitely 5 introduce aliasing.
By the way: practically 4 any software that does some resampling (audio, image 3 or whatever; example for the audio case: sox) takes 2 this into account, and frequently lets you 1 choose the underlying low-pass filter.
You need to apply a lowpass filter before 4 you downsample the signal to avoid "aliasing". The 3 cutoff frequency of the lowpass filter should 2 be less than the nyquist frequency, which 1 is half the sample frequency.
The "best" solution possible is indeed a 49 DFT, discarding the top 3/4 of the frequencies, and 48 performing an inverse DFT, with the domain 47 restricted to the bottom 1/4th. Discarding 46 the top 3/4ths is a low-pass filter in this 45 case. Padding to a power of 2 number of 44 samples will probably give you a speed benefit. Be 43 aware of how your FFT package stores samples 42 though. If it's a complex FFT (which is 41 much easier to analyze, and generally has 40 nicer properties), the frequencies will 39 either go from -22 to 22, or 0 to 44. In 38 the first case, you want the middle 1/4th. In 37 the latter, the outermost 1/4th.
You can 36 do an adequate job by averaging sample values 35 together. The naïve way of grabbing samples 34 four by four and doing an equal weighted 33 average works, but isn't too great. Instead 32 you'll want to use a "kernel" function that 31 averages them together in a non-intuitive 30 way.
Mathwise, discarding everything outside 29 the low-frequency band is multiplication 28 by a box function in frequency space. The 27 (inverse) Fourier transform turns pointwise 26 multiplication into a convolution of the 25 (inverse) Fourier transforms of the functions, and 24 vice-versa. So, if we want to work in the 23 time domain, we need to perform a convolution 22 with the (inverse) Fourier transform of 21 box function. This turns out to be proportional 20 to the "sinc" function (sin at)/at, where 19 a is the width of the box in the frequency 18 space. So at every 4th location (since 17 you're downsampling by a factor of 4) you 16 can add up the points near it, multiplied 15 by sin (a dt) / a dt, where dt is the distance 14 in time to that location. How nearby? Well, that 13 depends on how good you want it to sound. It's 12 common to ignore everything outside the 11 first zero, for instance, or just take the 10 number of points to be the ratio by which 9 you're downsampling.
Finally there's the 8 piss-poor (but fast) way of just discarding 7 the majority of the samples, keeping just 6 the zeroth, the fourth, and so on.
Honestly, if 5 it fits in memory, I'd recommend just going 4 the DFT route. If it doesn't use one of 3 the software filter packages that others 2 have recommended to construct the filter 1 for you.
The process you're after called "Decimation". There 3 are 2 steps:
- Applying Low Pass Filter on the data (In your case LPF with Cut Off at Pi / 4).
- Downsampling (In you case taking 1 out of 4 samples).
There are many methods to design 2 and apply the Low Pass Filter.
You may start 1 here:
You could make use of libsamplerate to do the heavy lifting. Libsamplerate 8 is a C API, and takes care of calculating 7 the filter coefficients. You to select from 6 different quality filters so that you can 5 trade off quality for speed.
If you would 4 prefer not to write any code, you could 3 just use Audacity to do the sample rate conversion. It 2 offers a powerful GUI, and makes use of 1 libsamplerate for it's sample rate conversion.
I would try applying DFT, chopping 3/4 of 3 the result and applying inverse DFT. I can't 2 tell if it will sound good without actually 1 trying tough.
I recently came across BruteFIR which may already 1 do some of what you're interested in?
You have to apply low-pass filter (removing 12 frequencies above 5500 Hz) and then apply 11 decimation (leave every Nth sample, every 10 4th in your case).
For decimation, FIR, not 9 IIR filters are usually employed, because 8 they don't depend on previous outputs and 7 therefore you don't have to calculate anything 6 for discarded samples. IIRs, generally, depends 5 on both inputs and outputs, so, unless a 4 specific type of IIR is used, you'd have 3 to calculate every output sample before 2 discarding 3/4 of them.
Just googled an intro-level 1 article on the subject: https://www.dspguru.com/dsp/faqs/multirate/decimation
More Related questions