Hi, I would do:
use v5.12; use PDL; use PDL::Constants qw(PI); use PDL::IO::Misc; # read all data, skip first row my ($f, $M11, $A11, $M21, $A21, $M12, $A12, $M22, $A22)=rcols "rem.s2p", {LINES=>"1:-1:1"}; my $rows=$f->nelem; my $M=pdl($M11, $M12, $M21, $M22); # Indices: filerow, position my $R=pdl($A11, $A12, $A21, $A22)*PI/180; # filerow, position my $S=$M*exp(i()*$R); # filerow, position my $Sm=$S->mv(0,-1) # position, filerow ->reshape(2,2, $rows); # column, row, filerow say $f, $Sm, $Sm->info; rcols would read all the data into columns. For each row $M and $R would have the magnitudes and the angles in radians, and $S would have the corresponding complex numbers. Then, for each row I rearrange the four corresponding $S's into a 2x2 matrix by first getting the row index out of the way and then doing a reshape. I ran it with a file rem2.s2p: Freq(MHz) MagS11 AngS11 MagS21 AngS21 MagS12 AngS12 MagS22 AngS22 100 0.588 50.208 0.770 -35.964 0.770 -35.964 0.588 50.208 200 0.589 51.209 1.771 -34.965 1.771 -34.965 1.589 51.209 300 1 0 1 45 1 90 1 135 and obtained [100 200 300] [ [ [0.376321423329891+0.451803260659953i 0.623227336287862-0.452203148268051i] [0.623227336287862-0.452203148268051i 0.376321423329891+0.451803260659953i] ] [ [0.368997536067325+0.459088029005597i 1.45133851899712-1.01491748594417i] [ 1.45133851899712-1.01491748594417i 0.995478921580609+1.23852441101849i] ] [ [ 1 6.12323399573677e-17+i] [ 0.707106781186548+0.707106781186547i -0.707106781186547+0.707106781186548i] ] ] PDL: CDouble D [2,2,3] In this case, the filerow index is the last one and I assume that the indices in the filecolumn labels are in the usual row-column order, so that, for example, the S12 element corresponding to frequency 300MHz would be $S->at(1,0,2) (permute the 12 to 21, as pdl uses column index first, and substract 1 to make the indices zero-based). Hope it helps. Regards, Luis On Thu, Jun 30, 2022 at 11:59:39PM -0700, Eric Wheeler wrote: > Hello all, > > I'm trying to read RF touchstone (.s2p) files that are in a format like so: > > Freq(MHz) MagS11 AngS11 MagS21 AngS21 MagS12 AngS12 MagS22 AngS22 > 100 0.588 50.208 0.770 -35.964 0.770 -35.964 0.588 50.208 > ... > 200 0.589 51.209 1.771 -34.965 1.771 -34.965 1.589 51.209 > > There are thousands of these lines in a file, one line for each measured > frequency. Each line represents a complex scattering (S-parameter) matrix > and the mag/angle format needs to be converted to a `cdouble` to work on > the matrix mathematically. > > A single-line 2x2 complex matrix might look as follows, where S_ji is a > complex value: > > [ S11 S12 ] > [ S21 S22 ] > > Since we are provided values in magnitude-angle format (in this example) > they must be converted to cdoubles so we can work on them. There are > several formats: RI, DB, and MA. For the MA (mag-angle) format this is > the transform where $a is mag and $b is angle: > > $complex = cos($b*pi()/180) + $a*sin($b*pi()/180) * i > > I can generate a vector of 2x4 matrices holding mag-angle pairs by reading > the file line by line like this: > > [ > [ > [ S11mag S11ang S12mag S12ang ] > [ S21mag S21ang S22mag S22ang ] > ] > ... for each line > ] > > I'm new to using PDL and at this point I'm not sure how to convert them to > a computable form. Since the mag/angle values need to be manipulated in > parallel before creating a cdouble out of them I'm not sure how to go > about this. > > Here are my questions: > > 1. How can I efficiently apply the > real = cos($b*pi()/180) > imag = $a*sin($b*pi()/180) > transform to each mag/angle pair where $a is mag and $b is angle? > > 2. How can I then (or simultaneously) convert the 2x4 real-imag matrix > from #1 into a 2x2 cdouble matrix to look something like this? > > [ > [ > [ S11 S12 ] > [ S21 S22 ] > ] > ... for each line where Sji are cdoubles > ] > > 3. Now that I've described the issue, is there a better way to do this? > > > Once they are in a matrix format then PDL can convert them efficiently to > other matrix types (T, A, Z, Y) to create parallel or series circuits with > matrix arithmetic at each frequency. We can then optimize the RF filter > circuits using component models published by manufacturers from actual > measurements. > > The resulting open-source tool will be comprised of Perl modules from this > work and published on CPAN. > > Thanks for your help! > > -- > Eric Wheeler > KJ7LNW > > > _______________________________________________ > pdl-general mailing list > pdl-general@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/pdl-general > -- o W. Luis Mochán, | tel:(52)(777)329-1734 /<(*) Instituto de Ciencias Físicas, UNAM | fax:(52)(777)317-5388 `>/ /\ Av. Universidad s/n CP 62210 | (*)/\/ \ Cuernavaca, Morelos, México | moc...@fis.unam.mx /\_/\__/ GPG: 791EB9EB, C949 3F81 6D9B 1191 9A16 C2DF 5F0A C52B 791E B9EB _______________________________________________ pdl-general mailing list pdl-general@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/pdl-general