// SPDX-FileCopyrightText: © 2016-2021 Oliver Jowett // SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: GPL-2.0-or-later //! Choosing how to read the sample stream a particular radio produces. //! //! Every radio hands over interleaved in-phase and quadrature samples, and //! every radio does it in a different word size. What the demodulator wants //! is neither of those: it wants the magnitude of each sample, which is what //! `dsp` computes. This module is the thin layer that knows which of those //! kernels applies. const std = @import("std"); const Allocator = std.mem.Allocator; const dsp = @import("dsp.zig"); /// The sample formats the supported radios produce. pub const InputFormat = enum { /// Unsigned 8-bit components, offset so that silence is 127.5. What the /// RTL-SDR produces, and by far the most common. uc8, /// Signed 16-bit components at full scale. sc16, /// Signed 16-bit words holding an eleven-bit fraction. The bladeRF's /// format, kept because recordings in it are common. sc16q11, /// The name as `--iformat` spells it. pub fn name(self: InputFormat) []const u8 { return switch (self) { .uc8 => "UC8", .sc16 => "SC16", .sc16q11 => "SC16Q11", }; } /// Parse an `--iformat` argument, case-insensitively. pub fn parse(text: []const u8) ?InputFormat { inline for (comptime std.enums.values(InputFormat)) |format| { if (std.ascii.eqlIgnoreCase(text, format.name())) return format; } return null; } /// How many bytes one sample takes up: two components of whatever width. pub fn bytesPerSample(self: InputFormat) usize { return switch (self) { .uc8 => 2, .sc16, .sc16q11 => 4, }; } }; /// Why a converter could not be built. pub const Error = error{ /// A DC filter was asked for. Upstream never implemented one either; the /// option exists because some SDRs put a large spike at the tuned /// frequency, and the answer for those is to tune slightly off it. DcFilterUnsupported, }; /// Options that change how samples are read. pub const Options = struct { /// The sample rate, which no current converter needs but a resampling one /// would. sample_rate: f64 = 2_400_000, /// Whether to remove the DC component. Not supported. filter_dc: bool = false, }; /// A configured sample converter. pub const Converter = struct { format: InputFormat, /// The magnitude lookup table, which only the UC8 path uses. uc8_table: ?dsp.Uc8Table, pub fn init(gpa: Allocator, format: InputFormat, options: Options) (Allocator.Error || Error)!Converter { if (options.filter_dc) return error.DcFilterUnsupported; return .{ .format = format, .uc8_table = if (format == .uc8) try .init(gpa) else null, }; } pub fn deinit(self: *Converter, gpa: Allocator) void { if (self.uc8_table) |*table| table.deinit(gpa); self.* = undefined; } /// How many bytes of input one sample takes. pub fn bytesPerSample(self: Converter) usize { return self.format.bytesPerSample(); } /// Convert a block of samples to magnitudes. /// /// `iq` must hold exactly `magnitudes.len` samples. pub fn convert(self: Converter, iq: []const u8, magnitudes: []u16) void { std.debug.assert(iq.len == magnitudes.len * self.bytesPerSample()); switch (self.format) { .uc8 => self.uc8_table.?.convert(iq, magnitudes), .sc16 => dsp.magnitudeSc16(iq, magnitudes), .sc16q11 => dsp.magnitudeSc16q11(iq, magnitudes), } } /// Convert a block of samples and measure its mean level and power, which /// is what the noise floor statistics and the adaptive gain control are /// computed from. pub fn convertAndMeasure(self: Converter, iq: []const u8, magnitudes: []u16) dsp.Level { self.convert(iq, magnitudes); return dsp.meanPower(magnitudes); } }; const testing = std.testing; test "format names round-trip" { for (comptime std.enums.values(InputFormat)) |format| { try testing.expectEqual(format, InputFormat.parse(format.name()).?); } try testing.expectEqual(InputFormat.uc8, InputFormat.parse("uc8").?); try testing.expectEqual(InputFormat.sc16q11, InputFormat.parse("Sc16Q11").?); try testing.expectEqual(@as(?InputFormat, null), InputFormat.parse("cs8")); } test "sample widths" { try testing.expectEqual(@as(usize, 2), InputFormat.uc8.bytesPerSample()); try testing.expectEqual(@as(usize, 4), InputFormat.sc16.bytesPerSample()); } test "a UC8 converter produces the table's magnitudes" { var converter: Converter = try .init(testing.allocator, .uc8, .{}); defer converter.deinit(testing.allocator); const samples = [_]u8{ 127, 127, 255, 255 }; var magnitudes: [2]u16 = undefined; converter.convert(&samples, &magnitudes); try testing.expectEqual(@as(u16, 290), magnitudes[0]); try testing.expectEqual(@as(u16, 65535), magnitudes[1]); } test "a signed converter needs no table" { var converter: Converter = try .init(testing.allocator, .sc16, .{}); defer converter.deinit(testing.allocator); try testing.expectEqual(@as(?dsp.Uc8Table, null), converter.uc8_table); const samples = [_]u8{ 0x00, 0x40, 0x00, 0x00 }; // +16384, 0 var magnitudes: [1]u16 = undefined; const level = converter.convertAndMeasure(&samples, &magnitudes); try testing.expectEqual(@as(u16, 32768), magnitudes[0]); try testing.expectApproxEqAbs(@as(f64, 0.5), level.mean_level, 1e-9); } test "a DC filter is refused rather than silently ignored" { try testing.expectError( error.DcFilterUnsupported, Converter.init(testing.allocator, .uc8, .{ .filter_dc = true }), ); }