### Processing IQ data formats (Part 1/5) đ

When working with SDRs, information about the signals your radio is receiving are communicated by streams of IQ data. IQ is short for âIn-phaseâ and âQuadratureâ, which means 90 degrees out of phase. Values in the IQ stream are commonly treated as complex numbers because it helps greatly when processing the IQ data for meaning.

I wonât get too deep into what IQ is or why complex numbers (mostly since I donât think I fully understand it well enough to explain it yet), but hereâs some basics in case this is your first interaction with IQ data before going off and reading more.

Each value in the stream is taken at a precisely spaced sampling interval (called the sampling rate of the radio). Jitter in that sampling interval, or a drift in the requested and actual sampling rate (usually represented in PPM, or parts per million â how many samples out of one million are missing) can cause errors in frequency. In the case of a PPM error, one radio may think itâs 100.1MHz and the other may think itâs 100.2MHz, and jitter will result in added noise in the resulting stream.

A single IQ sample is both the real and imaginary values, together. The complex number (both parts) is the sample. The number of samples per second is the number of real and imaginary value pairs per second.

Each sample is reading the electrical energy coming off the antenna at that exact time instant. Weâre looking to see how that goes up and down over time to determine what frequencies weâre observing around us. If the IQ stream is only real-valued measures (e.g., float values rather than complex values reading voltage from a wire), you can still send and receive signals, but those signals will be mirrored across your 0Hz boundary. That means if youâre tuned to 100MHz, and you have a nearby transmitter at 99.9MHz, youâd see it at 100.1MHz. If you want to get an intuitive understanding of this concept before getting into the heavy math, a good place to start is looking at how Quadrature encoders work. Using complex numbers means we can see âupâ in frequency as well as âdownâ in frequency, and understand that those are different signals.

The reason why we need negative frequencies is that our 0Hz is the center of our SDRâs tuned frequency, not actually at 0Hz in nature. Generally speaking, itâs doing loads in hardware (and firmware!) to mix the raw RF signals with a local oscillator to a frequency that can be sampled at the requested rate (fundamentally the same concept as a superheterodyne receiver), so a frequency of â-10MHzâ means that signal is 10 MHz below the center of our SDRâs tuned frequency.

The sampling rate dictates the amount of frequency representable in the data stream. Youâll sometimes see this called the Nyquist frequency. The Nyquist Frequency is one half of the sampling rate. Intuitively, if you think about the amount of bandwidth observable as being 1:1 with the sampling rate of the stream, and the middle of your bandwidth is 0 Hz, you would only have enough space to go up in frequency for half of your bandwidth â or half of your sampling rate. Same for going down in frequency.

#### Float 32 / Complex 64

IQ samples that are being processed by software are commonly processed as an interleaved pair of 32 bit floating point numbers, or a 64 bit complex number. The first float32 is the real value, and the second is the imaginary value.

The complex number `1+1i`

is represented as `1.0 1.0`

and the complex number
`-1-1i`

is represented as `-1.0 -1.0`

. **Unless otherwise specified, all the
IQ samples and pseudocode to follow assumes interleaved float32 IQ data
streams.**

Example interleaved float32 file (10Hz Wave at 1024 Samples per Second)

#### RTL-SDR

IQ samples from the RTL-SDR are encoded as a stream of interleaved unsigned 8 bit integers (uint8 or u8). The first sample is the real (in-phase or I) value, and the second is the imaginary (quadrature or Q) value. Together each pair of values makes up a complex number at a specific time instant.

The complex number `1+1i`

is represented as `0xFF 0xFF`

and the complex number
`-1-1i`

is represented as `0x00 0x00`

. The complex number `0+0i`

is not easily
representable â since half of `0xFF`

is `127.5`

.

Complex Number | Representation |

1+1i | []uint8{0xFF, 0xFF} |

-1+1i | []uint8{0x00, 0xFF} |

-1-1i | []uint8{0x00, 0x00} |

0+0i | []uint8{0x80, 0x80} or []uint8{0x7F, 0x7F} |

And finally, hereâs some pseudocode to convert an rtl-sdr style IQ sample to a floating point complex number:

```
...
in = []uint8{0x7F, 0x7F}
real = (float(iq[0])-127.5)/127.5
imag = (float(iq[1])-127.5)/127.5
out = complex(real, imag)
....
```

Example interleaved uint8 file (10Hz Wave at 1024 Samples per Second)

#### HackRF

IQ samples from the HackRF are encoded as a stream of interleaved signed 8 bit integers (int8 or i8). The first sample is the real (in-phase or I) value, and the second is the imaginary (quadrature or Q) value. Together each pair of values makes up a complex number at a specific time instant.

Formats that use signed integers do have one quirk due to
twoâs complement,
which is that the smallest negative number representableâs absolute
value is one more than the largest positive number. `int8`

values can
range between `-128`

to `127`

, which means thereâs bit of ambiguity in
how +1, 0 and -1 are represented. Either you can create perfectly symmetric
ranges of values between +1 and -1, but 0 is not representable, have more
possible values in the negative range, or allow values above (or just below)
the maximum in the range to be allowed.

Within my implementation, my approach has been to scale based on the max
integer value of the type, so the lowest possible signed value is actually
slightly smaller than `-1`

. Generally, if your code is seeing values that low
the difference in step between -1 and slightly less than -1 isnât very
significant, even with only 8 bits. Just a curiosity to be aware of.

Complex Number | Representation |

1+1i | []int8{127, 127} |

-1+1i | []int8{-128, 127} |

-1-1i | []int8{-128, -128} |

0+0i | []int8{0, 0} |

And finally, hereâs some pseudocode to convert a hackrf style IQ sample to a floating point complex number:

```
...
in = []int8{-5, 112}
real = (float32(in[0]))/127
imag = (float32(in[1]))/127
out = complex(real, imag)
....
```

Example interleaved int8 file (10Hz Wave at 1024 Samples per Second)

#### PlutoSDR

IQ samples from the PlutoSDR are encoded as a stream of interleaved signed 16 bit integers (int16 or i16). The first sample is the real (in-phase or I) value, and the second is the imaginary (quadrature or Q) value. Together each pair of values makes up a complex number at a specific time instant.

Almost no SDRs capture at a 16 bit depth natively, often youâll see 12 bit integers (as is the case with the PlutoSDR) being sent around as 16 bit integers. This leads to the next possible question, which is are values LSB or MSB aligned? The PlutoSDR sends data LSB aligned (which is to say, the largest real or imaginary value in the stream will not exceed 4095), but expects data being transmitted to be MSB aligned (which is to say the lowest set bit possible is the 5th bit in the number, or values can only be set in increments of 16).

As a result, the quirk observed with the HackRF (that the range of values between 0 and -1 is different than the range of values between 0 and +1) does not impact us so long as we do not use the whole 16 bit range.

Complex Number | Representation |

1+1i | []int16{32767, 32767} |

-1+1i | []int16{-32768, 32767} |

-1-1i | []int16{-32768, -32768} |

0+0i | []int16{0, 0} |

And finally, hereâs some pseudocode to convert a PlutoSDR style IQ sample to a floating point complex number, including moving the sample from LSB to MSB aligned:

```
...
in = []int16{-15072, 496}
// shift left 4 bits (16 bits - 12 bits = 4 bits)
// to move from LSB aligned to MSB aligned.
in[0] = in[0] << 4
in[1] = in[1] << 4
real = (float32(in[0]))/32767
imag = (float32(in[1]))/32767
out = complex(real, imag)
....
```

Example interleaved i16 file (10Hz Wave at 1024 Samples per Second)

#### Next Steps

Now that we can read (and write!) IQ data, we can get started first on the transmitter, which we can (in turn) use to test receiving our own BPSK signal, coming next in Part 2!