function CROSPECTRUM(action)
% This is a demo of a function generator & CRO & SPECTRUM ANALYZER. Here five types of functions
% can be generated. Using the popup button any function can be selected.
% There are two slider buttons. The amplitude of any function can be changed
% by sliding the first slider, named "Amplitude". And by sliding the
% seconed slider, named "Frequency", the frequency of any function can be
% changed. This particular demo gives the idea about the diffent types of
% function as Sinewave, CosineWave, SquareWave, TriangularWave,
% SwatoothWave. The value of amplitude and frequency can also be visibled.
% There is a "DOMAIN" button, by wchich anyone can sweep from time domain
% to frequency domain.
% Programmer : Suman Saha, Mtech, ECE, KGEC.
% Date: 25.04.2013 Time of Complete: 1.53pm
% Total duration for Pragraming: 1 hour.
% See Also: FunctionGenerator.m FunctionGenerator1.m FunctionGenerator3.m
if nargin<1,
action='initialize';
end;
if strcmp(action,'initialize')
%============================================
% TOTAL FIGURE
figNumber=figure('Name','CRO & SPECTRUM ANALYZER','NumberTitle','off','Units','inches','Position',[1.4 1.5 7 5],'Resize','off','ToolBar','figure');
colordef(figNumber,'black')
axes('Units','inches','Position',[0.5 0.4 4.6 4.3],'Visible','on');
%============================================
%THE CONSOLE FRAME
h1=uicontrol('Style','frame','Units','inches','Position',[5.3 0.02 1.75 5], ...
'BackgroundColor',0.8*[0.5 0.8 0.04]);
h=uicontrol('Style','frame','Units','inches','Position',[5.35 0.05 1.6 4.9], ...
'BackgroundColor',[0.50 0.750 0.050]);
%============================================
% FIRST SLIDER
sld1Hndl=uicontrol('Style','slider','Tag','Amplitude','Units','inches', ...
'Position',[5.41 4.2 1.5 0.2],'BackgroundColor',0.6*[0 1 1],'Callback','CROSPECTRUM(''build'')');
uicontrol('Style','text','String','Amplitude','Units','inches','FontName','monotype corsiva', ...
'Position',[5.41 4.6 1.5 0.3],'BackgroundColor',0.3*[0 0 1],'FontSize',15,...
'ForegroundColor',0.9*[1 0 1]);
uicontrol('Style','text','String',' 0 <--- [Volt] ---> 50','BackgroundColor',0.3*[0 0 1],'FontSize',10,...
'ForegroundColor',0.9*[0 1 1],'Units','inches','Position',[5.41 4.4 1.5 0.2]);
%============================================
% SECOND SLIDER
sld2Hndl=uicontrol('Style','slider','Tag','Frequency','Units','inches', ...
'Position',[5.41 3.0 1.5 0.2],'BackgroundColor',0.6*[0 1 1],'Callback','CROSPECTRUM(''build'')');
uicontrol('Style','text','String','Frequency','Units','inches','FontSize',15,'FontName','monotype corsiva', ...
'Position',[5.41 3.4 1.5 0.3],'BackgroundColor',0.3*[0 0 1],'ForegroundColor',0.9*[1 0 1]);
uicontrol('Style','text','String',' 0 <--- [Hz] ---> 50','Units','inches','ForegroundColor',0.9*[0 1 1],'FontSize',10,...
'Position',[5.41 3.2 1.5 0.2],'BackgroundColor',0.3*[0 0 1]);
%========================================
%TOGGLE BUTTON
sld3Hndl=uicontrol('Style','toggle','Tag','TFreq','Units','inches','String','Time/Freq',...
'Position',[5.41 1.0 1.5 0.2],'BackgroundColor',0.6*[0 1 1],'Callback','CROSPECTRUM(''build'')');
% uicontrol('Style','text','String','DOMAIN','Units','inches','FontSize',15,...
% 'Position',[5.41 1.4 1.5 0.3],'BackgroundColor',0.3*[0 0 1],'ForegroundColor',0.9*[1 0 1]);
uicontrol('Style','text','String','DOMAIN','Units','inches','ForegroundColor',0.9*[0 1 1],'FontSize',10,...
'Position',[5.41 1.2 1.5 0.2],'BackgroundColor',0.3*[0 0 1]);
%============================================
% SPECIFICATION TEXT
specstr=sprintf('Maximum Amplitude 50 Volt\nMaximum Frquency 50 Hz\nMade by Suman Saha');
uicontrol('Style','text','Units','inches','Position',[5.41 0.5 1.5 0.5],'String',specstr, ...
'ForegroundColor',[0 0 0],'BackgroundColor',[0.50 0.750 0.050],'FontSize',8);
%============================================
%THE CLOSE BUTTON
uicontrol('Style','push','Units','inches','Position',[5.41 0.2 1.5 0.3],'String','CLOSE','FontName','monotype corsiva', ...
'ForegroundColor','red','FontWeight','bold','FontSize',15,'Callback','close(gcf)');
%============================================
% POPUP BUTTON to Select Function Type
cmdList=strvcat('Sine','Cosine','Square','Triangular','Sawtooth');
hndl1=uicontrol('Style','popup','Units','inches','Position',[5.4 2.5 1.53 0.1],...
'ForegroundColor',0.9*[1 1 0],'BackgroundColor',0.3*[0 0 1],'FontWeight','bold',...
'String','SINE|COSINE|SQUARE|TRIANGULAR|SAWTOOTH','FontSize',11,...
'Callback','CROSPECTRUM build','UserData',cmdList);
hndlList=[hndl1];
set(figNumber,'Visible','on','UserData',hndlList);
CROSPECTRUM('build'); % call itself to build the defferent function
%============================================
%%-----Draw the initial sinewave-----%%
A=50*get(sld1Hndl,'Value');
F=50*get(sld2Hndl,'Value');
TF=get(sld3Hndl,'Value');
% [ts,x,y]=sincurve(A,F); % Optional
%============================================
%%---Take Slider Input as well as Popup button input & Redraw---%%
elseif strcmp(action,'build')
sld3Hndl=findobj(gcf,'Tag','TFreq');
TF=get(sld3Hndl,'Value');
sld2Hndl=findobj(gcf,'Tag','Frequency');
sld1Hndl=findobj(gcf,'Tag','Amplitude');
A=50*get(sld1Hndl,'Value');
F=50*get(sld2Hndl,'Value');
hndlList=get(gcf,'UserData');
n=length(hndlList);
for count=1:n,
newStrList=get(hndlList(count),'UserData');
newStrVal=get(hndlList(count),'Value');
newStr=deblank(newStrList(newStrVal,:)); % Optional
end
if TF==0 % for time domain
%------for each popup button's value run respective function type-----%
if newStrVal==1
[ts,x,y]=sincurve(A,F);
elseif newStrVal==2
[ts,x,y]=coscurve(A,F);
elseif newStrVal==3
[ts,x,y]=squarew(A,F);
elseif newStrVal==4
[ts,x,y]=track1(A,F);
elseif newStrVal==5
[ts,x,y]=sawto(A,F);
end
set(get(gca,'XLabel'),'String','Time (sec) ------>'); %Labeling the X-axis
set(get(gca,'YLabel'),'String','Amplitude (Volt) ------>'); %Labeling the Y-axis
set(get(gca,'Title'),'String','TIME DOMAIN SIGNAL');
else % for frequency domain
%------for each popup button's value run respective function type-----%
if newStrVal==1
[ts,x,y]=sincurve(A,F);
spectrum_analyzer(y,x,ts);
elseif newStrVal==2
[ts,x,y]=coscurve(A,F);
spectrum_analyzer(y,x,ts);
elseif newStrVal==3
[ts,x,y]=squarew(A,F);
spectrum_analyzer(y,x,ts);
elseif newStrVal==4
[ts,x,y]=track1(A,F);
spectrum_analyzer(y,x,ts);
elseif newStrVal==5
[ts,x,y]=sawto(A,F);
spectrum_analyzer(y,x,ts);
end
end
surfaceHndl=findobj(gcf,'Tag','surface1'); % optional
set(surfaceHndl,'XData',x,'YData',y); % Optional
set(gca,'XGrid','on','YGrid','on'); % Grid on
%------------Typing the value of Amplitud at the window--------%
uicontrol('Style','text','String',sprintf('%f Volt',A ),'Units','inches','FontWeight','bold','FontSize',11,...
'Position',[5.41 4 1.5 0.2],'BackgroundColor',0.3*[0 0 1],'ForegroundColor',0.9*[0 1 1]);
%------------Typing the value of Frequency at the window--------%
uicontrol('Style','text','String',sprintf('%f Hz',F ),'Units','inches','FontWeight','bold','FontSize',11,...
'Position',[5.41 2.8 1.5 0.2],'BackgroundColor',0.3*[0 0 1],'ForegroundColor',0.9*[0 1 1]);
drawnow % for redraw purpose
end
%================Main Program Ends===============%
%---------------Different functions declaration----------%
%------ Sine Wave Function--------------%
function [ts,t,outcurv]=sincurve(A,F)
NN=200;
NN(F>10)=F*20;
ts=1/NN;
t=linspace(0,1,NN);
outcurv=A*sin(2*pi*t*F);
h=plot(t,outcurv,'linew',1.5);
set( h,'tag','surface1'); % Optional
%------ Cosine Wave Function--------------%
function [ts,t,outcurv]=coscurve(A,F);
ts=1/200;
t=linspace(0,1,200);
outcurv=A*cos(2*pi*t*F);
h=plot(t,outcurv,'linew',1.5);
set( h,'tag','surface1'); % Optional
%------ Triangular Wave Function--------------%
function [ts,t,x]=track1(A,F)
t=linspace(0,1,F*90);
x=A*sawtooth(2*pi*F*t,0.5);
h=plot(t,x,'linew',2);
set( h,'tag','surface1'); % Optional
ts=1/length(x);
%------ Swatooth Wave Function--------------%
function [ts,t,x]=sawto(A,F)
t=linspace(0,1,F*90); x=A*sawtooth(2*pi*F*t,1);
h=plot(t,x,'linew',2);
set( h,'tag','surface1'); % Optional
ts=1/length(x);
%------ Square Wave Function--------------%
function [ts,t,b]=squarew(A,F)
F(F==1||F==0)=1;
F1(F<ceil(F))=ceil(F);
N=30;b=[];
a(1:2:2*F1)=0;
a(2:2:2*floor(F))=1;
for ii=1:length(a)
b(end+(1:N))=a(ii);
end
b=b*A;
t=linspace(0,1,length(b));
ts=1/length(b);
h=plot(t,b,'y','linew',2.5);
set( h,'tag','surface1'); % Optional
%-----------Spectrum Analyzer--------------%
function spectrum_analyzer(A,t,ts)
Lfft=length(t)/2;
Lfft=2^ceil(log2(Lfft));
B=fftshift(fft(A,Lfft))/Lfft;
w=(-Lfft/2:Lfft/2-1)/(Lfft*ts);
plot(w,abs(B),'linew',1.5);
xlabel('Frequency (Hz) ----> ');
ylabel('Magnitude ---->');
title('FREQUENCY DOMAIN MAGNITUDE SPECTRUM');
%%%%================================END================================%%%%