Python Spectrum analyzer 'Max hold' function
Implementation of 'Max Hold' in Python
-
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
- Moku:Pro
- Python API examples
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()