Contact Us

If you still have questions or prefer to get help directly from an agent, please submit a request.
We’ll get back to you as soon as possible.

Please fill out the contact form below and we will reply as soon as possible.

  • Knowledge Base Home
  • Contact Us
  • Home
  • Moku:Lab
  • Software integrations
  • Python examples

Python Spectrum analyzer 'Max hold' function

Implementation of 'Max Hold' in Python

Written by Paul Cracknell

Updated at March 3rd, 2021

Contact Us

If you still have questions or prefer to get help directly from an agent, please submit a request.
We’ll get back to you as soon as possible.

Please fill out the contact form below and we will reply as soon as possible.

  • Moku:Go
    Moku:Go Arbitrary Waveform Generator Moku:Go Data Logger Moku:Go Frequency Response Analyzer Moku:Go Logic Analyzer & Pattern Generator Moku:Go Oscilloscope & Voltmeter Moku:Go PID Controller Moku:Go Spectrum Analyzer Moku:Go Waveform Generator Moku:Go Power Supplies Moku:Go Digital Filter Box Moku:Go FIR Filter Builder Moku:Go Lock-in Amplifier
  • Moku:Lab
    Windows Moku:Lab general Moku:Lab Instruments iPad app Software integrations
  • Moku:Pro
    Moku:Pro Instruments
+ More

How can I create a 'Max Hold' in Python on the Moku:Lab Spectrum Analyzer

The Moku:Lab's Spectrum Analyzer includes a math channel on both the iPad and Windows app. This math channel has a useful 'max hold' function.

The attached Python script deploys a Spectrum Analyzer instrument, sweeps the frequency and captures this data into Python. Within Python, we then calculate and display a 'Max Hold' trace.


#
# pymoku example: Plotting Spectrum Analyzer - Max Hold
#
# This example demonstrates how you can configure the Spectrum Analyzer
# instrument and plot ad Max Hold of its spectrum data in real-time. 
#
# (c) 2019 Liquid Instruments Pty. Ltd.
#
from pymoku import Moku
from pymoku.instruments import SpectrumAnalyzer
import logging
import numpy as np


import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter


logging.basicConfig(format='%(asctime)s:%(name)s:%(levelname)s::%(message)s')
logging.getLogger('pymoku').setLevel(logging.INFO)


# Connect to your Moku by its device name
# Alternatively, use Moku.get_by_serial('#####') or Moku('192.168.###.###')
m = Moku('192.168.1.44')


# Use dBm scaling on the y-axis
dbm = True


try:
    i = m.deploy_or_connect(SpectrumAnalyzer)


    # Set spectrum analyzer configuration
    i.set_defaults()
    i.set_dbmscale(dbm)
    i.set_span(70e6, 130e6)
    i.set_rbw()  # Auto-mode




    # Configure ADC inputs
    i.set_frontend(1, fiftyr=True)
    i.set_frontend(2, fiftyr=True)


    # Set up basic plot configurations
    line1, = plt.plot([])
    line2, = plt.plot([])
    plt.ion()
    plt.show()
    plt.grid(b=True)
    if(dbm):
        plt.ylim([-200, 100])
    else:
        plt.ylim([-0.5, 1.0])
    plt.autoscale(axis='x', tight=True)


    # Get an initial frame of data to set any frame-specific plot parameters
    frame = i.get_realtime_data()


    # Format the x-axis as a frequency scale
    ax = plt.gca()
    ax.xaxis.set_major_formatter(FuncFormatter(frame.get_xaxis_fmt))
    ax.yaxis.set_major_formatter(FuncFormatter(frame.get_yaxis_fmt))
    ax.fmt_xdata = frame.get_xcoord_fmt
    ax.fmt_ydata = frame.get_ycoord_fmt
    
    counter = 0
    spectrum_length = len(frame.ch1)
    


    # Get and update the plot with new data
    while True:
        frame = i.get_realtime_data()
        plt.pause(0.0001)
        # Set the frame data for each channel plot
        line1.set_ydata(frame.ch1)
        if counter == 0:
            current_max = frame.ch1
        else:
            for x in range(0,spectrum_length):
                if frame.ch1[x]>current_max[x]:
                    current_max[x] = frame.ch1[x]
        line2.set_ydata(current_max)        
        # Frequency axis shouldn't change, but to be sure
        line1.set_xdata(frame.frequency)
        line2.set_xdata(frame.frequency)
        # Ensure the frequency axis is a tight fit
        ax.relim()
        ax.autoscale_view()


        # Redraw the lines
        plt.draw()
        counter = counter + 1
        
finally:
    m.close()
math channel max hold python spectrum spectrum analyzer

Was this article helpful?

Yes
No

Related Articles

  • Python: Frequency Response Analyzer (plotting)
  • Python: Waveform Generator (triggered)
  • Python: Phasemeter (streaming)
  • Python: Oscilloscope (plotting)

Sitemap

  • Moku:Lab
  • Instruments
  • Software
  • Company
  • Support
  • Store
  • Terms & Conditions
  • Privacy Policy

Offices

United States
+1 (619) 332-6230
740 Lomas Santa Fe Dr
Suite 102
Solana Beach, CA 92075

Australia
+61 2 6171 9730
243 Northbourne Avenue
Suite 2
Lyneham, ACT 2602

Follow us

Youtube LinkedIn

官方微信

Contact us
© 2021 Liquid Instruments. All rights reserved.

Definition by Author

0
0
Expand