Skip to content

Instantly share code, notes, and snippets.

@daniestevez
Created January 3, 2023 18:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daniestevez/0d519fd4044f3b9f44e170fd619fbb40 to your computer and use it in GitHub Desktop.
Save daniestevez/0d519fd4044f3b9f44e170fd619fbb40 to your computer and use it in GitHub Desktop.
Plot spectrum of SigMF recordings
#!/usr/bin/env python3
import json
import pathlib
import numpy as np
import matplotlib.pyplot as plt
def main():
NFFT = 2048
N = NFFT * 4096
files = pathlib.Path('.').glob('*.sigmf-data')
for file in files:
with open(file.name.replace('sigmf-data', 'sigmf-meta')) as f_meta:
meta = json.load(f_meta)
fs = meta['global']['core:sample_rate']
x = np.fromfile(file, 'int16', count=2*N)
x = x[::2] + 1j * x[1::2]
f = np.fft.fftshift(np.average(np.abs(np.fft.fft(x.reshape(-1, NFFT)))**2, axis=0))
freq = np.fft.fftshift(np.fft.fftfreq(NFFT, 1/fs))
plt.figure()
plt.plot(1e-6 * freq, 10*np.log10(f))
plt.title(file.name.replace('.sigmf-data', ''))
plt.ylabel('PSD (dB)')
plt.xlabel('Baseband frequency (MHz)')
plt.savefig(file.name.replace('sigmf-data', 'png'))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment