| 1 | package Lab::Moose::Instrument::SpectrumAnalyzer; | ||
| 2 | $Lab::Moose::Instrument::SpectrumAnalyzer::VERSION = '3.881'; | ||
| 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__ | ||
| 258 | |||
| 259 | =pod | ||
| 260 | |||
| 261 | =encoding UTF-8 | ||
| 262 | |||
| 263 | =head1 NAME | ||
| 264 | |||
| 265 | Lab::Moose::Instrument::SpectrumAnalyzer - Role of Generic Spectrum Analyzer for Lab::Moose::Instrument | ||
| 266 | |||
| 267 | =head1 VERSION | ||
| 268 | |||
| 269 | version 3.881 | ||
| 270 | |||
| 271 | =head1 DESCRIPTION | ||
| 272 | |||
| 273 | Basic commands to make functional basic spectrum analyzer | ||
| 274 | |||
| 275 | =head1 NAME | ||
| 276 | |||
| 277 | Lab::Moose::Instrument::SpectrumAnalyzer - Role of Generic Spectrum Analyzer | ||
| 278 | |||
| 279 | =head1 Hardware capabilities and presets attributes | ||
| 280 | |||
| 281 | Not all devices implemented full set of SCPI commands. | ||
| 282 | With following we can mark what is available | ||
| 283 | |||
| 284 | =head2 C<capable_to_query_number_of_X_points_in_hardware> | ||
| 285 | |||
| 286 | Can hardware report the number of points in a sweep. I.e. can it respont | ||
| 287 | to analog of C<[:SENSe]:SWEep:POINts?> command. | ||
| 288 | |||
| 289 | Default is 1, i.e true. | ||
| 290 | |||
| 291 | =head2 C<capable_to_set_number_of_X_points_in_hardware> | ||
| 292 | |||
| 293 | Can hardware set the number of points in a sweep. I.e. can it respont | ||
| 294 | to analog of C<[:SENSe]:SWEep:POINts> command. | ||
| 295 | |||
| 296 | Default is 1, i.e true. | ||
| 297 | |||
| 298 | =head2 C<hardwired_number_of_X_points> | ||
| 299 | |||
| 300 | Some hardware has fixed/unchangeable number of points in the sweep. | ||
| 301 | So we can set it here to simplify the logic of some commands. | ||
| 302 | |||
| 303 | This is not set by default. | ||
| 304 | Use C<has_hardwired_number_of_X_points> to check for its availability. | ||
| 305 | |||
| 306 | =head1 METHODS | ||
| 307 | |||
| 308 | Driver assuming this role must implements the following high-level method: | ||
| 309 | |||
| 310 | =head2 C<get_traceXY> | ||
| 311 | |||
| 312 | $data = $sa->traceXY(timeout => 10, trace => 2); | ||
| 313 | |||
| 314 | Perform a single sweep and return the resulting spectrum as a 2D PDL: | ||
| 315 | |||
| 316 | [ | ||
| 317 | [freq1, freq2, freq3, ..., freqN], | ||
| 318 | [power1, power2, power3, ..., powerN], | ||
| 319 | ] | ||
| 320 | |||
| 321 | I.e. the first dimension runs over the sweep points. | ||
| 322 | |||
| 323 | This method accepts a hash with the following options: | ||
| 324 | |||
| 325 | =over | ||
| 326 | |||
| 327 | =item B<timeout> | ||
| 328 | |||
| 329 | timeout for the sweep operation. If this is not given, use the connection's | ||
| 330 | default timeout. | ||
| 331 | |||
| 332 | =item B<trace> | ||
| 333 | |||
| 334 | number of the trace (1..3). Defaults to 1. | ||
| 335 | |||
| 336 | =back | ||
| 337 | |||
| 338 | =head2 get_StartX and get_StopX | ||
| 339 | |||
| 340 | Returns start and stop frequency | ||
| 341 | |||
| 342 | =head2 get_traceY | ||
| 343 | |||
| 344 | $data = $inst->get_traceY(timeout => 1, trace => 2, precision => 'single'); | ||
| 345 | |||
| 346 | Return Y points of a given trace in a 1D PDL: | ||
| 347 | |||
| 348 | This implementation is SCPI friendly. | ||
| 349 | |||
| 350 | =over | ||
| 351 | |||
| 352 | =item B<timeout> | ||
| 353 | |||
| 354 | timeout for the sweep operation. If this is not given, use the connection's | ||
| 355 | default timeout. | ||
| 356 | |||
| 357 | =item B<trace> | ||
| 358 | |||
| 359 | number of the trace 1, 2, 3 and so on. Defaults to 1. | ||
| 360 | It is hardware depended and validated by C<validate_trace_papam>, | ||
| 361 | which need to be implemented by a specific instrument driver. | ||
| 362 | |||
| 363 | =item B<precision> | ||
| 364 | |||
| 365 | floating point type. Has to be 'single' or 'double'. Defaults to 'single'. | ||
| 366 | |||
| 367 | =back | ||
| 368 | |||
| 369 | =head1 Required hardware dependent methods | ||
| 370 | |||
| 371 | =head2 validate_trace_param | ||
| 372 | |||
| 373 | Validates or applies hardware friendly aliases to trace parameter. | ||
| 374 | Need to be implemented by the instrument driver. For example | ||
| 375 | |||
| 376 | sub validate_trace_param { | ||
| 377 | my ( $self, $trace ) = @_; | ||
| 378 | if ( $trace < 1 || $trace > 3 ) { | ||
| 379 | confess "trace has to be in (1..3)"; | ||
| 380 | } | ||
| 381 | return $trace; | ||
| 382 | } | ||
| 383 | |||
| 384 | Use like this | ||
| 385 | |||
| 386 | $trace = $self->validate_trace_param( $trace ); | ||
| 387 | |||
| 388 | =head1 COPYRIGHT AND LICENSE | ||
| 389 | |||
| 390 | This software is copyright (c) 2023 by the Lab::Measurement team; in detail: | ||
| 391 | |||
| 392 | Copyright 2018 Eugeniy E. Mikhailov | ||
| 393 | 2020 Andreas K. Huettel | ||
| 394 | |||
| 395 | |||
| 396 | This is free software; you can redistribute it and/or modify it under | ||
| 397 | the same terms as the Perl 5 programming language system itself. | ||
| 398 | |||
| 399 | =cut |