OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

get spectrum value with numpy only

  • Thread starter Thread starter JayMore
  • Start date Start date
J

JayMore

Guest
I want to do a kind of frequency monitoring program, using rtl-sdr wrapper in python and numpy. This program must run in console mode, no graphic interface. I do not want to have a dependency with matplotlib or scipy, so I'm looking for a pure python and numpy solution.

I know how to read data from the rtl-sdr dongle (for example, centered at 446MHz with a sample rate of 250kHz should give me a spectrum from 445,875MHz to 446,125MHz). But how to get this spectrum data ? How to compute the power of 80 linear bands in this range (freq resolution will be 3,125kHz) on data sampled for 100ms ?

I know I have to read 25000 samples (100ms at 250kHz), but I do not know how to compute the frequency power in numpy only.

Here is my skeleton code. the get_spectrum() function is what I want to do, but all the attempt I made with numpy (based on fft) where probably wrong because results were inconsistent (nothing special happens when I transmit on this frequency).

Code:
import numpy as np
from rtlsdr import RtlSdr

def get_spectrum( samples, sample_rate, freq_resolution):
  # TODO :)
  return [0,1,2,3,4]
  
sdr = RtlSdr()
sdr.sample_rate = 250_000
sdr.center_freq = 446_000_000
sdr.gain = 40
duration = 0.100# in second
sdr.read_samples(2048) # get rid of initial empty samples
while(True):
  samples = sdr.read_samples(duration * sdr.sample_rate)
  spectrum = get_spectrum(
    samples = samples,
    sample_rate = sdr.sample_rate,
    freq_resolution = sdr.sample_rate / 80) # to be drawn in a 80 columns terminal
  # normalize data for easy visualization
  min_val = np.min(spectrum)
  max_val = np.max(spectrum)
  scaled_data = 9 * ((spectrum - min_val) / (max_val - min_val))
  for i in scaled_data:
      v = int(i)
      print(int(i), end='')
  print()

For my use case, the power level have not to be accurate, no need for a real scientific unit. It is only to be able to visualize and compare power of each frequency.

I know it is related to FFT and/or Power Spectral Density, but i dont know how to do it only in Numpy.
<p>I want to do a kind of frequency monitoring program, using rtl-sdr wrapper in python and numpy. This program must run in console mode, no graphic interface. I do not want to have a dependency with matplotlib or scipy, so I'm looking for a pure python and numpy solution.</p>
<p>I know how to read data from the rtl-sdr dongle (for example, centered at 446MHz with a sample rate of 250kHz should give me a spectrum from 445,875MHz to 446,125MHz).
But how to get this spectrum data ? How to compute the power of 80 linear bands in this range (freq resolution will be 3,125kHz) on data sampled for 100ms ?</p>
<p>I know I have to read 25000 samples (100ms at 250kHz), but I do not know how to compute the frequency power in numpy only.</p>
<p>Here is my skeleton code. the <code>get_spectrum()</code> function is what I want to do, but all the attempt I made with numpy (based on fft) where probably wrong because results were inconsistent (nothing special happens when I transmit on this frequency).</p>
<pre class="lang-py prettyprint-override"><code>import numpy as np
from rtlsdr import RtlSdr

def get_spectrum( samples, sample_rate, freq_resolution):
# TODO :)
return [0,1,2,3,4]

sdr = RtlSdr()
sdr.sample_rate = 250_000
sdr.center_freq = 446_000_000
sdr.gain = 40
duration = 0.100# in second
sdr.read_samples(2048) # get rid of initial empty samples
while(True):
samples = sdr.read_samples(duration * sdr.sample_rate)
spectrum = get_spectrum(
samples = samples,
sample_rate = sdr.sample_rate,
freq_resolution = sdr.sample_rate / 80) # to be drawn in a 80 columns terminal
# normalize data for easy visualization
min_val = np.min(spectrum)
max_val = np.max(spectrum)
scaled_data = 9 * ((spectrum - min_val) / (max_val - min_val))
for i in scaled_data:
v = int(i)
print(int(i), end='')
print()
</code></pre>
<p>For my use case, the power level have not to be accurate, no need for a real scientific unit. It is only to be able to visualize and compare power of each frequency.</p>
<p>I know it is related to FFT and/or Power Spectral Density, but i dont know how to do it only in Numpy.</p>
 

Latest posts

Top