rtlsdr_nbfm.zig
This example is a Narrowband FM radio receiver. It can be used to listen to analog commercial, police, and emergency services, amateur radio operators, NOAA weather radio in the US, and more, on the VHF and UHF bands. It uses the RTL-SDR as an SDR source and plays audio with PulseAudio.
This NBFM demodulator composition is available in ZigRadio as the NBFMDemodulator
block.
const std = @import("std");
const radio = @import("radio");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const args = try std.process.argsAlloc(gpa.allocator());
defer std.process.argsFree(gpa.allocator(), args);
if (args.len < 2) {
std.debug.print("Usage: {s} <frequency>\n", .{args[0]});
std.posix.exit(1);
}
const frequency = try std.fmt.parseFloat(f64, args[1]);
const tune_offset = -50e3;
const deviation = 5e3;
const bandwidth = 4e3;
var source = radio.blocks.RtlSdrSource.init(frequency + tune_offset, 960000, .{ .debug = true });
var tuner = radio.blocks.TunerBlock.init(tune_offset, 2 * (deviation + bandwidth), 10);
var fm_demod = radio.blocks.FrequencyDiscriminatorBlock.init(deviation);
var af_filter = radio.blocks.LowpassFilterBlock(f32, 128).init(bandwidth, .{});
var af_downsampler = radio.blocks.DownsamplerBlock(f32).init(2);
var sink = radio.blocks.PulseAudioSink(1).init();
var top = radio.Flowgraph.init(gpa.allocator(), .{ .debug = true });
defer top.deinit();
try top.connect(&source.block, &tuner.block);
try top.connect(&tuner.block, &fm_demod.block);
try top.connect(&fm_demod.block, &af_filter.block);
try top.connect(&af_filter.block, &af_downsampler.block);
try top.connect(&af_downsampler.block, &sink.block);
try top.start();
radio.platform.waitForInterrupt();
_ = try top.stop();
}
Usage: ./zig-out/bin/example-rtlsdr_nbfm <frequency>
For example, listen to NOAA1, 162.400 MHz:
$ ./zig-out/bin/example-rtlsdr_nbfm 162.400e6
Additional NOAA weather radio station frequencies: 162.400 MHz
(NOAA1),
162.425 MHz
(NOAA2), 162.450 MHz
(NOAA3), 162.475 MHz
(NOAA4),
162.500 MHz
(NOAA5), 162.525 MHz
(NOAA6), 162.550 MHz
(NOAA7).