Skip to content

Instantly share code, notes, and snippets.

@olgierd
Created December 8, 2022 20:27
Show Gist options
  • Save olgierd/a002743f4ee80f445afd914d17151546 to your computer and use it in GitHub Desktop.
Save olgierd/a002743f4ee80f445afd914d17151546 to your computer and use it in GitHub Desktop.
Peak-n-hold on FFT for IQ data
import scipy.io
import matplotlib.pyplot as plt
import numpy as np
from numpy.fft import fft, fftshift
path = '/home/oli/baseband_144288239Hz_17-59-43_08-12-2022.wav'
sr, d = scipy.io.wavfile.read(path)
d = d[...,0] + 1j * d[...,1] # convert to complex array
sta, stop = 30, 60 # time of sweep beginning and end in .wav file
fftsize = 1024
cfreq = 144288.239 # center frequency in kHz
fspan = sr/1000 # span in kHz = sample rate/1000
xlabel = np.linspace(cfreq-fspan/2, cfreq+fspan/2, fftsize) # label for each bin
win = np.hamming(fftsize) # hamming window for filtering fft result
maxes = np.zeros(fftsize)-999 # array for recording maximum value of each fft bucket
for s in range(sta*sr, stop*sr, fftsize):
data = win*d[s:s+fftsize] # cut out chunk of data
yf = np.abs(fftshift(fft(data))) # take fft, reorder the output, get modulus
s_mag = yf * 2 / np.sum(win)
s_dbfs = 20 * np.log10(s_mag/32768) # normalize and convert to log scale
maxes = np.maximum(maxes, s_dbfs) # record peak values
plt.plot(xlabel, maxes) # plot
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment