how to use LTSpice values in Matlab

132 views (last 30 days)
hi there,
i need to export the transfer function of a circuit from LTSpice to Matlab as a .txt file, and then i need to calculate some parameters using that transfer function.For example, assuming that H is my transfer function, i need to calculate G = 100*H/(1-H) using Matlab. How can i do it? I'm attaching the .txt file here.

Accepted Answer

Star Strider
Star Strider on 26 Dec 2021
Try this —
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/844665/Draft3.txt';
T1 = readtable(filename, 'VariableNamingRule','preserve')
T1 = 401×2 table
Freq. V(n002) ______ ______________________________________________________ 10 {'(-6.02060034191108e+000dB,-1.79999994078238e-002°)'} 10.233 {'(-6.02060036211185e+000dB,-1.84192732265253e-002°)'} 10.471 {'(-6.02060038326466e+000dB,-1.88483131850069e-002°)'} 10.715 {'(-6.02060040541437e+000dB,-1.92873467657410e-002°)'} 10.965 {'(-6.02060042860797e+000dB,-1.97366067499370e-002°)'} 11.22 {'(-6.02060045289464e+000dB,-2.01963313409642e-002°)'} 11.482 {'(-6.02060047832591e+000dB,-2.06667642906489e-002°)'} 11.749 {'(-6.02060050495572e+000dB,-2.11481550285142e-002°)'} 12.023 {'(-6.02060053284056e+000dB,-2.16407587940291e-002°)'} 12.303 {'(-6.02060056203956e+000dB,-2.21448367719392e-002°)'} 12.589 {'(-6.02060059261468e+000dB,-2.26606562307481e-002°)'} 12.882 {'(-6.02060062463075e+000dB,-2.31884906644261e-002°)'} 13.183 {'(-6.02060065815569e+000dB,-2.37286199374180e-002°)'} 13.49 {'(-6.02060069326061e+000dB,-2.42813304330296e-002°)'} 13.804 {'(-6.02060073001998e+000dB,-2.48469152052698e-002°)'} 14.125 {'(-6.02060076851176e+000dB,-2.54256741342303e-002°)'}
V2c = cellfun(@(x)sscanf(x, '(%fdB,%f°'), T1.('V(n002)'), 'Unif',0);
V2m = cell2mat(V2c')';
T2 = table('Size',[size(T1.('Freq.'),1) 3],'VariableTypes',{'double','double','double'}, 'VariableNames',{'FreqHz','MagndB','PhasDg'});
T2.FreqHz = T1.('Freq.');
T2.MagndB = V2m(:,1);
T2.PhasDg = V2m(:,2)
T2 = 401×3 table
FreqHz MagndB PhasDg ______ _______ _________ 10 -6.0206 -0.018 10.233 -6.0206 -0.018419 10.471 -6.0206 -0.018848 10.715 -6.0206 -0.019287 10.965 -6.0206 -0.019737 11.22 -6.0206 -0.020196 11.482 -6.0206 -0.020667 11.749 -6.0206 -0.021148 12.023 -6.0206 -0.021641 12.303 -6.0206 -0.022145 12.589 -6.0206 -0.022661 12.882 -6.0206 -0.023188 13.183 -6.0206 -0.023729 13.49 -6.0206 -0.024281 13.804 -6.0206 -0.024847 14.125 -6.0206 -0.025426
Mag = db2mag(T2.MagndB);
Phs = deg2rad(T2.PhasDg);
Rsp = Mag.*exp(1j*Phs)
Rsp =
0.5000 - 0.0002i 0.5000 - 0.0002i 0.5000 - 0.0002i 0.5000 - 0.0002i 0.5000 - 0.0002i 0.5000 - 0.0002i 0.5000 - 0.0002i 0.5000 - 0.0002i 0.5000 - 0.0002i 0.5000 - 0.0002i 0.5000 - 0.0002i 0.5000 - 0.0002i 0.5000 - 0.0002i 0.5000 - 0.0002i 0.5000 - 0.0002i 0.5000 - 0.0002i 0.5000 - 0.0002i 0.5000 - 0.0002i 0.5000 - 0.0002i 0.5000 - 0.0002i 0.5000 - 0.0002i 0.5000 - 0.0003i 0.5000 - 0.0003i 0.5000 - 0.0003i 0.5000 - 0.0003i 0.5000 - 0.0003i 0.5000 - 0.0003i 0.5000 - 0.0003i 0.5000 - 0.0003i 0.5000 - 0.0003i 0.5000 - 0.0003i 0.5000 - 0.0003i 0.5000 - 0.0003i 0.5000 - 0.0003i 0.5000 - 0.0003i 0.5000 - 0.0004i 0.5000 - 0.0004i 0.5000 - 0.0004i 0.5000 - 0.0004i 0.5000 - 0.0004i 0.5000 - 0.0004i 0.5000 - 0.0004i 0.5000 - 0.0004i 0.5000 - 0.0004i 0.5000 - 0.0004i 0.5000 - 0.0004i 0.5000 - 0.0005i 0.5000 - 0.0005i 0.5000 - 0.0005i 0.5000 - 0.0005i 0.5000 - 0.0005i 0.5000 - 0.0005i 0.5000 - 0.0005i 0.5000 - 0.0005i 0.5000 - 0.0005i 0.5000 - 0.0006i 0.5000 - 0.0006i 0.5000 - 0.0006i 0.5000 - 0.0006i 0.5000 - 0.0006i 0.5000 - 0.0006i 0.5000 - 0.0006i 0.5000 - 0.0007i 0.5000 - 0.0007i 0.5000 - 0.0007i 0.5000 - 0.0007i 0.5000 - 0.0007i 0.5000 - 0.0007i 0.5000 - 0.0008i 0.5000 - 0.0008i 0.5000 - 0.0008i 0.5000 - 0.0008i 0.5000 - 0.0008i 0.5000 - 0.0008i 0.5000 - 0.0009i 0.5000 - 0.0009i 0.5000 - 0.0009i 0.5000 - 0.0009i 0.5000 - 0.0009i 0.5000 - 0.0010i 0.5000 - 0.0010i 0.5000 - 0.0010i 0.5000 - 0.0010i 0.5000 - 0.0011i 0.5000 - 0.0011i 0.5000 - 0.0011i 0.5000 - 0.0011i 0.5000 - 0.0012i 0.5000 - 0.0012i 0.5000 - 0.0012i 0.5000 - 0.0012i 0.5000 - 0.0013i 0.5000 - 0.0013i 0.5000 - 0.0013i 0.5000 - 0.0014i 0.5000 - 0.0014i 0.5000 - 0.0014i 0.5000 - 0.0015i 0.5000 - 0.0015i 0.5000 - 0.0015i 0.5000 - 0.0016i 0.5000 - 0.0016i 0.5000 - 0.0016i 0.5000 - 0.0017i 0.5000 - 0.0017i 0.5000 - 0.0018i 0.5000 - 0.0018i 0.5000 - 0.0018i 0.5000 - 0.0019i 0.5000 - 0.0019i 0.5000 - 0.0020i 0.5000 - 0.0020i 0.5000 - 0.0021i 0.5000 - 0.0021i 0.5000 - 0.0022i 0.5000 - 0.0022i 0.5000 - 0.0023i 0.5000 - 0.0023i 0.5000 - 0.0024i 0.5000 - 0.0024i 0.5000 - 0.0025i 0.5000 - 0.0025i 0.5000 - 0.0026i 0.5000 - 0.0027i 0.5000 - 0.0027i 0.5000 - 0.0028i 0.5000 - 0.0029i 0.5000 - 0.0029i 0.5000 - 0.0030i 0.5000 - 0.0031i 0.5000 - 0.0031i 0.5000 - 0.0032i 0.5000 - 0.0033i 0.5000 - 0.0034i 0.5000 - 0.0034i 0.5000 - 0.0035i 0.5000 - 0.0036i 0.5000 - 0.0037i 0.5000 - 0.0038i 0.5000 - 0.0039i 0.5000 - 0.0039i 0.5000 - 0.0040i 0.5000 - 0.0041i 0.5000 - 0.0042i 0.5000 - 0.0043i 0.5000 - 0.0044i 0.5000 - 0.0045i 0.5000 - 0.0046i 0.5000 - 0.0047i 0.5000 - 0.0049i 0.5000 - 0.0050i 0.4999 - 0.0051i 0.4999 - 0.0052i 0.4999 - 0.0053i 0.4999 - 0.0054i 0.4999 - 0.0056i 0.4999 - 0.0057i 0.4999 - 0.0058i 0.4999 - 0.0060i 0.4999 - 0.0061i 0.4999 - 0.0063i 0.4999 - 0.0064i 0.4999 - 0.0065i 0.4999 - 0.0067i 0.4999 - 0.0069i 0.4999 - 0.0070i 0.4999 - 0.0072i 0.4999 - 0.0073i 0.4999 - 0.0075i 0.4999 - 0.0077i 0.4999 - 0.0079i 0.4999 - 0.0081i 0.4999 - 0.0082i 0.4999 - 0.0084i 0.4999 - 0.0086i 0.4998 - 0.0088i 0.4998 - 0.0090i 0.4998 - 0.0092i 0.4998 - 0.0095i 0.4998 - 0.0097i 0.4998 - 0.0099i 0.4998 - 0.0101i 0.4998 - 0.0104i 0.4998 - 0.0106i 0.4998 - 0.0109i 0.4998 - 0.0111i 0.4997 - 0.0114i 0.4997 - 0.0116i 0.4997 - 0.0119i 0.4997 - 0.0122i 0.4997 - 0.0125i 0.4997 - 0.0128i 0.4997 - 0.0131i 0.4996 - 0.0134i 0.4996 - 0.0137i 0.4996 - 0.0140i 0.4996 - 0.0143i 0.4996 - 0.0146i 0.4996 - 0.0150i 0.4995 - 0.0153i 0.4995 - 0.0157i 0.4995 - 0.0161i 0.4995 - 0.0164i 0.4994 - 0.0168i 0.4994 - 0.0172i 0.4994 - 0.0176i 0.4994 - 0.0180i 0.4993 - 0.0184i 0.4993 - 0.0189i 0.4993 - 0.0193i 0.4992 - 0.0197i 0.4992 - 0.0202i 0.4991 - 0.0207i 0.4991 - 0.0212i 0.4991 - 0.0216i 0.4990 - 0.0221i 0.4990 - 0.0227i 0.4989 - 0.0232i 0.4989 - 0.0237i 0.4988 - 0.0243i 0.4988 - 0.0248i 0.4987 - 0.0254i 0.4986 - 0.0260i 0.4986 - 0.0266i 0.4985 - 0.0272i 0.4984 - 0.0278i 0.4984 - 0.0285i 0.4983 - 0.0291i 0.4982 - 0.0298i 0.4981 - 0.0305i 0.4980 - 0.0312i 0.4980 - 0.0319i 0.4979 - 0.0327i 0.4978 - 0.0334i 0.4976 - 0.0342i 0.4975 - 0.0350i 0.4974 - 0.0358i 0.4973 - 0.0366i 0.4972 - 0.0375i 0.4970 - 0.0383i 0.4969 - 0.0392i 0.4968 - 0.0401i 0.4966 - 0.0410i 0.4965 - 0.0420i 0.4963 - 0.0429i 0.4961 - 0.0439i 0.4959 - 0.0449i 0.4957 - 0.0460i 0.4955 - 0.0470i 0.4953 - 0.0481i 0.4951 - 0.0492i 0.4949 - 0.0503i 0.4946 - 0.0515i 0.4944 - 0.0526i 0.4941 - 0.0538i 0.4939 - 0.0550i 0.4936 - 0.0563i 0.4933 - 0.0576i 0.4930 - 0.0589i 0.4926 - 0.0602i 0.4923 - 0.0616i 0.4919 - 0.0630i 0.4916 - 0.0644i 0.4912 - 0.0658i 0.4908 - 0.0673i 0.4903 - 0.0688i 0.4899 - 0.0703i 0.4894 - 0.0719i 0.4889 - 0.0735i 0.4884 - 0.0752i 0.4879 - 0.0768i 0.4873 - 0.0785i 0.4868 - 0.0803i 0.4862 - 0.0820i 0.4855 - 0.0838i 0.4849 - 0.0857i 0.4842 - 0.0875i 0.4835 - 0.0894i 0.4827 - 0.0914i 0.4819 - 0.0934i 0.4811 - 0.0954i 0.4802 - 0.0974i 0.4793 - 0.0995i 0.4784 - 0.1016i 0.4774 - 0.1038i 0.4764 - 0.1060i 0.4754 - 0.1082i 0.4743 - 0.1105i 0.4731 - 0.1128i 0.4719 - 0.1151i 0.4707 - 0.1175i 0.4694 - 0.1199i 0.4680 - 0.1223i 0.4666 - 0.1248i 0.4652 - 0.1273i 0.4637 - 0.1298i 0.4621 - 0.1324i 0.4604 - 0.1350i 0.4587 - 0.1376i 0.4569 - 0.1403i 0.4551 - 0.1430i 0.4532 - 0.1457i 0.4512 - 0.1484i 0.4491 - 0.1512i 0.4470 - 0.1540i 0.4447 - 0.1568i 0.4424 - 0.1596i 0.4400 - 0.1624i 0.4376 - 0.1653i 0.4350 - 0.1681i 0.4324 - 0.1710i 0.4296 - 0.1739i 0.4268 - 0.1768i 0.4239 - 0.1796i 0.4209 - 0.1825i 0.4177 - 0.1854i 0.4145 - 0.1882i 0.4112 - 0.1911i 0.4078 - 0.1939i 0.4043 - 0.1967i 0.4007 - 0.1995i 0.3970 - 0.2022i 0.3931 - 0.2050i 0.3892 - 0.2077i 0.3852 - 0.2103i 0.3811 - 0.2129i 0.3768 - 0.2154i 0.3725 - 0.2179i 0.3681 - 0.2203i 0.3636 - 0.2227i 0.3590 - 0.2250i 0.3542 - 0.2272i 0.3494 - 0.2294i 0.3446 - 0.2314i 0.3396 - 0.2334i 0.3345 - 0.2353i 0.3294 - 0.2371i 0.3242 - 0.2387i 0.3189 - 0.2403i 0.3135 - 0.2418i 0.3081 - 0.2431i 0.3026 - 0.2444i 0.2971 - 0.2455i 0.2915 - 0.2465i 0.2859 - 0.2474i 0.2803 - 0.2482i 0.2746 - 0.2488i 0.2689 - 0.2493i 0.2631 - 0.2497i 0.2574 - 0.2499i 0.2516 - 0.2500i 0.2459 - 0.2500i 0.2401 - 0.2498i 0.2344 - 0.2495i 0.2287 - 0.2491i 0.2230 - 0.2485i 0.2173 - 0.2479i 0.2117 - 0.2470i 0.2061 - 0.2461i 0.2005 - 0.2450i 0.1950 - 0.2439i 0.1895 - 0.2426i 0.1842 - 0.2412i 0.1788 - 0.2397i 0.1736 - 0.2380i 0.1684 - 0.2363i 0.1633 - 0.2345i 0.1583 - 0.2326i 0.1533 - 0.2306i 0.1485 - 0.2285i 0.1437 - 0.2263i 0.1390 - 0.2240i 0.1345 - 0.2217i 0.1300 - 0.2193i 0.1256 - 0.2169i 0.1213 - 0.2143i 0.1171 - 0.2118i 0.1131 - 0.2092i 0.1091 - 0.2065i 0.1052 - 0.2038i 0.1014 - 0.2011i 0.0978 - 0.1983i 0.0942 - 0.1955i 0.0907 - 0.1927i 0.0874 - 0.1899i 0.0841 - 0.1870i 0.0809 - 0.1841i 0.0778 - 0.1813i 0.0749 - 0.1784i 0.0720 - 0.1755i 0.0692 - 0.1726i 0.0665 - 0.1698i 0.0639 - 0.1669i 0.0614 - 0.1640i 0.0589 - 0.1612i 0.0566 - 0.1584i 0.0543 - 0.1556i 0.0521 - 0.1528i 0.0500 - 0.1500i 0.0480 - 0.1472i 0.0460 - 0.1445i
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, 1) % 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 x1 -2e+05 B = u1 x1 256 C = x1 y1 390.6 D = u1 y1 0 K = y1 x1 0 Parameterization: FREE form (all coefficients in A, B, C free). Feedthrough: none Disturbance component: none Number of free coefficients: 3 Use "idssdata", "getpvec", "getcov" for parameters and their uncertainties. Status: Estimated using SSEST on frequency response data "sysfr". Fit to estimation data: 100% FPE: 1.13e-31, MSE: 1.119e-31
figure
compare(sysfr, sys_ss)
sys_tf = tfest(sysfr, 1,1) % Transfer Function Realisation
sys_tf = 1e05 -------- s + 2e05 Continuous-time identified transfer function. Parameterization: Number of poles: 1 Number of zeros: 1 Number of free coefficients: 3 Use "tfdata", "getpvec", "getcov" for parameters and their uncertainties. Status: Estimated using TFEST on frequency response data "sysfr". Fit to estimation data: 100% FPE: 1.58e-30, MSE: 1.557e-30
figure
compare(sysfr, sys_tf)
figure
pzmap(sys_ss)
grid on
format long E
Poles = pole(sys_ss)
Poles =
-2.000000000000001e+05
Zeros = zero(sys_ss)
Zeros = 0×1 empty double column vector
figure
subplot(2,1,1)
semilogx(T2.FreqHz, mag2db(abs(Rsp))) % Changed
title('Amplitude (dB)')
grid
subplot(2,1,2)
semilogx(T2.FreqHz, angle(Rsp)) % Changed
title('Phase (°)')
grid
xlabel('Frequency')
syms s
H = (s + 1e05 ) / (s + 200000)
H = 
G = H*100/(1 - H)
G = 
G = simplifyFraction(G)
G = 
GdB = vpa(20*log10(abs(G)))
GdB = 
figure
hfp = fplot(G, [0 1E+5]);
Xv = hfp.XData
Xv = 1×45
1.0e+00 * 0 2.085524378664551e+03 4.545454545454545e+03 6.924930780053428e+03 9.090909090909090e+03 1.132082961640785e+04 1.363636363636364e+04 1.614407746540481e+04 1.818181818181818e+04 2.071481972960689e+04 2.272727272727273e+04 2.491983619176126e+04 2.727272727272727e+04 2.945752839701479e+04 3.181818181818182e+04 3.435343593193532e+04 3.636363636363636e+04 3.880114675676139e+04 4.090909090909091e+04 4.330127399050895e+04 4.545454545454546e+04 4.770619139409764e+04 5.000000000000000e+04 5.227332318762936e+04 5.454545454545454e+04 5.690852009058446e+04 5.909090909090909e+04 6.154579464415575e+04 6.363636363636364e+04 6.587009057391543e+04
Yv = hfp.YData
Yv = 1×45
1.000000000000000e+02 1.020855243786645e+02 1.045454545454545e+02 1.069249307800534e+02 1.090909090909091e+02 1.113208296164078e+02 1.136363636363636e+02 1.161440774654048e+02 1.181818181818182e+02 1.207148197296069e+02 1.227272727272727e+02 1.249198361917613e+02 1.272727272727273e+02 1.294575283970148e+02 1.318181818181818e+02 1.343534359319353e+02 1.363636363636364e+02 1.388011467567614e+02 1.409090909090909e+02 1.433012739905089e+02 1.454545454545455e+02 1.477061913940976e+02 1.500000000000000e+02 1.522733231876294e+02 1.545454545454546e+02 1.569085200905845e+02 1.590909090909091e+02 1.615457946441558e+02 1.636363636363636e+02 1.658700905739154e+02
grid
xlabel('Frequency (Hz)')
ylabel('Absolute Amplitude')
figure
semilogx(Xv, Yv, 'LineWidth',2)
grid
xlabel('Frequency (Hz)')
ylabel('Amplitude (dB)')
.
  4 Comments
francesco baldi
francesco baldi on 26 Dec 2021
and once i calculate G in the Laplace domain, is there a way to put the sigma variable of Laplace equal to zero? because i need to separate G between real and imaginary part, and i don't know how to do it in the Laplace domain.
Star Strider
Star Strider on 26 Dec 2021
I sould do something like this —
s = tf('s');
G = s/1000 + 100;
[mag,phs,wout] = bode(G);
magv = squeeze(mag)
magv = 55×1
100.0050 100.0200 100.0273 100.0374 100.0511 100.0699 100.0956 100.1307 100.1787 100.2443
phsv = squeeze(phs)
phsv = 55×1
0.5729 1.1458 1.3398 1.5666 1.8318 2.1418 2.5042 2.9277 3.4225 4.0005
Gv = magv .* exp(1j*phsv);
Gre = real(Gv)
Gre = 55×1
84.0354 41.2434 22.9033 0.4184 -25.8194 -54.0882 -80.4422 -97.8490 -96.2513 -65.4845
Gim = imag(Gv)
Gim = 55×1
54.2131 91.1207 97.3700 100.0365 96.6622 84.1930 59.5667 21.2539 -27.7752 -75.8992
figure
bode(G)
grid
Experiment!
.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!