view rs_fsp7_noisetest.py @ 46:a6e5c97b4767

Add noise test
author Daniel O'Connor <darius@dons.net.au>
date Thu, 20 Dec 2012 11:48:19 +1030
parents
children fa728cf34f50
line wrap: on
line source

#!/usr/bin/env python

# Copyright (c) 2012
#      Daniel O'Connor <darius@dons.net.au>.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

import math
import rsib
import scpi
import sys

def setup(r):
    # Reset to defaults
    r.write("*RST")

    # Set to single sweep mode
    r.write("INIT:CONT OFF")

    # Enable display updates
    r.write("SYST:DISP:UPD ON")

    # Set frequency range
    r.write("FREQ:CEN 10.7MHz;SPAN 1MHz")

    # Switch marker 1 on in screen A
    r.write("CALC:MARK1 ON")

    # Enable noise measurement
    r.write("CALC:MARK1:FUNC:NOIS ON")

    # Set number of sweeps
    r.write("SWE:COUN 20")

    # Set resolution bandwidth
    r.write("SENS1:BAND:RES 10kHz")

    # Set video bandwidth (10x res BW)
    r.write("SENS1:BAND:VID 100kHz")
    
def getnoise(r):
    # Trigger the sweep
    r.write("INIT;*WAI")

    # Wait for it to be done
    r.write("*OPC?")
    opc = scpi.getdata(r.read(60), int)
    #print "OPC - %d" % (opc)
    assert(opc == 1)
    
    # Set data format
    r.write("FORM:DATA ASC")

    # Read noise value
    r.write("CALC:MARK1:FUNC:NOIS:RES?")
    data = r.read(10)
    #print "Data - " + data

    return float(data)

def setnoise(r, en):
    if en:
        val = "ON"
    else:
        val = "OFF"
    r.write("DIAG:SERV:NSO " + val)

def calcnf(enrdb, offdb, ondb):
    # Not possible but weak results may give it
    if ondb <= offdb:
        return 0
    ydb = ondb - offdb
    y = 10 ** (ydb / 10)
    enr = 10 ** (enrdb / 10)
    nf = 10 * math.log10(enr / (y - 1))
    return nf
    
def donoisetest(r, enr):
    print "Acquiring with noise off.."
    setnoise(r, False)
    off = getnoise(r)
    print "Acquiring with noise on.."
    setnoise(r, True)
    on = getnoise(r)
    return off, on, calcnf(enr, off, on)

if __name__ == '__main__':
    enr = 15.55 # From back of noise source
    r = rsib.RSIBDevice('analyzer')

    # ID instrument
    r.write('*IDN?')
    print "ID is " + r.read(5)

    #setup(r)
    r.write("INIT:CONT OFF")

    while True:
        off, on, nf = donoisetest(r, enr)
        print "Off %.3f dB, on %.3f dB, NF %.2f" % (off, on, nf)
        print "Press enter to perform a new measurement"
        sys.stdin.readline()