aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugeniy Mikhailov <evgmik@gmail.com>2015-08-11 17:04:07 -0400
committerEugeniy Mikhailov <evgmik@gmail.com>2015-08-11 17:04:07 -0400
commite96ff33859dffcb66902e352e05769f2528a1662 (patch)
tree0498b070a8854bb16b72e912a528d2f8fc49a1d8
parentf6d14fdba9d85067f32d5673a9d9fd7f55017a6a (diff)
downloadlinux-matlab-gpib-e96ff33859dffcb66902e352e05769f2528a1662.tar.gz
linux-matlab-gpib-e96ff33859dffcb66902e352e05769f2528a1662.zip
Device communication is done OS dependent
-rw-r--r--E440a_take_data.m188
1 files changed, 107 insertions, 81 deletions
diff --git a/E440a_take_data.m b/E440a_take_data.m
index e963222..e62a2fe 100644
--- a/E440a_take_data.m
+++ b/E440a_take_data.m
@@ -1,61 +1,80 @@
-function spectrum_analyzer = E4440a_take_data(data_save_flag, data_plot_flag)
+function spectrum_analyzer = E4440a_take_data(varargin)
% This script reads data from E440a spectrum analyzer and saves it to a file
%
% Eugeniy E. Mikhailov eemikh@wm.edu
% Gleb Romanov gromanov@hellok.org
% 6/20/2013
% 7/20/2015 added a choice for saving and plotting
+% 7/27/2015 added a choice to save only specific channels
%% some sane defaults
-switch (nargin)
- case 0
- data_plot_flag = true;
- data_save_flag = true;
- case 1
- data_plot_flag = true;
- case 2
- % we are good, nothing to do
- otherwise
- warning('Mora arguments than needed are provided');
- data_plot_flag = true;
- data_save_flag = true;
+nVarargs = length(varargin);
+if (nVarargs > 3 )
+ error ('wrong number of arguments');
end
+if (nVarargs < 3 )
+ channels_to_grab_flag = [ true, true, true]; % grab all channels
+else
+ channels_to_grab_flag = varargin{3};
+end
+if (nVarargs < 2 )
+ data_plot_flag = true; % Default to plot Data
+else
+ data_plot_flag = varargin{2};
+end
+if (nVarargs < 1 )
+ data_save_flag = true; % Default to save data
+else
+ data_save_flag = varargin{1};
+end
+
+
-%% Define instrument parameters
-board_index = 0;
-gpib_address = 21;
-bufSize = 100000;
%% Data file parameters
data_prefix = 'S';
data_path = 'Z:\qol_comp_data\data\';
run_number_file = 'Z:\qol_comp_data\data\autofile\runnum.dat';
-%% Find and initialize instrument
-%obj1 = instrfind('Type', 'gpib', 'BoardIndex', board_index, 'PrimaryAddress', gpib_address, 'Tag', '');
-
-% Create the GPIB object if it does not exist
-% otherwise use the object that was found.
-%if isempty(obj1)
- %obj1 = gpib('NI', board_index, gpib_address);
-%else
- %fclose(obj1);
- %obj1 = obj1(1);
-%end
-
+%% Windows computer parameters
+if ispc
+ % Define instrument parameters
+ board_index = 0;
+ gpib_address = 21;
+ bufSize = 100000;
+
+ %% Find and initialize instrument
+ obj1 = instrfind('Type', 'gpib', 'BoardIndex', board_index, 'PrimaryAddress', gpib_address, 'Tag', '');
+ % alternatively
+ % obj1 = instrfind('Type', 'visa-gpib', 'RsrcName', 'GPIB0::21::INSTR', 'Tag', '');
+
+ % Create the GPIB object if it does not exist
+ % otherwise use the object that was found.
+ if isempty(obj1)
+ obj1 = gpib('NI', board_index, gpib_address);
+ else
+ fclose(obj1);
+ obj1 = obj1(1);
+ end
+
+ % Adjust the buffers so the traces fit.
+ % Do this before fopen(obj1);
+ obj1.InputBufferSize = bufSize;
+ obj1.OutputBufferSize = bufSize;
+
+ %% Connect to instrument object, obj1.
+ fopen(obj1);
+end
-% Adjust the buffers so the traces fit.
-% Do this before fopen(obj1);
-%obj1.InputBufferSize = bufSize;
-%obj1.OutputBufferSize = bufSize;
+%% Unix specific parameters
+if isunix
+ obj1=lgpib('Agilent_E4405b')
+end
-obj1=lgpib('Agilent_E4405b')
-%% Connect to instrument object, obj1.
-%fopen(obj1);
-disp('--------------------')
+%disp('--------------------')
device_string = query(obj1, '*IDN?');
-disp(horzcat('Connected to ', device_string));
+%disp(horzcat('Connected to ', device_string));
% Communicating with instrument object, obj1.
%
@@ -64,15 +83,34 @@ disp(horzcat('Connected to ', device_string));
%
% Or read stuff using:
% _data_ = query(obj1, '_command_');
-disp('Reading traces...');
+%disp('Reading traces...');
+
+%% Find number of points
+Npoints_string = query(obj1, ':SENSe:SWEep:POINts?');
+Npoints = sscanf(Npoints_string, '%f');
+%Npoints=4695;
+
+tr1 = NaN(Npoints,1); % prefill traces with NaN
+tr2 = NaN(Npoints,1);
+tr3 = NaN(Npoints,1);
+
%% Read traces
% switch to ASCII trace transfer
-write(obj1, ':FORMAT:TRACE:DATA ASCII');
-tr1_string = query(obj1, ':TRACE:DATA? TRACE1;');
+fwrite(obj1, ':FORMAT:TRACE:DATA ASCII');
+
+if channels_to_grab_flag(1);
+tr1_string = query(obj1, ':TRACE:DATA? TRACE1;'); % select traces to grab
+end
+
+if channels_to_grab_flag(2);
tr2_string = query(obj1, ':TRACE:DATA? TRACE2;');
+
+end
+if channels_to_grab_flag(3);
tr3_string = query(obj1, ':TRACE:DATA? TRACE3;');
+end
-disp('Reading Spectrum Analyzer parameters...');
+%disp('Reading Spectrum Analyzer parameters...');
%% Read various spectrum analyzer parameters
freq_start_string = query(obj1, ':SENSE:FREQUENCY:START?');
freq_stop_string = query(obj1, ':SENSE:FREQUENCY:STOP?');
@@ -89,13 +127,24 @@ vbw_string = query(obj1, 'SENSE:BANDWIDTH:VIDEO?');
sweep_time_string = query(obj1, ':SENSE:SWEEP:TIME?');
%% Disconnect from instrument object, obj1.
-%fclose(obj1);
-disp('Spectrum Analyzer data communincation is done');
+if ispc
+fclose(obj1);
+end
+
+%disp('Spectrum Analyzer data communincation is done');
-%% Convert the traces from strings into vectors
+%% Convert the grabbed traces from strings into vectors
+if channels_to_grab_flag(1);
tr1 = sscanf(tr1_string, '%f,');
+end
+
+if channels_to_grab_flag(2);
tr2 = sscanf(tr2_string, '%f,');
+end
+
+if channels_to_grab_flag(3);
tr3 = sscanf(tr3_string, '%f,');
+end
% Transpose the vectors
tr1 = tr1';
@@ -105,7 +154,6 @@ tr3 = tr3';
% Create the frequency vector
freq_start = sscanf(freq_start_string, '%f');
freq_stop = sscanf(freq_stop_string, '%f');
-Npoints = length(tr1);
freq = linspace(freq_start,freq_stop, Npoints);
@@ -123,7 +171,7 @@ if (data_save_flag)
save_to_file = qol_get_next_data_file( data_prefix, data_path, run_number_file );
%
% Write the data to a file
- disp(' ');
+ %disp(' ');
disp(horzcat('Saving data to ',save_to_file));
save_to_file_handle = fopen(save_to_file,'wt');
fprintf(save_to_file_handle,'%s',horzcat('# ', datestr(clock)));
@@ -161,17 +209,21 @@ if (data_plot_flag)
%figure1 = figure(111);
%
% Create axes
- axes1 = axes('Parent',figure111,'YGrid','on','XGrid','on','FontSize',14);
- box(axes1,'on');
- hold(axes1,'all');
+% axes1 = axes('Parent',figure111,'YGrid','on','XGrid','on','FontSize',14);
+% box(axes1,'on');
+% hold(axes1,'all');
%ylim([-2,12])
%ylim([-85,-70])
%
% Create plot
- plot(freq/1e6,tr1,'Color',[1 0 0],'Parent',axes1,'DisplayName','Trace 1')
- plot(freq/1e6,tr2,'Color',[0 0 0],'Parent',axes1,'DisplayName','Trace 2')
- plot(freq/1e6,tr3,'Color',[0 0 1],'Parent',axes1,'DisplayName','Trace 3')
- %
+ plot(freq/1e6, tr1, 'Color', [1 0 0], 'DisplayName', 'Trace 1'); hold on
+ plot(freq/1e6, tr2, 'Color', [0 0 0], 'DisplayName', 'Trace 2');
+ plot(freq/1e6, tr3, 'Color', [0 0 1], 'DisplayName', 'Trace 3'); hold off
+
+% plot(freq/1e6,tr1,'Color',[1 0 0],'Parent',axes1,'DisplayName','Trace 1')
+% plot(freq/1e6,tr2,'Color',[0 0 0],'Parent',axes1,'DisplayName','Trace 2')
+% plot(freq/1e6,tr3,'Color',[0 0 1],'Parent',axes1,'DisplayName','Trace 3')
+%
% Create xlabel
xlabel('Detection frequency, MHz','FontSize',14);
%
@@ -183,33 +235,7 @@ if (data_plot_flag)
grid on;
- %% Plot processed traces
-
- % Open a window
- figure222 = figure(222);
- close(figure222);
- figure222 = figure(222);
- %
- % Create axes
- axes2 = axes('Parent',figure222,'YGrid','on','XGrid','on','FontSize',14);
- box(axes2,'on');
- hold(axes2,'all');
- %ylim([-2,12])
- %ylim([-85,-70])
- %
- % Create plot
- plot(freq/1e6,tr1-tr1,'Color',[0 0 0],'Parent',axes2,'DisplayName','Zero')
- plot(freq/1e6,tr2-tr1,'Color',[1 0 0],'Parent',axes2,'DisplayName','Tr2 - Tr1')
- plot(freq/1e6,tr3-tr1,'Color',[0 0 1],'Parent',axes2,'DisplayName','Tr3 - Tr1')
- %
- % Create xlabel
- xlabel('Detection frequency, MHz','FontSize',14);
- %
- % Create ylabel
- ylabel('Noise power, dB','FontSize',14);
- %
- % Show legend
- legend('show');
+
end
%drawnow;