function varargout = tls(varargin) % TLS -- Transmission Line Simulator % Simulates a lossless transmission line. Given appropriate input values, % the TLS GUI will calculate the associated output values. % Various plots, including an animated plot, can be selected by the user. % % Copyright (c) 2005, C. H. G. Wright and R. G. Jacquot % Version 1.13 Last modified on 20 May 2005 % M-file for tls.fig % TLS, by itself, creates a new TLS or raises the existing % singleton*. % % H = TLS returns the handle to a new TLS or the handle to % the existing singleton*. % % TLS('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in TLS.M with the given input arguments. % % TLS('Property','Value',...) creates a new TLS or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before tls_OpeningFunction gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to tls_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES % Copyright 2002-2003 The MathWorks, Inc. % Edit the above text to modify the response to help tls % Last Modified by GUIDE v2.5 13-May-2005 17:20:09 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @tls_OpeningFcn, ... 'gui_OutputFcn', @tls_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % --- Executes just before tls is made visible. function tls_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to tls (see VARARGIN) % Choose default command line output for tls handles.output = hObject; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Initialize various structure fields to be used later % Ensure this matches reset values and startup strings % Uses startup strings defined as part of FIG file % Done this way to force string formatting to a certain style % Otherwise could just call pushbutton3 callback handles.L = 5500; % line length handles.up = 3e8; % propagation velocity handles.Z0 = 300; % characteristic impedance--real value for lossless line handles.Zg = 500 + 0j; % source impedance handles.ZL = 500 + 0j; % load impedance handles.f = 1e5; % frequency handles.Vg = 1; % input voltage (not user selectable) % Set plotting flag to false handles.ReadyToPlot = 0; % Another way to initialize data, but string formatting will be different %pushbutton3_Callback(hObject, eventdata, handles); % read and display the diagram of the transmission line setup handles.diagram = imread('tls.png','PNG'); % Read the image file axes(handles.axes1); image(handles.diagram) % display the image file axis square; % prevent aspect ratio distortion set(handles.axes1, 'Visible', 'off'); % turn off the axis labels % Update handles structure guidata(hObject, handles); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % --- Outputs from this function are returned to the command line. function varargout = tls_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output; % Propagation velocity function edit3_Callback(hObject, eventdata, handles) % hObject handle to edit3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of edit3 as text % str2double(get(hObject,'String')) returns contents of edit3 as a double up = str2double(get(hObject, 'String')); if (isnan(up) || ~isreal(up) || up<=0) % uses short-circuit logical OR set(hObject, 'String', num2str(handles.up)); errordlg('Propagation velocity must be a real number > 0','Error'); else % Save the new value in structure handles.up = up; handles.ReadyToPlot = 0; guidata(hObject,handles) end % Propagation velocity % --- Executes during object creation, after setting all properties. function edit3_CreateFcn(hObject, eventdata, handles) % hObject handle to edit3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % Line length function edit4_Callback(hObject, eventdata, handles) % hObject handle to edit4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of edit4 as text % str2double(get(hObject,'String')) returns contents of edit4 as a double L = str2double(get(hObject, 'String')); if (isnan(L) || ~isreal(L) || L<=0) % uses short-circuit logical OR set(hObject, 'String', num2str(handles.L)); errordlg('Line length must be a real number > 0','Error'); else % Save the new value in structure handles.L = L; handles.ReadyToPlot = 0; guidata(hObject,handles) end % Line length % --- Executes during object creation, after setting all properties. function edit4_CreateFcn(hObject, eventdata, handles) % hObject handle to edit4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % Characteristic impedance function edit5_Callback(hObject, eventdata, handles) % hObject handle to edit5 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of edit5 as text % str2double(get(hObject,'String')) returns contents of edit5 as a double Z0 = str2double(get(hObject, 'String')); if (isnan(Z0) || ~isreal(Z0) || Z0<=0) % uses short-circuit logical OR set(hObject, 'String', num2str(handles.Z0)); errordlg('Characteristic impedance must be a real number > 0.','Error'); else % Save the new value in structure handles.Z0 = Z0; handles.ReadyToPlot = 0; guidata(hObject,handles) end % Characteristic impedance % --- Executes during object creation, after setting all properties. function edit5_CreateFcn(hObject, eventdata, handles) % hObject handle to edit5 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % Source impedance function edit6_Callback(hObject, eventdata, handles) % hObject handle to edit6 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of edit6 as text % str2double(get(hObject,'String')) returns contents of edit6 as a double Zg = str2double(get(hObject, 'String')); if (isnan(Zg) || abs(angle(Zg)) > pi/2) % uses short-circuit logical OR set(hObject, 'String', num2str(handles.Zg)); errordlg('Source impedance must be a real or complex number (with a positive real component).','Error'); else % Save the new value in structure handles.Zg = Zg; handles.ReadyToPlot = 0; guidata(hObject,handles) end % Source impedance % --- Executes during object creation, after setting all properties. function edit6_CreateFcn(hObject, eventdata, handles) % hObject handle to edit6 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % Load impedance function edit7_Callback(hObject, eventdata, handles) % hObject handle to edit7 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of edit7 as text % str2double(get(hObject,'String')) returns contents of edit7 as a double ZL = str2double(get(hObject, 'String')); if (isnan(ZL) || abs(angle(ZL)) > pi/2) % uses short-circuit logical OR set(hObject, 'String', num2str(handles.ZL)); errordlg('Load impedance must be a real or complex number.','Error'); else % Save the new value in structure handles.ZL = ZL; handles.ReadyToPlot = 0; guidata(hObject,handles) end % Load impedance % --- Executes during object creation, after setting all properties. function edit7_CreateFcn(hObject, eventdata, handles) % hObject handle to edit7 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % Frequency function edit8_Callback(hObject, eventdata, handles) % hObject handle to edit8 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of edit8 as text % str2double(get(hObject,'String')) returns contents of edit8 as a double f = str2double(get(hObject, 'String')); if (isnan(f) || ~isreal(f) || f<=0) % uses short-circuit logical OR set(hObject, 'String', num2str(handles.f)); errordlg('Frequency must be a real number > 0','Error'); else % Save the new value in structure handles.f = f; handles.ReadyToPlot = 0; guidata(hObject,handles) end % Frequency % --- Executes during object creation, after setting all properties. function edit8_CreateFcn(hObject, eventdata, handles) % hObject handle to edit8 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % Calculate output values % --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Select current axes and clear the plot axes(handles.axes1); cla reset; % display the transmission line diagram %axes(handles.axes1); image(handles.diagram) % display the image file axis square; % prevent aspect ratio distortion set(handles.axes1, 'Visible', 'off'); % turn off the axis labels % Extract user data from structure L=handles.L; % line length up=handles.up; % propagation velocity Z0=handles.Z0; % characteristic impedance Zg=handles.Zg; % source impedance ZL=handles.ZL; % load impedance f=handles.f; % frequency Vg=handles.Vg; % input voltage (not user selectable) %This code calculates all necessary data for the output and plots betaL=2*pi*L*f/up; zoverL=[-1:.01:0]; Zin=Z0*((ZL+j*Z0*tan(betaL))/(Z0+j*ZL*tan(betaL))); gamma=(ZL-Z0)/(ZL+Z0); VSWR=(1+abs(gamma))/(1-abs(gamma)); Fact=(Vg*Zin/(Zg+Zin))/(exp(j*betaL)+gamma*exp(-j*betaL)); Vofz=Fact*(exp(-j*betaL*zoverL)+gamma*exp(j*betaL*zoverL)); lambda=up/f; lim=max(abs(Vofz)); mag=abs(Vofz); angl=angle(Vofz); % calculate the animation data omegat=linspace(0,8*pi,801); framedata=zeros(801,101); for i=1:801 framedata(i,:)=mag.*cos(omegat(1,i)*ones(1,101)+angl); end % Write output data to GUI set(handles.text31, 'String', num2str(gamma)); set(handles.text32, 'String', num2str(VSWR)); set(handles.text33, 'String', num2str(Zin)); set(handles.text34, 'String', num2str(lambda)); % Update handles structure guidata(handles.figure1, handles); % Store new calculated values in data structure for plots handles.betaL = betaL; handles.zoverL = zoverL; handles.Zin = Zin; handles.gamma = gamma; handles.VSWR = VSWR; handles.Vofz = Vofz; handles.lambda = lambda; handles.mag = mag; handles.angl = angl; handles.omegat = omegat; handles.framedata = framedata; handles.lim = lim; % Set plotting flag to true handles.ReadyToPlot = 1; % Update handles structure guidata(hObject, handles); % Reset input values % --- Executes on button press in pushbutton3. function pushbutton3_Callback(hObject, eventdata, handles) % hObject handle to pushbutton3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Initialize the structure fields handles.L = 5500; % line length handles.up = 3e8; % propagation velocity handles.Z0 = 300; % characteristic impedance handles.Zg = 500 + 0j; % source impedance handles.ZL = 500 + 0j; % load impedance handles.f = 1e5; % frequency handles.Vg = 1; % input voltage (not user selectable) % Set plotting flag to false handles.ReadyToPlot = 0; % Update handles structure guidata(hObject, handles); % write the default input data values to the GUI % note that source voltage Vg is always 1 % Below are two methods. Using Method 1 the formatting of the strings may not be % what you want. If you want to control the formatting of the strings % explictly, then use Method 2 that follows Method 1 % METHOD 1 % set(handles.edit4, 'String', num2str(handles.L)); %Line length in meters % set(handles.edit3, 'String', num2str(handles.up)); %Propagation velocity on the line % set(handles.edit5, 'String', num2str(handles.Z0)); %Line characteristic impedance % set(handles.edit6, 'String', num2str(handles.Zg)); %Source impedance % set(handles.edit7, 'String', num2str(handles.ZL)); %Load impedance % set(handles.edit8, 'String', num2str(handles.f)); %Frequency of driving source in Hz. % METHOD 2 set(handles.edit4, 'String', '5500'); %Line length in meters set(handles.edit3, 'String', '3e8'); %Propagation velocity on the line set(handles.edit5, 'String', '300'); %Line characteristic impedance set(handles.edit6, 'String', '500 + 0j'); %Source impedance set(handles.edit7, 'String', '500 + 0j'); %Load impedance set(handles.edit8, 'String', '1e5'); %Frequency of driving source in Hz. % clear the output values set(handles.text31, 'String', ''); set(handles.text32, 'String', ''); set(handles.text33, 'String', ''); set(handles.text34, 'String', ''); % clear the plot axes axes(handles.axes1); cla reset; % display the transmission line diagram %axes(handles.axes1); image(handles.diagram) % display the image file axis square; % prevent aspect ratio distortion set(handles.axes1, 'Visible', 'off'); % turn off the axis labels % Update handles structure guidata(handles.figure1, handles); % Output: Reflection Coefficient % --- Executes during object creation, after setting all properties. function text31_CreateFcn(hObject, eventdata, handles) % hObject handle to text31 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. % Output: VSWR % --- Executes during object creation, after setting all properties. function text32_CreateFcn(hObject, eventdata, handles) % hObject handle to text32 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. % Output: Input Impedance % --- Executes during object creation, after setting all properties. function text33_CreateFcn(hObject, eventdata, handles) % hObject handle to text33 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. % Output: Wavelength % --- Executes during object creation, after setting all properties. function text34_CreateFcn(hObject, eventdata, handles) % hObject handle to text34 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. % List of plots; this callback not currently used in this program % --- Executes on selection change in popupmenu1. function popupmenu1_Callback(hObject, eventdata, handles) % hObject handle to popupmenu1 (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 popupmenu1 contents as cell array % contents{get(hObject,'Value')} returns selected item from popupmenu1 % List of plots % --- Executes during object creation, after setting all properties. function popupmenu1_CreateFcn(hObject, eventdata, handles) % hObject handle to popupmenu1 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % Update the plot % --- Executes on button press in pushbutton4. function pushbutton4_Callback(hObject, eventdata, handles) % hObject handle to pushbutton4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % make sure Calculate button has been pressed at least once if (handles.ReadyToPlot == 0) myPlotErr=sprintf('Either a new input value was entered\n'); myPlotErr=sprintf([myPlotErr 'or no outputs have yet been calculated.\n']); myPlotErr=sprintf([myPlotErr 'Please press "Calculate Output Data" now.']); errordlg(myPlotErr,'New output values required!'); return; end % Select current axes and clear the plot axes(handles.axes1); cla reset; % Extract values from data structure for plots betaL=handles.betaL; zoverL=handles.zoverL; Zin=handles.Zin; gamma=handles.gamma; VSWR=handles.VSWR; Vofz=handles.Vofz; lambda=handles.lambda; mag=handles.mag; angl=handles.angl; omegat=handles.omegat; framedata=handles.framedata; lim=handles.lim; L=handles.L; % line length up=handles.up; % propagation velocity Z0=handles.Z0; % characteristic impedance Zg=handles.Zg; % source impedance ZL=handles.ZL; % load impedance f=handles.f; % frequency Vg=handles.Vg; % input voltage (not user selectable) % take action based upon which plot is selected popup_sel_index = get(handles.popupmenu1, 'Value'); switch popup_sel_index case 1 plot(zoverL,abs(Vofz)); axis([-1 0 0 1.2*lim]) xlabel('Dimensionless Distance, z/L') ylabel('Standing Wave Amplitude') case 2 omegat2=linspace(0,4*pi,101); Vin=Vg*Zin/(Zin+Zg); VL=Vin*((1+gamma)/(exp(j*betaL)+gamma*exp(-j*betaL))); Vint=abs(Vin)*cos(omegat2+angle(Vin)*ones(1,101)); VLt=abs(VL)*cos(omegat2+angle(VL)*ones(1,101)); Vgt=abs(Vg)*cos(omegat2+angle(Vg)*ones(1,101)); plot([0 omegat2(1,101)], [0 0],'k') % a black line at V=0 hold on %myPlotHandle=plot(omegat2,Vint,'--',omegat2,VLt,'-.',omegat2,Vgt,'.'); myPlotHandle=plot(omegat2,Vgt,omegat2,Vint,omegat2,VLt); hold off % in GUI we need explicit handle to axes for legend to work legend(myPlotHandle,'v_g','v_i_n','v_L'); lim2=max(abs([Vg VL Vin])); axis([0 4*pi -1.1*lim2 1.1*lim2]) xlabel('Dimensionless Time, \omega t') ylabel('v_g(t), v_i_n(t), v_L(t), volts') case 3 [X,Y]=meshgrid(omegat,zoverL); mesh(X,Y,framedata') view(45,30) text(30, -.6,-1.5*lim,'Distance, z/L','Rotation', 22,'FontSize',12) text(3,-1,-1.5*lim,'Dimensionless Time, \omegat','Rotation',-20,'FontSize',12) zlabel('Line Voltage, v(z,t)') case 4 set(handles.pushbutton4,'Enable','Off'); % Gray-out the button L=plot(zoverL,framedata(1,:),'k','EraseMode','xor'); hold on plot([-1 -1],[-1.2*lim 1.2*lim]) hold on plot([0 0],[-1.2*lim 1.2*lim]) hold on axis([-1.2 .2 -1.2*lim 1.2*lim]) xlabel('Dimensionless Distance on the Line, z/L') ylabel('Line Voltage, v(z,t)') myQuest=sprintf('Click YES to begin the animation.\n'); myQuest=sprintf([myQuest 'When you wish to stop the animation,\n']); myQuest=sprintf([myQuest 'click the grayed-out "Update Plot" button.']); ButtonName=questdlg(myQuest,'Animate Plot?','Yes','Cancel','Yes'); M=plot([-1 0],[framedata(1,1) framedata(1,101)],'o','EraseMode','xor'); switch ButtonName, case 'Cancel', set(handles.pushbutton4,'Enable','On'); return; case 'Yes', global Animate; Animate = 1; while (Animate) for i=1:201%801 set(L,'Ydata',framedata(i,:)) set(M,'Ydata',[framedata(i,1),framedata(i,101)]) pause(.01) if (Animate == 0) set(handles.pushbutton4,'Enable','On'); return end end end set(handles.pushbutton4,'Enable','On'); end % of switch end % of pushbutton4 Callback % Executes on mouse press over pushbutton4 when it is grayed-out. function pushbutton4_ButtonDownFcn(hObject, eventdata, handles) % hObject handle to pushbutton4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global Animate; Animate = 0; % for debugging only %disp('pushbutton4 was pressed!')