networking · beginner · ~15 min

Count IQ samples

Convert a byte count to a sample count.

Challenge

Software-defined-radio (SDR) captures store interleaved I/Q samples. Given a buffer size and the bytes per sample, compute how many complete samples it holds.

Task

Implement int iq_sample_count(int nbytes, int bytes_per_sample) that returns how many whole samples fit in nbytes (integer division). Return 0 if bytes_per_sample <= 0.

Input

  • nbytes: the buffer size in bytes.
  • bytes_per_sample: bytes occupied by one I/Q sample.

Output

Returns nbytes / bytes_per_sample (truncated), or 0 when bytes_per_sample <= 0.

Example

iq_sample_count(1024, 4)   ->   256
iq_sample_count(10, 4)     ->   2
iq_sample_count(10, 0)     ->   0

Edge cases

  • bytes_per_sample <= 0: return 0 (avoid dividing by zero).
  • A partial trailing sample is not counted.

Input format

Two integers: buffer size in bytes (nbytes) and bytes per sample.

Output format

The number of whole samples (nbytes / bytes_per_sample), or 0 if bytes_per_sample <= 0.

Constraints

Integer division; guard bytes_per_sample <= 0 by returning 0.

Starter code

int iq_sample_count(int nbytes, int bytes_per_sample) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.