# Lair Of The Multimedia Guru

## 2007-03-25

### Resampling

Resampling is the problem of converting a signal which is sampled at a given samplerate to one which is sampled at another samplerate. This is the same as converting a discretely sampled signal to a continuous one and then converting it back to a discretely sampled one.
There are many ways to look at this problem which dont lead to the same solutions

#### The optimization point of view

The problem can be seen as one of optimization that is choose the function which converts the discrete samples to a continous signal so that the error to the original correct signal is minimized. And then select the function which converts the continuous signal to discreet samples so that together with a optimal back conversation the error is minimzed.
While this is a nice generic definition of the problem it doesnt point to a obvious solution, also the optimal solution will almost certainly not be remotely close to a common sense definition of what samples represent, it might be more similar to a vector quantizer or other odd non linear thingy …

#### The linear optimization point of view

Like in the previous point we again try to find continuous->discrete->continuous convertion functions which minimize the error to the original but we restrict ourselfs to linear convolutional filters. Note continuous signal here means one which is either given analytically (like a polynom) or discretely sampled at higher resolution.
Here its a matter of linear algebra and a training set of signals to find the
optimal discrete->continuous filter for a given continuous->discrete filter. And the optimal continuous->discrete filter for a given discrete->continuous filter.
In this framework it is also possible to consider output and input devices. For example a monitor which converts the discretely sampled data into very tiny fuzzy colorfull dots, which when they are all added together give a continous signal. The monitor here is a discrete -> continuous filter for which we can find the optimal linear least squares filter which changes a given signal to a discretly sampled one.

#### The bandlimited point of view

Its possible to look at a amplitude per time signal also as a amplitude per frequency signal. If our signal now happens to not contain any non zero frequency component above a specific frequency X then this signal is said to be band limited. If we now sample such a signal with a sampling rate >2X then we can exactly restore the original continuous signal from these samples by simple convolution with the sinc filter (sin(x)/x) (the discrete samples here would be at x=…, -pi, 0, pi, 2pi, …).
And to remove all frequencies above a given frequency X while retaining all below again simply convolution with sinc is the awnser. So resampling becomes a simple matter of using the sinc filter with the correct scaling to find the new samples. Sadly there are a few problems and missunderstandings with this approch.

##### Summing a infinite number of samples fallacy

Some people say sinc cannot be used because it would require summing an infinite number of samples, this is of course not true as images and audio on computers tend not to contain infinite many samples to begin with. Or more precissely a signal of N samples can simply be converted into frequency domain with a FFT the too high frequency coefficients could then be set to 0 and then a inverese FFT could be applied. This is exactly equivalent to infinite periodic extension and application of the sinc filter. Direct applicaton of the sinc filter in time domain is trickier.

##### sincs latency

As the sinc filter has infinite support (theres no X after which all values are 0) it cannot be applied in realtime, it needs all input samples before it can output anything.
That also means all applications and libraries which claim to use sinc for resampling in realtime lie.

##### Causality and sinc

Most output samples depend on most (practically all) future input samples

##### Shooting sinc and the whole bandlimited theory down

Imagine absolute silence, open space, no obstacles to reflect sound and a gun, theres no doubt that the correct continuous sound amplitude will be exactly 0 (the air pressure will not change), now the gun is fired, which causes a very loud “bang” pretty much a very sharp spike in the amplitude and air pressure and then perfect silence again. Now this signal is clearly not bandlimited, so lets make it bandlimited by convolving it with sinc, the resulting signal will of course look like the sinc filter itself (convolution of a single spike is simply a identity operation). At that point we will have a bandlimited signal of a gunshot. Lets assume our signal is sampled at 8khz
in that case the amplitude 1 second before the gunshot will be 1/(pi*8000) of the loudness of the gunshot while this seems very little given a loud enough gunshot its audible and this simply isnt correct. Also for shorter distances like for example 5 samples before the gushot its 1/(pi*5) ~ 1/16 which for example when resampling an image with a sharp edge leads to terrible ringing artifacts.

#### Windowed sinc

An alternative to the infinite support sinc filter are windowed sinc filters, which are simply sinc componentwisely multiplied by a window function. These filers have compact support (are zero everywhere except a few samples). For them the the size of the transition band where frequencies are neither left at their amplitude nor attenuated to near 0 cannot be 0. Its size, the length of the filter the stopband attenuation and all the other parameters depend on each other and a trandeoff must be made …

Filed under: Uncategorized — Michael @ 03:50

1. not to mention ‘sinc’ has negative values, which makes some of the resampled image pixels negative… how do you display negative photons?

Comment by User — 2007-03-27 @ 02:22

2. see also Gibbs phenomenon ( http://en.wikipedia.org/wiki/Gibbs_phenomenon ) and Sigma approximation ( http://en.wikipedia.org/wiki/Sigma-approximation ) which a possible adjustment to sinc approximation that can be used to eliminate Gibbs ringing phenomenon.

Comment by User — 2007-03-27 @ 02:51

3. I don’t agree – all signals are bandlimited, unless we accept that time isn’t continuous. In the gunshot example, you just aren’t sampling fast enough.

I agree that sinc isn’t the be-all end-all, however a windowed approximation it’s rather well suited for certain uses:

http://www.all-in-one.ee/~dersch/interpolator/interpolator.html

Comment by Some User — 2007-04-16 @ 19:37

4. > I donâ€™t agree – all signals are bandlimited,

this simply isnt true, as proof just take a triangular wave, its fourier series is infinite
thus it is not band limited

> unless we accept that time isnâ€™t continuous.

see above, the triangular wave is continuous yet it is not band limited

> In the gunshot example, you just arenâ€™t sampling fast enough.

the gunshot example was not sampled it was using an ideal exact and continuous represenstation
which was then low pass filtered after which the signal was no longer causal that is the gunshot could be heared long before the gunshot, at the end it was sampled yes but this doesnt change the signal anymore

Comment by Michael — 2007-04-17 @ 12:53

5. Alright, you do have a point, I didn’t know what crossed my head.

However, doesn’t the human auditive system work in the frequency domain and is effectively bandlimited (20-20K Hz)?

I don’t know the technical terms for this, but keep in mind that even in a very short very loud sound you are likely to take a positive sample and a negative one (for example +30000 followed by -30000), and when you do the sinc sum, both would help cancel each other faster.

If humans can hear up to ~20KHz, then you should sample at >40KHz. If the gunshot lasts at least 0.05 ms, then you have two samples. If it lasts less, it’s over 20KHz and you won’t hear it anyway.

Then again, I’m not a expert on this, so I might be very wrong.

Comment by Some User — 2007-04-17 @ 14:02