## libsamplerate

In This Posting, I gave much thought, to how the ‘Digital Audio Workstation’ named QTractor might hypothetically do a sample-rate conversion.

I thought of several combinations, of “Half-Band Filters” that are based on the Sinc Function, and ‘Polynomial Smoothing’. The latter possibility would have often caused a computational penalty. But there was one, simpler combination of methods, which I did not think of.

QTractor uses a GPL Linux library named ‘libsamplerate‘. Its premise starts out with the idea, that a number of Half-Band Filters can be applied in correct sequences with 2x oversampling or 2x down-sampling, to achieve a variety of effects.

But then, ‘libsamplerate‘ does something ingenious in its simplicity: A Linear Interpolation! Linear interpolation will not offer as clean a spectrum as polynomial smoothing will in one step. But then, this library makes up for that, by just offering a finer resolution of oversampling, if the client application chooses it.

This library offers three quality levels:

1. SRC_SINC_FASTEST
2. SRC_SINC_MEDIUM_QUALITY
3. SRC_SINC_BEST_QUALITY

Now, in This Posting, I identified an additional issue which arises, when we are doing an “Arbitrary Re-Sampling” and down-sampling. This issue was, that the source stream contains frequency components that are higher than the output stream Nyquist Frequency, and which need to be eliminated, even though the output stream is not in sync with the source stream.

To the best of my understanding, this problem can be solved, by making a temporary output stream 2x as fast as the final output stream, and then down-sampling by a factor of 2 again…

Sincerely,

Dirk

(Edit 07/21/2016 : ) The ‘GPL’ requires that this library be kept as free software, because it is in the nature of the GPL license, that any work derived from the code must also stay GPL, which stands of the “General Public License”.

But, because the possibility exists of some commercial exploitation being sought after, the Open-Source Software movement allows for a type of license, which is called the ‘LGPL’, which stands for the “Lesser General Public License”. The LGPL will allow for some software to be derived from the original code, which can be migrated into the private domain, so that the author of the derived code may close their source-code and sell their product for profit.

There exists a library similar to this one, that is named ‘libresample‘, with the express purpose that that one be LGPL code.

Yet, the authors of ‘libsamplerate‘ believe that this GPL version of the library is the superior one, which they would therefore have kept in the public domain.

## When Audacity Down-Samples a Track

In This Posting, the reader may have seen me struggle to interpret, what the application ‘QTractor‘ actually does, when told to re-sample a 44.1 kHz audio clip, into a 48 kHz audio clip. The conclusion I reached was that at maximum, the source track can be over-sampled 4x, after which the maximum frequencies are also much lower than the Nyquist Frequency, so that if a Polynomial Filter is applied to pick out points sampled at 48 kHz, minimum distortion will take place.

If the subject is instead, how the application ‘Audacity‘ down-samples a 48 kHz clip into a 44.1 kHz clip, the problem is not the same. Because the Nyquist Frequency of the target sample-rate is then lower than that of the source, it follows that frequencies belong to the source, which will be too high for that. And so an explicit attempt must be made to get rid of those frequency components.

The reason Audacity is capable of that, is the fact that a part of its framework causes a Fourier Transform to be computed for each track, with which that track is also subdivided into overlapping sampling windows. The necessary manipulation can also be performed on the Fourier Transform, which can then be inverted and merged back into a resulting track in the time-domain.

So for Audacity just to remove certain frequency ranges, before actually re-sampling the track, is trivial.

If my assumption is, that QTractor does not have this as part of its framework, then perhaps it would be best for this application only to offer to re-sample from 44.1 kHz to 48 kHz, and not the other way around…

Dirk

## A Further Note on Half-Band Filters

In This Posting, I mentioned “Half-Band Filters”, which are a special case of the “Sinc Filter”, that are faster to compute than typical sinc filters, because in this case, half the coefficients are equal to zero, which means that half the samples which are usually multiplied by coefficients, do not need to be.

Even though this is easy to say, there is one consideration in coding the principle, essential to make it work.

A tight loop would need to dereference half the samples in the input stream, in a pattern which also skips the center sample, which is usually multiplied by the coefficient (+1) as an exception. After this loop has finished, the central input sample would be added to the accumulated, weighted sum, to arrive at the final value for the output sample.

Dirk

(Edit 03/21/2017 : )

There is an additional observation to offer, on half-band filters. Their direct use for playback would suffer from a problem I have yet to characterize. If used directly as input / output filters, this kind of filter assumes that an analog filter is capable of passing an intended signal-frequency, while suppressing a frequency adequately, which is only twice as high – i.e. one octave higher.

The problem with this is the fact that realistic analog low-pass filters never bring down the amplitude completely to zero. And if they are given only one octave to work with, then they will effectively be reducing the signal energy at the oversampled Nyquist Frequency to -50db. While this might sound adequate, for many purposes it is not.

This parasitic energy which is passed will simply show up in critiques of the technology as noise. And if it is either the goal or the achievement, that the noise-level be as low as -90db, and yet that the frequency-response curve be very clean, then the analog filters need to be given 2 or 3 octaves to work with. And this will mean that the digital system needs to be oversampled 4x or maybe even 8x.

This can be accomplished, if several half-band filters are put in a dyadic chain, but then something else cannot be accomplished.

## A Note on Polynomial Fitting

In This Posting, I wrote that Polynomial Smoothing / Fitting / Approximation can be used in conjunction with a Sinc Filter, to convert Sample-Rates. I should note that sometimes, Polynomial functions can be used by themselves.

This could be an explanation for how early CD Players eventually replaced the sinc filter, with “Mathematical Sound Shaping” (M.A.S.H.). The sinc filters were much maligned for their lack of immediacy. In their place, M.A.S.H. suffered from some level of distortion at frequencies near the Nyquist Frequency.

Further, experiments I have yet to carry out with ‘QTractor‘, may show, that it will convert

44.1 kHz -> 48 kHz

using the exact same method as

48 kHz -> 44.1 kHz

Assuming that is, I set the Global filtering method to a (slower) non-default setting.

Dirk