Moose/Instrument/SpectrumAnalyzer.pm untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 145 CC 7 SCAR 15.9
CC
7
Cov
0%
get_Xpoints_number
12.0
get_NameY
12.0
get_traceXY
6.0
CRAP 4.9
1 package Lab::Moose::Instrument::SpectrumAnalyzer;
2 $Lab::Moose::Instrument::SpectrumAnalyzer::VERSION = '3.931';
3 #ABSTRACT: Role of Generic Spectrum Analyzer for Lab::Moose::Instrument
4  
5 use v5.20;
6  
7  
8 use PDL::Core qw/pdl cat nelem/;
9  
10 use Carp;
11 use POSIX 'strftime';
12 use Moose::Role;
13 use MooseX::Params::Validate;
14 use Lab::Moose::Instrument qw/
15 timeout_param
16 precision_param
17 validated_getter
18 validated_setter
19 validated_channel_getter
20 validated_channel_setter
21 /;
22  
23 #use Lab::Moose::Instrument::Cache;
24  
25 requires qw(
26 sense_frequency_start_query
27 sense_frequency_start
28 sense_frequency_stop_query
29 sense_frequency_stop
30 sense_sweep_points_query
31 sense_sweep_points
32 sense_sweep_count_query
33 sense_sweep_count
34 sense_bandwidth_resolution_query
35 sense_bandwidth_resolution
36 sense_bandwidth_video_query
37 sense_bandwidth_video
38 sense_sweep_time_query
39 sense_sweep_time
40 display_window_trace_y_scale_rlevel_query
41 display_window_trace_y_scale_rlevel
42 unit_power_query
43 unit_power
44 sense_power_rf_attenuation_query
45 sense_power_rf_attenuation
46 validate_trace_param
47 );
48  
49 with 'Lab::Moose::Instrument::DisplayXY';
50  
51  
52 has 'capable_to_query_number_of_X_points_in_hardware' => (
53 is => 'rw',
54 isa => 'Bool',
55 required => 1,
56 default => 1,
57 );
58  
59 has 'capable_to_set_number_of_X_points_in_hardware' => (
60 is => 'rw',
61 isa => 'Bool',
62 required => 1,
63 default => 1,
64 );
65  
66 has 'hardwired_number_of_X_points' => (
67 is => 'rw',
68 isa => 'Int',
69 predicate => 'has_hardwired_number_of_X_points',
70 );
71  
72  
73 sub sense_sweep_points_from_traceY_query {
74  
75 # quite a lot of hardware does not report it, so we deduce it from Y-trace data
76 my ( $self, $channel, %args ) = validated_channel_getter( \@_ );
77 return nelem( $self->get_traceY(%args) );
78 }
79  
80 sub get_Xpoints_number {
81 my ( $self, $channel, %args ) = validated_channel_getter( \@_ );
82 if ( $self->has_hardwired_number_of_X_points ) {
83  
84 #carp("using hardwired number of points: " . $self->hardwired_number_of_X_points . "\n" );
85 return $self->cached_sense_sweep_points(
86 $self->hardwired_number_of_X_points );
87 }
88 if ( $self->capable_to_query_number_of_X_points_in_hardware ) {
89  
90 #carp("using hardware capabilities to detect number of points in a sweep\n");
91 return $self->sense_sweep_points_query(%args);
92 }
93  
94 #carp("trying heuristic to detect number of points in a sweep\n");
95 return $self->cached_sense_sweep_points(
96 $self->sense_sweep_points_from_traceY_query(%args) );
97 }
98  
99  
100 sub get_StartX {
101 my ( $self, %args ) = @_;
102 return $self->cached_sense_frequency_start();
103 }
104  
105 sub get_StopX {
106 my ( $self, %args ) = @_;
107 return $self->cached_sense_frequency_stop();
108 }
109  
110  
111 sub get_traceY {
112  
113 # grab what is on display for a given trace
114 my ( $self, %args ) = validated_hash(
115 \@_,
116 timeout_param(),
117 precision_param(),
118 trace => { isa => 'Int', default => 1 },
119 );
120  
121 my $precision = delete $args{precision};
122 my $trace = delete $args{trace};
123  
124 $trace = $self->validate_trace_param($trace);
125  
126 # Switch to binary trace format
127 $self->set_data_format_precision( precision => $precision );
128  
129 # above is equivalent to cached call
130 # $self->format_data( format => 'Real', length => 32 );
131  
132 # Get data.
133 my $binary = $self->binary_query(
134 command => "TRAC? TRACE$trace",
135 %args
136 );
137 my $traceY = pdl $self->block_to_array(
138 binary => $binary,
139 precision => $precision
140 );
141 return $traceY;
142 }
143  
144 sub get_traceXY {
145 my ( $self, %args ) = @_;
146  
147 my $traceY = $self->get_traceY(%args);
148  
149 # number of sweep points is known from the length of traceY
150 # so we set it to avoid extra call to get_traceY
151 if ( !$self->capable_to_query_number_of_X_points_in_hardware ) {
152  
153 #carp("setting cache with sweep number of points via heuristic");
154 $self->cached_sense_sweep_points( nelem($traceY) );
155 }
156 my $traceX = $self->get_traceX(%args);
157  
158 return cat( $traceX, $traceY );
159 }
160  
161 sub get_NameX {
162 my ( $self, %args ) = @_;
163 return 'Frequency';
164 }
165  
166 sub get_UnitX {
167 my ( $self, %args ) = @_;
168 return 'Hz';
169 }
170  
171 sub get_NameY {
172 my ( $self, %args ) = @_;
173 my $name;
174 my $unitY = $self->get_UnitY(%args);
175 if ( $unitY =~ qr/dbm|w/i ) { $name = 'Power'; }
176 elsif ( $unitY =~ qr/dbmv|dbuv|w/i ) { $name = 'Amplitude'; }
177 else { $name = 'Unknown'; carp( "Unknow Y unit " . $unitY ); }
178 return $name;
179 }
180  
181 sub get_UnitY {
182 my ( $self, %args ) = @_;
183 return $self->unit_power_query(%args);
184 }
185  
186 sub get_loggable_state {
187 my ( $self, %args ) = @_;
188  
189 my %state;
190 $state{Id} = $self->idn(%args);
191 $state{date} = strftime( '%Y-%m-%dT%H:%M:%S', localtime() );
192 $state{VBW} = $self->sense_bandwidth_video_query(%args);
193 $state{RBW} = $self->sense_bandwidth_resolution_query(%args);
194 $state{SweepTime} = $self->sense_sweep_time_query(%args);
195 $state{NameX} = $self->get_NameX(%args);
196 $state{UnitX} = $self->get_UnitX(%args);
197 $state{NameY} = $self->get_NameY(%args);
198 $state{UnitY} = $self->get_UnitY(%args);
199 $state{RefLevel} = $self->display_window_trace_y_scale_rlevel_query(%args);
200 $state{InputAttenuation} = $self->sense_power_rf_attenuation_query(%args);
201  
202 return %state;
203 }
204  
205 sub get_log_header {
206 my ( $self, %args ) = @_;
207 my %state = $self->get_loggable_state(%args);
208  
209 my @header_names = qw/
210 Id
211 date
212 VBW
213 RBW
214 SweepTime
215 NameX
216 UnitX
217 NameY
218 UnitY
219 RefLevel
220 InputAttenuation
221 /;
222 my $header_str='';
223 for my $name (@header_names) {
224 $header_str .= "$name = $state{$name}\n";
225 }
226  
227 return $header_str;
228 }
229  
230 sub get_plot_title {
231 my ( $self, %args ) = @_;
232 my %state = $self->get_loggable_state(%args);
233  
234 my @title_names = qw/
235 date
236 VBW
237 RBW
238 SweepTime
239 RefLevel
240 InputAttenuation
241 /;
242 my $title_str;
243 my $separator=' ';
244 for my $name (@title_names) {
245 if ( defined $title_str ) {
246 $title_str .= "$separator";
247 }
248 $title_str .= "$name = $state{$name}";
249 }
250  
251 return $title_str;
252 }
253  
254  
255 1;
256  
257 __END__