(directory, basename)
| 16 | |
| 17 | |
| 18 | def spectro_maker(directory, basename): |
| 19 | metaname = basename + ".sigmf-meta" |
| 20 | dataname = basename + ".sigmf-data" |
| 21 | metaFile = directory + "\\" + metaname |
| 22 | dataFile = directory + "\\" + dataname |
| 23 | |
| 24 | if (not os.path.exists(metaFile)) or (not os.path.exists(dataFile)): |
| 25 | # print("Either meta or data file does not exist") |
| 26 | return |
| 27 | |
| 28 | print("Creating png for", directory, basename) |
| 29 | |
| 30 | with open(metaFile, "r") as f: |
| 31 | meta_data = json.loads(f.read()) |
| 32 | with open(dataFile, "rb") as f: |
| 33 | bytes = f.read() |
| 34 | meta_data_global = meta_data.get("global", {}) |
| 35 | datatype = meta_data_global.get("core:datatype", "cf32_le") |
| 36 | sample_rate = float(meta_data_global.get("core:sample_rate", 0)) / 1e6 # MHz |
| 37 | center_freq = ( |
| 38 | float(meta_data.get("captures", [])[0].get("core:frequency", 0)) / 1e6 |
| 39 | ) # MHz |
| 40 | |
| 41 | if datatype == "cf32_le": |
| 42 | samples = np.frombuffer(bytes, dtype=np.complex64) |
| 43 | elif datatype == "ci16_le": |
| 44 | samples = np.frombuffer(bytes, dtype=np.int16) |
| 45 | samples = samples[::2] + 1j * samples[1::2] |
| 46 | elif datatype == "ri16_le": |
| 47 | samples = np.frombuffer(bytes, dtype=np.int16) |
| 48 | else: |
| 49 | print("Datatype not implemented") |
| 50 | samples = np.zeros(1024) |
| 51 | |
| 52 | # Generate spectrogram |
| 53 | num_rows = int(np.floor(len(samples) / fftSize)) |
| 54 | spectrogram = np.zeros((num_rows, fftSize)) |
| 55 | for i in range(num_rows): |
| 56 | spectrogram[i, :] = np.log10( |
| 57 | np.abs( |
| 58 | np.fft.fftshift(np.fft.fft(samples[i * fftSize: (i + 1) * fftSize])) |
| 59 | ) |
| 60 | ** 2 |
| 61 | ) |
| 62 | |
| 63 | fig = plt.figure() |
| 64 | ax = fig.add_subplot(1, 1, 1) |
| 65 | ax.imshow( |
| 66 | spectrogram, |
| 67 | aspect="auto", |
| 68 | extent=[ |
| 69 | sample_rate / -2 + center_freq, |
| 70 | sample_rate / 2 + center_freq, |
| 71 | 0, |
| 72 | len(samples) / sample_rate, |
| 73 | ], |
| 74 | ) |
| 75 | plt.axis("off") |
no test coverage detected