MATLAB Answers

touchstone file could not be processed in matlab

74 views (last 30 days)
fima v
fima v on 22 Apr 2020
Edited: dpb on 23 Apr 2020
Hello , I have created a touchstone in cadence virtuoso of two port system(i have renamed it in txt so i could upload it here) but Matlab is not recocnising it.
i tried to import the file into matlab as shown in the code bellow, i get the following error.
Where did i go wrong?
S = sparameters('touchstone_file.s2p');
***************************ERROR MASSAGE********************
S = sparameters('Error using rf.file.touchstone.Data/createoptionlist
Invalid option line for the Touchstone format:
# Hz S RI R 33.755800 50.000000
Error in rf.file.touchstone.Data/read
Error in rf.file.touchstone.Data
Error in rf.internal.netparams.AllParameters/readRFFile
Error in rf.internal.netparams.AllParameters
Error in rf.internal.netparams.ScatteringParameters
Error in sparameters (line 75)
obj = obj@rf.internal.netparams.ScatteringParameters(varargin{:});


dpb on 22 Apr 2020
Know idea what a Touchstone format valid option line would be, but looking at the Touchstone File Spec document I don't see a case of more than one value for the reference resistance documented...altho can see for a 2-port analyzer needing one for each channel.
Just for grins, try changing that line to
# Hz S RI R 33.755800
and retry to see if works then.
After that, dunno what else to tell you either if it does or doesn't but will be interesting to know result.
dpb on 22 Apr 2020
[Answer moved to comment...dpb]
correct its working only for 50 ohm ports.
S = sparameters('spp4');
dpb on 22 Apr 2020
Well, that confirms the suspicion I had from the Standard document -- I don't know whether it is that that link is outdated or whether it is a vendor extension beyond the file spec in the particular instrument you're using.
I don't have necessary toolbox so can't look at the code to see how it is implemented; the format is pretty simple such that if it is m-code it should be pretty easy to create a version that will handle the other inputs.
Or, for just the two-port case, doesn't look like would be very hard to just read the file format directly with low-level i/o functions like textscan and/or fscanf and friends...

Sign in to comment.

Accepted Answer

dpb on 23 Apr 2020
Edited: dpb on 23 Apr 2020
For your specific data file in order to not have to modify the file, the following should return the data in usable fashion...
fid=fopen('touchstone_file.txt','r'); % open the file to read the header
while ~feof(fid) % read by line until find the first option line
if startsWith(l,'#'),break,end
R=str2num(strip(extractAfter(l,['R' char(9)]))); % convert the numeric values after R
while ~feof(fid) % now find beginning of numeric data
l=fgets(fid); % read line, include \n chars at end
if ~startsWith(l,{'#','!'}),break,end % quit find something other than remark, option
fseek(fid,-length(l),0) % back up to beginning this line
S=cell2mat(textscan(fid,'','CollectOutput',1)); % read the freq, S data to one array
fid=fclose(fid); % done with the file
clear i; % make sure is imaginary i
% and create a table of frequency and S values...assume complex data
tS=table(S(:,1), S(:,2)+i*S(:,3), S(:,4)+i*S(:,5), S(:,6)+i*S(:,7), S(:,8)+i*S(:,9), ...
% or create an sparameter object from the data...
S=sparameters(tS{:,2:end},tS.Freq,R(1)); % use {} to return array from table
The MATLAB function can only take one R value as discussed elsewhere, though.
For your file and any other of same shape as this one, this returns
>> R
R =
33.7558 50.0000
>> tS(1:10,:)
ans =
10×5 table
Freq S12 S11 S21 S22
_______ __________________ ___________________ _____________________ _________________
5e+09 0.99984+0.015619i -0.10198+0.0047799i 3.0572e-05+0.0019382i 0.82423-0.038319i
5.2e+09 0.99985+0.014558i -0.10195+0.0050565i 3.4759e-05+0.0020153i 0.82406-0.039836i
5.4e+09 0.99986+0.01356i -0.10193+0.0053301i 3.9108e-05+0.0020924i 0.82388-0.04135i
5.6e+09 0.99987+0.012617i -0.10191+0.005601i 4.3621e-05+0.0021696i 0.82369-0.042863i
5.8e+09 0.99988+0.011723i -0.10189+0.0058696i 4.8299e-05+0.0022468i 0.8235-0.044374i
6e+09 0.99988+0.010873i -0.10187+0.0061361i 5.314e-05+0.002324i 0.8233-0.045882i
6.2e+09 0.99989+0.010063i -0.10186+0.0064007i 5.8145e-05+0.0024013i 0.8231-0.047389i
6.4e+09 0.99989+0.0092902i -0.10184+0.0066635i 6.3315e-05+0.0024787i 0.82289-0.048893i
6.6e+09 0.99989+0.0085497i -0.10183+0.0069249i 6.8649e-05+0.002556i 0.82267-0.050396i
6.8e+09 0.99989+0.0078391i -0.10182+0.0071848i 7.4147e-05+0.0026334i 0.82245-0.051896i
I presume if there's more to the struct sparamteters creates in using builtin methods there's probably a way to stuff these data into it...altho you may run into a limitation that it doesn't know how to handle the R array, either.
The information for the sparameters function includes the optional form:
"sobj = sparameters(data,freq, Z0) creates an S-parameter object from the S-parameter data, data, and frequencies, freq, with a given reference impedance Z0."
Unfortunately, it also states that the Z0 reference impedance is only a single reference value, not one per channel.
should give you a compatible object but with the limitation of only one reference R. But, that's going to be a limitation on everything the whole suite does with the object.
I suppose one could incorporate the two values in the calculation of the returned results into the scaling of the various product terms and then use a reference of 1 for the object parameter.
Or, alternatively scale the return channel data from other than 50 (say) to 50 as if both channels used 50 (or vice versa to use other channel reference). Same idea.
As noted in the Comment below, this seems a real shortcoming in the implementation worthy of support request.


fima v
fima v on 23 Apr 2020
rftoolbox s-param functions are compatible with the tS object you created?
dpb on 23 Apr 2020
As is, undoubtedly not...that's a MATLAB table; I have no idea what the RF Toolbox object structure is...
The idea was to read the data and hopefully you could figure out how to put in into the form...I don't have the TB.
BTW, I see I missed a couple steps when pasted stuff from command history into should work as advertised now...didn't actually read the S data as was, having left out a line.
dpb on 23 Apr 2020
Seems that the problem should be worthy of a bug report/enhancement request to the official TMW support.
I "know nuthink!" about the subject nor the toolboxes so as outlined above don't know whether the problem is one that the given file is vendor-specific extension or the Matlab support functions and the spec I found are not up to date.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!