Page 1 of 1

[newbie] how to adapt this windows-code to run under linux

Posted: Sat Dec 06, 2014 3:25 pm
by jeandubois
I got some spectrum-analyzer-code from a friend which runs under windows, however I'd like to run it under Linux, as I am both a beginner in Python and rtl-sdr I'd appreciate very much if someone could tell which parts to change (I have other working rtl-sdr code made with gnuradio-companion running under Linux).
Here is the beginning of the code:

Code: Select all

#!/usr/bin/env python

from pylab import *
from rtlsdr import *

import Tkinter
from Tkinter import *
import time
import os
import math
import sys
import numpy

top = Tkinter.Tk()
top.geometry("840x660+200+85")
top.title(" RTLSDR Spectrum Analyser")

sdr = RtlSdr()
#~ sdr.DEFAULT_ASYNC_BUF_NUMBER = 16 
#~ sdr.DEFAULT_READ_SIZE = 128

var_fc = StringVar()
var_span = StringVar()
var_gain = StringVar()
var_0 = StringVar()
var_RBW = StringVar()
var_level = IntVar()
vr = IntVar()
va = IntVar()
vRBW = IntVar() 
vHz = IntVar()
var_average = IntVar()
#~ vxHz = IntVar()
spinbox_fc = DoubleVar()
spinbox_r = DoubleVar()
spinbox_i = DoubleVar()

texta=[NONE]*12
v= [0]*1000
line1=[NONE]*800
scan =0
level =0
textl=[NONE]*10
IF_gain=1
def btn_on_cmd():
        global scan
        scan=1
        display_on()
                
def btn_off_cmd():
        global scan
        scan=0
        

def btn_level_plus_cmd():
        global level
        if (level >0):level = level -1
        set_level()

def btn_level_min_cmd():
        global level
        if(level <5):level = level +1
        set_level()
        
def set_level():
        global level
        global textl
        global IF_gain
        for k in range (0,9):
                C.delete(textl[k])
                textl[k]= C.create_text(15, 15+ 49.5*k, text = str(-10*(k+1) - 10*level) )      
        if(level==0):
                sdr.gain = 0
                IF_gain =1.2
        if(level==1):
                sdr.gain = 10
                IF_gain =0.7
        if(level==2):
                sdr.gain = 20
                IF_gain = 0.55
        if(level==3):
                sdr.gain = 30
                IF_gain =0.65
        if(level==4):
                sdr.gain = 40
                IF_gain = 0.45
        if(level==5):
                sdr.gain = 50
                IF_gain =0.45
                
        if(level >0):   
                var_gain.set(" Gain: "+ str(sdr.get_gain()) +" dB")
        else:
                var_gain.set(" Gain: "+  " 0 "  +" dB")
        
        

# configure device
#~================== 
#sdr.sample_rate = 2.4e6
sdr.set_freq_correction(50)
sdr.center_freq = 120e6
fc = 120e6
fc_old =fc
fs = 2400000
sdr.sample_rate = fs
sdr.gain = 0
s=0
print(sdr.get_gains())  
        
def display_on():
        global fc
        global fc_old
        global v
        global scan
        global s
        global IF_gain
        FrSpan = vHz.get()*vr.get()
        Avg = 1*va.get()
        Nf= vRBW.get()
        #.........................................................................
        if(FrSpan == 500e6):
                if (fc < 275e6):
                        fc = 275e6
                        Spinbox_fc.delete(0,END)
                        Spinbox_fc.insert(0,275)
                R500k.select()  
                Gfft = 700*IF_gain
                textb3.delete(0.1,END)
                textb3.insert(0.1, str(s))              

                sdr.center_freq = fc - (FrSpan/2) + 1e6  + s*2e6
                        
                librtlsdr.rtlsdr_reset_buffer(sdr.dev_p)  #clear buffer
                samples = sdr.read_samples(512)
                samples = samples - (sum(samples)/len(samples))  # remove DC component
                samples[0:512:4]
                data = psd(samples, NFFT=4, window= numpy.blackman(4) )
                for t in range (0,4):
                        v[int((t+s*4)*0.8)]=(50+10*math.log10(data[0][t]*Gfft))*5
                s =s+1
                if(s==249):
                        s=0
        #.........................................................................
        if(FrSpan >=10e6 and FrSpan < 500e6):
                Pad = int(2400e6/FrSpan)
                Gfft = (4000/float(Nf))*0.62*IF_gain
                
                if(Nf > Pad):
                        textb2.delete(0.1,END)
                        textb2.insert(0.1,"RBW to small")
                else:
                        textb2.delete(0.1,END)  
                
                if(s > int(FrSpan/2e6)):
                        s=0

                sdr.center_freq = fc - (FrSpan/2) + 1e6  + s*2e6
                librtlsdr.rtlsdr_reset_buffer(sdr.dev_p)  #clear buffer
                samples = sdr.read_samples(512*Avg)
.
.
.
I put my friend's line "from rtlsdr import *" in comment and I have added the line:
import osmosdr
But when I run the script I get this message:
[email protected]:~$ ./spectrum_analyser_2c.py
linux; GNU C++ version 4.8.2; Boost_105400; UHD_003.007.002-0-g25f67e01

Traceback (most recent call last):
File "./spectrum_analyser_2c.py", line 18, in <module>
sdr = RtlSdr()
NameError: name 'RtlSdr' is not defined

Can anyone here help me further?

thanks in advance
jean

Re: [newbie] how to adapt this windows-code to run under lin

Posted: Mon Dec 08, 2014 5:21 pm
by jeandubois
I figured it out myself, I just had to install the module pyrtlsdr like this:
pip install pyrtlsdr

then, the code worked fine, just as it did under windows

regards,
jean