Random Quote Board

Gnu Radio and Sample Rate

Gary Schafer, April 2021

Perhaps one of the most misunderstood aspects of Gnu Radio, and SDR in general, is the concept of sample rate. Let's go through it a little bit at a time.

SDR and Digitizing

A software defined radio (SDR) has two, basic purposes. These are:

  1. Amplify, filter and frequency shift RF signals to an intermediate frequency (IF). The first part of all SDRs is the RF front end. When you connect an antenna to your SDR, regardless of the SDR you have, the signals will pass through this section. There will most likely be an amplifier and some filtering. There may or may not be a frequency conversion circuit depending on the frequency range of the SDR. SDRs that operate in the HF range (3 - 30 MHz) or less may go directly from an amplifier and/or filter direct to a digitizer. No need for frequency conversion. Those systems simply digitize the whole spectrum, then perform any needed frequency conversion in software. If the SDR does have a frequency conversion circuit, it most likely is a superheterodyne system. This simply means that it converts tuned frequency to a fixed intermediate frequency (IF). This is an age-old technique that allows for any follow-on circuits to operate on just one frequency. This makes them much simpler, which in turn makes them cheaper and more reliable.
  2. Digitize the RF signals. All RF signals are analog. SDRs means "software". Software means computers. Computers, at least the ones we tend to use, do not work on analog signals. We need digital. Therefore, there will be a circuit to convert the analog signal to a digital signal. That's the digitizer or analog-to-digital converter (ADC). Any follow-on circuitry will do nothing more than prepare the samples for transfer over a USB or Ethernet connection.
This is a Great Scott Gadget's HackRF One software defined radio. I've opened it up and annotated the components so that you can see the general signal flow. As stated above, the first part is the RF front end, which I also call a "tuner" for those systems that provide frequency conversion. This is followed by a quadrature demodulator, which creates in-phase and quadrature (IQ) signals, followed by a digitizer. In this system, due to the fact that the I & Q come before the digitizer, there has to be two digitizers, one each for the I & Q signals. (Image credit: Gary Schafer)
This is a RTL-SDR.com RTL-based SDR. All of the primary functionality of the SDR is contained within two chips. The RF front end is in the R820T2 chip, while the digitizer is contained within the RTL2832 chip. All of the remaining circuitry is support for these two chips and to take the digitized samples and output them onto the USB connector. (Image credit: Gary Schafer)

There's one other function that SDRs almost always do but they do not have to do, and that is to turn the input signals, which are real, and turn them into complex signals. The two SDRs shown above both output complex digital samples. The Signal Hound BB60C, however, outputs real samples, then the computer, using software, turns them into complex samples.

Real vs Complex Samples

I still don't have a good write-up to explain the difference between real vs complex samples. Someday, maybe. There are plenty of other people on these here interwebz who have taken a stab at explaining the difference between real vs complex sampling. Check those out. In the mean time, let me boil this down to two key concepts that I always bear in mind when processing digital signals.

This shows the basic frequency limits of complex (top) and real (bottom) sampled signals. The complex sample rate is 2.4 MHz. The range of the spectrum is from -1/2 of this (-1.2 MHz) to +1/2 of this (+1.2 MHz). Note that the range of the complex spectrum is the full sample rate. The sample rate of the real spectrum is 240 kHz. Its usable range extends from 0 - 120 kHz, or 1/2 of the real sample rate.

All of the SDRs that connect to Gnu Radio output complex samples. That's what the blue connection on the SDR source block means. A complex sample rate must be at least equal to the bandwidth of the signal being sampled. This is why the available sample rate for a SDR determines the maximum bandwidth it can input.

This is the graph used to make the spectral images above. The source block, "osmocom Source", has a blue connector, meaning it is outputting complex samples. The source, in this case a basic RTL-SDR, is set to a sample rate of 2.4 MHz.

In Gnu Radio, real samples are called "floats" and are represented by orange connectors, while complex samples are still called "complex" and are represented by blue connectors.

When it comes to Gnu Radio and these sample types, remember that real samples are called "floats" (represented by orange connectors) and complex samples are still called "complex" (represented by blue connectors).

Changing Sample Rates

Why change the sample rate? There are several reasons, such as having to create a sample rate that a piece of hardware must use, or just trying to keep the sample rate as low as necessary. The higher the sample rate, the more processing required by the computer. Computers are still limited in their processing power. By reducing the sample rate to the minimum required, you're keeping the amount of processing required to a minimum.

There are two methods to change the sample rate. These are:

Here's the thing about processing digital samples. You can only work on whole samples. There's no such thing as processing 10.2 or 28.392 samples. It's either all of the sample, or none of it. This means that, if you want to change the sample rate, you have to do so in a way that uses integers. Any time you set a decimation or interpolation value, it must be an integer value. Again, no decimal points allowed.

But what if you need to change the sample rate by a fractional value? You have to create a fraction based on an integer interpolation and integer decimation value. For example, if you need to change the sample rate by 2/3, you first interpolate by 2, then decimate by 3.

Gnu Radio provides several blocks that allow you to change the sample rate. Here are some examples:

The "Low Pass Filter" block, as with many of the filter blocks, can be set to either decimate or interpolate the input sample rate.
The "Rational Resampler" is specifically designed to change the sample rate. It can provide both decimation and interpolation in one block.
The "Repeat" block. It interpolates the input stream, as the "Interpolation" value suggests.
The "Packed to Unpacked" block. This block interpolates, even though it doesn't say it does. The interpolation value depends on the types of values its processing. The interpolation value is the same number of bits as represents each value (byte = 8, short = 16, int = 32).

Gnu Radio and Sample Rate

When looking at a Gnu Radio flowgraph, the sample rate is always set by connected hardware or "Throttle" block. This is the baseline for the sample rates for the rest of the flowgraph. The sample rates of any other blocks in the flowgraph will depend on this base sample rate and any decimation (reduction in sample rate) or interpolation (increase in sample rate) from that hardware source block to the block in question. If there are two hardware blocks, such as a SDR as a source and an audio sink, then there could be problems if the different decimation or interpolation values in between don't match up.

Let's take a quick stroll through a flowgraph. We'll start with the flowgraph used to make the spectra shown above.

This is the various sample rates at different points in a basic Gnu Radio flowgraph. It starts with the sample rate of the source. This is the actual hardware connected to the computer, in this case a RTL-SDR. NOTE: I've added the sample rate values along the various "edges" (that's the technical term for the lines connecting blocks).

The first block is the source. In this case, it's a piece of hardware, a RTL-SDR. The sample rate setting for this block is 2.4 MHz, and it sets the sample rate of its digitizer. This means the RTL-SDR connected to my computer is running at a sample rate of 2.4 MHz. Because it controls a piece of hardware, this block sets the sample rate for the rest of the flowgraph.

The output complex samples go to a lowpass filter. The two, key settings here (with respect to the sample rate) are "Sample Rate" and "Decimation". Here's the thing about the "Sample Rate" setting. It's actually meaningless for the actual sample rate. Digital filters do not operate on absolute numbers. They operate on ratios, specifically the ratio of the cutoff frequency to the sample rate, or the ratio of the transition width to the sample rate. That's all that value "Sample Rate" in this block is used for. It's used to calculate the ratios for the various values (cutoff frequency, transition width). Those ratios are used to calculate the filter tap values for the filter itself. Again, it has absolutely no effect on the actual sample rate going through the block. I could set this sample rate value to 1. Then, so long as I set the "Cutoff Freq" and "Transition Width" values such that the ratios are still the same (1/24 for the cutoff frequency, 1/120 for the transition width), the filter will still operate exactly the same. The one value that does affect the sample rate is the "Decimation" value. Decimation means "reduce the sample rate". The value of "10" here means "keep 1 sample out of every 10". The rest? Throw them away. Trash'em. Get rid of 'em. The result is an output sample rate that is 1/10th of the input sample rate.

The next stop for our sample stream is the "Quadrature Demod" block. This is the frequency demodulator. It doesn't affect the sample rate, per se, but it does affect the samples. It turns the input complex samples into an output of real samples. The result? Our available bandwidth just dropped by 1/2. The sample rate at the input and output are the same, but since the output is real samples, it only has an available bandwidth of 1/2 of the input sample rate.

The last block is just a frequency sink. It doesn't have a sample rate setting, but it does have a setting for "Bandwidth". That is nothing more than the sample rate at the input of the block. Further, just as with the "Sample Rate" setting, it has absolutely no affect on the actual sample rate of the block. It uses the "Bandwidth" setting to properly scale the horizontal (frequency) axis of the graph.

Let's sum up the parts that actually affect the sample rate of this flowgraph. The first is the "osmocom Source" block. It sets the sample rate for the rest of the flowgraph. The "Low Pass Filter" block uses decimation to lower the sample rate. That decimation value is the only setting in that block that actually affects the sample rate. The "Quadrature Demod" block does not affect the sample rate, but it does affect the samples (turns them from complex into real). That's it. None of the other components have any affect on the sample rate.

With respect to Gnu Radio, there are a couple of things that you must bear in mind:

Some Other Sample Rate Examples

This is the same flowgraph as above (again, annotated with the sample rates), except I've added a second lowpass filter and an audio sink. This flowgraph is controlling two pieces of hardware. The input RTL-SDR, controlled by the "osmocom Source" block, and the audio card, controlled by the "Audio Sink" block. The input RTL-SDR is set to a sample rate of 2.4 MHz. The follow-on lowpass filter decimates it by a factor of 10. The sample rate out of the filter is thus 2.4 MHz / 10 = 240 kHz. The "Quadrature Demod" block turns the complex sample stream into real samples, but the sample rate stays the same at 240 kHz. Another lowpass filter decimates the sample stream by a factor of 5. The output sample rate is 240 kHz / 5 = 48 kHz. This matches the audio card sample rate of 48 kHz.
Another example of a flowgraph annotated to show the different sample rates at the outputs of each block. I've also set it up where the audio card is set to 32 kHz, one of the common sample rates that an audio card can handle. The input source is set to a sample rate of 2 MHz. Between that and the audio sink are three blocks that affect the sample rate. These are the two lowpass filters and the rational resampler. The first lowpass filter decimates the sample stream by a factor of 8. This gives an output sample rate of 2 MHz / 8 = 250 kHz. The second lowpass filter decimates by 5, which gives an output sample rate of 250 kHz / 5 = 50 kHz. The rational resampler, whose sole job is to change the sample rate, first interpolates by a factor of 32, then decimates by a factor of 50. This gives an output sample rate of 50 kHz x 32 / 50 = 32 kHz.

Some Problems of Incorrect Sample Rates in Gnu Radio

And what are some of the problems that might arise if the sample rate is incorrect? Here's a sampling (heh) of possible problems when the sample rate is incorrect:

Some Last Key Points

Here are a few, key points with respect to the sample rate of a digital system:

File Source and Sinks and Gnu Radio

Gnu Radio makes it possible to record signals. This is the "File Sink" block. You can then play back those signals using the "File Source" block. Here's the thing about these blocks. They're not hardware. There's no sample rate entry. Gnu Radio does not record any sample rates when the files are made. A file sink, used to create a file of samples, does not record any metadata of any kind. This means you have to record that yourself. I typically put the sample rate and sample type (real, complex) as part of the file name.

If you take the output of a hardware source and send it directly to a "File Sink" block, that block will write those samples to a file. Here's the cool part. Reading those samples back will be exactly as if they came directly off of the hardware source originally. There's absolutely no difference.

This is a basic file sink that allows for the recording of samples directly from the output of a SDR. Once recorded, there is no difference between the recorded samples and those coming from the original hardware source. You can treat the original samples just as if they had come from the a hardware source. Again, no difference.

There's one other point I want to make about file sources. Because they are not hardware, you cannot use them to set the sample rate for the flowgraph. This means that, if you don't have another piece of hardware in the flowgraph, such as an audio sink, then you need to put a "Throttle" block in the flowgraph.

You can record a file at one sample rate, and read it back at a different rate. Gnu Radio will have no clue that anything is wrong. Any processed signals from a file that is read back at the wrong rate will either not process correctly, or it will provide a wrong output. Again, because Gnu Radio sets all of the sample rates manually, it's up to you to ensure that they're all correct.

This is an example flowgraph showing playback from a file source. The recorded samples were from a FM broadcast station. The key point here is that the audio sink is the only piece of hardware connected to the flowgraph. It will set the sample rate for the rest of the flowgraph.
This is another example flowgraph showing playback from a file source in which there is no hardware connected to the flowgraph. In this case, we have to put in a "Throttle" block to ensure that the timing is kept in check.

Here's a Random Fact...