1

$124$ Hz sampled at $256$ Hz has "beats", but $128$ doesn't - a phenomenon of this question:

enter image description here

Whether these are "legitimate" beats is up to interpretation, but suppose the goal is the underlying continuous-time function and we are bandlimited, and beats are supposed to reflect amplitude - then it's not legitimate.

Question is, what interpolation do audio readers use, if any? For above example, a .wav generated by scipy with 256 samples yields no beats in either case, but if we use 2048 samples with frequencies scaled proportionally (and beats graphically verified), there are beats.

Above with 8 samples is just a brief bleep, despite the sampling rate specifying a 1 sec duration, so one might think they're converted directly to voltages, but... electricity is fast enough that I don't have to hear 2048 samples at all.

Extra detail

What I'm really trying to figure out is when to favor calling the left figure "beats". I'm not the only one who thinks they're beats; librosa.stft:

The culprit is imperfect analyticity. What I'm unsure of, is whether the spectrogram should show a flat line; again, I think it depends on interpretation, and I'm trying to interpret it for audio.

(As a side note, if imperfect analyticity yields "more accurate" amplitude in some sense, I wonder if it does the same for instantaneous frequency.)

Audio code

import numpy as np
from scipy.io.wavfile import write

N = 2048 t = np.linspace(0, 1, N, 0) f0, f1 = N//2 - 4, N//2

x0 = np.cos(2np.pi f0 * t) x1 = np.cos(2np.pi f1 * t)

A = np.iinfo(np.int16).max x0, x1 = x0 * A, x1 * A sr = int(1/np.diff(t)[0])

write('x0.wav', sr, x0.astype(np.int16)) write('x1.wav', sr, x1.astype(np.int16))

OverLordGoldDragon
  • 8,912
  • 5
  • 23
  • 74
  • 1
    What do you mean by "audio reader? Also: beats cannot be reliable inferred from a line plot, because samples are joined by straight lines, which causes artifacts. The beats can be seen in the frequency domain, though. – MBaz May 14 '22 at 21:05
  • @MBaz Whatever gets the data from bytes to my ears. In my case, Windows Media Player reading a .wav. And, these are exact sines, represented as diracs in DFT: note the integer frequencies and endpoint=False. (but I suppose you mean we can tell if the beats are legitimate from frequency domain -- then agreed) – OverLordGoldDragon May 14 '22 at 21:06
  • Windows Media Player just gets the bits from the .wav file to your sound device -- the sound device itself has a lot to do with the process. To really investigate the process would involve hooking up an oscilloscope to your sound card -- and then getting a bazzilion different sound cards, because each one is going to be different. – TimWescott May 15 '22 at 00:44

2 Answers2

4

What kind of interpolation is used depends on the particular implementation of each device.

Once I tried to answer this question for my computer's sound card. What I did was to produce a wav file filled with zeros, except for a single one in the middle. I connected my sound card's output to an oscilloscope and configured the trigger.

What I saw was a truncated sinc wave, seven sampling intervals long (IIRC, it might have been a bit more or less). The conclusion was that the impulse response of my sound card's interpolator was a sinc pulse with a few taps.

My guess would be other devices use different impulse responses. They might use a longer sinc, or splines, or even linear interpolation in the case of very cheap devices. If you have a scope (there are cheap USB ones these days), you can repeat this experiment and find out what your specific device is doing.

MBaz
  • 15,314
  • 9
  • 30
  • 44
  • Makes sense, thanks. Though I'm surprised I hear beats (even at 30kHz sampling rate), my laptop plays music quite well through headphones. Could splines yield beats? Sinc should make a pure sine at least away from edges, but I hear beats for the whole duration. – OverLordGoldDragon May 14 '22 at 21:27
  • What I would do is look at the wav in the frequency domain. If it's a pure sine wave, and you hear beats, your hardware is defective (maybe by design -- it may conceivably use a suboptimal interpolator that works well enough for music but fails for pure tones). If the spectrum shows harmonics where there shouldn't be, then the beats might be in the original signal. – MBaz May 14 '22 at 21:33
  • I don't think splines or other good interpolators can produce beats. A linear interpolator (joining samples with straight lines) certainly could. In the experiment I described above, I think a linear interpolator would produce a triangular pulse. – MBaz May 14 '22 at 21:35
  • The device seems to be not the only factor: WMP allows me to hear what Audacity, VLC, and Discord don't. Do these software do filtering, or also interpolation? For one, Audacity's visual render shows the waveform as-is. Can you hear the audio files in question? My friend's device and any software can't, whether I generate the files or their computer. – OverLordGoldDragon May 14 '22 at 22:02
  • 1
    True -- the application you use and indeed the OS sound drivers can mess up the signal. In my experience, Windows in general distorts more than Linux. If I have to use Windows to play a wav, I use Matlab's or SciPy's sound utilities. In my Linux machine, using mpv, x0 beats but x1 doesn't. – MBaz May 14 '22 at 22:15
  • 1
    @MBaz this is a very nice experiment that reveals the implementation of the reconstruction filter of your device. This is of course, as you've both already mentioned provided the OS and hardware drivers do not interfere further with the signal to be reproduced. Unfortunately I am not familiar with driver implementation so I can't really say what kind of processing this may (or may not) introduce to the signal pipeline. I believe though that it is quite difficult to separate the effect of a specific system component in nowadays systems due to increased complexity. – ZaellixA May 15 '22 at 06:47
  • 1
    @ZaellixA Fully agreed! One benefit of running an open source OS like Linux is that the drivers can (at least in principle) be studied. – MBaz May 15 '22 at 15:19
1

there are beats.

There are generally no beats. This is purely an artifact on how you draw it: which is simply by connecting neighboring samples with straight lines. This implies that you assume that the value of $x(nT+\tau)$ can be obtained by linear interpolation between the two neighboring samples $x(nT)$ and $x((n+1)T)$. This assumption is wrong and it's especially wrong at higher frequencies.

Values between samples can be accurately calculated using Whitaker-Shannon interpolation. See: https://en.wikipedia.org/wiki/Whittaker%E2%80%93Shannon_interpolation_formula

How do audio readers interpolate?

Audio readers don't interpolate at all. There is no need to, since all information is fully contained in the samples.

When you play the audio back, the DAC will apply an anti aliasing low pass filter which provide interpolation, so it will look smooth again in the analog domain.

In rare cases you can get audible beats for sine waves with frequencies very close to Nyquist. This is simply caused by the actual anti-aliasing not being "good enough". The ideal interpolation filter is an ideal low pass filter, which can't be implemented in practice.

Let's look at a trivial example $x_0[n]=\cos(n\frac{\pi}{2})$ and $x_1[n]= \cos(n\frac{\pi}{2} +\frac{\pi}{4})$. Obviously these have the same amplitude and the same frequency but if we draw them "naively" they look very different. enter image description here

If your frequency would be slightly higher or lower than $pi/2$ you would actually see some "beating" but this is just a drawing artifact and nothing else.

Hilmar
  • 44,604
  • 1
  • 32
  • 63