forked from timlardner/SpectrumAnalyser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HP_Spec.m
93 lines (80 loc) · 2.95 KB
/
HP_Spec.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% Copyright (C) 2016 University of Strathclyde %
% %
% Author: Timothy Lardner %
% Email: timothy.lardner@strath.ac.uk %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
classdef HP_Spec < handle
properties
comm_obj;
last_frame;
freq_range;
end
methods
function self = HP_Spec(varargin)
if nargin==2
BoardAd = varargin{0};
PrimAd = varargin{1};
elseif nargin==0
BoardAd = 0;
PrimAd = 18;
else
error('Unexpected number of arguments');
end
obj1 = instrfind('Type', 'gpib', 'BoardIndex', BoardAd, 'PrimaryAddress', PrimAd, 'Tag', '');
if isempty(obj1)
obj1 = gpib('NI', BoardAd, PrimAd);
else
fclose(obj1);
obj1 = obj1(1);
end
self.comm_obj = obj1;
fopen(self.comm_obj);
self.comm_obj.EOSMode = 'read&write';
self.comm_obj.EOSCharCode = 'LF';
end
function initialise(self,freq_range)
self.freq_range = freq_range;
fprintf(self.comm_obj,'TDF B;');
fprintf(self.comm_obj,'IP;SNGLS;');
fprintf(self.comm_obj,'CF %G;',freq_range);
fprintf(self.comm_obj,'SP %G;',freq_range*2);
end
function output = getFrame(self)
formatSpec = '%c';
fprintf(self.comm_obj,'TS;');
fprintf(self.comm_obj,'TRA?;');
combined = [];
while true
data = fscanf(self.comm_obj,formatSpec);
if isempty(data) || strcmp(data(end),char(10))
break
else
combined = [combined data];
end
end
output = self.fix_data(combined);
self.last_frame = output;
end
function close(self)
fclose(self.comm_obj);
self.comm_obj = [];
end
function output = fix_data(~,data)
split = strsplit(data,',');
output = str2double(split);
end
function plotLastFrame(self)
x_axis = get_x_axis(self);
figure(7656); clf;
plot(x_axis,self.last_frame); ylim([-80 0]);
ylabel('Amplitude (dB)'); xlabel('Freqency (Hz)');
drawnow;
end
function x_axis = get_x_axis(self)
x_axis = linspace(0,self.freq_range,length(self.last_frame));
end
end
end