I have this code to plot PSD of a signal – but are encountering the below error looping over the peaks returned by signal.find_peaks to then annotate the frequency on the plot.
Note: that the signal.find_peaks returns the indices which I am then converting to a frequency (peak_freqs) before plotting on x axis with:
((peak/1024)*sample_rate) + (center_freq-(sample_rate/2))
The juypter notebook for the plot is:
from scipy import signal
import numpy as np
from matplotlib import pyplot as plt
samples = np.fromfile('test.fc32', dtype=np.complex64)
sample_rate = 768000
center_freq = 160500000
size = len(samples)
x = samples
fft_freqs = np.linspace(sample_rate / -2, sample_rate / 2, 1024) + center_freq
psd = plt.psd(x, NFFT=1024, Fs=768000, Fc=160500000)
peaks = signal.find_peaks(psd[0], prominence=0.000000000001),
for peak in peaks[0]:
peak_freq = ((peak/1024)*sample_rate) + (center_freq-(sample_rate/2))
plt.annotate("test", xy=(peak_freq , -115))
plt.show()
Which creates this plot:
However I get an error:
[1.6033875e+08 1.6050525e+08 1.6060125e+08 1.6070775e+08]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[37], line 17
15 peaks = signal.find_peaks(psd[0], prominence=0.000000000001),
16 for peak in peaks[0]:
---> 17 peak_freq = ((peak/1024)*sample_rate) + (center_freq-(sample_rate/2))
18 print ( peak_freq)
19 plt.annotate("test", xy=( peak_freq , -115))
TypeError: unsupported operand type(s) for /: 'dict' and 'int'
Error in callback <function _draw_all_if_interactive at 0x00000222FDD080E0> (for post_execute), with arguments args (),kwargs {}:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
File ~AppDataLocalPackagesPythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0LocalCachelocal-packagesPython311site-packagesmatplotlibpyplot.py:197, in _draw_all_if_interactive()
195 def _draw_all_if_interactive() -> None:
196 if matplotlib.is_interactive():
--> 197 draw_all()
There are two errors above, one related to the type being a dict and the second fatal one probably because peak_freq ends up being an array.
How can I loop over the peaks returned from signal.find_peaks.