Free Sky

RTLSDR Autonome #Python

Voici le programme que j’utilise pour récupérer les échos de météores de façon autonome. Il s’agit d’une boucle infinie qui récupère les données de l’antenne, applique une FFT et si les résultats sont intéressants crée une image.

La library RTLSDR pour python :

https://pypi.org/project/pyrtlsdr/

#!/usr/bin/env python3.5

print("""
                 .:'
             _.::'
   .-;;-.   (_.'
  / ;;;' \\
 |.  `:   |    METEOR SCATTER SERVER V1.0 by flo 
  \:   `;/
   '-..-'
""")

from matplotlib.pyplot import psd
from matplotlib.pyplot import clf
from rtlsdr import *
import numpy as np
import gc
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
from Console import Console
import datetime

def rgb(minimum, maximum, value):
    minimum, maximum = float(minimum), float(maximum)
    ratio = 2 * (value-minimum) / (maximum - minimum)
    b = int(max(0, 255*(1 - ratio)))
    r = int(max(0, 255*(ratio - 1)))
    g = 255 - b - r
    return r, g, b

def addLineToPNG(png, line, y):
    mi = min(line)
    ma = 14
    for i in range(len(line)):
        if line[i] > ma:
            line[i] = ma
        png[i,y] = rgb(mi,ma,line[i])

def img_save(arr, time):
    l = len(arr)
    band = len(arr[0])
    new_im = Image.new('RGB', (band,l))
    png = new_im.load()
    for i in range(l):
        addLineToPNG(png, arr[i], i)
    new_im.save('out/'+time+".png", "PNG")


Console.print("Start")
while True:
    height = 90
    sample_rate = 250000
    band = 512
    gain = 50
    freq = 143050000

    sdr = RtlSdr()
    sdr.sample_rate = sample_rate
    sdr.center_freq = freq
    sdr.gain = gain
    
    z = 0
    arr = [[]] * height
    count = 0
    while(True):
        if(z == 500):
            sdr.close()            
            z = 0
            sdr = RtlSdr()
            sdr.sample_rate = sample_rate
            sdr.center_freq = freq
            sdr.gain = gain

        Pxx, f = psd(sdr.read_samples(sample_rate/2), NFFT=sample_rate, Fs=band/1e6, Fc=freq/1e6)
        X = Pxx[int((sample_rate/2)-(band/2)) : int((sample_rate/2)+(band/2))]
        X = np.float16(Pxx)
        mn = np.mean(X)
        if (mn > 3.5 and count == 0):
            count = height - 5

        if count > 0:
            count -= 1
            if count == 0:
                now = str(datetime.datetime.now()).replace(':', '-').replace(' ', '_')
                print('save : '+now)
                img_save(arr, now)
        i = height-1
        while i > 0:
            arr[i] = arr[i-1]
            i -= 1
        arr[0] = X
        del Pxx
        del f
        clf()
        gc.collect()
        z += 1
    Console.print('Stop send')