A C# library implementing common tools for working with microwave networks including reading/writing to Touchstone (.snp) files, de/embedding network parameters, etc. Download:
- Full support for Touchstone file format version 1.0
- Cascading/embedding/de-embedding 2-port networks
- S and T parameters
- Full support for Touchstone file format version 2.0
- Cascading n-port networks using the symmetry extension method
- Other parameter types (admittance, impedance, etc.)
- Interpolation
// Simple "ideal" 3 dB attenuator at some frequency with an excellent match
ScatteringParametersMatrix atten = new ScatteringParametersMatrix(numPorts: 2)
{
[1, 1] = NetworkParameter.FromPolarDecibelDegree(-50, 0),
[1, 2] = NetworkParameter.FromPolarDecibelDegree(-3, 0),
[2, 1] = NetworkParameter.FromPolarDecibelDegree(-3, 0),
[2, 2] = NetworkParameter.FromPolarDecibelDegree(-50, 0)
};
NetworkParameter ten_dBLoss = NetworkParameter.FromPolarDecibelDegree(-10, 0);
var exampleData = new NetworkParametersCollection<ScatteringParametersMatrix>(2)
{
// Create a new 2-port S-parameter matrix, and set S21 to a scalard 10 dB loss.
[1.0e9] = new ScatteringParametersMatrix(2) { [2, 1] = ten_dBLoss },
// Alternatively, can index the frequency and specific matrix index all at once
// i.e. at 2 GHz, set S21 to -10 dB
[2.0e9, 2, 1] = ten_dBLoss,
// etc.
[5.0e9, 2, 1] = ten_dBLoss
};
INetworkParametersCollection coll = Touchstone.ReadAllData(@"C:\example.s2p");
foreach (FrequencyParametersPair pair in coll)
{
(double frequency, NetworkParametersMatrix matrix) = pair;
double insertionLoss = matrix[2, 1].Magnitude_dB;
Console.WriteLine($"Insertion loss at {frequency} is {insertionLoss} dB");
}
This example uses the exampleData
collection shown in Create Frequency-Dependent Network Data.
// Create a new Touchstone file with all default settings
Touchstone tsFile = new Touchstone(exampleData);
tsFile.Write(@"C:\example_data.s2p");
Rather than loading all of the data into memory, the following example enumerates each matrix one at a time from a file to allow for runtime manipulation without allocating unnecessary memory.
string path = @"C:\example.s2p";
var filteredInsertionLoss = from FrequencyParametersPair pair in Touchstone.ReadData(path)
where pair.Frequency_Hz > 2.0e9 && pair.Frequency_Hz < 5.0e9
let InsertionLoss_dB = pair.Parameters[2, 1].Magnitude_dB
select new { pair.Frequency_Hz, InsertionLoss_dB };
The TouchstoneWriter
and TouchstoneReader
classes allow for precise control over Touchstone File IO, including full support for asynchronous file operations. This example uses the exampleData
collection shown in Create Frequency-Dependent Network Data.
TouchstoneWriterSettings settings = new TouchstoneWriterSettings
{
IncludeColumnNames = true,
NumericFormatString = "G3"
};
using (TouchstoneWriter writer = TouchstoneWriter.Create(@"C:\example_data.s2p", settings))
{
writer.Options.FrequencyUnit = FrequencyUnit.GHz;
writer.WriteCommentLine("This is an example comment at the top of the file.");
// The header will be written automatically if not called manually as soon as the first call is made to WriteData().
// However, you can manually invoke this if you would like to control where it is placed in relation to other comments.
writer.WriteHeader();
foreach (FrequencyParametersPair pair in exampleData)
writer.WriteData(pair);
}