home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 3.ddi / DEMOS.DI$ / SIGDEMO2.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  6.9 KB  |  249 lines

  1. function sigdemo2(action,in1,in2);
  2. %SIGDEMO2 Interactive signal demo - 2 : continuous FT of a signal
  3. %    Demonstrates MATLAB's graphic user interface using Handle Graphics
  4. %       while illustrating basic Fourier transform (FT) properties,
  5. %    including modulation.
  6.  
  7. %    Author: T. Krauss
  8. %     11/3/92, updated 2/9/93
  9. %    (c) Copyright 1984-93, by The MathWorks, Inc.
  10.  
  11. %    possible actions:
  12. %      'start'
  13. %      'down'       - in1=1 ==> time_line, in1=2 ==> freq_line
  14. %      'move'
  15. %      'up'
  16. %      'redraw'     
  17. %      'setfreq'    - in1=1 ==> from slider, in1=2 ==> from edit text
  18. %      'done'
  19.  
  20. if nargin<1,
  21.     action='start';
  22. end;
  23.  
  24. global SIGDEMO2_DAT
  25.  
  26. if strcmp(action,'start'),
  27.     % graphics initialization
  28.     clf reset;
  29.     set(gcf,'Units','normalized','backingstore','off');
  30.  
  31.     % Create intial signal
  32.     min_freq = 1;
  33.     max_freq = 5;
  34.     min_amp = .1;
  35.     max_amp = 1;
  36.     amp=0.5;
  37.     freq=2.5;    % hertz
  38.     [t,f,w,F]=tffunc(amp,freq);
  39.  
  40.     freq_text=uicontrol('Style','text','Position',[.18 .02 .38 .07],...
  41.         'Units','normalized','BackgroundColor','black',...
  42.         'ForegroundColor','white','String','Modulation Frequency (Hertz):');
  43.  
  44.     uicontrol('style','text','pos',[.14 .07 .02 .05],...
  45.         'Units','normalized','BackgroundColor','black',...
  46.         'ForegroundColor','white','String',num2str(min_freq));
  47.  
  48.     uicontrol('style','text','pos',[.74 .07 .02 .05 ],...
  49.         'Units','normalized','BackgroundColor','black',...
  50.         'ForegroundColor','white','String',num2str(max_freq));
  51.  
  52.     freq_field=uicontrol('Style','edit','Position',[.59 .02 .12 .07],...
  53.         'Units','normalized','String',num2str(freq),...
  54.         'CallBack','sigdemo2(''setfreq'',2); sigdemo2(''redraw'');');
  55.  
  56.     freq_slider=uicontrol('Style','slider','Position',[.15 .12 .6 .04],...
  57.         'Units','normalized','Value',freq,'Max',max_freq,'Min',min_freq,...
  58.         'Callback','sigdemo2(''setfreq'',1); sigdemo2(''redraw'');');
  59.    
  60.     close_button=uicontrol('Style','Pushbutton','Position',[.85 .02 .12 .07],...
  61.         'Units','normalized','Callback','sigdemo2(''done'')','String','Done');
  62.  
  63.     help sig2help    % show help in command window
  64.  
  65.     %  frequency domain
  66.     ax_freq=axes('Position',[.15 .28 .8 .26],'XLim',[-8 8],'YLim',[-.5 2]);
  67.     freq_line=plot(w,F,'EraseMode','xor');
  68.     axis([-8 8 -.5 2]);
  69.     grid;
  70.     ylabel('Magnitude');
  71.     xlabel('Frequency (Hertz)');
  72.  
  73.     % time domain
  74.     ax_time=axes('Position',[.15 .66 .8 .26],'XLim',[-1 1],'YLim',[-1 1]);
  75.     time_line=plot(t,f,'EraseMode','xor');
  76.     % (set to xor mode to prevent re-rendering, that is, for speed)
  77.     axis([-1 1 -1 1]);
  78.     grid;
  79.     ylabel('Waveform');
  80.     xlabel('Time (Seconds)');
  81.     title('Click and drag waveforms to change frequency and amplitude');
  82.  
  83.     set(time_line,'ButtonDownFcn','sigdemo2(''down'',1)');
  84.     set(freq_line,'ButtonDownFcn','sigdemo2(''down'',2)');
  85.     drawnow;
  86.  
  87.     SIGDEMO2_DAT = [freq; amp; min_freq; max_freq; min_amp; max_amp; 0; 0; ...
  88.          time_line; freq_line; freq_field; freq_slider; ax_time; ax_freq ];
  89.  
  90. elseif strcmp(action,'down'),
  91.     % assumes that a line was clicked 
  92.  
  93.     if (in1==1),
  94.         line_handle = SIGDEMO2_DAT(9);
  95.         ax = SIGDEMO2_DAT(13);
  96.     else   % assume in1 == 2 otherwise (might not be true)
  97.         line_handle = SIGDEMO2_DAT(10);
  98.         ax = SIGDEMO2_DAT(14);
  99.     end;
  100.  
  101.     if (ax~=gca),
  102.         axes(ax);
  103.         drawnow discard;
  104.     end;
  105.  
  106.     % Obtain coordinates of mouse click location in axes units
  107.     pt=get(gca,'currentpoint');
  108.     x=pt(1,1);
  109.     y=pt(1,2);
  110.  
  111.     % find closest point on line to mouse click loc (call it fixed_x, fixed_y)
  112.     line_x=get(line_handle,'XData');
  113.     line_y=get(line_handle,'YData');
  114.     dist=(line_x-x).^2 + (line_y-y).^2;
  115.     [temp,i]=min(dist);
  116.     fixed_x=line_x(i);
  117.     fixed_y=line_y(i);
  118.  
  119.     set(line_handle,'LineStyle','--');
  120.     drawnow;
  121.  
  122.     SIGDEMO2_DAT(7)=fixed_x;
  123.     SIGDEMO2_DAT(8)=fixed_y;
  124.  
  125.     set(gcf,'WindowButtonMotionFcn', sprintf('sigdemo2(''move'',%g)',in1));
  126.     set(gcf,'WindowButtonUpFcn', sprintf('sigdemo2(''up'',%g)',in1));
  127.  
  128. elseif strcmp(action,'move'),
  129.     freq=SIGDEMO2_DAT(1);
  130.     amp=SIGDEMO2_DAT(2);
  131.     min_freq=SIGDEMO2_DAT(3);
  132.     max_freq=SIGDEMO2_DAT(4);
  133.     min_amp=SIGDEMO2_DAT(5);
  134.     max_amp=SIGDEMO2_DAT(6);
  135.     fixed_x=SIGDEMO2_DAT(7);
  136.     fixed_y=SIGDEMO2_DAT(8);
  137.     time_line=SIGDEMO2_DAT(9);
  138.     freq_line=SIGDEMO2_DAT(10);
  139.     freq_field=SIGDEMO2_DAT(11);
  140.     freq_slider=SIGDEMO2_DAT(12);
  141.  
  142.     pt=get(gca,'currentpoint');
  143.     x=pt(1,1);
  144.     y=pt(1,2);
  145.  
  146.     amp1=y/fixed_y*amp;
  147.     if (amp1>max_amp ),
  148.        amp1=max_amp ;
  149.     end;
  150.     if (amp1<min_amp ),
  151.        amp1=min_amp ;
  152.     end;
  153.     if (in1==1),
  154.         freq1=fixed_x/x*freq;
  155.     else
  156.         freq1=x/fixed_x*freq;
  157.     end;
  158.     if (freq1>max_freq),
  159.        freq1=max_freq;
  160.     end;
  161.     if (freq1<min_freq),
  162.        freq1=min_freq;
  163.     end;
  164.  
  165.     [t,f,w,F]=tffunc(amp1,freq1);
  166.     set(time_line,'YData',f);
  167.     set(freq_line,'YData',F);
  168.     set(freq_field,'String',num2str(freq1));
  169.     set(freq_slider,'Value',freq1);
  170.  
  171. elseif strcmp(action,'up'),
  172.     freq=SIGDEMO2_DAT(1);
  173.     amp=SIGDEMO2_DAT(2);
  174.     min_freq=SIGDEMO2_DAT(3);
  175.     max_freq=SIGDEMO2_DAT(4);
  176.     min_amp=SIGDEMO2_DAT(5);
  177.     max_amp=SIGDEMO2_DAT(6);
  178.     fixed_x=SIGDEMO2_DAT(7);
  179.     fixed_y=SIGDEMO2_DAT(8);
  180.  
  181.     pt=get(gca,'currentpoint');
  182.     x=pt(1,1);
  183.     y=pt(1,2);
  184.  
  185.     amp1=y/fixed_y*amp;
  186.     if (amp1>max_amp ),
  187.        amp1=max_amp ;
  188.     end;
  189.     if (amp1<min_amp ),
  190.        amp1=min_amp ;
  191.     end;
  192.     if (in1==1),
  193.         freq1=fixed_x/x*freq;
  194.     else
  195.         freq1=x/fixed_x*freq;
  196.     end;
  197.     if (freq1>max_freq),
  198.        freq1=max_freq;
  199.     end;
  200.     if (freq1<min_freq),
  201.        freq1=min_freq;
  202.     end;
  203.  
  204.     set(gcf,'WindowButtonMotionFcn','');
  205.     set(gcf,'WindowButtonUpFcn','');
  206.  
  207.     set(SIGDEMO2_DAT(9),'linestyle','-');
  208.     set(SIGDEMO2_DAT(10),'linestyle','-');
  209.     SIGDEMO2_DAT(1)=freq1;  % set amplitude and frequency
  210.     SIGDEMO2_DAT(2)=amp1;
  211.  
  212.     sigdemo2('redraw');
  213.  
  214. elseif strcmp(action,'redraw'),
  215.     freq=SIGDEMO2_DAT(1);
  216.     amp=SIGDEMO2_DAT(2);
  217.  
  218.     set(SIGDEMO2_DAT(11),'string',num2str(freq));
  219.     set(SIGDEMO2_DAT(12),'value',freq);
  220.  
  221.     [t,f,w,F]=tffunc(amp,freq);
  222.     set(SIGDEMO2_DAT(9),'YData',f);
  223.     set(SIGDEMO2_DAT(10),'YData',F);
  224.  
  225.     drawnow;
  226.  
  227. elseif strcmp(action,'setfreq'),
  228.     if (in1==1),    % set from slider
  229.         SIGDEMO2_DAT(1)=get(SIGDEMO2_DAT(12),'value');
  230.     else  % set from edit text
  231.         min_freq=SIGDEMO2_DAT(3);
  232.         max_freq=SIGDEMO2_DAT(4);
  233.         freq=str2num(get(SIGDEMO2_DAT(11),'string'));
  234.         if (freq>max_freq),
  235.             freq=max_freq;
  236.         end;
  237.         if (freq<min_freq),
  238.            freq=min_freq;
  239.         end;
  240.         SIGDEMO2_DAT(1)=freq;
  241.     end
  242.  
  243. elseif strcmp(action,'done'),
  244.     clf reset;
  245.     clear global SIGDEMO2_DAT
  246.  
  247. end
  248.  
  249.