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
  • MATLAB examples

MATLAB: Phasemeter (plotting)

Written by Paul Cracknell

Updated at December 22nd, 2020

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

Example MATLAB script to implement the Phasemeter (plotting)

%% Plotting Phasemeter Example
%
% This example demonstrates how you can configure the Phasemeter instrument
% and stream dual-channel samples of the form [fs, f, count, phase, I, Q]. 
% The signal amplitude is calculated using these samples, and plotted for 
% real-time viewing.
% 
% (c) 2017 Liquid Instruments Pty. Ltd.
% 
%% Connect to your Moku
ip = input('Please enter your Moku:Lab IP address: ', 's');

% Connect to your Moku and deploy the desired instrument
m = MokuPhasemeter(ip);

%% Configure the instrument
% Set the samplerate to ~30Hz
m.set_samplerate('slow');

% Set up signal generator outputs
% Channel 1 - 5MHz, 0.5Vpp Sinewave
% Channel 2 - 10MHz, 1.0Vpp Sinewave
m.gen_sinewave(1, 0.5, 5e6);
m.gen_sinewave(2, 1.0, 10e6);

% Set the phase-tracking frequency of both channels
m.set_initfreq(1,5e6);
m.set_initfreq(2,10e6);

%% Start network-streaming data
% Stop any previous streaming sessions
m.stop_stream_data();
% Start a 30sec dual-channel streaming session
m.start_stream_data('duration',30,'ch1','true','ch2','true');

%% Set up the plots
% Set up a phase and amplitude line plot per channel
% Plot up to 500 samples at a time
n_plot_points = 500;
ts = (-(n_plot_points-1):1:0)/m.get_samplerate();

% No samples as yet
phase_ch1 = NaN(1,n_plot_points);
phase_ch2 = NaN(1,n_plot_points);
amp_ch1 = NaN(1,n_plot_points);
amp_ch2 = NaN(1,n_plot_points);

figure;

subplot(2,1,1);
alh = plot(ts, amp_ch1, ts, amp_ch2);
ylabel(gca,'Amplitude (V)')
xlabel('Time (s)');
xlim([ts(1),ts(end)])

subplot(2,1,2);
plh = plot(ts, phase_ch1, ts, phase_ch2);
ylabel(gca,'Phase (cyc)');
xlabel('Time (s)');
xlim([ts(1),ts(end)]);


%% Receive and plot new samples
% Number of samples to retrieve at a time
n_samples = 10;

while 1 
 % Receive up to 'n' samples of data per channel
 samples = m.get_stream_data('n',n_samples);
 
 % This moulds the samples matrix into a readable format
 % for each channel.
 samples_ch1 = samples{1};
 samples_ch2 = samples{2};
 
 % Break out of the loop if we receive empty arrays
 % This denotes the session has completed
 if isempty(samples_ch1) || isempty(samples_ch2)
 disp('Stream complete')
 break
 end
 
 % Append phase data to the plot
 phase_ch1_new = samples_ch1(:,4)';
 phase_ch2_new = samples_ch2(:,4)'; 
 phase_ch1 = [phase_ch1(length(phase_ch1_new)+1:end), phase_ch1_new];
 phase_ch2 = [phase_ch2(length(phase_ch2_new)+1:end), phase_ch2_new];
 
 % Append amplitude data to the plot
 % Amplitude is calculated as sqrt(I^2 + Q^2)
 amp_ch1_new = sqrt(samples_ch1(:,5).^2 + samples_ch1(:,6).^2)';
 amp_ch2_new = sqrt(samples_ch2(:,5).^2 + samples_ch2(:,6).^2)';
 amp_ch1 = [amp_ch1(length(amp_ch1_new)+1:end), amp_ch1_new];
 amp_ch2 = [amp_ch2(length(amp_ch2_new)+1:end), amp_ch2_new];
 
 % Plot the updated phase for each channel
 set(plh(1),'YData',phase_ch1);
 set(plh(2),'YData',phase_ch2);
 set(alh(1),'YData',amp_ch1);
 set(alh(2),'YData',amp_ch2);
 pause(0.1)
end

%% Close the network-streaming session
% Denote that we are done with the data streaming session to clean up 
% device and network resources.
m.stop_stream_data();
moku:lab

Was this article helpful?

Yes
No

Related Articles

  • MATLAB: Lock-in Amplifier (plotting)
  • MATLAB: Frequency Response Analyzer (plotting)
  • MATLAB: Laser Lock Box (basic)
  • MATLAB: Phasemeter

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