home *** CD-ROM | disk | FTP | other *** search
- function sigdemo2(action,in1,in2);
- %SIGDEMO2 Interactive signal demo - 2 : continuous FT of a signal
- % Demonstrates MATLAB's graphic user interface using Handle Graphics
- % while illustrating basic Fourier transform (FT) properties,
- % including modulation.
-
- % Author: T. Krauss
- % 11/3/92, updated 2/9/93
- % (c) Copyright 1984-93, by The MathWorks, Inc.
-
- % possible actions:
- % 'start'
- % 'down' - in1=1 ==> time_line, in1=2 ==> freq_line
- % 'move'
- % 'up'
- % 'redraw'
- % 'setfreq' - in1=1 ==> from slider, in1=2 ==> from edit text
- % 'done'
-
- if nargin<1,
- action='start';
- end;
-
- global SIGDEMO2_DAT
-
- if strcmp(action,'start'),
- % graphics initialization
- clf reset;
- set(gcf,'Units','normalized','backingstore','off');
-
- % Create intial signal
- min_freq = 1;
- max_freq = 5;
- min_amp = .1;
- max_amp = 1;
- amp=0.5;
- freq=2.5; % hertz
- [t,f,w,F]=tffunc(amp,freq);
-
- freq_text=uicontrol('Style','text','Position',[.18 .02 .38 .07],...
- 'Units','normalized','BackgroundColor','black',...
- 'ForegroundColor','white','String','Modulation Frequency (Hertz):');
-
- uicontrol('style','text','pos',[.14 .07 .02 .05],...
- 'Units','normalized','BackgroundColor','black',...
- 'ForegroundColor','white','String',num2str(min_freq));
-
- uicontrol('style','text','pos',[.74 .07 .02 .05 ],...
- 'Units','normalized','BackgroundColor','black',...
- 'ForegroundColor','white','String',num2str(max_freq));
-
- freq_field=uicontrol('Style','edit','Position',[.59 .02 .12 .07],...
- 'Units','normalized','String',num2str(freq),...
- 'CallBack','sigdemo2(''setfreq'',2); sigdemo2(''redraw'');');
-
- freq_slider=uicontrol('Style','slider','Position',[.15 .12 .6 .04],...
- 'Units','normalized','Value',freq,'Max',max_freq,'Min',min_freq,...
- 'Callback','sigdemo2(''setfreq'',1); sigdemo2(''redraw'');');
-
- close_button=uicontrol('Style','Pushbutton','Position',[.85 .02 .12 .07],...
- 'Units','normalized','Callback','sigdemo2(''done'')','String','Done');
-
- help sig2help % show help in command window
-
- % frequency domain
- ax_freq=axes('Position',[.15 .28 .8 .26],'XLim',[-8 8],'YLim',[-.5 2]);
- freq_line=plot(w,F,'EraseMode','xor');
- axis([-8 8 -.5 2]);
- grid;
- ylabel('Magnitude');
- xlabel('Frequency (Hertz)');
-
- % time domain
- ax_time=axes('Position',[.15 .66 .8 .26],'XLim',[-1 1],'YLim',[-1 1]);
- time_line=plot(t,f,'EraseMode','xor');
- % (set to xor mode to prevent re-rendering, that is, for speed)
- axis([-1 1 -1 1]);
- grid;
- ylabel('Waveform');
- xlabel('Time (Seconds)');
- title('Click and drag waveforms to change frequency and amplitude');
-
- set(time_line,'ButtonDownFcn','sigdemo2(''down'',1)');
- set(freq_line,'ButtonDownFcn','sigdemo2(''down'',2)');
- drawnow;
-
- SIGDEMO2_DAT = [freq; amp; min_freq; max_freq; min_amp; max_amp; 0; 0; ...
- time_line; freq_line; freq_field; freq_slider; ax_time; ax_freq ];
-
- elseif strcmp(action,'down'),
- % assumes that a line was clicked
-
- if (in1==1),
- line_handle = SIGDEMO2_DAT(9);
- ax = SIGDEMO2_DAT(13);
- else % assume in1 == 2 otherwise (might not be true)
- line_handle = SIGDEMO2_DAT(10);
- ax = SIGDEMO2_DAT(14);
- end;
-
- if (ax~=gca),
- axes(ax);
- drawnow discard;
- end;
-
- % Obtain coordinates of mouse click location in axes units
- pt=get(gca,'currentpoint');
- x=pt(1,1);
- y=pt(1,2);
-
- % find closest point on line to mouse click loc (call it fixed_x, fixed_y)
- line_x=get(line_handle,'XData');
- line_y=get(line_handle,'YData');
- dist=(line_x-x).^2 + (line_y-y).^2;
- [temp,i]=min(dist);
- fixed_x=line_x(i);
- fixed_y=line_y(i);
-
- set(line_handle,'LineStyle','--');
- drawnow;
-
- SIGDEMO2_DAT(7)=fixed_x;
- SIGDEMO2_DAT(8)=fixed_y;
-
- set(gcf,'WindowButtonMotionFcn', sprintf('sigdemo2(''move'',%g)',in1));
- set(gcf,'WindowButtonUpFcn', sprintf('sigdemo2(''up'',%g)',in1));
-
- elseif strcmp(action,'move'),
- freq=SIGDEMO2_DAT(1);
- amp=SIGDEMO2_DAT(2);
- min_freq=SIGDEMO2_DAT(3);
- max_freq=SIGDEMO2_DAT(4);
- min_amp=SIGDEMO2_DAT(5);
- max_amp=SIGDEMO2_DAT(6);
- fixed_x=SIGDEMO2_DAT(7);
- fixed_y=SIGDEMO2_DAT(8);
- time_line=SIGDEMO2_DAT(9);
- freq_line=SIGDEMO2_DAT(10);
- freq_field=SIGDEMO2_DAT(11);
- freq_slider=SIGDEMO2_DAT(12);
-
- pt=get(gca,'currentpoint');
- x=pt(1,1);
- y=pt(1,2);
-
- amp1=y/fixed_y*amp;
- if (amp1>max_amp ),
- amp1=max_amp ;
- end;
- if (amp1<min_amp ),
- amp1=min_amp ;
- end;
- if (in1==1),
- freq1=fixed_x/x*freq;
- else
- freq1=x/fixed_x*freq;
- end;
- if (freq1>max_freq),
- freq1=max_freq;
- end;
- if (freq1<min_freq),
- freq1=min_freq;
- end;
-
- [t,f,w,F]=tffunc(amp1,freq1);
- set(time_line,'YData',f);
- set(freq_line,'YData',F);
- set(freq_field,'String',num2str(freq1));
- set(freq_slider,'Value',freq1);
-
- elseif strcmp(action,'up'),
- freq=SIGDEMO2_DAT(1);
- amp=SIGDEMO2_DAT(2);
- min_freq=SIGDEMO2_DAT(3);
- max_freq=SIGDEMO2_DAT(4);
- min_amp=SIGDEMO2_DAT(5);
- max_amp=SIGDEMO2_DAT(6);
- fixed_x=SIGDEMO2_DAT(7);
- fixed_y=SIGDEMO2_DAT(8);
-
- pt=get(gca,'currentpoint');
- x=pt(1,1);
- y=pt(1,2);
-
- amp1=y/fixed_y*amp;
- if (amp1>max_amp ),
- amp1=max_amp ;
- end;
- if (amp1<min_amp ),
- amp1=min_amp ;
- end;
- if (in1==1),
- freq1=fixed_x/x*freq;
- else
- freq1=x/fixed_x*freq;
- end;
- if (freq1>max_freq),
- freq1=max_freq;
- end;
- if (freq1<min_freq),
- freq1=min_freq;
- end;
-
- set(gcf,'WindowButtonMotionFcn','');
- set(gcf,'WindowButtonUpFcn','');
-
- set(SIGDEMO2_DAT(9),'linestyle','-');
- set(SIGDEMO2_DAT(10),'linestyle','-');
- SIGDEMO2_DAT(1)=freq1; % set amplitude and frequency
- SIGDEMO2_DAT(2)=amp1;
-
- sigdemo2('redraw');
-
- elseif strcmp(action,'redraw'),
- freq=SIGDEMO2_DAT(1);
- amp=SIGDEMO2_DAT(2);
-
- set(SIGDEMO2_DAT(11),'string',num2str(freq));
- set(SIGDEMO2_DAT(12),'value',freq);
-
- [t,f,w,F]=tffunc(amp,freq);
- set(SIGDEMO2_DAT(9),'YData',f);
- set(SIGDEMO2_DAT(10),'YData',F);
-
- drawnow;
-
- elseif strcmp(action,'setfreq'),
- if (in1==1), % set from slider
- SIGDEMO2_DAT(1)=get(SIGDEMO2_DAT(12),'value');
- else % set from edit text
- min_freq=SIGDEMO2_DAT(3);
- max_freq=SIGDEMO2_DAT(4);
- freq=str2num(get(SIGDEMO2_DAT(11),'string'));
- if (freq>max_freq),
- freq=max_freq;
- end;
- if (freq<min_freq),
- freq=min_freq;
- end;
- SIGDEMO2_DAT(1)=freq;
- end
-
- elseif strcmp(action,'done'),
- clf reset;
- clear global SIGDEMO2_DAT
-
- end
-
-