# How to approximate the sample rate?

I have a simple program that captures audio from an audio device. I have configured a nominal sample rate of 48000Hz and a buffer size of 1 millisecond. The audio sub system should execute my capture code with approximately 1000Hz.

My assumption is that the audio clock (LRCLK) is not running at exactly 48000Hz. Using this 1kHz-callback from the audio system I want to approximate the actual sampling frequency relativ to another clock. Currently I use the system clock of my workstation as a reference. I plan to use a network clock.

For my very first experiment I used a simple moving average with a period of 1000 but there is still very much jitter. Sometimes the average jumps by 2Hz which seems much to me.

What is the proper way to go here?

Many thanks!

• Can I please ask if the "audio device" is a sound card or other codec chipset? Also, how do you derive that 2Hz jitter? (Is it via sampling a reference sinusoid for example?) – A_A Nov 28 '16 at 20:34
• Currently I'm working on a PC with an on-board sound card. I plan to work on embedded devices in future though. The 2Hz jitter comes from my first experiment with a simple moving average. I sample the duration between calls to my capturing code which should be called with ~1kHz. The window has 1000 samples. From the average duration I derive the approximate sampling frequency in Hz. One example of extrem jitter is this sequence: 48000.6, 47974.1, 48025.2 – Jan Deinhard Nov 28 '16 at 21:36
• You will never get rid of that with an off the shelf sound card. Try an ASIO capable piece of hardware. It will run smoothly and go as low as buffers of 256 samples at 44.1kHz with absolutely no problem at all (that's approximately 5ms by the way). – A_A Nov 28 '16 at 21:41
• But even with better audio hardware I assume some jitter is always expected right? The question remains: How to approximate the actual sampling frequency relativ to another clock. I can measure the duration between the calls to my code. I also consider to go "nearer" to the hardware (interrupt handler) to get rid of system related jitter. – Jan Deinhard Nov 28 '16 at 21:53
• I can assure you that there is no noticeable jitter on an ASIO capable sound card. If there is jitter it is excruciatingly low (much, much smaller than the sampling period). You would discover it by measuring it (over a very long period of time), not by ear. What you are looking for is resampling and ultimately interpolation beteween the sample rate of one clock and another assuming that both can keep their "tempo" once started. – A_A Nov 28 '16 at 22:01

A simple experiment is to generate a single-tone signal, then receive it and find its frequency. Say your reference is the transmitter and it's operating at 48,000 samples per second. Feed the transmitter a sequence 0, 1, 0, -1, 0, .... This will produce a sine wave of nominal frequency 12,000 Hz. In the receiver, you sample this signal for a relatively long time (to have good frequency resolution) and use the DFT to determine its frequency to be, say, $f_2$. Then, you can calculate $$\Delta=\frac{12000}{f_2}$$ which is the factor by which the receiver's clock is faster than the transmitter's. Of course, this is only one of many possible approaches.