I'm trying to measure the power level of the signal captured by rtl sdr dongle and then compare several measurements to get the best level of power, but I don't know exactly how to return instantly the power level to my main function.
Here is my code:
import asyncio
from rtlsdr import RtlSdr
import numpy as np
import time
global power
async def streaming():
#SDR CONFIGURATION
sdr = RtlSdr()
sdr.rs = 2.4e6
sdr.fc = 801e6
async for samples in sdr.stream(512):
global power
samples = samples - np.mean(samples)
calibrate = 3.2
power = np.mean(np.abs(samples ** 2))
#print('Relative power:', calibrate * 10 * np.log10(power), 'dB')
await sdr.stop()
sdr.close()
#return power
# i traied this function to return the result from streaming() but it didn't work !!
async def get_result():
global power
while True:
yield power
async def main():
global power
nb_config = 100
fpower = -100
for i in range(nb_config):
# Get the current event loop.
loop = asyncio.get_running_loop()
#try to get the power value from the loop
task = streaming()
power = loop.run_until_complete(asyncio.gather(*task))
loop.close()
if power > fpower :
fpower = power
print(10*np.log10(fpower))
asyncio.run(main(), debug=True)