how to plot transfer function exported from LTSpice

22 views (last 30 days)
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.

Answers (1)

Star Strider
Star Strider on 26 Dec 2021
Edited: Star Strider on 26 Dec 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')
T1 = 401×2 table
Var1 Var2 _______________________________________________ ___________ {'1.00000000000000e+001→4.99999950651983e-001'} -0.00015708 {'1.02329299228075e+001→4.99999948326283e-001'} -0.00016074 {'1.04712854805090e+001→4.99999945890976e-001'} -0.00016448 {'1.07151930523761e+001→4.99999943340896e-001'} -0.00016831 {'1.09647819614319e+001→4.99999940670635e-001'} -0.00017223 {'1.12201845430196e+001→4.99999937874529e-001'} -0.00017625 {'1.14815362149688e+001→4.99999934946646e-001'} -0.00018035 {'1.17489755493953e+001→4.99999931880776e-001'} -0.00018455 {'1.20226443461741e+001→4.99999928670417e-001'} -0.00018885 {'1.23026877081238e+001→4.99999925308757e-001'} -0.00019325 {'1.25892541179417e+001→4.99999921788668e-001'} -0.00019775 {'1.28824955169313e+001→4.99999918102682e-001'} -0.00020236 {'1.31825673855641e+001→4.99999914242981e-001'} -0.00020707 {'1.34896288259165e+001→4.99999910201378e-001'} -0.00021189 {'1.38038426460288e+001→4.99999905969300e-001'} -0.00021683 {'1.41253754462275e+001→4.99999901537771e-001'} -0.00022188
V2c = cellfun(@(x)sscanf(x, '%f\t%f'), T1.Var1, 'Unif',0);
V2m = [cell2mat(V2c')' T1.Var2]
V2m = 401×3
10.0000 0.5000 -0.0002 10.2329 0.5000 -0.0002 10.4713 0.5000 -0.0002 10.7152 0.5000 -0.0002 10.9648 0.5000 -0.0002 11.2202 0.5000 -0.0002 11.4815 0.5000 -0.0002 11.7490 0.5000 -0.0002 12.0226 0.5000 -0.0002 12.3027 0.5000 -0.0002
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)
T2 = 401×3 table
FreqHz MagndB PhasDg ______ ______ ___________ 10 0.5 -0.00015708 10.233 0.5 -0.00016074 10.471 0.5 -0.00016448 10.715 0.5 -0.00016831 10.965 0.5 -0.00017223 11.22 0.5 -0.00017625 11.482 0.5 -0.00018035 11.749 0.5 -0.00018455 12.023 0.5 -0.00018885 12.303 0.5 -0.00019325 12.589 0.5 -0.00019775 12.882 0.5 -0.00020236 13.183 0.5 -0.00020707 13.49 0.5 -0.00021189 13.804 0.5 -0.00021683 14.125 0.5 -0.00022188
D = table2array(T2)
D = 401×3
10.0000 0.5000 -0.0002 10.2329 0.5000 -0.0002 10.4713 0.5000 -0.0002 10.7152 0.5000 -0.0002 10.9648 0.5000 -0.0002 11.2202 0.5000 -0.0002 11.4815 0.5000 -0.0002 11.7490 0.5000 -0.0002 12.0226 0.5000 -0.0002 12.3027 0.5000 -0.0002
Mag = db2mag(T2.MagndB); % Absolute MAgnitude (Not Decibels)
Phs = deg2rad(T2.PhasDg); % Radian Phase Angle
Rsp = Mag.*exp(1j*Phs)
Rsp =
1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0000i 1.0593 - 0.0001i 1.0593 - 0.0001i 1.0593 - 0.0001i 1.0593 - 0.0001i 1.0593 - 0.0001i 1.0593 - 0.0001i 1.0593 - 0.0001i 1.0593 - 0.0001i 1.0593 - 0.0001i 1.0593 - 0.0001i 1.0593 - 0.0001i 1.0593 - 0.0001i 1.0593 - 0.0001i 1.0593 - 0.0001i 1.0593 - 0.0001i 1.0593 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0001i 1.0592 - 0.0002i 1.0592 - 0.0002i 1.0592 - 0.0002i 1.0592 - 0.0002i 1.0592 - 0.0002i 1.0592 - 0.0002i 1.0592 - 0.0002i 1.0592 - 0.0002i 1.0592 - 0.0002i 1.0592 - 0.0002i 1.0592 - 0.0002i 1.0592 - 0.0002i 1.0592 - 0.0002i 1.0592 - 0.0002i 1.0592 - 0.0002i 1.0592 - 0.0002i 1.0592 - 0.0002i 1.0592 - 0.0002i 1.0592 - 0.0002i 1.0592 - 0.0002i 1.0592 - 0.0002i 1.0592 - 0.0002i 1.0592 - 0.0003i 1.0592 - 0.0003i 1.0592 - 0.0003i 1.0592 - 0.0003i 1.0592 - 0.0003i 1.0592 - 0.0003i 1.0592 - 0.0003i 1.0592 - 0.0003i 1.0592 - 0.0003i 1.0592 - 0.0003i 1.0592 - 0.0003i 1.0592 - 0.0003i 1.0592 - 0.0003i 1.0592 - 0.0003i 1.0592 - 0.0003i 1.0592 - 0.0004i 1.0592 - 0.0004i 1.0592 - 0.0004i 1.0591 - 0.0004i 1.0591 - 0.0004i 1.0591 - 0.0004i 1.0591 - 0.0004i 1.0591 - 0.0004i 1.0591 - 0.0004i 1.0591 - 0.0004i 1.0591 - 0.0004i 1.0591 - 0.0005i 1.0591 - 0.0005i 1.0591 - 0.0005i 1.0591 - 0.0005i 1.0591 - 0.0005i 1.0591 - 0.0005i 1.0591 - 0.0005i 1.0590 - 0.0005i 1.0590 - 0.0006i 1.0590 - 0.0006i 1.0590 - 0.0006i 1.0590 - 0.0006i 1.0590 - 0.0006i 1.0590 - 0.0006i 1.0590 - 0.0006i 1.0590 - 0.0006i 1.0589 - 0.0007i 1.0589 - 0.0007i 1.0589 - 0.0007i 1.0589 - 0.0007i 1.0589 - 0.0007i 1.0589 - 0.0007i 1.0588 - 0.0008i 1.0588 - 0.0008i 1.0588 - 0.0008i 1.0588 - 0.0008i 1.0588 - 0.0008i 1.0587 - 0.0008i 1.0587 - 0.0009i 1.0587 - 0.0009i 1.0587 - 0.0009i 1.0586 - 0.0009i 1.0586 - 0.0010i 1.0586 - 0.0010i 1.0585 - 0.0010i 1.0585 - 0.0010i 1.0585 - 0.0010i 1.0584 - 0.0011i 1.0584 - 0.0011i 1.0584 - 0.0011i 1.0583 - 0.0011i 1.0583 - 0.0012i 1.0582 - 0.0012i 1.0582 - 0.0012i 1.0581 - 0.0012i 1.0581 - 0.0013i 1.0580 - 0.0013i 1.0580 - 0.0013i 1.0579 - 0.0014i 1.0578 - 0.0014i 1.0578 - 0.0014i 1.0577 - 0.0014i 1.0576 - 0.0015i 1.0576 - 0.0015i 1.0575 - 0.0015i 1.0574 - 0.0016i 1.0573 - 0.0016i 1.0572 - 0.0017i 1.0571 - 0.0017i 1.0570 - 0.0017i 1.0569 - 0.0018i 1.0568 - 0.0018i 1.0567 - 0.0018i 1.0566 - 0.0019i 1.0565 - 0.0019i 1.0564 - 0.0020i 1.0563 - 0.0020i 1.0561 - 0.0020i 1.0560 - 0.0021i 1.0558 - 0.0021i 1.0557 - 0.0022i 1.0555 - 0.0022i 1.0554 - 0.0023i 1.0552 - 0.0023i 1.0550 - 0.0023i 1.0548 - 0.0024i 1.0546 - 0.0024i 1.0544 - 0.0025i 1.0542 - 0.0025i 1.0540 - 0.0026i 1.0538 - 0.0026i 1.0536 - 0.0027i 1.0533 - 0.0027i 1.0531 - 0.0028i 1.0528 - 0.0028i 1.0525 - 0.0029i 1.0523 - 0.0029i 1.0520 - 0.0030i 1.0517 - 0.0030i 1.0514 - 0.0031i 1.0510 - 0.0031i 1.0507 - 0.0032i 1.0504 - 0.0032i 1.0500 - 0.0033i 1.0496 - 0.0033i 1.0493 - 0.0034i 1.0489 - 0.0034i 1.0485 - 0.0035i 1.0481 - 0.0035i 1.0476 - 0.0036i 1.0472 - 0.0036i 1.0468 - 0.0037i 1.0463 - 0.0037i 1.0458 - 0.0038i 1.0453 - 0.0038i 1.0448 - 0.0039i 1.0443 - 0.0039i 1.0438 - 0.0040i 1.0433 - 0.0040i 1.0427 - 0.0041i 1.0422 - 0.0041i 1.0416 - 0.0041i 1.0410 - 0.0042i 1.0405 - 0.0042i 1.0399 - 0.0042i 1.0393 - 0.0043i 1.0386 - 0.0043i 1.0380 - 0.0043i 1.0374 - 0.0044i 1.0367 - 0.0044i 1.0361 - 0.0044i 1.0354 - 0.0044i 1.0348 - 0.0044i 1.0341 - 0.0044i 1.0335 - 0.0045i 1.0328 - 0.0045i 1.0321 - 0.0045i 1.0314 - 0.0045i 1.0307 - 0.0045i 1.0301 - 0.0045i 1.0294 - 0.0045i 1.0287 - 0.0045i 1.0280 - 0.0045i 1.0273 - 0.0045i 1.0267 - 0.0045i 1.0260 - 0.0045i 1.0253 - 0.0044i 1.0247 - 0.0044i 1.0240 - 0.0044i 1.0233 - 0.0044i 1.0227 - 0.0044i 1.0221 - 0.0043i 1.0214 - 0.0043i 1.0208 - 0.0043i 1.0202 - 0.0042i 1.0196 - 0.0042i 1.0190 - 0.0042i 1.0184 - 0.0041i 1.0178 - 0.0041i 1.0172 - 0.0041i 1.0167 - 0.0040i 1.0161 - 0.0040i 1.0156 - 0.0039i 1.0151 - 0.0039i 1.0146 - 0.0038i 1.0141 - 0.0038i 1.0136 - 0.0037i 1.0131 - 0.0037i 1.0126 - 0.0036i 1.0122 - 0.0036i 1.0117 - 0.0036i 1.0113 - 0.0035i 1.0109 - 0.0034i 1.0105 - 0.0034i 1.0101 - 0.0033i 1.0097 - 0.0033i 1.0094 - 0.0032i 1.0090 - 0.0032i 1.0087 - 0.0031i 1.0083 - 0.0031i 1.0080 - 0.0030i 1.0077 - 0.0030i 1.0074 - 0.0029i 1.0071 - 0.0029i 1.0068 - 0.0028i 1.0065 - 0.0028i 1.0063 - 0.0027i 1.0060 - 0.0027i 1.0058 - 0.0026i 1.0055 - 0.0026i 1.0053 - 0.0025i
Real = real(Rsp) % REAL Part Of Complex Response Vector
Real = 401×1
1.0593 1.0593 1.0593 1.0593 1.0593 1.0593 1.0593 1.0593 1.0593 1.0593
Imag = imag(Rsp) % IMAG Part Of Complex Response Vector
Imag = 401×1
1.0e+00 * -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000
Sizes = [size(T2.FreqHz); size(Rsp)]
Sizes = 2×2
401 1 401 1
sysfr = idfrd(Rsp,T2.FreqHz,0,'FrequencyUnit','Hz') % Create System Response Data Object
sysfr = IDFRD model. Contains Frequency Response Data for 1 output(s) and 1 input(s). Response data is available at 401 frequency points, ranging from 10 Hz to 1e+05 Hz. Status: Created by direct construction or transformation. Not estimated.
sys_ss = ssest(sysfr, 3) % State Space Realisation
sys_ss = Continuous-time identified state-space model: dx/dt = A x(t) + B u(t) + K e(t) y(t) = C x(t) + D u(t) + e(t) A = x1 x2 x3 x1 -1.967e+05 4.001e+05 1.656e+08 x2 2.268e-07 1.976e+05 1.66e+08 x3 -8.426e-11 -8.339e-11 5.911e+11 B = u1 x1 1.475e-10 x2 -1.957e-10 x3 1.049e+06 C = x1 x2 x3 y1 1359 -1362 -5.638e+05 D = u1 y1 0 K = y1 x1 0 x2 0 x3 0 Parameterization: FREE form (all coefficients in A, B, C free). Feedthrough: none Disturbance component: none Number of free coefficients: 15 Use "idssdata", "getpvec", "getcov" for parameters and their uncertainties. Status: Estimated using SSEST on frequency response data "sysfr". Fit to estimation data: 100% FPE: 4.055e-14, MSE: 3.935e-14
figure
compare(sysfr, sys_ss)
sys_tf = tfest(sysfr, 2, 2) % Transfer Function Realisation
sys_tf = s^2 + 885.4 s - 4.117e10 ------------------------ s^2 - 860.3 s - 3.887e10 Continuous-time identified transfer function. Parameterization: Number of poles: 2 Number of zeros: 2 Number of free coefficients: 5 Use "tfdata", "getpvec", "getcov" for parameters and their uncertainties. Status: Estimated using TFEST on frequency response data "sysfr". Fit to estimation data: 100% FPE: 4.796e-14, MSE: 4.678e-14
figure
compare(sysfr, sys_tf)
figure
pzmap(sys_ss)
grid on
format long E
Poles = pole(sys_ss)
Poles = 3×1
1.0e+00 * -1.967237778489041e+05 1.975767036132812e+05 5.911429745307852e+11
Zeros = zero(sys_ss)
Zeros = 2×1
1.0e+00 * -2.033539159391936e+05 2.024607634016894e+05
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 Comments
francesco baldi
francesco baldi on 26 Dec 2021
the figure is correct, i don't understand why the scale on the y axis is different. The amplitude should go from -6dB to approximately -16dB, and the phase from 0 to approximately -77° in that range of frequencies.
Star Strider
Star Strider on 26 Dec 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.
.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!