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 (plotting)

Written by Paul Cracknell

Updated at May 16th, 2022

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
  • Python API examples
+ More

Example Python script to implement the Spectrum Analyzer (plotting)

This example Python script shows how to setup Moku:Lab's Spectrum Analyzer instrument and how to plot the data in real time. This example is not compatible with Moku:Go or Moku:Pro.

#
# pymoku example: Plotting Spectrum Analyzer
#
# This example demonstrates how you can configure the Spectrum Analyzer
# instrument and plot its spectrum data in real-time. It also shows how
# you can use its embedded signal generator to generate a sweep and single
# frequency waveform on the output channels.
#
# (c) 2019 Liquid Instruments Pty. Ltd.
#
from pymoku import Moku
from pymoku.instruments import SpectrumAnalyzer
import logging

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.get_by_name('Moku')

# 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(0, 70e6)
 i.set_rbw() # Auto-mode

 # Set up the embedded signal generator
 i.gen_sinewave(1, 1.0, 0, sweep=True)
 i.gen_sinewave(2, 0.5, 20e6)

 # 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

 # Get and update the plot with new data
 while True:
 frame = i.get_realtime_data()
 plt.pause(0.001)

 # Set the frame data for each channel plot
 line1.set_ydata(frame.ch1)
 line2.set_ydata(frame.ch2)
 # 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()
finally:
 m.close()
moku:lab

Was this article helpful?

Yes
No

Related Articles

  • Python: Frequency Response Analyzer (basic)
  • Using Python to generate arbitrary waveforms while observing the output signal
  • Python: Phasemeter (plotting)
  • Moku:Lab - a capacitance meter

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