HydrogenAudio

Hydrogenaudio Forum => General Audio => Topic started by: Gecko on 2004-08-27 23:40:33

Title: Why is perfect resampling impossible?
Post by: Gecko on 2004-08-27 23:40:33
I've taken it for granted that there is no such thing as perfect resampling, but now I ask myself: why?

The sampling theorem says a signal of limited bandwidth f will be perfectly reproduced when sampled with at least 2*f.

From a 44.1kHz file one can perfectly recreate frequencies < 22.05kHz.
From a 48kHz file one can perfectly recreate frequencies < 24kHz.

So why can't I take the perfectly reproduced content of the 44.1kHz file and somehow put it into a 48kHz file? That 48kHz file is more than adequate to reproduce frequencies below 22.05kHz.

The only thing I can come up with is amplitude quantization (AFAIK the sampling theorem only fully works when considering infinite amplitude resolution). Of course I am only talking about upsampling here.
Title: Why is perfect resampling impossible?
Post by: sven_Bent on 2004-08-27 23:54:41
maybe I'm out of moy boundries. but there is my exlanation.

because you dong have infinite resoluted rond wave anymore but a digital "steps" and  the step interval doesn fit the new step interval.

its like resizing af pictures from 80x600 to 1024x768.. some of the pixles/sampels have to consing of the values of to (or more) pixels/sampels which are impossible (its hard to have one value that is two values)
Title: Why is perfect resampling impossible?
Post by: /\/ephaestous on 2004-08-28 00:03:55
It's posible to do perfect reampling only if you have an infinite bit depth and all the sound frequrencies are below the max frequency of the new samplerate. For example, it's posible to divide 10/9 but you can't represent the answer correctly using decimals.
Title: Why is perfect resampling impossible?
Post by: Omion on 2004-08-28 00:13:40
I may be completely wrong about this, but here goes:

When upsampling, I think you may also need a perfect brick wall filter to get rid of any resampling artifacts above the old nyquist limit. But that requires an infinite amount of time data to calculate correctly.

When downsampling, the problem is backwards: you need the brick wall filter before the resampling to remove any frequencies that may produce artifacts after resampling.

Or something? 

Please read any eventual replies to this before calling it the truth, though...
Title: Why is perfect resampling impossible?
Post by: bryant on 2004-08-28 00:27:07
I think maybe you can upsample perfectly (or as accurately as you want). Imagine just converting the digital signal into an analog signal and resampling it (mathematically, of course).

The problem is with downsampling because you have to filter out the information past f/2, and that's where you run into problems. You can make filters that are arbitrarily good in any one characteristic (i.e. stopband ripple) but you can't make one good at everything. It's always a compromise.

Or maybe not... 
Title: Why is perfect resampling impossible?
Post by: Pio2001 on 2004-08-28 00:55:32
Upsampling, the problem is to guess the value of the new samples, that are between the old ones. If you take the nearest neighbour, then lowpass, how can you be sure that the jitter introduced by the time shift, when a value given at time t is translated to its nearest neighbour that stands at t+e, won't introduce some distortion well below the maximum frequency of the recording ? I don't know if it is the case, but I think it might be the main problem.
The same goes if you use linear interpolation. The error is reduced, but you may still introduce audible distortion. Bryant's idea is interesting. If you find an algorithm that can describe a continuous curve that passes through all old values, and have no frequency above the old limit, you win. You just have to take its value at the new sample positions.

I wonder if one would have to solve an equation with as much unknown values as the number of samples of the track.
Title: Why is perfect resampling impossible?
Post by: Omion on 2004-08-28 01:03:58
Quote
I wonder if one would have to solve an equation with as much unknown values as the number of samples of the track.
[a href="index.php?act=findpost&pid=237521"][{POST_SNAPBACK}][/a]

It can be done with a fourier transform as long as the file. You'll end up with an amplitude + phase for a whole bunch of frequencies, which you can use to regenerate the sines and cosines in the waveform.

It seems that that would do it (assuming infinite sample precision), but I'm not 100% sure... maybe I'm just bieng too skeptical today.

[edit] Wait a second... doesn't the fourier transform assume the signal is periodic? I think that would screw things up, then. You might need to add an infinite amount of padding at the beginning and end.
Title: Why is perfect resampling impossible?
Post by: idioteque on 2004-08-28 01:19:07
Quote
I think maybe you can upsample perfectly (or as accurately as you want). Imagine just converting the digital signal into an analog signal and resampling it (mathematically, of course).

The problem is with downsampling because you have to filter out the information past f/2, and that's where you run into problems. You can make filters that are arbitrarily good in any one characteristic (i.e. stopband ripple) but you can't make one good at everything. It's always a compromise.

Or maybe not... 
[a href="index.php?act=findpost&pid=237513"][{POST_SNAPBACK}][/a]


Agreed.  It has always been my impression that the filtering introduces the 'real world' limitations that prevent the theoretical perfect reconstruction.
Title: Why is perfect resampling impossible?
Post by: Pio2001 on 2004-08-28 01:40:42
Then how do you explain the SB live resampling being bad ? Where do come all these alien sounds in the "udial" killer sample from, when you upsample from 44.1 to 48 kHz ?
Title: Why is perfect resampling impossible?
Post by: idioteque on 2004-08-28 01:44:23
[span style='font-size:8pt;line-height:100%']edit: thread split[/span]
Title: Why is perfect resampling impossible?
Post by: Pio2001 on 2004-08-28 01:46:59
Quote
[edit] Wait a second... doesn't the fourier transform assume the signal is periodic? I think that would screw things up, then. You might need to add an infinite amount of padding at the beginning and end.
[a href="index.php?act=findpost&pid=237523"][{POST_SNAPBACK}][/a]


No, the Fourier Serial assumes it (sum of sines), not the Fourier Transform (integral of complex functions). Anyway, here we have none of them. It is a Discrete Fourier Transform (DFT), that we compute, thanks to a Fast Fourier Transform (FFT) algorithm. So we'd need an FFT size of the power of 2 superior to the number of samples. That is still a huge amount of calculus. I wonder if the computation time grows faster than the sample size.
Title: Why is perfect resampling impossible?
Post by: idioteque on 2004-08-28 01:49:45
Quote
Then how do you explain the SB live resampling being bad ? Where do come all these alien sounds in the "udial" killer sample from, when you upsample from 44.1 to 48 kHz ?
[a href="index.php?act=findpost&pid=237529"][{POST_SNAPBACK}][/a]


You're talking about a specific implementation which could be flawed for a number of reasons.  I don't know the details of how the sample rate converter is broken in the SB Live so I can't speculate. 

I was talking about what the limitations on a practical implementation are.
Title: Why is perfect resampling impossible?
Post by: Pio2001 on 2004-08-28 01:51:19
Yes, but doesn't the udial sample produce audible aliases whatever 44.1 to 48 kHz converter is used ?
Title: Why is perfect resampling impossible?
Post by: idioteque on 2004-08-28 01:55:55
Quote
Yes, but doesn't the udial sample produce audible aliases whatever 44.1 to 48 kHz converter is used ?
[a href="index.php?act=findpost&pid=237534"][{POST_SNAPBACK}][/a]

I don't know, do you get aliasing with the foobar SRC?
Title: Why is perfect resampling impossible?
Post by: Garf on 2004-08-28 02:26:34
Quote
Yes, but doesn't the udial sample produce audible aliases whatever 44.1 to 48 kHz converter is used ?
[a href="index.php?act=findpost&pid=237534"][{POST_SNAPBACK}][/a]


No. Any high quality filter will not have any at all. Not audible, and the best ones not visible (on a spectrograph) either.
Title: Why is perfect resampling impossible?
Post by: Omion on 2004-08-28 02:44:01
Quote
No, the Fourier Serial assumes it (sum of sines), not the Fourier Transform (integral of complex functions). Anyway, here we have none of them. It is a Discrete Fourier Transform (DFT), that we compute, thanks to a Fast Fourier Transform (FFT) algorithm. So we'd need an FFT size of the power of 2 superior to the number of samples. That is still a huge amount of calculus. I wonder if the computation time grows faster than the sample size.
[a href="index.php?act=findpost&pid=237531"][{POST_SNAPBACK}][/a]

The FFT is O(n log n), so if you double the number of samples, the computation time will more than double. However, it approaches double for extremely large data sets.

So, you're saying that the DFT is not periodic, but needs to be a power of 2? I was under the impression that DFTs could be any length you wanted.

[edit:] Aah, the FFT is a power of 2. But what do you extend it with? I'd think that anything would change the frequency components inside the song.
Title: Why is perfect resampling impossible?
Post by: bleh on 2004-08-28 02:45:59
Quote
I wonder if the computation time grows faster than the sample size.
[a href="index.php?act=findpost&pid=237531"][{POST_SNAPBACK}][/a]

The FFT function is O(n*log n) IIRC, so, yes, it does.

edit - beaten to it :|.
Title: Why is perfect resampling impossible?
Post by: Bionic-Badger on 2004-08-28 03:12:33
Barring bit-resolution problems, wouldn't it be possible to achieve an as-perfect-as-you-can-get down/upsampling by applying an ideal low-pass filter to the sampled waveform and then sampling again?  In practice that would mean calculating a sinc-pulse value for every sample in the original waveform for each sample in the resulting waveform, but given the bit resolution, you "only" need 2 * (2^n) samples (n = bits per sample in original) to be very close to the noise floor of the original bit-depth (i.e. a sample 2^n samples away has 1/(2^n) effect on a sample).

Not a close-to-real-time solution, but doable on a computer.

Please correct me if I'm wrong.

-BB-
Title: Why is perfect resampling impossible?
Post by: Elohe on 2004-08-28 05:00:56
For upsampling exist a simple tecnic based in the splines, but is computational interesting, it assume than the signal are a function betwen a maximun and minimun, and only calculate a polinomic(  lagrance, newton...) interpolation betwen a consecutive maximun and minimun, it produces a good signal. And if posible to aply a simple armonic restaurator for the los signals, like sbr or plusV, we can recreate the lost signals but with no much fidelity, it can be sound great the upsampling in a extremes cases like 8 -> 48 , the problen if the values of the armonic amplification., a good aproximation is "armonic/((armonic-1)*e)"
Title: Why is perfect resampling impossible?
Post by: bryant on 2004-08-28 05:40:14
Quote
The same goes if you use linear interpolation. The error is reduced, but you may still introduce audible distortion.
[a href="index.php?act=findpost&pid=237521"][{POST_SNAPBACK}][/a]

You're right, linear interpolation is not good for this. However, there is a very simple formula for the "perfect" interpolation filter:

cf = sin(d*pi) / (d*pi)

where cf is the coefficient for a given sample's weight and d is the distance from that sample to the desired interpolation position (in samples). You simply have to multiply every sample by its coefficient and add them up to get the interpolated value.

The practical problem with this is that it doesn't taper quickly enough as you move away from the center, so in real-world applications you have to weigh the whole filter with a fixed-length windowing function.
Title: Why is perfect resampling impossible?
Post by: Omion on 2004-08-28 07:18:20
Quote
Quote
The same goes if you use linear interpolation. The error is reduced, but you may still introduce audible distortion.
[a href="index.php?act=findpost&pid=237521"][{POST_SNAPBACK}][/a]

You're right, linear interpolation is not good for this. However, there is a very simple formula for the "perfect" interpolation filter:

cf = sin(d*pi) / (d*pi)

where cf is the coefficient for a given sample's weight and d is the distance from that sample to the desired interpolation position (in samples). You simply have to multiply every sample by its coefficient and add them up to get the interpolated value.

The practical problem with this is that it doesn't taper quickly enough as you move away from the center, so in real-world applications you have to weigh the whole filter with a fixed-length windowing function.
[a href="index.php?act=findpost&pid=237568"][{POST_SNAPBACK}][/a]

Wow. Just add up a bunch of sinc impulses. It's so simple! 

But there's still the problem of what to assume the samples past either end of the song are. The obvious thing to do is assume they're 0, and only calculate the function for the samples actually contained in the file. However, in real life, the song will probably be followed by another song, which will tweak the frequencies slightly.

Imagine a sine wave chopped into two files, where the file boundary is at a waveform max. On resampling the waveforms separately, Gibbs' Phenomenon will show its ugly head and give ripples on either end of the files. This is correct if the files are to be played completely separately, but incorrect if they will be played together.

All in all, though, that would seem to be the perfect filter, barring a few pedantic real-world technicalities. Seeing as there's no way to know what will follow in anybody's playlist, it looks like the "most perfect" possible.
Title: Why is perfect resampling impossible?
Post by: Bionic-Badger on 2004-08-28 11:39:57
You're also looking at waves at the sampling frequency.  More than likely you'll see more distortion from mastering artifacts than from missing the samples before and after the "lifetime" of the wave.  At 22050+ hz, and with most tracks starting and ending with silence, nobody would be able to detect the difference realistically.

-BB-
Title: Why is perfect resampling impossible?
Post by: Lyx on 2004-08-28 12:51:19
Quote
...and with most tracks starting and ending with silence...


Dont be so fast in asuming that. With mainstream music you may be right, but as soon as you leave the mainstream sector(and with mainstream, i also mean popular acts of subcultures) a significant number of albums have seamless trackchanges. For example, about 1/3 of my music collection has seamless trackchanges.

I think you also misunderstood the intention of the topic a bit - its was more a theoretical question... the search for the perfect resampler. The question was less if one could notice the difference in everyday-listening (most people cannot notice a resampler anyways), but more a technical motivation to "achieve the impossible".

- Lyx
Title: Why is perfect resampling impossible?
Post by: Pio2001 on 2004-08-28 14:21:36
Ah thanks, Bryant, it is clearer now.
So the computational limit is how much neughborous samples must be taken into account so that the error is below the definition of the recording.
Unfortunately, the sum of the sinc functions is majored by sum(1/x, x from n to infinite), and it may be as high as this if all contributions are all in phase.
I've not got my math courses anymore, but I think I remember that this sum is infinite for any n.
But the track that we resample is not infinite. Maybe there is a way to ensure that when we stop at a given distance from the new sample, all remaining contributions can't exeed the quantisation noise.

Anyway, all this is only valid if the Nyquist frequency (22050 Hz for example) is strictly inaudible. Have you already tried a sinc resampling for enlarging pictures ? The result is awful, ripple is visible everywhere ! Bicubic resampling looks much better.
Title: Why is perfect resampling impossible?
Post by: Yaztromo on 2004-08-28 15:06:27
Quote
The sampling theorem says a signal of limited bandwidth f will be perfectly reproduced when sampled with at least 2*f.

From a 44.1kHz file one can perfectly recreate frequencies < 22.05kHz.
From a 48kHz file one can perfectly recreate frequencies < 24kHz.

So why can't I take the perfectly reproduced content of the 44.1kHz file and somehow put it into a 48kHz file? That 48kHz file is more than adequate to reproduce frequencies below 22.05kHz.


Don't want to drag this thread off topic but you've brought up a question that i've wondered before.

If what you say is correct, would it make more sense upsampling 44.1khz to 88.2khz, instead of upsampling to 48khz when playing back through cards such as Creative's Audigy 2 series.

Thanks
Title: Why is perfect resampling impossible?
Post by: Garf on 2004-08-28 15:19:05
Quote
Don't want to drag this thread off topic but you've brought up a question that i've wondered before.

If what you say is correct, would it make more sense upsampling 44.1khz to 88.2khz, instead of upsampling to 48khz when playing back through cards such as Creative's Audigy 2 series.

Thanks
[a href="index.php?act=findpost&pid=237638"][{POST_SNAPBACK}][/a]


Only if the card could actually play the 88.2kHz signal without resampling again, which it won't
Title: Why is perfect resampling impossible?
Post by: Yaztromo on 2004-08-28 15:23:02
Quote
Only if the card could actually play the 88.2kHz signal without resampling again, which it won't


I was under the impression that Audigy 2 cards resampled everything from 44.1khz downwards but did not resample anything 48khz up to 96khz. I believe I am correct on this  . So in this case it does make sense to go to 88.2khz instead.
Title: Why is perfect resampling impossible?
Post by: Gecko on 2004-08-28 18:00:49
Thank you all very much for your feedback.

Those of you who blame the filtering are probably correct, I suppose. The sampling theorem also requires an ideal lowpass which we do not have. Without an ideal lowpass you can't perfectly reconstruct the original waveform to begin with, so we're stuck.

If you could use an ideal lowpass, the interpolation type should be irrelevant. If I am not mistaken, then ANY form of interpolation only creates additional frequencies above f/2 which could then be removed by the ideal lowpass.

The multiply-everything-with-sinc idea sounds a lot like convolution to me, which would in effect only be another way of going the Fourier route, but I may be completely wrong here.

The Fourier method seems very interesting. I'll see if I can make it work in Matlab. The discrete Fourier transform doesn't offer an optimal lowpass because of discrete frequencies so maybe there will be problems.
Title: Why is perfect resampling impossible?
Post by: Gecko on 2004-08-28 19:06:11
Well, this seems to work pretty good, judging from what I hear and looking at Cool Edit spectrographs:

Code: [Select]
% MATLAB
source = wavread('udial.wav');
spectrum = fft(source);

% length means number of samples
new_length = length(source)*480/441;
padding = round((new_length - length(spectrum))/2);

% ye mägygk:
new_spectrum = fftshift([ zeros(padding,2); fftshift(spectrum); zeros(padding,2) ]);

% there are some non-zero imaginary components which we have to eliminate
resampled = real(ifft(new_spectrum));

wavplay(source,44100)
wavplay(resampled,48000)

wavwrite(resampled,48000,16,'udial_resampled.wav');


The above only works for stereo samples and only does 44.1kHz to 48kHz but should be easily modifiable. For optimal results the input file should have an even number of samples but you could build something that pads in an extra zero sample at the end.

Is udial.wav supposed to have so much high frequency content? I guess that is what trips up bad resamplers. I can hear parts of it and it is damn annoying. I didn't think I could hear that high. My soundcard is a Terratec Aureon Sky (flashed to Prodigy) which shouldn't resample.
Title: Why is perfect resampling impossible?
Post by: Omion on 2004-08-28 21:56:35
I've been running with the sinc-impulse idea, and it seems that it would be fairly easy to do downsampling with it.

The problem is that you need to filter out everything above the new, lower nyquist limit to avoid aliasing, which the sinc thing does not do. However, if you make the sinc waves wider, you effectively get a low-pass filter.

For example: if you're downsampling by 2, use the function sin(pi x / 2) / (pi x). It is a low-pass filter, and it seems to work pretty well, but I haven't done extensive testing, and I don't know if this would be the "perfect" downsampler. Any thoughts?
Title: Why is perfect resampling impossible?
Post by: Pio2001 on 2004-08-28 22:44:13
Quote
The sampling theorem also requires an ideal lowpass which we do not have.


We have it : perform a DFT, cut the frequencies that you want to remove, then perform the IDFT (Inverse Discrete Fourier Transform). You can also perform a convolution with the needed sinc function, which is exactly the same thing.

Quote
If you could use an ideal lowpass, the interpolation type should be irrelevant. [...] If I am not mistaken, then ANY form of interpolation only creates additional frequencies above f/2 which could then be removed by the ideal lowpass.


I don't think so, look, let's consider this wave file :

(http://perso.numericable.fr/laguill2/pictures/resampling/20k.png)

If you resample it to 6/5th its original sample rate, you insert one null sample every 5 original samples. Now if you do it with the simplest possible interpolation, which consists in taking the value of the previous sample, you get

(http://perso.numericable.fr/laguill2/pictures/resampling/20kresampled.png)

It seems clear to me that with some bits of the original wavs put like this, some distortion is introduced below the Nyquist frequency.
An oversampling that consists in doubling the sample rate only introduces frequencies above f/2. But I don't think that it is the case with resampling at other sample rates.
Example, here is the sonogram of udial.wav from 20 to 22050 Hz :

(http://perso.numericable.fr/laguill2/pictures/resampling/udial44.png)

Now, I resample it to 46000 Hz with the lowest quality, and apply an antialias, with SoundForge :

(http://perso.numericable.fr/laguill2/pictures/resampling/udial46.png)

We can see that the interpolation process has created some artifacts well below 22050 Hz. You can remove what you want above with the lowpass that you want, something will always remain below.

Now, if we want to use Fourier Transform in order to resample, we can forget about DFT. All we can do with it is this :

(http://perso.numericable.fr/laguill2/pictures/resampling/dft.png)

We can apply the brickwall filter, but when we perform the IDFT, we are stuck with one discrete value per original sample. We have still to resample, and we still don't have any perfect algorithm.
Since we need to compute the values taken by the lowpassed function between the samples, we need a complete description of it, not only a discrete one.

We can achieve this with a convolution with the sinc function, that will lead to an ideal lowpass :

(http://perso.numericable.fr/laguill2/pictures/resampling/sinc.png)

This means to sum up the sinc function multiplied with each sample :

(http://perso.numericable.fr/laguill2/pictures/resampling/moresinc.png)

Since we can compute sinc(x)=sin(x)/x for any x, we can get the right value for any position.
The only thing we're left with is computing a sum of N values for each sample. N being the total number of samples of the wave file.
We could begin with computing the value of sinc(x) for any x that can be used, but the distances from one original sample to all the new samples can be different for all original samples. So we must calculate the sinc function NxN times.
For a 7 minutes wave file, we have 37,044,000 sample, thus we need to compute 1,372,257,936,000,000 times the value of a given sinc(x). I let math experts search for a possible way to do this.
Title: Why is perfect resampling impossible?
Post by: Pio2001 on 2004-08-28 22:50:53
Quote
Well, this seems to work pretty good, judging from what I hear and looking at Cool Edit spectrographs:[a href="index.php?act=findpost&pid=237675"][{POST_SNAPBACK}][/a]


If I understand well, you are moving the frequencies inside a new frequency description of the wave, but it seems to me that all your new frequencies will be wrong. If you resample a 1000 Hz sine this way, your padding changes it into a 1044 Hz sine !
Title: Why is perfect resampling impossible?
Post by: NumLOCK on 2004-08-28 23:36:15
Quote
We could begin with computing the value of sinc(x) for any x that can be used, but the distances from one original sample to all the new samples can be different for all original samples. So we must calculate the sinc function NxN times.
For a 7 minutes wave file, we have 37,044,000 sample, thus we need to compute 1,372,257,936,000,000 times the value of a given sinc(x). I let math experts search for a possible way to do this.
[a href="index.php?act=findpost&pid=237715"][{POST_SNAPBACK}][/a]


With current hardware I think this would take about a year 

Maybe we can have a look at the symbolic result (using the series decomposition of the N*N sinc(x) calculations) and see if something cancels out in the results ?
Title: Why is perfect resampling impossible?
Post by: Pio2001 on 2004-08-29 00:05:30
The problem is that each of the N values are something like

Sum (sin (xi)/xi, i, 0, N), with xi being a set of N unpredictable x values. I don't see any way to cancel something.

The only hope is that the result becomes unsignificant once |x| gets big enough, because it is majored by 1/|x|. At least it should be possible to estimate the maximum x above which it is unnessesary to go, given the lenght of the wave file, and the value of the lowpass.

Once sum(1/x, x, a, b) gets significantly below the quantization noise (dithering being taken into account), [a,b] being the interval from the outside of the window already calculated until the end of the wav, we can leave it out.

With some luck, maybe we'll only have to calculate 1000 x N sinc values, which is 37,000 times shorter. According to your estimation, it would take 14 minutes.
Title: Why is perfect resampling impossible?
Post by: aspifox on 2004-08-29 15:00:13
Quote
I've been running with the sinc-impulse idea, and it seems that it would be fairly easy to do downsampling with it.
[a href="index.php?act=findpost&pid=237701"][{POST_SNAPBACK}][/a]

FYI, I'm pretty sure that the official (xiph) oggenc uses sinc resampling internally, if that's at all interesting.  However, oggenc's resampling is famously damaging to audible frequencies!  (I assume/hope that's an implementation error...)
Title: Why is perfect resampling impossible?
Post by: blargg on 2004-08-29 15:42:19
Take a look at Julius O. Smith III's Digital Audio Resampling Home Page (http://ccrma-www.stanford.edu/~jos/resample/), where ideal resampling is described. The PDF version of the page (http://ccrma-www.stanford.edu/~jos/resample/resample.pdf) might be easier to follow than the cluttered web poage.

Laurent de Soras' The Quest For The Perfect Resampler (http://ldesoras.free.fr/doc/articles/resampler.pdf) (PDF) describes a similar method with some optimizations and good illustrations of the process.
Title: Why is perfect resampling impossible?
Post by: Garf on 2004-08-29 16:00:17
Quote
Quote
I've been running with the sinc-impulse idea, and it seems that it would be fairly easy to do downsampling with it.
[a href="index.php?act=findpost&pid=237701"][{POST_SNAPBACK}][/a]

FYI, I'm pretty sure that the official (xiph) oggenc uses sinc resampling internally, if that's at all interesting.  However, oggenc's resampling is famously damaging to audible frequencies!  (I assume/hope that's an implementation error...)
[a href="index.php?act=findpost&pid=237864"][{POST_SNAPBACK}][/a]


It is an implementation problem.

SRC is for example based on exactly the same system, as are libresample and PPHS.
Title: Why is perfect resampling impossible?
Post by: CosmoKramer on 2004-08-29 16:48:50
Quote
I was under the impression that Audigy 2 cards resampled everything from 44.1khz downwards but did not resample anything 48khz up to 96khz. I believe I am correct on this   . So in this case it does make sense to go to 88.2khz instead.
[a href="index.php?act=findpost&pid=237642"][{POST_SNAPBACK}][/a]


The PCI Audigy 2 variants can only play back multiples of 48 kHz (48,96, possibly 192) without resampling. This can easily be demonstrated (which I have done).
Title: Why is perfect resampling impossible?
Post by: Gecko on 2004-08-29 18:43:55
Quote
If I understand well, you are moving the frequencies inside a new frequency description of the wave, but it seems to me that all your new frequencies will be wrong. If you resample a 1000 Hz sine this way, your padding changes it into a 1044 Hz sine !
[a href="index.php?act=findpost&pid=237716"][{POST_SNAPBACK}][/a]


Well, I just tested it with a single sine at 10kHz and the result was also a sine of 10kHz. A frequency sweep over the entire range from 0 to 22050 Hz produced some artifacts during most of the resampled file at the nyquist frequency of the original 44.1kHz file (gone exactly in the middle).

Originally I had tried myself at explaining what the MATLAB script does, but I couldn't understand what I was saying myself so I deleted it.

Think time and not number of samples. Just for argument's sake: take a 1 second audio snippet, record that at 10Hz. You get 10 samples. DFT those 10 samples and you get 10 fourier coefficients. Now record the same audio snippet at 12Hz -> 12 samples -> 12 DFT coefficients. The coefficients are the spectrum plus a mirrored version. Fftshift helps inject zeros in the middle.

Coefficients 10Hz: a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
Coefficients 12Hz: b1 b2 b3 b4 b5 b6 b7 b8 b9 b010 b11 b12

a1 & b1 are the offset
a2 & b2 represent a sine wave of 1 period
a3 & b3 a sine of 2 periods
...
a6 & b6 a sine wave of 5 periods
a7 = a5 a sine of 4 periods while b7 represents a sine of 6 periods
b8 = b6 a sine of 5 periods
...
a10 a sine of 1 period

b10 a sine of 3 periods
b11 a sine of 2 periods
b12 a sine of 1 period

So if the input and output are both supposed to be 1 second long, then the frequency of an X period sine will be the same --> a2 and b2 represent the same frequency. When you IDFT you get 10 and 12 samples respectively. If both are to represent 1 second of audio, then the first must be assumed to be sampled at 10Hz while the later must be assumed to be sampled at 12Hz.

Since the input doesn't contain frequencies above 5 periods, in the output, all coefficients representing more than 5 periods must be zeroed. --> In the above example b7 must be zero. All other input coeffs must be copied to the output coeffs representing the same number of periods (or frequency).

The above Matlab code should do just that.
- Take input and determine number of samples
- Based on desired resampling, the output, in order to be of the same length, must have N * output_f/input_f samples (the way I built the above code only works for upsampling I think, but you could just as well throw away some of the input coeffs to downsample)
- Construct the spectrum of the output based on the input spectrum and zero all higher frequency components
- Perform IDFT and claim: output is sampled at output_f

edit: I found a little flaw in the resampling process as posted above: The resulting signal will be quieter than the input. This is because from the IDFTs point of view, the total energy in the resampled spectrum is less, but this should be relatively easy to fix.

Another shortcoming is the huge amount of memory needed. Matlab handles audio data as double-precision floats internally. A 30sec 44kHz 16bit stereo audio file takes up roughly 20MB. The complex spectrum twice that much.
Title: Why is perfect resampling impossible?
Post by: Gecko on 2004-08-29 18:54:53
Quote
Take a look at Julius O. Smith III's Digital Audio Resampling Home Page (http://ccrma-www.stanford.edu/~jos/resample/), where ideal resampling is described. The PDF version of the page (http://ccrma-www.stanford.edu/~jos/resample/resample.pdf) might be easier to follow than the cluttered web poage.

Laurent de Soras' The Quest For The Perfect Resampler (http://ldesoras.free.fr/doc/articles/resampler.pdf) (PDF) describes a similar method with some optimizations and good illustrations of the process.
[a href="index.php?act=findpost&pid=237888"][{POST_SNAPBACK}][/a]

Thank you for those links. They seem very promising.
Title: Why is perfect resampling impossible?
Post by: Gecko on 2004-08-29 19:18:50
Quote
We have it : perform a DFT, cut the frequencies that you want to remove, then perform the IDFT (Inverse Discrete Fourier Transform). You can also perform a convolution with the needed sinc function, which is exactly the same thing.

I don't think this is entirely true: since the DFT produces a discretized spectrum, what happens between one discrete frequency and another?


Quote
If you resample it to 6/5th its original sample rate, you insert one null sample every 5 original samples. Now if you do it with the simplest possible interpolation, which consists in taking the value of the previous sample, you get

I was thinking of it like this:
First step: find a continuous function, that touches all sampled points. An easy way of this would be using linear interpolation between each sample. When you zoom in real close, you will see some zig-zag lines. Now that you have a continuous representation, you can resample by taking new samples at arbitrary points. Most new samples will be along one of thise zig-zag lines. Some coincide with the old samples.

Here's my logic: since all the action is going on in between the old sample points, the only frequencies you can introduce have to be equal or higher than what could previously happen in between 2 sample points. With linear interpolation, you are in effect inserting many sawtooth like signals with period lengths of 1/sampling_rate and additional harmonics above that. Those additional harmonics is what we don't want and have to eliminate with a lowpass. I can see where you're going with the rest of your post, so I may be totally wrong and sawtooths and other interpolations constructed like that have lower frequency components.
Title: Why is perfect resampling impossible?
Post by: blargg on 2004-08-29 21:04:50
The resampling papers I linked to are a little heavy on the math, so I made a set of graphs to summarize how ideal resampling works. The in:out sampling frequency ratio in this example is 5:6 (5 input samples for every 6 output samples).

(http://www.slack.net/~ant/bl-synth/ha-resample.gif)

The first graph shows the input signal. The next four graphs show the contribution of the first four non-zero input samples to the continuous waveform. Each is a sinc function (sinx/x) scaled by the input sample, with zero-crossings at all input sample points except the middle one. The next graph shows the sum of these four sinc functions, with the original four sample points to show that they match.

The final graph shows the continuous waveform corresponding to the discrete input signal, which is the sum of sinc functions for every input sample. The dots are sample points taken at the output sampling rate. Note how much complexity is hidden in the seemingly innocent input signal (just to be sure I verified that this continuous signal sampled at the input rate matched the input signal).

(I made the diagrams with a little graphic builder I made to experiment with DSP functions)
Title: Why is perfect resampling impossible?
Post by: Gecko on 2004-08-29 21:34:49
Quote
The resampling papers I linked to are a little heavy on the math, so I made a set of graphs to summarize how ideal resampling works. The in:out sampling frequency ratio in this example is 5:6 (5 input samples for every 6 output samples).
[a href="index.php?act=findpost&pid=237942"][{POST_SNAPBACK}][/a]

Thank you very much. This sounds like what others described above.

Quote
Each is a sinc function (sinx/x) scaled by the input sample, with zero-crossings at all input sample points except the middle one.

Does this mean you can get away with a lot less operations?

But I suppose for every target sample, you have to evaluate N times a sinc function and sum it up, since the target sample points will likely not coincide with the zero-crossings (with N being the number of samples in the input).
Title: Why is perfect resampling impossible?
Post by: blargg on 2004-08-30 03:30:03
Quote
Each is a sinc function (sinx/x) scaled by the input sample, with zero-crossings at all input sample points except the middle one.


Quote
Does this mean you can get away with a lot less operations?

But I suppose for every target sample, you have to evaluate N times a sinc function and sum it up, since the target sample points will likely not coincide with the zero-crossings (with N being the number of samples in the input).


Unless the output rate is a whole multiple of the input rate, you'll only hit zero-crossings occasionally.

The sinc function can be windowed; the shorter the window, the more gradual the rolloff. If you have a sampled sine at just under Nyquist, you'll need a really wide window to properly reconstruct the sine otherwise it'll have amplitude modulation.

I should have mentioned that what I described only works for increasing the sampling rate (where no filtering is necessary). If decreasing the sampling rate, the sinc function should be generated at a lower frequency, i.e. if halving the sampling rate, use sin(0.5x)/(0.5x):

(http://www.slack.net/~ant/bl-synth/ha-resample2.gif)

Note how the sinc function used has zero crossings every other input sample (since the output rate is half the input rate). Arbitrary frequency response can be had by filtering the sinc function used.

The input this time is a sine sweep from the the input Nyquist limit to the input rate / 8. You can see above that the output doesn't start to appear until the input sine wave has four samples per cycle, which is when its frequency goes below the Nyquist limit of the output rate.

In both cases the sinc function can be pre-calculated, windowed (since it has infinite length), and sampled at the output interval at several subsample offsets. This reduces processing for each input sample to determining the relative offset to the first output sample that intersects the windowed impulse (which can be done with just an integer multiply then shift), selecting the appropriate set of samples of the sinc function at that offset, and adding each value sequentially to the output buffer.
Title: Why is perfect resampling impossible?
Post by: KikeG on 2004-08-30 20:01:32
Well, in my opinion, programs such as SSRC slow mode or CEP/Audition with pre/post filtering and high quality mode, are able to perfom "virtually" perfect resampling. At last, the difficulty to achieve perfect resampling is just the same difficulty as perfectly brickwall filtering a signal, and these programs filters are very good. Of course, I'm talking of bandlimited interpolation, or what is the same, sinc interpolation, which is the same as "perfect" (upon implementation) interpolation. AFAIK the programs mentioned use this method.

Said that, the only really challenging part of resampling is the filtering part, the rest is not very difficult. AFAIK (I'm not expert in this but I think I know how it works) the easiest way to perform resampling is, summarizing:  first, oversample the signal, then filter everything over original (in case of upsampling) or final (in case of downsampling) fs/2 in order to remove image reflections due to previous process, and then decimate signal.

Say we want to upsample a 32 KHz signal to 48 KHz. The frequency relationship is 1.5, or what is the same, 3/2. The first step, oversampling, is very easy. Just insert 2 zeroes every sample. Now, whe have the signal oversampled 3 times, at 96 KHz. This process leaves intact baseband signal, however it creates image reflections over the original fs/2 (that is, over 16 KHz). The next step is filtering these images, or what is the same, filter everything from 16 KHz and over, using a steep brick wall, linear phase, FIR filter. As a result of this process, signal original amplitude is reduced by a certain factor, that can be easily compensated. Now, we have a signal perfectly upsampled to 96 KHz. The next step, decimation, is achieved just removing 1 every two samples, so at the end of this process whe have the signal decimated by 2, at 48 KHz.

In case of downsampling from, say 48 KHz, to 32 KHz, first we would oversample x2 to 96 KHz, then filter everything over 16 KHz (final fs/2 this time), and then decimate by 3.

For a more general case, the thing is to upsample to the first common multiple of both frequencies, filter, and then decimate.Of course, the trick is to perform the process quickly, perfoming the whole process over chunks of the signal, same way as filtering of signals is usually performed.

Note that this brickwall filtering will, over certain kind of signals, produce time-domain ringing at the frequency cut-off point. In case of upsampling, there will be little or no added ringing, since the original signal must have been already filtered at this same frequency, in order to meet Nyquist requirements for sampling. When downsampling, there may be ringing if there is significant amplitude, impulsive content at the filter frequency cut-off. This ringing can be traded for a gentler roll-off of the brickwall filter, either attenuating the upper part of the original signal spectrum, or allowing some of the image reflections to pass, or a combination of both.

Edit: typos.
Title: Why is perfect resampling impossible?
Post by: 2Bdecided on 2004-08-31 10:09:50
Quote
Take a look at Julius O. Smith III's Digital Audio Resampling Home Page (http://ccrma-www.stanford.edu/~jos/resample/), where ideal resampling is described. The PDF version of the page (http://ccrma-www.stanford.edu/~jos/resample/resample.pdf) might be easier to follow than the cluttered web poage.
[a href="index.php?act=findpost&pid=237888"][{POST_SNAPBACK}][/a]


At last! I was looking for that link.

KikeG's idea is simple in terms of understanding the processing, but the above link explains how it's done in practice. Just remember that a sinc function is a low pass filter.


The reason people say that perfect resampling is impossible is this: if you resample (perfectly) to a higher sample rate, and then resample (perfectly) back to the original sample rate, you will not get back to the original data. There's added ringing at the Nyquist limit due to the infinitely long brick wall filter. (You can calculate an infinitely long filter because the input sample is bounded - assuming the signal is zero beyond these bounds means most of this infinite filter can be ignored).

I've often wondered if it's possible to design the filters to ensure that the extra ringing is cancelled out. You can always make the second low pass filter slightly lower than the first, but that's not perfect.

However, it is possible to design resampling filters for integer relationships that do allow perfect up/down-sampling. You ensure that all the original samples (forming every Nth sample in the upsampled version) remain untouched, and the interpolated samples are formed to give the correct result - it is possible (though whether it counts as perfect or not, I haven't checked). Then, to downsample, you simply dump all the interpolated samples and only keep the original ones. Bingo - perfect resampling!


Finally, when upsampling or downsampling where either the source or destination Nyquist frequency is in the audible range, it's unwise to use a perfect brick wall filter - the ringing is easily audible. Try it with a long FFT in Cool Edit on something like Castanets. It sounds horrible. It's better to move the cut off frequency slightly lower, and to use a gentler filter in order to reduce the time domain ringing.

Cheers,
David.
Title: Why is perfect resampling impossible?
Post by: KikeG on 2004-08-31 12:04:47
Just say, again, that when upsampling a real-world signal (not just an artificial delta pulse), this signal will have been already filtered at the ADC at Nyquist frequency (or before), so additional filtering with a very steep filter at this Nyquist frequency will add very little or no ringing, since there will be little content left at this frequency.

In this same case of upsampling a real-world signal, downsampling it then to original frequency, will result in different sample information, but that information in fact will be the same signal, with a little bit of lineal phase displacement, or what is the same, a small time difference, due to the processing performed. If you could perfectly time-align those signals, and substract them, the difference would be very close to zero, in practice probably below noise floor of original signal, even at Nyquist frequency.

However, in the different case of just downsampling a real-world signal, you are removing information from it, and removing frequencies that were present, so there can be ringing for the kind of signals I explained at my previous post, and of course, upsampling the signal to the original frequency you will have lost these frequencies so there will definitely will be a difference.
Title: Why is perfect resampling impossible?
Post by: bandpass on 2012-06-15 08:11:00
In this same case of upsampling a real-world signal, downsampling it then to original frequency, will result in different sample information, but that information in fact will be the same signal, with a little bit of lineal phase displacement, or what is the same, a small time difference, due to the processing performed.

Apologies for replying to such an old post, but I don't think that's right. There's no inherent time difference due to processing—providing that the resampling filter is linear-phase, the signals can match perfectly time-wise. I know that many resamplers do introduce sub-sample time-shifts but this is probably the implementation discarding the wrong samples during downsampling; if you discard the right ones, correct phase is maintained.

A major factor here though is quantisation error: to stand a hope of reconstructing the original signal from the intermediate signal, the latter must have higher bit-depth.
Title: Why is perfect resampling impossible?
Post by: Woodinville on 2012-06-16 05:21:09
Perfect resampling would require a filter of infinite length. You could never succeed in completing the first sample.

As i'm sure somebody's pointed out that you can be "perfect to quantization noise level or below" which is appropriate.
Title: Why is perfect resampling impossible?
Post by: Kees de Visser on 2012-06-16 15:31:52
As i'm sure somebody's pointed out that you can be "perfect to quantization noise level or below" which is appropriate.
A colleague mastering engineer recently claimed to have measured his Weiss Saracon SRC to be bit-transparent for a 24/44.1->24/96->24/44.1 roundtrip. I was rather surprised since there's low-pass filtering and probably re-dithering involved. Assuming the source material contained some energy close to the Nyquist frequency, wouldn't bit-transparency be quite unlikely ?
Title: Why is perfect resampling impossible?
Post by: bandpass on 2012-06-16 16:01:27
In theory a filter could be tight enough for the error to fall far enough below the quantisation and still be of finite length but it would still need to be horribly long, probably not implementable. 

For any practical demonstration of 'perfect' resampling along the lines suggested, I think the best one could show would be

trim(down(up(x))) = trim(x)

where the only constraint on x is to have a small amount (e.g. < 1%) of freq. headroom (and 'trim' removes a short section of audio from each end of the signal).

Also, the bit-depth must be increased for the intermediate file, and dithering must be disabled for the downsample.

A possible test signal say, at 48kHz (as the maths is easier), is a linear sweep over 10s from 3k down to 0, up to 24k, then down to 21k. Trim away the first and last 1s after conversion and the result is a fullband sweep (without Gibbs at the ends).  I don't think any resampler would ever pass this test. It could do if you trim away a little more at the end though, as this effectively maps to non-zero freq. headroom requirement.
Title: Why is perfect resampling impossible?
Post by: bandpass on 2012-06-18 07:35:50
A colleague mastering engineer recently claimed to have measured his Weiss Saracon SRC to be bit-transparent for a 24/44.1->24/96->24/44.1 roundtrip. I was rather surprised since there's low-pass filtering and probably re-dithering involved. Assuming the source material contained some energy close to the Nyquist frequency, wouldn't bit-transparency be quite unlikely ?

Yes, I find this hard to believe: even with dithering disabled, the test signal would need to be band-limited (if only by a small amount), and carefully engineered not to have quantisation error (but I don't think that's possible with a non-zero signal), and to fade in and out very gently to avoid Gibbs at the ends.

If the resampler has error amplitude Er, then for an error-free round trip, 2 × Er + Eqi must be < Eqo, where Eqi and Eqo are the (maximum) quantisation error of the intermediate and original signals; so even if Er is 0, Eqi must be < Eqo.

Perhaps you could ask your colleague for further details of the test?  Given that perfect resampling is impractical, it would be interesting to establish test criteria for 'practically perfect' resampling—the 'Poppins test', perhaps.
Title: Why is perfect resampling impossible?
Post by: Alexey Lukin on 2012-06-18 16:47:30
Many engineers consider "bit transparent" to be the same as "diff signal not audible".
Title: Why is perfect resampling impossible?
Post by: Alexey Lukin on 2012-06-18 17:01:09
As i'm sure somebody's pointed out that you can be "perfect to quantization noise level or below" which is appropriate.

In the most general case, this is impossible too, JJ. There are digital signals for which sinc interpolation does not converge towards a fixed analog signal as you increase the support of sincs. In other words, as you use better-and-better-quality reconstruction, your reconstructed signal changes and never stops, and rises infinitely in amplitude (which never brings it within a quantization step of a perfect reconstruction because a perfect reconstruction in fact diverges to infinity). A simplest example of such digital signal is {...1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1 ...}
Title: Why is perfect resampling impossible?
Post by: Alexey Lukin on 2012-06-18 17:09:32
For any practical demonstration of 'perfect' resampling along the lines suggested, I think the best one could show would be
trim(down(up(x))) = trim(x)
where the only constraint on x is to have a small amount (e.g. < 1%) of freq. headroom (and 'trim' removes a short section of audio from each end of the signal).

This "frequency headroom" is the key. Most audio signals do not have this headroom as they undergo A/D conversion, so there's no sense in trying to "perfectly" reconstruct them. For such signals it's better to use some reasonably-steep reconstruction filter (like 3-ms long), rather than try to be "perfect" and get a lot of ringing near Nyquist that decays as 1/t.
Title: Why is perfect resampling impossible?
Post by: knutinh on 2012-06-18 20:20:21
So we'd need an FFT size of the power of 2 superior to the number of samples.

http://www.fftw.org/ (http://www.fftw.org/)
"Arbitrary-size transforms. (Sizes with small prime factors are best, but FFTW uses O(N log N) algorithms even for prime sizes.)"
Title: Why is perfect resampling impossible?
Post by: knutinh on 2012-06-18 20:25:59
Perfect resampling would require a filter of infinite length. You could never succeed in completing the first sample.

But a _file_ of finite length contains only a (finite) discrete set of frequencies?

-k
Title: Why is perfect resampling impossible?
Post by: greynol on 2012-06-18 22:16:54
But a _file_ of finite length contains only a (finite) discrete set of frequencies?

The answer is no.
Title: Why is perfect resampling impossible?
Post by: Woodinville on 2012-06-18 22:20:39
As i'm sure somebody's pointed out that you can be "perfect to quantization noise level or below" which is appropriate.

In the most general case, this is impossible too, JJ. There are digital signals for which sinc interpolation does not converge towards a fixed analog signal as you increase the support of sincs. In other words, as you use better-and-better-quality reconstruction, your reconstructed signal changes and never stops, and rises infinitely in amplitude (which never brings it within a quantization step of a perfect reconstruction because a perfect reconstruction in fact diverges to infinity). A simplest example of such digital signal is {...1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1 ...}


Except, Alexey, that signal (1, 0, 1, 0, ...) is only permissable if it's infinitely long. Otherwise, the signal that created that in the analog domain could not possibly have been properly sampled, as it has content outside -fs/2 to fs/2.

Your signal is exactly 1/2 (i.e. DC) and .5*cos(n*pi), which is exactly at fs/2, and which therefore can not ever be recorded in the real world without violating the sampling theorem. Ditto for reconstruction.
Title: Why is perfect resampling impossible?
Post by: Woodinville on 2012-06-18 22:23:47
But a _file_ of finite length contains only a (finite) discrete set of frequencies?

The answer is no.


If it is sampled, however, and therefore consists of 'n' samples in the time domain, it can be precisely reproduced by 'n' samples in a frequency domain.

This is not the same as what the continuous fourier transform of the analog original (it being a finite length signal) will be, of course.
Title: Why is perfect resampling impossible?
Post by: Alexey Lukin on 2012-06-18 22:43:58
Except, Alexey, that signal (1, 0, 1, 0, ...) is only permissable if it's infinitely long. Otherwise, the signal that created that in the analog domain could not possibly have been properly sampled, as it has content outside -fs/2 to fs/2.

Right, it has to be infinitely long for summation to diverge. But you've missed my point about reconstructing signals that were never sampled, but rather just produced (or edited, or distorted) in digital.
If we only consider finite-length signals, then I agree that "perfect" reconstruction (down to a quantization step) is achievable, but the filters should be quite long.
Title: Why is perfect resampling impossible?
Post by: Woodinville on 2012-06-19 00:25:03
Except, Alexey, that signal (1, 0, 1, 0, ...) is only permissable if it's infinitely long. Otherwise, the signal that created that in the analog domain could not possibly have been properly sampled, as it has content outside -fs/2 to fs/2.

Right, it has to be infinitely long for summation to diverge. But you've missed my point about reconstructing signals that were never sampled, but rather just produced (or edited, or distorted) in digital.
If we only consider finite-length signals, then I agree that "perfect" reconstruction (down to a quantization step) is achievable, but the filters should be quite long.


Oh, sorry, missed that. We agree then.
Title: Why is perfect resampling impossible?
Post by: bandpass on 2012-06-26 16:43:29
Many engineers consider "bit transparent" to be the same as "diff signal not audible".

Many people like FLAC because it's zero-risk, even if they can't discern an audible difference between it and say, 320kb/s mp3.

So perhaps folk would also like to use a demonstrably bit-perfect resampler, as far as that is possible.

If one generates two files with 0-24kHz sine sweeps (with tails, as described above) at two different sample rates >= 48kHz, then one should be able to resample one, and obtain the other (subject to slight trimming).

If, after trimming away t% of the high end of the sweep, the difference signal is < 2-n-1, then one could claim that the resampler is n-bit transparent to 100-t % (band occupancy).

So a resampler's performance (perhaps at a site like the infinitewaves one) could be given in just two numbers (okay, you might want to add a range of rate-changes over which it has been tested).

For example, after a quick check with libsamplerate: after trimming the top 7% of the resampled sweep, I get a difference signal peaking at ~-128dB ~= 2-21.2, so this resampler is 20-bit transparent, to 93%.
Title: Why is perfect resampling impossible?
Post by: Alexey Lukin on 2012-06-26 17:56:51
This test may not exactly work for many resamplers because of subsample signal shifts. Resamplers do not usually guarantee some particular timing or latency of the resampled signal.
Title: Why is perfect resampling impossible?
Post by: Alexey Lukin on 2012-06-26 17:58:57
For example, when resampling a 3-point file from 88k to 44k, where should the first point go? There are at least 2 options which seem logical; and they result in different latencies.
Title: Why is perfect resampling impossible?
Post by: bandpass on 2012-06-26 21:15:57
For example, when resampling a 3-point file from 88k to 44k, where should the first point go? There are at least 2 options which seem logical; and they result in different latencies.

Given that the test above worked, libsamplerate and the sweep generator agree that time starts at 0.  However, it doesn't matter so much: it's just one of the details of the test that would have to be well-defined for it to become a useful standard.

This test may not exactly work for many resamplers because of subsample signal shifts. Resamplers do not usually guarantee some particular timing or latency of the resampled signal.

Well, there'd be no point in coming up with a new test if every resampler were currently expected to pass it.  But it's a very powerful test: if you happen to need a resampler with zero group delay then this test determines that—and that every other aspect of the implementation is correct.

People tend to want (at least the option of) perfection wherever they believe it should be possible to have it: in their playback chain, from their de-emphasis filter, in file compression; if it's available, they'll want it from their resampler too.  If folk find it a useful test, then those resamplers that don't currently pass it would probably 'fall in behind' it, much like, for example, with the 'acid test' for web browsers.

Maybe we could save a little energy too: if people understand they're getting 'bit-perfect' conversion at standard settings, they might think twice before using the 200dB (in some cases) stop-band rejection option all the time.
Title: Why is perfect resampling impossible?
Post by: benski on 2012-06-26 21:18:27
Setting aside floating point precision issues, you should be able to achieve bit-perfect conversion by selecting a filter kernel of length 2n+1.  However, this is going to be a VERY large (and slow) filter, especially on the downsampling filter in non-integer ratios (e.g. 44.1kHz->48kHz)
Title: Why is perfect resampling impossible?
Post by: Alexey Lukin on 2012-06-26 21:33:03
bandpass, I understand your point. This is a good test, when it works.
Title: Why is perfect resampling impossible?
Post by: knutinh on 2012-06-27 10:00:16
But a _file_ of finite length contains only a (finite) discrete set of frequencies?

The answer is no.

I dont understand your post. Please elaborate.

A finite size file contains finite information. How can it contain anything but a finite set of frequencies?

-k
Title: Why is perfect resampling impossible?
Post by: knutinh on 2012-06-27 10:06:41
Setting aside floating point precision issues, you should be able to achieve bit-perfect conversion by selecting a filter kernel of length 2n+1.  However, this is going to be a VERY large (and slow) filter, especially on the downsampling filter in non-integer ratios (e.g. 44.1kHz->48kHz)

Are you talking about regular zero-insertion->convolution->sample dropping? What is "n"?

As long as the filter has a transition-band between passband and stop-band, ripple in the passband and finite attenuation in the stop-band, how can such resampling be theoretically flawless (in practice, for our application, it probably will be)?

-k
Title: Why is perfect resampling impossible?
Post by: bandpass on 2012-06-27 10:27:19
Whether or not perfect resampling is theoretically possible, I think we've established above that it's not practical.

The only way to demonstrate bit-perfect conversion with a resampler you might actually want to use for real tasks, is to band-limit the signal, just a tad, e.g. by pre-filtering a real-world signal, or by using a generated test-signal.

With a pre-filtered real-world signal, the demonstration would have to be 'round trip' (which has issues re the validity of the intermediate signal); with a generated signal, the demonstration can be of a single conversion.