how to plot transfer function exported from LTSpice
Afficher commentaires plus anciens
hi there, i used the following code to plot the bode diagram of a trasnfer function exported from LTSpice:
filename = 'nameofthefile.txt';
fidi = fopen(filename, 'rt');
Dc = textscan(fidi, '%f(%fdB,%f°)', 'CollectOutput',1);
D = cell2mat(Dc);
figure
subplot(2,1,1)
semilogx(D(:,1), D(:,2))
title('Amplitude (dB)')
grid
subplot(2,1,2)
semilogx(D(:,1), D(:,3))
title('Phase (°)')
grid
xlabel('Frequency')
the fact is that it works well if i export the data from Spice in polar coordinates (dB and deg). Now i'd like to obtain the same result by exporting the data in cartesian coordinates (real and imm), and i don't know what to modify in this code. i attach the data set i'd like to plot.
Réponses (1)
Star Strider
le 26 Déc 2021
Modifié(e) : Star Strider
le 26 Déc 2021
I have no idea what would be necessary for LTSPice to export in Cartesian coordiantes, however to convert them to Cartesian in MATLAB is straightforward, by extracting the real and imag components of the ‘Rsp’ vector as the ‘Real’ and ‘Imag’ vectors (this also identifies the system, since I had that code ready to go) —
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/844455/Draft3.txt';
T1 = readtable(filename, 'VariableNamingRule','preserve')
V2c = cellfun(@(x)sscanf(x, '%f\t%f'), T1.Var1, 'Unif',0);
V2m = [cell2mat(V2c')' T1.Var2]
T2 = table('Size',[size(T1.Var1,1) 3],'VariableTypes',{'double','double','double'}, 'VariableNames',{'FreqHz','MagndB','PhasDg'});
T2.FreqHz = V2m(:,1);
T2.MagndB = V2m(:,2);
T2.PhasDg = V2m(:,3)
D = table2array(T2)
Mag = db2mag(T2.MagndB); % Absolute MAgnitude (Not Decibels)
Phs = deg2rad(T2.PhasDg); % Radian Phase Angle
Rsp = Mag.*exp(1j*Phs)
Real = real(Rsp) % REAL Part Of Complex Response Vector
Imag = imag(Rsp) % IMAG Part Of Complex Response Vector
Sizes = [size(T2.FreqHz); size(Rsp)]
sysfr = idfrd(Rsp,T2.FreqHz,0,'FrequencyUnit','Hz') % Create System Response Data Object
sys_ss = ssest(sysfr, 3) % State Space Realisation
figure
compare(sysfr, sys_ss)
sys_tf = tfest(sysfr, 2, 2) % Transfer Function Realisation
figure
compare(sysfr, sys_tf)
figure
pzmap(sys_ss)
grid on
format long E
Poles = pole(sys_ss)
Zeros = zero(sys_ss)
figure
subplot(2,1,1)
semilogx(D(:,1), D(:,2))
title('Amplitude (dB)')
grid
subplot(2,1,2)
semilogx(D(:,1), D(:,3))
title('Phase (°)')
grid
xlabel('Frequency')
EDIT — (26 Dec 2021 at 13:24)
Corrected name provided to readtable, and provided subsequent correct parsing of the resulting table.
With those changes, my code runs without error and produces the correct results.
.
9 commentaires
I copied tther correct file to ‘filename’ however forgot to substitute ‘filename’ in the readtable call. Also, the textscan format string no longer works with this file, as the data are significantly different.
My cellfun call:
V2c = cellfun(@(x)sscanf(x, '%f\t%f'), T1.Var1, 'Unif',0);
converts it correctly.
This is significantly different in format from the previous files, so it required a different way to parse them.
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/844455/Draft3.txt';
T1 = readtable(filename, 'VariableNamingRule','preserve')
V2c = cellfun(@(x)sscanf(x, '%f\t%f'), T1.Var1, 'Unif',0);
V2m = [cell2mat(V2c')' T1.Var2]
T2 = table('Size',[size(T1.Var1,1) 3],'VariableTypes',{'double','double','double'}, 'VariableNames',{'FreqHz','MagndB','PhasDg'});
T2.FreqHz = V2m(:,1);
T2.MagndB = V2m(:,2);
T2.PhasDg = V2m(:,3)
D = table2array(T2)
Mag = db2mag(T2.MagndB); % Absolute MAgnitude (Not Decibels)
Phs = deg2rad(T2.PhasDg); % Radian Phase Angle
Rsp = Mag.*exp(1j*Phs)
Real = real(Rsp) % REAL Part Of Complex Response Vector
Imag = imag(Rsp) % IMAG Part Of Complex Response Vector
Sizes = [size(T2.FreqHz); size(Rsp)]
sysfr = idfrd(Rsp,T2.FreqHz,0,'FrequencyUnit','Hz') % Create System Response Data Object
sys_ss = ssest(sysfr, 3) % State Space Realisation
figure
compare(sysfr, sys_ss)
sys_tf = tfest(sysfr, 2,2) % Transfer Function Realisation
figure
compare(sysfr, sys_tf)
figure
pzmap(sys_ss)
grid on
format long E
Poles = pole(sys_ss)
Zeros = zero(sys_ss)
figure
subplot(2,1,1)
semilogx(D(:,1), D(:,2))
title('Amplitude (dB)')
grid
subplot(2,1,2)
semilogx(D(:,1), D(:,3))
title('Phase (°)')
grid
xlabel('Frequency')
Everything now runs without error and the system is correctly identified. It took a few experiments to determine what the variables were, and where they were.
With the file correctly parsed, ‘Real’ and ‘Imag’ are correct, and the system is correctly identified
.
francesco baldi
le 26 Déc 2021
Star Strider
le 26 Déc 2021
I have no idea what is in the file, since the columns are not labeled. The code posted in the Question shed no light on this, and the textscan format string is completely inappropriate for this file, since there are no ‘dB’ or ‘°’ characters in it. The delimiters are also inconsistent.
The first 3 rows of the file (that has no header lines) are —
1.00000000000000e+001 4.99999950651983e-001,-1.57079617176353e-004
1.02329299228075e+001 4.99999948326283e-001,-1.60738470739046e-004
1.04712854805090e+001 4.99999945890976e-001,-1.64482549896060e-004
The first column appears to be frequency, the second column magnitude, and the third, phase. (It required some effort to parse the file correctly.) That asssumption works with respect to the identification part of the code as well.
My code appears to parse the data correctly and identify the systems correctly.
If I did not guess correctly what the file contained, it is because I had no reliable information to work with.
I will do my best to help, however I need to know what information is provided in the file so I can work with it correctly.
francesco baldi
le 26 Déc 2021
Star Strider
le 26 Déc 2021
Modifié(e) : Star Strider
le 26 Déc 2021
In that instance, only a few changes in my code are necessary, those being to construct the response vector ‘Rsp’ using the real and imaginary parts of the signal —
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/844455/Draft3.txt';
T1 = readtable(filename, 'VariableNamingRule','preserve')
V2c = cellfun(@(x)sscanf(x, '%f\t%f'), T1.Var1, 'Unif',0);
V2m = [cell2mat(V2c')' T1.Var2]
T2 = table('Size',[size(T1.Var1,1) 3],'VariableTypes',{'double','double','double'}, 'VariableNames',{'FreqHz','Real','Imag'});
T2.FreqHz = V2m(:,1);
T2.Real = V2m(:,2);
T2.Imag = V2m(:,3)
D = table2array(T2)
Rsp = hypot(T2.Imag,T2.Real).*exp(1j*atan2(T2.Imag,T2.Real)) % Create Complex Response Vector
Sizes = [size(T2.FreqHz); size(Rsp)]
sysfr = idfrd(Rsp,T2.FreqHz,0,'FrequencyUnit','Hz') % Create System Response Data Object
sys_ss = ssest(sysfr, 1) % State Space Realisation
figure
compare(sysfr, sys_ss)
sys_tf = tfest(sysfr, 1,1) % Transfer Function Realisation
figure
compare(sysfr, sys_tf)
figure
pzmap(sys_ss)
grid on
format long E
Poles = pole(sys_ss)
Zeros = zero(sys_ss)
figure
subplot(2,1,1)
semilogx(D(:,1), mag2db(abs(Rsp))) % Changed
title('Amplitude (dB)')
grid
subplot(2,1,2)
semilogx(D(:,1), angle(Rsp)) % Changed
title('Phase (°)')
grid
xlabel('Frequency')
This is now revealed to be a simple, first-order system.
EDIT — (26 Dec 2021 at 17:42)
Changed magnitude plot call to —
semilogx(D(:,1), mag2db(abs(Rsp)))
.
francesco baldi
le 26 Déc 2021
Star Strider
le 26 Déc 2021
In the magnitude plot, the y-axis scales are slightly different, so the curves appear slightly different. In the phase plots, the phase is ’off’ (at least in the plot) by 360° (so actually correct). This may be a minor bug in the System Identification Toolbox functions. (I intend to mention this to MathWorks.)
Since I apparently (finally) understood the contents of the file and parsed it correctly, I have no idea how they should be ‘different’. Unless there is other missing information, my code is correct and the plots are correct.
If this is incorrect, how should the plots look? How do they look when LTSpice plots them?
.
francesco baldi
le 26 Déc 2021
Star Strider
le 26 Déc 2021
In the last plot, I did not initially plot it in dB so I went back and edited the magnitude plot call to do that.
Now, all the plots agree.
.
Catégories
En savoir plus sur Annotations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!











