⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smith.m

📁 smith圆图的matlab实现,快速解决一些电阻电容方面的计算
💻 M
📖 第 1 页 / 共 3 页
字号:
            ht2 = findobj('color',[0 1 0]);
            ht3 = findobj('color',[0 0 1]);
            ht4 = findobj('color',[0 0 0]);
            delete(ht1);
            delete(ht2);
            delete(ht3);
            delete(ht4);
            
            text(X+0.02,Y+0.02,'Zin','HitTest','off','color',[0 0 0]);
            plot(X,Y,'+','color',[0 0 0],'HitTest','off');
            Draw_Circle(0,0,abs(T),[1 0 0]);
            Draw_Circle((Z_Re/(Z_Re+1)),0,(1/(1+Z_Re)),[0 1 0]);
            
            if Z_Im == 0
                plot([-1 1],[0 0],'color',[0 0 1]);
            else
                Draw_Arch(1,1/Z_Im,[0 0 1]);
            end            
        end
        set(h1,'string',Z_Re);
        set(h2,'string',Z_Im);
        set(h3,'string',absolute);
        set(h4,'string',phase);
        set(h5,'string',K);
        set(h6,'string',p);  
        set(h7,'string',Zc1_min);
        set(h8,'string',Zc1_max);
        set(h9,'string',min_length);
        set(h10,'string',max_length);
            
    end
end

function Mouse(hObject, eventdata, handles)
global h1 h2 h3 h4 h5 h6 h7 h8 h9 h10 I X Y pic1 pic2 distance Z0 hv hi hvi him;
subplot(pic1);
p=get(gca,'currentpoint');
X=p(1);
Y=p(3);
T=X+j*Y;
Z_Re=real((1+X+j*Y)/(1-X-j*Y));
Z_Im=imag((1+X+j*Y)/(1-X-j*Y));
Z=complex(Z_Re,Z_Im);
absolute=sqrt(((1-Z_Re)^2+Z_Im^2)/((1+Z_Re)^2+Z_Im^2));
phase=atan(Z_Im/(Z_Re-1))-atan(Z_Im/(Z_Re+1));
if (phase>=pi/2)
    phase=phase-pi;
elseif(phase <= -pi/2)
    phase=phase+pi;
end
p=(1+absolute)/(1-absolute);
K=1/p;
if(Z_Im==0)
    Zc1_min=sqrt(Z_Re);
    Zc1_max=0;
    min_length=0;
    max_length=0;
else
    Zc1_min=1/sqrt(p);
    Zc1_max=sqrt(p);
    max_length=phase/4/pi;
    if(max_length>0.25)
    min_length=max_length-0.25;
    else 
        min_length=max_length+0.25;
    end
end

if (I==1)
    ht5=findobj('color','y');
    ht4=findobj('color','m');
    ht6=findobj('color','c');
    delete(ht5);
    delete(ht4);
    delete(ht6);
    if(X^2+Y^2<=1)
        if(isempty(findobj('color',[1 0 0])))
            text(X+0.02,Y+0.02,'Zin','HitTest','off','color',[0 0 0]);
            plot(X,Y,'+','color',[0 0 0],'HitTest','off');
            Draw_Circle(0,0,abs(T),[1 0 0]);
            Draw_Circle((Z_Re/(Z_Re+1)),0,(1/(1+Z_Re)),[0 1 0]);
            if Z_Im == 0
                plot([-1 1],[0 0],'color',[0 0 1]);
            else
                Draw_Arch(1,1/Z_Im,[0 0 1]);
            end
        else
            ht1 = findobj('color',[1 0 0]);
            ht2 = findobj('color',[0 1 0]);
            ht3 = findobj('color',[0 0 1]);
            ht4 = findobj('color',[0 0 0]);
            delete(ht1);
            delete(ht2);
            delete(ht3);
            delete(ht4);
            
            text(X+0.02,Y+0.02,'Zin','HitTest','off','color',[0 0 0]);
            plot(X,Y,'+','color',[0 0 0],'HitTest','off');
            Draw_Circle(0,0,abs(T),[1 0 0]);
            Draw_Circle((Z_Re/(Z_Re+1)),0,(1/(1+Z_Re)),[0 1 0]);

            if Z_Im == 0
                plot([-1 1],[0 0],'color',[0 0 1]);
            else
                Draw_Arch(1,1/Z_Im,[0 0 1]);
            end            
        end
        set(h1,'string',Z_Re);
        set(h2,'string',Z_Im);
        set(h3,'string',absolute);
        set(h4,'string',phase);
        set(h5,'string',K);
        set(h6,'string',p); 
        set(h7,'string',Zc1_min);
        set(h8,'string',Zc1_max);
        set(h9,'string',min_length);
        set(h10,'string',max_length);

        
%       connect to the wave figure
        Z0=((Z-j*tan(2*pi*distance))/(1-j*Z*tan(2*pi*distance)));
        [Mg, Ph] = ConvertZ2Mg_Ph(real(Z0), imag(Z0));
        if (get(hv,'Value') == get(hv,'Max'))
            index=1;
        else
            if (get(hi,'Value') == get(hi,'Max'))
            index=2;
            else if (get(hvi,'Value') == get(hvi,'Max'))
                 index=3;
                else
                    index=4;
                end
            end
        end

        subplot(pic2);
        cla;
        %popup_sel_index = get(handles.uipanel4, 'Value');
        switch index
            case 1
                Draw_Voltage_Amplitude(Mg, Ph);
            case 2
                Draw_Current_Amplitude(Mg, Ph);
            case 3
                Draw_Voltage_And_Current_Amplitude(Mg, Ph);
            case 4
                Draw_Impedance(real(Z0),imag(Z0));
        end       
    end
end
        

% --- Executes on button press in grid_on.
function grid_on_Callback(hObject, eventdata, handles)
global pic2;
axes(pic2);
grid on;
% hObject    handle to grid_on (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of grid_on


% --- Executes on button press in grid_off.
function grid_off_Callback(hObject, eventdata, handles)
global pic2;
axes(pic2);
grid off;
% hObject    handle to grid_off (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of grid_off

% --- Executes on slider movement.
function Dis_slider_Callback(hObject, eventdata, handles)
% hObject    handle to Dis_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global h1 h2 h3 h4 h5 h6 h7 h8 h9 h10 distance Z0 pic1 pic2;
distance=2-get(hObject,'Value');
set(handles.distance,'string',distance);
Z=((Z0+j*tan(2*pi*distance))/(1+j*Z0*tan(2*pi*distance)));
absolute=sqrt(((1-real(Z))^2+imag(Z)^2)/((1+real(Z))^2+imag(Z)^2));
phase=atan(imag(Z)/(real(Z)-1))-atan(imag(Z)/(real(Z)+1));
if (phase>=pi/2)
    phase=phase-pi;
elseif(phase <= -pi/2)
    phase=phase+pi;
end
p=(1+absolute)/(1-absolute);
K=1/p;
if(imag(Z)==0)
    Zc1_min=sqrt(Z_Re);
    Zc1_max=0;
    min_length=0;
    max_length=0;
else
    Zc1_min=1/sqrt(p);
    Zc1_max=sqrt(p);
    max_length=phase/4/pi;
    if(max_length>0.25)
    min_length=max_length-0.25;
    else 
        min_length=max_length+0.25;
    end
end
set(h1,'string',real(Z));
set(h2,'string',imag(Z));
set(h3,'string',absolute);
set(h4,'string',phase);
set(h5,'string',K);
set(h6,'string',p); 
set(h7,'string',Zc1_min);
set(h8,'string',Zc1_max);
set(h9,'string',min_length);
set(h10,'string',max_length);

Z_Real=real(Z);
Z_Image=imag(Z);
subplot(pic1);
cla;
Draw_Background;
T=(Z-1)/(Z+1);
x=real(T);
y=imag(T);
text(x+0.02,y+0.02,'Zin','HitTest','off','color',[0 0 0]);
plot(x,y,'+','color',[0 0 0]);
Draw_Circle(0,0,abs(T),[1 0 0]);
Draw_Circle((Z_Real/(Z_Real+1)),0,(1/(1+Z_Real)),[0 1 0]);
if Z_Image == 0
    plot([-1 1],[0 0],'color',[0 0 1]);
else
    Draw_Arch(1,1/Z_Image,[0 0 1]);
end

subplot(pic2);
if(isempty(findobj('color','k')))
    plot([distance distance],[-5 5],'k');
else
    ht = findobj('color','k');
    delete(ht);
    plot([distance distance],[-5 5],'k');
end


% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


% --- Executes during object creation, after setting all properties.
function Dis_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Dis_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in position.
function position_Callback(hObject, eventdata, handles)
% hObject    handle to position (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns position contents as cell array
%        contents{get(hObject,'Value')} returns selected item from position


% --- Executes during object creation, after setting all properties.
function position_CreateFcn(hObject, eventdata, handles)
% hObject    handle to position (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

% --- Executes on button press in distance_set.
function distance_set_Callback(hObject, eventdata, handles)
% hObject    handle to distance_set (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global distance Z0 h1 h2 h3 h4 h5 h6 pic1 pic2;

pop_index=get(handles.position,'Value');
switch pop_index
    case 1
        distance = 0;
    case 2
        distance = 1/8;
    case 3
        distance = 1/4;
    case 4
        distance = 3/8;
    case 5
        distance = 1/2;
end
subplot(pic2);
if(isempty(findobj('color','k')))
    plot([distance distance],[-5 5],'k');
else
    ht = findobj('color','k');
    delete(ht);
    plot([distance distance],[-5 5],'k');
end

set(handles.distance,'string',distance);
set(handles.Dis_slider,'Value',2-distance);
Z=((Z0+j*tan(2*pi*distance))/(1+j*Z0*tan(2*pi*distance)));
absolute=sqrt(((1-real(Z))^2+imag(Z)^2)/((1+real(Z))^2+imag(Z)^2));
phase=atan(imag(Z)/(real(Z)-1))-atan(imag(Z)/(real(Z)+1));
if (phase>=pi/2)
    phase=phase-pi;
elseif(phase <= -pi/2)
    phase=phase+pi;
end
p=(1+absolute)/(1-absolute);
K=1/p;
set(h1,'string',real(Z));
set(h2,'string',imag(Z));
set(h3,'string',absolute);
set(h4,'string',phase);
set(h5,'string',K);
set(h6,'string',p);   
Z_Real=real(Z);
Z_Image=imag(Z);
subplot(pic1);
cla;
Draw_Background;
T=(Z-1)/(Z+1);
x=real(T);
y=imag(T);
text(x+0.02,y+0.02,'Zin','HitTest','off','color',[0 0 0]);
plot(x,y,'+','color',[0 0 0]);
Draw_Circle(0,0,abs(T),[1 0 0]);
Draw_Circle((Z_Real/(Z_Real+1)),0,(1/(1+Z_Real)),[0 1 0]);
if Z_Image == 0
    plot([-1 1],[0 0],'color',[0 0 1]);
else
    Draw_Arch(1,1/Z_Image,[0 0 1]);
end

function Tl = ConvertMg_Ph2Tl(Mg, Ph)
Tl = complex(Mg*cos(Ph), Mg*sin(Ph));


function [Mg, Ph] =  ConvertZ2Mg_Ph(Z_Re, Z_Im)
Z = complex(Z_Re, Z_Im);
if(Z == -1)
    errordlg('Divided by zero during calculating of reflection coeficient')';
    %Usually it's impossible for the real part to be negative
end
if(Z == 1)
    Ph = 0;
    Mg = 0;%For zero relection coeficient, set the phase to zero.
else
    Refl_Coef = (Z - 1) / (Z + 1);
    Refl_Coef_Re = real(Refl_Coef);
    Refl_Coef_Im = imag(Refl_Coef);
    Mg = sqrt(Refl_Coef_Re^2 + Refl_Coef_Im^2);
    Ph_temp = atan(Refl_Coef_Im/Refl_Coef_Re);
%Phase value between -0.5pi to 1.5pi
    if (Refl_Coef_Re < 0)
        Ph = Ph_temp + pi;
    else
        Ph = Ph_temp;
    end
end

% --- Executes on button press in mode_set.
function mode_set_Callback(hObject, eventdata, handles)
% hObject    handle to mode_set (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global pic2 Z0 distance;
[Mg, Ph] = ConvertZ2Mg_Ph(real(Z0), imag(Z0));
if (get(handles.V_amp,'Value') == get(handles.V_amp,'Max'))
    index=1;
else
    if (get(handles.I_amp,'Value') == get(handles.I_amp,'Max'))
    index=2;
    else if (get(handles.VI_amp,'Value') == get(handles.VI_amp,'Max'))
         index=3;
        else
            index=4;
        end
    end
end

subplot(pic2);
cla;
plot([distance distance],[-5 5],'k');
%popup_sel_index = get(handles.uipanel4, 'Value');
switch index
    case 1
        Draw_Voltage_Amplitude(Mg, Ph);
    case 2
        Draw_Current_Amplitude(Mg, Ph);
    case 3
        Draw_Voltage_And_Current_Amplitude(Mg, Ph);
    case 4
        Draw_Impedance(real(Z0),imag(Z0));   
end

function Draw_Voltage_Amplitude(Mg, Ph)
N_lamda = 2%The length of transmission line(No. of lamda) to be displayed
z = 0:0.01:N_lamda;
Uz = fliplr(sqrt(1 + Mg^2 + 2 * Mg * cos(2 * 2 * pi * z - Ph)));
z_flip = fliplr(z);
plot(z_flip, Uz);
axis([0, N_lamda, 0 , 2]);

function Draw_Current_Amplitude(Mg,Ph)
N_lamda = 2%The length of transmission line(No. of lamda) to be displayed
z = 0:0.01:N_lamda;
Uz =  fliplr(sqrt(1 + Mg^2 - 2 * Mg * cos(2 * 2 * pi * z - Ph))); %suppose current amplitude is smaller than unit
z_flip = fliplr(z);
z_flip = z_flip;
plot(z_flip, Uz, '--r', 'LineWidth', 2);
axis([0, N_lamda, 0 , 2]);

function Draw_Voltage_And_Current_Amplitude(Mg, Ph)
Draw_Voltage_Amplitude(Mg, Ph);
Draw_Current_Amplitude(Mg,Ph);

function Draw_Impedance(Z_Re, Z_Im)
N_lamda = 2%The length of transmission line(No. of lamda) to be displayed
z = 0:0.01:N_lamda;
Rz = fliplr((Z_Re * (sec(2 * pi * z)).^2)./((1 - Z_Im * tan(2 * pi * z)).^2 + (Z_Re * tan(2 * pi * z)).^2));
z_flip = fliplr(z);
plot(2-z_flip,Rz);
axis([0, 2, -3, 3]);
text(0.5,2.5,'R value','color','b');
hold on;
Xz=fliplr(((1 - Z_Im * tan(2 * pi * z)) .* (Z_Im + tan(2 * pi * z)) - Z_Re^2 * tan(2 * pi * z))./((1 - Z_Im * tan(2 * pi * z)).^2 + (Z_Re * tan(2 * pi * z)).^2));
plot(2-z_flip, -Xz, 'r');
axis([0, 2, -3, 3]);
text(0.5,1.5,'X value','color','r');

% --- Executes on button press in V_amp.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -