| 1 | package Lab::Moose::Instrument::VNASweep; | ||||||||||
| 2 | 3 | $Lab::Moose::Instrument::VNASweep::VERSION = '3.931'; | |||||||||
| 3 | #ABSTRACT: Role for network analyzer sweeps | ||||||||||
| 4 | |||||||||||
| 5 | 3 | ▶ | use v5.20; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | # Some default exports like 'inner' would collide with PDL | ||||||||||
| 8 | 3 | ▶ | use Moose::Role qw/with requires/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 3 | ▶ | use MooseX::Params::Validate 'validated_hash'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 3 | ▶ | use Moose::Util::TypeConstraints 'enum'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 3 | ▶ | use Lab::Moose::Instrument qw/ | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | timeout_param getter_params precision_param validated_setter | ||||||||||
| 14 | 3 | /; | |||||||||
| 15 | |||||||||||
| 16 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 3 | ▶ | use PDL; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | 3 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | 3 | with qw( | |||||||||
| 23 | Lab::Moose::Instrument::Common | ||||||||||
| 24 | |||||||||||
| 25 | Lab::Moose::Instrument::SCPI::Format | ||||||||||
| 26 | |||||||||||
| 27 | Lab::Moose::Instrument::SCPI::Instrument | ||||||||||
| 28 | |||||||||||
| 29 | Lab::Moose::Instrument::SCPI::Sense::Average | ||||||||||
| 30 | Lab::Moose::Instrument::SCPI::Sense::Bandwidth | ||||||||||
| 31 | Lab::Moose::Instrument::SCPI::Sense::Frequency | ||||||||||
| 32 | Lab::Moose::Instrument::SCPI::Sense::Sweep | ||||||||||
| 33 | |||||||||||
| 34 | Lab::Moose::Instrument::SCPI::Source::Power | ||||||||||
| 35 | |||||||||||
| 36 | Lab::Moose::Instrument::SCPI::Initiate | ||||||||||
| 37 | |||||||||||
| 38 | Lab::Moose::Instrument::SCPIBlock | ||||||||||
| 39 | ); | ||||||||||
| 40 | |||||||||||
| 41 | 3 | requires qw/sparam_sweep_data sparam_catalog/; | |||||||||
| 42 | |||||||||||
| 43 | 6 | ▶ | sub _get_data_columns { | ||||||||
|
Subroutine
_get_data_columns: called CC=4 SCAR=14.0
| |||||||||||
| 44 | 6 | my ( $self, $catalog, $freq_array, $points ) = @_; | |||||||||
| 45 | |||||||||||
| 46 | 6 | $freq_array = pdl($freq_array); | |||||||||
| 47 | 6 | $points = pdl($points); | |||||||||
| 48 | |||||||||||
| 49 | 6 | my $num_rows = $freq_array->nelem(); | |||||||||
| 50 | 6 | ▶ | if ( $num_rows != $self->cached_sense_sweep_points() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 0 | croak | |||||||||
| 52 | "length of frequency array not equal to number of configured points"; | ||||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 6 | my $num_columns = @{$catalog}; | |||||||||
| 56 | |||||||||||
| 57 | 6 | my $num_points = $points->nelem(); | |||||||||
| 58 | |||||||||||
| 59 | 6 | ▶ | if ( $num_points != $num_columns * $num_rows ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 0 | croak "$num_points != $num_columns * $num_rows"; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | # One pdl for each column. Will cat these before we return. | ||||||||||
| 64 | 6 | my @data_columns; | |||||||||
| 65 | |||||||||||
| 66 | 6 | for my $col_index ( 0 .. $num_columns / 2 - 1 ) { | |||||||||
| 67 | |||||||||||
| 68 | 6 | my $start = $col_index * $num_rows * 2; | |||||||||
| 69 | 6 | my $stop = $start + 2 * ( $num_rows - 1 ); | |||||||||
| 70 | |||||||||||
| 71 | 6 | my $real = $points->slice( [ $start, $stop, 2 ] ); | |||||||||
| 72 | 6 | my $im = $points->slice( [ $start + 1, $stop + 1, 2 ] ); | |||||||||
| 73 | |||||||||||
| 74 | 6 | my $amplitude = 10 * log10( $real**2 + $im**2 ); | |||||||||
| 75 | 6 | my $phase = atan2( $im, $real ); | |||||||||
| 76 | |||||||||||
| 77 | 6 | push @data_columns, $real, $im, $amplitude, $phase; | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 6 | return cat( $freq_array, @data_columns ); | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | |||||||||||
| 84 | 6 | ▶ | sub sparam_sweep { | ||||||||
|
Subroutine
sparam_sweep: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 85 | 6 | my ( $self, %args ) = validated_hash( | |||||||||
| 86 | \@_, | ||||||||||
| 87 | timeout_param(), | ||||||||||
| 88 | type => { isa => enum( ['frequency'] ), default => 'frequency' }, | ||||||||||
| 89 | average => { isa => 'Int', default => 1 }, | ||||||||||
| 90 | precision_param() | ||||||||||
| 91 | ); | ||||||||||
| 92 | |||||||||||
| 93 | 6 | my $average_count = delete $args{average}; | |||||||||
| 94 | 6 | my $precision = delete $args{precision}; | |||||||||
| 95 | |||||||||||
| 96 | # Not used so far. | ||||||||||
| 97 | 6 | my $sweep_type = delete $args{type}; | |||||||||
| 98 | |||||||||||
| 99 | 6 | my $catalog = $self->sparam_catalog(); | |||||||||
| 100 | |||||||||||
| 101 | 6 | my $freq_array = $self->sense_frequency_linear_array(); | |||||||||
| 102 | |||||||||||
| 103 | # Ensure single sweep mode. | ||||||||||
| 104 | 6 | ▶ | if ( $self->cached_initiate_continuous() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | 2 | $self->initiate_continuous( value => 0 ); | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | # Set average and sweep count. | ||||||||||
| 109 | |||||||||||
| 110 | 6 | ▶ | if ( $self->cached_sense_average_count() != $average_count ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 2 | $self->sense_average_count( value => $average_count ); | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 6 | ▶ | if ( $self->cached_sense_sweep_count() != $average_count ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 1 | $self->sense_sweep_count( value => $average_count ); | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | # Ensure correct data format | ||||||||||
| 119 | 6 | $self->set_data_format_precision( precision => $precision ); | |||||||||
| 120 | |||||||||||
| 121 | # Query measured traces. | ||||||||||
| 122 | |||||||||||
| 123 | # Get data. | ||||||||||
| 124 | $args{read_length} = $self->block_length( | ||||||||||
| 125 | 6 | num_points => @{$catalog} * @{$freq_array}, | |||||||||
| 126 | precision => $precision | ||||||||||
| 127 | ); | ||||||||||
| 128 | |||||||||||
| 129 | 6 | my $binary = $self->sparam_sweep_data(%args); | |||||||||
| 130 | |||||||||||
| 131 | 6 | my $points_ref = $self->block_to_array( | |||||||||
| 132 | binary => $binary, | ||||||||||
| 133 | precision => $precision | ||||||||||
| 134 | ); | ||||||||||
| 135 | |||||||||||
| 136 | 6 | return $self->_get_data_columns( $catalog, $freq_array, $points_ref ); | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | 2 | ▶ | sub _ensure_single_point_mode { | ||||||||
|
Subroutine
_ensure_single_point_mode: called CC=3 SCAR=12.0
| |||||||||||
| 140 | 2 | my $self = shift; | |||||||||
| 141 | 2 | my $points = $self->cached_sense_sweep_points(); | |||||||||
| 142 | 2 | ▶ | if ( $points != 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 143 | 0 | croak "not in single point mode (have $points points)"; | |||||||||
| 144 | } | ||||||||||
| 145 | 2 | my $start = $self->cached_sense_frequency_start(); | |||||||||
| 146 | 2 | my $stop = $self->cached_sense_frequency_stop(); | |||||||||
| 147 | 2 | ▶ | if ( $start != $stop ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 148 | 0 | croak <<"EOF"; | |||||||||
| 149 | not in single point mode: | ||||||||||
| 150 | start frequency: $start | ||||||||||
| 151 | stop frequency: $stop | ||||||||||
| 152 | EOF | ||||||||||
| 153 | } | ||||||||||
| 154 | } | ||||||||||
| 155 | |||||||||||
| 156 | 2 | ▶ | sub _rel_error { | ||||||||
|
Subroutine
_rel_error: called CC=1 SCAR=0.0
| |||||||||||
| 157 | 2 | my $a = shift; | |||||||||
| 158 | 2 | my $b = shift; | |||||||||
| 159 | 2 | return ( abs( ( $a - $b ) / $b ) ); | |||||||||
| 160 | } | ||||||||||
| 161 | |||||||||||
| 162 | |||||||||||
| 163 | 1 | ▶ | sub set_frq { | ||||||||
|
Subroutine
set_frq: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||
| 164 | 1 | my ( $self, $value, %args ) = validated_setter( \@_ ); | |||||||||
| 165 | 1 | my $points = $self->cached_sense_sweep_points(); | |||||||||
| 166 | 1 | ▶ | if ( $points != 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 167 | 1 | $self->sense_sweep_points( value => 1 ); | |||||||||
| 168 | } | ||||||||||
| 169 | 1 | my $start = $self->cached_sense_frequency_start(); | |||||||||
| 170 | 1 | my $stop = $self->cached_sense_frequency_stop(); | |||||||||
| 171 | |||||||||||
| 172 | 1 | ▶ | if ( _rel_error( $start, $value ) > 1e-14 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 173 | 1 | $self->sense_frequency_start( value => $value ); | |||||||||
| 174 | } | ||||||||||
| 175 | 1 | ▶ | if ( _rel_error( $stop, $value ) > 1e-14 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 176 | 1 | $self->sense_frequency_stop( value => $value ); | |||||||||
| 177 | } | ||||||||||
| 178 | |||||||||||
| 179 | 1 | $self->_ensure_single_point_mode(); | |||||||||
| 180 | } | ||||||||||
| 181 | |||||||||||
| 182 | |||||||||||
| 183 | 1 | ▶ | sub get_frq { | ||||||||
|
Subroutine
get_frq: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 184 | |||||||||||
| 185 | # ensure single point mode | ||||||||||
| 186 | 1 | my $self = shift; | |||||||||
| 187 | 1 | $self->_ensure_single_point_mode(); | |||||||||
| 188 | 1 | return $self->cached_sense_frequency_start(); | |||||||||
| 189 | } | ||||||||||
| 190 | |||||||||||
| 191 | |||||||||||
| 192 | 3 | 1; | |||||||||
| 193 | |||||||||||
| 194 | __END__ | ||||||||||