Issue
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)
Solution
The short answer is that if you want an async function to yield up multiple values, just yield them:
async def powers(): # was 'streaming', but that doesn't seem to describe it
...
async for samples in sdr.stream(512):
samples = samples - np.mean(samples)
calibrate = 3.2
power = np.mean(np.abs(samples ** 2))
yield power
await sdr.stop()
sdr.close()
But then you need to get these values inside main
:
async def main():
...
async for power in powers():
print(power)
Since this is cooperative multithreading, any time python is in main()
(or anything else), it won't be in powers()
. Whether that matters depends on how the tools in question are built, but you might need to be careful not to full up buffers once your code gets longer, if your SDR is still putting out data.
Answered By - 2e0byo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.