📄 sigdemo1.m
字号:
function sigdemo1(action,in1,in2);
%SIGDEMO1 Interactive signal demo - 1 : DFT of a signal
% Demonstrates MATLAB's graphic user interface using Handle Graphics
% while illustrating some basic signal processing concepts, such as
% sampling, aliasing, and windowing.
% Author: T. Krauss
% 11/3/92, updated 2/9/93
% Adapted for Expo: dlc, 7-93
% Copyright (c) 1988-98 by The MathWorks, Inc.
% $Revision: 5.8 $ $Date: 1997/12/02 18:36:42 $
% possible actions:
% 'start'
% 'down'
% 'move'
% 'up'
% 'redraw'
% 'done'
% 'setfreq'
% 'setwindow'
% 'showwind'
if nargin<1,
action='start';
end;
global SIGDEMO1_DAT
global ADDIT_DAT % this is for WINDOW pop-up button
if strcmp(action,'start'),
%====================================
% Graphics initialization
oldFigNumber = watchon;
figNumber = figure;
set(gcf, ...
'NumberTitle','off', ...
'Name','Discrete Fourier Transform', ...
'backingstore','off',...
'Units','normalized');
%====================================
% Information for all buttons
labelColor=192/255*[1 1 1];
top=0.95;
bottom=0.05;
left=0.75;
yInitLabelPos=0.90;
left = 0.78;
labelWid=0.18;
labelHt=0.05;
btnWid = 0.18;
btnHt=0.07;
% Spacing between the label and the button for the same command
btnOffset=0.003;
% Spacing between the button and the next command's label
spacing=0.05;
%====================================
% The CONSOLE frame
frmBorder=0.02;
yPos=0.05-frmBorder;
frmPos=[left-frmBorder yPos btnWid+2*frmBorder 0.9+2*frmBorder];
h=uicontrol( ...
'Style','frame', ...
'Units','normalized', ...
'Position',frmPos);
%====================================
% The SIGNAL command popup button
btnNumber=1;
yLabelPos=top-(btnNumber-1)*(btnHt+labelHt+spacing);
labelStr=' Signal';
% Generic label information
labelPos=[left yLabelPos-labelHt labelWid labelHt];
uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position',labelPos, ...
'HorizontalAlignment','left', ...
'String',labelStr);
btnPos=[left yLabelPos-labelHt-btnHt-btnOffset btnWid btnHt];
popup=uicontrol('Style','Popup','String','sine|square|sawtooth',...
'Units','normalized',...
'Position', btnPos, ...
'CallBack','sigdemo1(''redraw'')');
%====================================
% The WINDOW command popup button
btnNumber=2;
yLabelPos=top-(btnNumber-1)*(btnHt+labelHt+spacing);
labelStr=' Window';
popupStr= ' rectangle| triangular| hanning| hamming';
if ~isstudent
popupStr= [popupStr '| chebyshev | kaiser'];
end
callbackStr= 'sigdemo1(''setwindow'')';
% Generic label information
labelPos=[left yLabelPos-labelHt labelWid labelHt];
uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position',labelPos, ...
'HorizontalAlignment','left', ...
'String',labelStr);
% Generic popup button information
btnPos=[left yLabelPos-labelHt-btnHt-btnOffset btnWid btnHt];
winHndl = uicontrol( ...
'Style','popup', ...
'Units','normalized', ...
'Position',btnPos, ...
'String',popupStr, ...
'Callback',callbackStr);
%====================================
% The FUNDAMENTAL editable text box
btnNumber=3;
yLabelPos=top-(btnNumber-1)*(btnHt+labelHt+spacing);
labelPos=[left yLabelPos-labelHt labelWid labelHt];
freq_text = uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position', labelPos, ...
'String','Fundamental');
btnPos=[left+0.02 yLabelPos-labelHt-btnHt-btnOffset ...
0.5*btnWid+frmBorder btnHt];
freq_field = uicontrol( ...
'Style','edit', ...
'Units','normalized', ...
'Position', btnPos, ...
'BackgroundColor','w',...
'String',' 5',...
'CallBack','sigdemo1(''setfreq''); sigdemo1(''redraw'')');
%====================================
% The INFO button
uicontrol( ...
'Style','push', ...
'Units','normalized', ...
'Position',[left bottom+(2*labelHt)+spacing btnWid 2*labelHt], ...
'String','Info', ...
'Callback','sigdemo1(''info'')');
%========================================
% The CLOSE button
done_button=uicontrol('Style','Pushbutton', ...
'Units','normalized',...
'Position',[left bottom btnWid 2*labelHt], ...
'Callback','sigdemo1(''done'')','String','Close');
%====================================
% Create intial signal
N=201; % number of samples
% N=21;
amp=0.5;
freq=5; % hertz
t0=0; % seconds
t1=1; % seconds
t=linspace(t0,t1,N)';
T=(t1-t0)/N; % sampling rate in seconds
M=256; % length of fft
window=ones(N,1); % use window to lower side-lobes in the freq. domain
% (makes peaks wider)
min_dB = -40; % freq. domain lower axis limit
% create axes for time domain and frequency domain plot
ax_freq=axes('Position',[.12 .14 .6 .3],'XLim',...
[0 1/(2*T)],'YLim',[min_dB 50]);
% time domain
val = get(popup,'Value');
if (val == 1),
f=amp*sin(freq*t*2*pi);
elseif (val == 2), % square wave
tt=freq*t*2*pi;
tmp=rem(tt,2*pi);
f=amp*(2*rem((tt<0)+(tmp>pi | tmp<-pi)+1,2)-1);
elseif (val == 3), % sawtooth
tt=freq*t*2*pi;
f=amp*((tt < 0) + rem(tt,2*pi)/2/pi - .5)*2;
end;
% frequency domain
F=fft(window.*f,2*M);
F=F(1:M);
w=(0:M-1)*pi/M;
FF=20*log10(abs(F));
ind=find(FF<min_dB);
FF(ind)=NaN*ones(size(ind)); % put NaN's in where
% min_dB shows up - this is to work around no clipping in xor mode
freq_line=plot(w/2/pi/T,FF,'EraseMode','xor');
axis([0 1/(2*T) min_dB 50]);
grid on;
ylabel('Magnitude (dB)');
xlabel('Frequency (Hertz)');
ax_time=axes('Position',[.12 .58 .6 .3],'XLim',[t0 t1],'YLim',[-1 1]);
time_line=plot(t,f,'EraseMode','xor');
axis([t0 t1 -1 1]);
% (set to xor mode to prevent re-rendering, that is, for speed)
grid on;
ylabel('Waveform');
xlabel('Time (Seconds)');
text(0.12, 1.55,' Click and drag waveform to change');
text(0.12, 1.3,'fundamental frequency and amplitude');
set(time_line,'ButtonDownFcn','sigdemo1(''down'')');
SIGDEMO1_DAT = [freq; amp; N; M; min_dB; 0; 0; ...
time_line; freq_line; freq_field; popup; -1; gcf; t(:); window(:)];
ADDIT_DAT = winHndl;
watchoff(oldFigNumber);
elseif strcmp(action,'down'),
% assumes that a line was clicked
time_line=SIGDEMO1_DAT(8);
axes(get(time_line,'parent')); % set the right axes
% Obtain coordinates of mouse click location in axes units
pt=get(gca,'currentpoint');
x=pt(1,1);
y=pt(1,2);
% find closest vertex of line to mouse click location (call it fixed_x, fixed_y)
line_x=get(time_line,'XData');
line_y=get(time_line,'YData');
units_str = get(gca,'units'); % save normalized state
set(gca,'units','pixels'); % distance must be in pixels
p=get(gca,'pos');
xa=get(gca,'xlim');
ya=get(gca,'ylim');
dist=((line_x-x)*p(3)/(xa(2)-xa(1))).^2 + ...
((line_y-y)*p(4)/(ya(2)-ya(1))).^2;
[temp,i]=min(dist);
fixed_x=line_x(i);
fixed_y=line_y(i);
set(time_line,'LineStyle','--');
SIGDEMO1_DAT(6)=fixed_x;
SIGDEMO1_DAT(7)=fixed_y;
set(gca,'units',units_str );
set(gcf,'WindowButtonMotionFcn', 'sigdemo1(''move'')');
set(gcf,'WindowButtonUpFcn', 'sigdemo1(''up'')');
% set(gcf,'userdata',u);
elseif strcmp(action,'move'),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -