Contents
function varargout=g1(varargin)
% A simple FUNCTION to show regression of cyclical data, taken straight % from the very first example that we had you do % % How to run: % % OPTION 1: % g1
(no input arguments)
OPTION 2: g1([amplitudes],[periods],[phase angles]);
(all the same size, latter two in radians)
OPTION 3:
[t,y]=g1 [t,y]=g1([amplitudes],[periods],[phase angles]);
This gives output:
t is the time
y is the signal
% Set your netid lab name netid='mynetidl05'; if nargin==0 % Supply 'default' inputs and give them meaningful names A=[1 3]; P=[10 15]; % Remember how a 'sine' and a 'cosine' are pi/2 offset in phase ph=[0 pi/2]; else % Extract those same inputs from the command line call A=varargin{1}; P=varargin{2}; ph=varargin{3}; end % Define a set of times [s] t=linspace(0,100,200); % Make the signal, start from 'nothing' y=0; for index=1:length(A) % These are the individual pieces yp(index,:)=A(index)*cos(2*pi*t/P(index)+ph(index)); % And now we make the sum signal y=y+yp(index,:); end % Collect some colors to be used cols={'b','g','r','m','y'}; % Make the plot figure % Firs the pieces for index=1:length(A) plot(t,yp(index,:),cols{index}) hold on end % On top of that plot the summed signal hold on plot(t,y,'k','linew',2) hold off % Here is the "format string" fstr=repmat('%5.2f ',1,length(A)); % And here you encounter sprintf again title(sprintf(['%s - A ' fstr 'P ' fstr 'ph ' fstr],... netid,A,P,ph)) % Embellish xlabel('time [s]') ylabel('signals (individual pieces and their sum)') hold off % First clip to exactly what you have axis tight % Then make the yaxis range 10 percent bigger than max(abs()) ylim([-1 1]*max(abs(y))*1.1) grid on % Provide output if requested varns={t,y}; varargout=varns(1:nargout); % Print it out print('-dpdf',netid)
![](g1_01.png)