Code covered by the BSD License  

Highlights from
Function generator with CRO & Spectrum analyzer using GUI

Function generator with CRO & Spectrum analyzer using GUI

by

 

Function generator with CRO & Spectrum analyzer using GUI

CROSPECTRUM2(action)
function CROSPECTRUM2(action)
% This is a demo of a function generator along with CRO & SPECTRUM ANALYZER. 
% Here five types of functions can be generated.
% Using the popup button any function can be selected.
% There are three 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. With the help of third slider buttons the time period can be set.
% 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: 01.05.2013 Time of Complete: 1.02pm  
% Total duration for Pragraming: 2.5 hours.
% See Also: ANALOGCOMM5.m
if nargin<1,
    action='initialize';
end;
if strcmp(action,'initialize')
    close
%============================================
xpos=5.7;ypos=0.2;xw=1.5;yw=0.2;
% TOTAL FIGURE
figNumber=figure('Name','CRO','NumberTitle','off','Units','inches','Position',[2 1.5 7.3 5.5],'Resize','off','Toolbar','figure');
colordef(figNumber,'black')
axes('Units','inches','Position',[0.5 0.4 4.6 4.4],'Visible','off');
%============================================
%THE CONSOLE FRAME
h1=uicontrol('Style','frame','Units','inches','Position',[xpos-0.07 0.02 1.75 5.5], ...
        'BackgroundColor',0.8*[0.5 0.8 0.04]);
h=uicontrol('Style','frame','Units','inches','Position',[xpos-0.04 0.05 1.6 5.4], ...
        'BackgroundColor',[0.50 0.750 0.050]);
%============================================
% FIRST SLIDER  
sld1Hndl=uicontrol('Style','slider','Tag','Amplitude','Units','inches', ...
        'Position',[xpos ypos+4.5 xw yw],'BackgroundColor',0.6*[0 1 1],'Callback','CROSPECTRUM2(''build'')');
uicontrol('Style','text','String','Amplitude','Units','inches','FontName','monotype corsiva', ...
        'Position',[xpos ypos+4.9 xw yw+0.1],'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',[xpos ypos+4.7 xw yw]);
%============================================
% Text for Inputing Frequency range
uicontrol('Style','text','FontName','Times new roman','FontWeight','bold',...
      'FontSize',10,'HorizontalAlignment','center','Units','inches', ...
      'String','Input Frequency Range','BackgroundColor','yellow', ...
      'Position',[xpos ypos+3.8 xw yw+0.25]);
FF=uicontrol('Style','edit','FontName','monotype corsiva', ...
    'FontSize',14,'HorizontalAlignment','left', ...
    'Units','inches','Max',1,'String','50','BackgroundColor',[0.7 0.7 0.5], ...
    'Callback','CROSPECTRUM2(''build'')','Position',[xpos+0.1 ypos+3.84 xw-0.2 yw]);
%============================================
% SECOND SLIDER
sld2Hndl=uicontrol('Style','slider','Tag','Frequency','Units','inches', ...
        'Position',[xpos ypos+3.05 xw yw],'BackgroundColor',0.6*[0 1 1],'Callback','CROSPECTRUM2(''build'')');
uicontrol('Style','text','String','Frequency','Units','inches','FontSize',15,'FontName','monotype corsiva', ...
        'Position',[xpos ypos+3.45 xw yw+0.1],'BackgroundColor',0.3*[0 0 1],'ForegroundColor',0.9*[1 0 1]);
uicontrol('Style','text','Tag','FRange','Units','inches','ForegroundColor',0.9*[0 1 1],'FontSize',10,...
         'Position',[xpos ypos+3.25 xw yw],'BackgroundColor',0.3*[0 0 1]);
%========================================
%CHOOSE DOMAIN TOGGLE BUTTON
 sld3Hndl=uicontrol('Style','toggle','Tag','TFreq','Units','inches','FontSize',10,'FontWeight','bold',...
        'Position',[xpos ypos+1.95 xw yw],'BackgroundColor',0.6*[0 1 1],'Callback','CROSPECTRUM2(''build'')');
uicontrol('Style','text','String','Choose Domain','Units','inches','ForegroundColor',[0 0 0],'FontSize',12,...
         'Position',[xpos ypos+2.15 xw yw],'BackgroundColor',[0.50 0.750 0.050],'FontName','times new roman','FontWeight','bold');
%============================================
% 3rd slider for Time period
sld4Hndl=uicontrol('Style','slider','Tag','Timeperiod','Units','inches', ...
        'Position',[xpos ypos+1.2 xw yw],'BackgroundColor',0.6*[0 1 1],'Callback','CROSPECTRUM2(''build'')');
uicontrol('Style','text','String','Select Time Period','Units','inches','ForegroundColor',0.9*[1 0 1],'FontSize',11,...
         'Position',[xpos ypos+1.6 xw yw],'BackgroundColor',0.3*[0 0 1]);
uicontrol('Style','text','String','0  <---[+0.1sec]--->  10','Units','inches','ForegroundColor',0.9*[0 1 1],'FontSize',10,...
         'Position',[xpos ypos+1.4 xw yw],'BackgroundColor',0.3*[0 0 1]);
%=====================================================
%----Initialize button------%
  uicontrol( ...
      'Style','push', ...
      'FontName','Times new roman','FontWeight','bold',...
      'FontSize',10, ...
      'HorizontalAlignment','center', ...
      'Units','inches', ...
      'String','Initialize', ...
      'BackgroundColor','yellow', ...
      'Callback','CROSPECTRUM2(''initialize'')', ...
      'Position',[xpos ypos+0.65 xw yw]);
%=====================================================
% SPECIFICATION TEXT
specstr=sprintf('Maximum Amplitude 50 Volt\nNo limit for maximum freq.\nMade by Suman Saha');
uicontrol('Style','text','Units','inches','Position',[xpos ypos+0.05 xw yw+0.4],'String',specstr, ...
       'ForegroundColor',[0 0 0],'BackgroundColor',[0.50 0.750 0.050],'FontSize',8);
%============================================
%THE CLOSE BUTTON
uicontrol('Style','push','Units','inches','Position',[xpos 0.1 xw yw+0.1],'String','CLOSE','FontName','monotype corsiva', ...
       'ForegroundColor','red','FontWeight','bold','FontSize',15,'Callback','close(gcf)');
%============================================
% Text for Chose function
uicontrol('Style','text','Units','inches','Position',[xpos ypos+2.5 xw yw+0.1],'String','Choose Function','FontName','times new roman', ...
       'ForegroundColor','black','FontWeight','bold','FontSize',11,'BackgroundColor',[0.50 0.750 0.050]);
% POPUP BUTTON to Select Function Type
cmdList=strvcat('Sine','Cosine','Square','Triangular','Sawtooth');
hndl1=uicontrol('Style','popup','Units','inches','Position',[xpos ypos+2.5 xw+0.03 yw-0.1],...
       'ForegroundColor',0.9*[1 1 0],'BackgroundColor',0.3*[0 0 1],'FontWeight','bold',...
       'String','SINE|COSINE|SQUARE|TRIANGULAR|SAWTOOTH','FontSize',9,...
       'Callback','CROSPECTRUM2 build','UserData',cmdList);
%=============================================
% Typing Different parameter values
uicontrol('Style','text','Tag','AmplDis','Units','inches','FontWeight','bold','FontSize',11,...
        'Position',[xpos ypos+4.3 xw yw],'BackgroundColor',0.3*[0 0 1],'ForegroundColor',0.9*[0 1 1]);
uicontrol('Style','text','Tag','FreqDis','Units','inches','FontWeight','bold','FontSize',11,...
        'Position',[xpos ypos+2.85 xw yw],'BackgroundColor',0.3*[0 0 1],'ForegroundColor',0.9*[0 1 1]);
uicontrol('Style','text','Tag','TimeP','Units','inches','FontWeight','bold','FontSize',11,...
        'Position',[xpos ypos+1 xw yw],'BackgroundColor',0.3*[0 0 1],'ForegroundColor',0.9*[0 1 1]);
%==========================================================================
hndlList=[hndl1,FF];
hndlListFF=[FF];
set(figNumber,'Visible','on','UserData',hndlList);
CROSPECTRUM2('build');        % call itself to build the defferent function
%==========================================================================
%%---Take Slider Input as well as Popup button input & Redraw---%%
elseif strcmp(action,'build')
     hndlList=get(gcf,'UserData');
     FF=hndlList(2);
     fr=str2num(get(FF(1),'String'));
     TF=get(findobj(gcf,'Tag','TFreq'),'Value');
     A=50*get(findobj(gcf,'Tag','Amplitude'),'Value');
     F=fr*get(findobj(gcf,'Tag','Frequency'),'Value');
     TP=10*get(findobj(gcf,'Tag','Timeperiod'),'Value');
     [rn,cn]=size(hndlList(1,:));
     for count=1:rn
        newStrList=get(hndlList(count),'UserData');
        newStrVal=get(hndlList(count),'Value');
        newStr=deblank(newStrList(newStrVal,:));           % Optional
     end
     %-------- for time domain
%------for each popup button's value run respective function type-----%
         if newStrVal==1
                 [ts,x,y]=sincurve(A,F,TP);
         elseif newStrVal==2
                 [ts,x,y]=coscurve(A,F,TP);
         elseif newStrVal==3
                 [ts,x,y]=squarew(A,F,TP);
         elseif newStrVal==4
                 [ts,x,y]=track1(A,F,TP);
         elseif newStrVal==5
                 [ts,x,y]=sawto(A,F,TP);
         end
xlabel 'Time (sec) ------>';ylabel 'Amplitude (Volt) ------>'; title 'TIME DOMAIN SIGNAL';
        set(findobj(gcf,'Tag','TFreq'),'String','TIME');
    if TF==1                % for frequency domain
        set(findobj(gcf,'Tag','TFreq'),'String','FREQUENCY');
%------for each popup button's value run respective function type-----%
         if newStrVal==1
                 [ts,x,y]=sincurve(A,F,TP);
                 spectrum_analyzer(y,x,ts);
         elseif newStrVal==2
                 [ts,x,y]=coscurve(A,F,TP);
                 spectrum_analyzer(y,x,ts);
         elseif newStrVal==3
                 [ts,x,y]=squarew(A,F,TP);
                 spectrum_analyzer(y,x,ts);
         elseif newStrVal==4
                 [ts,x,y]=track1(A,F,TP);
                 spectrum_analyzer(y,x,ts);
         elseif newStrVal==5
                 [ts,x,y]=sawto(A,F,TP);
                 spectrum_analyzer(y,x,ts);
         end
    end
    grid on;
    set(findobj(gcf,'Tag','AmplDis'),'String',sprintf('%f  Volt',A ));
    set(findobj(gcf,'Tag','FreqDis'),'String',sprintf('%f  Hz',F ));
    set(findobj(gcf,'Tag','TimeP'),'String',sprintf('%f  sec',TP));
    set(findobj(gcf,'Tag','FRange'),'String',sprintf('  0   <--- [Hz] --->  %d',fr));
  drawnow      % for redraw purpose
end
%================Main Program Ends===============%
%---------------Different functions declaration----------%
%------ Sine Wave Function--------------%
function [ts,t,outcurv]=sincurve(A,F,TP)
    NN=200;
    NN(F>10)=F*20;
    ts=1/NN;
    t=linspace(0,TP,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,TP) 
    NN=200;
    NN(F>10)=F*20;
    ts=1/NN;
    t=linspace(0,TP,NN); 
    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,TP)
    t=linspace(0,TP,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,TP)
 t=linspace(0,TP,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,TP)
    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,TP,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================================%%%%

Contact us