Skip to main content

pyhackrf

Motivation

All python bindings for hackrf are experimental at best, and the installation is quite quirky and weird. This is an attempt to change that.

Installation

sudo apt-get update
sudo apt-get install -y libusb-1.0-0-dev libfftw3-dev
pip install pyhackrf

Quick Example

To take samples and plot the power spectral density:

from hackrf import *
from pylab import *     # for plotting

with HackRF() as hrf:
	hrf.sample_rate = 20e6
	hrf.center_freq = 88.5e6

	samples = hackrf.read_samples(2e6)

	# use matplotlib to estimate and plot the PSD
	psd(samples, NFFT=1024, Fs=hackrf.sample_rate/1e6, Fc=hackrf.center_freq/1e6)
	xlabel('Frequency (MHz)')
	ylabel('Relative power (dB)')
	show()

More Example Use

To create a hackrf device:

from hackrf import *

hrf = HackRF()

If you have two HackRFs plugged in, you can open them with the device_index argument:

hackrf1 = HackRF(device_index = 0)
hackrf2 = HackRF(device_index = 1)

Callbacks

def my_callback(hackrf_transfer):
    c = hackrf_transfer.contents
    values = cast(c.buffer, POINTER(c_byte*c.buffer_length)).contents
    iq = bytes2iq(bytearray(values))

    return 0


# Start receiving...
hackrf.start_rx(my_callback)

# If you want to stop receiving...
hackrf.stop_rx()

Gains

There is a 14 dB amplifier at the front of the HackRF that you can turn on or off. The default is off.

The LNA gain setting applies to the IF signal. It can take values from 0 to 40 dB in 8 dB steps. The default value is 16 dB.

The VGA gain setting applies to the baseband signal. It can take values from 0 to 62 dB in 2 dB steps. The default value is 16 dB.

The LNA and VGA gains are set to the nearest step below the desired value. So if you try to set the LNA gain to 17-23 dB, the gain will be set to 16 dB. The same applies for the VGA gain; trying to set the gain to 27 dB will result in 26 dB.

# enable/disable the built-in amplifier:
hackrf.enable_amp()
hackrf.disable_amp()

# setting the LNA or VGA gains
hackrf.lna_gain = 8
hackrf.vga_gain = 22

# can also use setters or getters
hackrf.set_lna_gain(8)
hackrf.set_vga_gain(22)

Acknowledgements

For now most of the work is based on this. That is going to change, also this notice will be removed then.

License

This project is licensed under the GPL-3 license.

Metadata

Release files for pyhackrf 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyhackrf 0.2.0
File Size Uploaded
pyhackrf-0.2.0.tar.gz 8.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyhackrf 0.2.0
File Interpreter ABI Platform
pyhackrf-0.2.0-cp39-cp39-manylinux_2_31_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.31+ x86-64 Details

Total release size: 28.1 kB

Release files / pyhackrf-0.2.0.tar.gz

Download URL pyhackrf-0.2.0.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4a31b956ad3d5e4bcf53518734c9dbd20373405346f20b150c78f724065dfe77
BLAKE2b-256 checksum
How to use checksums
061068bd0fb978dda79abba2f9c13cdf4cd36caa7381ff930c5cb83bfe3a03df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.5 CPython/3.9.1 Linux/4.15.0-140-generic

Release files / pyhackrf-0.2.0-cp39-cp39-manylinux_2_31_x86_64.whl

Download URL pyhackrf-0.2.0-cp39-cp39-manylinux_2_31_x86_64.whl
Size 19.4 kB
Tags CPython 3.9 Linux glibc 2.31+ x86-64
SHA-256 checksum
How to use checksums
0ba9cd8eba422ad5b75cddd3ea85b4a1ff5749201aa65f510c2f3f8fa1669021
BLAKE2b-256 checksum
How to use checksums
c02b4272d631c80d0e6eefd64853f3d74e70c72b0e98ef0a1f4e977438139624
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.5 CPython/3.9.1 Linux/4.15.0-140-generic

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page