Monitor with ADALM Pluto 2

https://techcoderadio.blogspot.com/2025/04/monitor-with-adalm-pluto.html

Enhanced detect code for Monitor with ADALM Pluto:

# 1. Remove DC offset
samples_clean = (samples - np.mean(samples)) * np.hanning(len(samples))

# 2. Shifted magnitude spectrum
spectrum = np.abs(np.fft.fftshift(np.fft.fft(samples_clean)))

# 3. Map to exact frequency bins
freqs = np.fft.fftshift(np.fft.fftfreq(len(samples), d=1/sample_rate)) + center_freq

# 4. Find peak
peak_idx = np.argmax(spectrum)
peak_freq_mhz = f"{freqs[peak_idx] / 1e6:.3f}"

 

Monitor with AD9361 fastlock. Uses libiio 1.00.

import time
import adi
import numpy as np

sdr = adi.Pluto("ip:192.168.2.1")
sdr.sample_rate = int(4e6)
sdr.rx_rf_bandwidth = int(4e6)
sdr.rx_buffer_size = 16384
sdr.gain_control_mode_chan0 = "manual"
sdr.rx_hardwaregain_chan0 = 50.0

# Minimize buffers
# use with libiio 0.26 not working in 1.00
# sdr._rxadc.set_kernel_buffers_count(1)

rx_lo_chan = sdr._ctrl.find_channel("altvoltage0", is_output=True)

freqs = [
    int(410e6),
    int(414e6),
    int(418e6),
    int(422e6),
    int(426e6),
    int(430e6),
    int(434e6),
    int(438e6),
]
THR = -44.0

print("Calibrating fastlock")
for slot, f in enumerate(freqs):
    sdr.rx_lo = f
    time.sleep(0.04)
    rx_lo_chan.attrs["fastlock_store"].value = str(slot)

# Precompute FFT
window = np.hanning(sdr.rx_buffer_size)
bin_offsets = np.fft.fftshift(np.fft.fftfreq(sdr.rx_buffer_size, d=1.0 / sdr.sample_rate))
full_scale = (2**11) * np.sum(window) / 2.0  # 12-bit ADC

print("\nFastlock scan")
try:
    while True:
        for slot, cf in enumerate(freqs):

            rx_lo_chan.attrs["fastlock_recall"].value = str(slot)
            
            _ = sdr.rx()
            sdr.rx_destroy_buffer() # for libiio 1.00
            iq = sdr.rx()
            
            # PSD
            fft_vals = np.fft.fftshift(np.fft.fft(iq * window))
            psd_dbfs = 20 * np.log10(np.abs(fft_vals) / full_scale + 1e-12)
            
            hits = np.where(psd_dbfs > THR)[0]
            if len(hits) > 0:
                peak_idx = hits[np.argmax(psd_dbfs[hits])]
                detected_freq = (cf + bin_offsets[peak_idx]) / 1e6
                peak_power = psd_dbfs[peak_idx]
                print(f"[Slot {slot}] Hit at {detected_freq:.3f} MHz | Peak: {peak_power:.1f} dBFS")

except KeyboardInterrupt:
    print("\nScan stopped.")

 

Libiio 1.00 can produce errors in Python, fix:

1. Open the file shown trace:
C:\Users\xxx\AppData\Local\Programs\Python\Python315\Lib\site-packages\iio\__init__.py
 

2. Line 370
_libc = _cdll(find_library("c"))

3. Replace it with Win compatible fallback
import os
_libc_name = "msvcrt" if os.name == "nt" else find_library("c")
_libc = _cdll(_libc_name)

4. Save the file and run import adi again

Comments

Popular posts from this blog

Telive-2 how-to

Modifying LILYGO T3 LoRa32 RTL_433 ESP OOK receiver example - log found devices to file

Monitor with Python and RTL-SDR